Skip to content

Commit e8ed3d0

Browse files
authored
Merge pull request #14 from VadymHrechukha/AHN-131_extended_hascontext_trait_to_include_method_for_fetch_context
HP-2824: extended HasContext trait to include method for fetch context by key
2 parents 2815666 + 47fccc3 commit e8ed3d0

File tree

11 files changed

+178
-16
lines changed

11 files changed

+178
-16
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: phpunit-tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: "0 9 * * 1"
8+
9+
jobs:
10+
PHPUnit:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
php: ['8.3']
15+
coverage-driver: [pcov]
16+
name: PHP ${{ matrix.php }}
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Install PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ matrix.php }}
25+
extensions: gmp
26+
coverage: pcov
27+
tools: composer:v2, infection
28+
29+
- name: Cache Composer packages
30+
id: composer-cache
31+
uses: actions/cache@v4
32+
with:
33+
path: vendor
34+
key: ${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
35+
36+
- name: Update composer
37+
run: composer self-update
38+
39+
- name: Composer install
40+
if: steps.composer-cache.outputs.cache-hit != 'true'
41+
run: composer install -n
42+
43+
- name: PHPUnit
44+
run: vendor/bin/phpunit
45+
env:
46+
XDEBUG_MODE: coverage

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"yiisoft/yii2": "dev-master",
5656
"yiisoft/arrays": "*@dev",
5757
"yiisoft/strings": "*@dev",
58+
"phpunit/phpunit": "^12.0",
5859
"yiisoft/composer-config-plugin": "dev-master",
5960
"opis/closure": "3.x-dev as 3.6.x-dev"
6061
},
@@ -78,5 +79,11 @@
7879
"type": "composer",
7980
"url": "https://asset-packagist.org"
8081
}
81-
]
82+
],
83+
"config": {
84+
"allow-plugins": {
85+
"yiisoft/yii2-composer": true,
86+
"yiisoft/composer-config-plugin": true
87+
}
88+
}
8289
}

phpunit.xml.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="false" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/_bootstrap.php">
33
<testsuites>
4-
<testsuite name="Unit Test Suite">
4+
<testsuite name="unit">
55
<directory>./tests/unit/</directory>
66
</testsuite>
77
<testsuite name="Functional Test Suite">
88
<directory>./tests/functional/</directory>
99
</testsuite>
10+
<!--
1011
<testsuite name="Readme Unit Test Suite">
1112
<directory>.hidev/vendor/hiqdev/hidev-readme/tests/unit/</directory>
1213
</testsuite>
1314
<testsuite name="License Unit Test Suite">
1415
<directory>.hidev/vendor/hiqdev/hidev-license/tests/unit/</directory>
1516
</testsuite>
17+
-->
1618
<!--
1719
<testsuite name="Readme Functional Test Suite">
1820
<directory>.hidev/vendor/hiqdev/hidev-readme/tests/functional/</directory>

src/exception/HasContext.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace hidev\exception;
46

57
use Throwable;
8+
use yii\helpers\ArrayHelper;
69

710
trait HasContext
811
{
@@ -13,6 +16,7 @@ abstract public function getPrevious(): ?Throwable;
1316
public function addContext(array $data): self
1417
{
1518
$this->context = array_merge($this->context, $data);
19+
1620
return $this;
1721
}
1822

@@ -28,4 +32,9 @@ public static function make(string $message, array $context): self
2832

2933
return $exception;
3034
}
35+
36+
public function getContextValue(string $fieldName)
37+
{
38+
return ArrayHelper::getValue($this->context, $fieldName, '');
39+
}
3140
}

src/handlers/YamlHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ public function __construct(Interpolator $interpolator, $options = [])
3232
*/
3333
public function renderType($data)
3434
{
35-
/// XXX TODO fix getItems crutch
36-
return Yaml::dump(ArrayHelper::toArray(method_exists($data, 'getItems') ? $data->getItems() : $data), 5);
35+
if (!is_array($data)) {
36+
/// XXX TODO fix getItems crutch
37+
$data = ArrayHelper::toArray(method_exists($data, 'getItems') ? $data->getItems() : $data);
38+
}
39+
40+
return Yaml::dump($data, 5);
3741
}
3842

3943
/**

tests/_bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
error_reporting(E_ALL & ~E_NOTICE);
1212

1313
require_once __DIR__ . '/../config/bootstrap.php';
14-
require_once __DIR__ . '/../.hidev/vendor/autoload.php';
14+
require_once __DIR__ . '/../vendor/autoload.php';

tests/functional/InitTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@ class InitTest extends \PHPUnit\Framework\TestCase
1717
*/
1818
protected $tester;
1919

20-
protected function setUp()
20+
protected function setUp(): void
2121
{
22+
parent::setUp();
23+
24+
$this->previousExceptionHandler = set_exception_handler(function() {});
25+
restore_exception_handler();
26+
2227
$this->tester = new Tester($this);
2328
}
2429

25-
protected function tearDown()
30+
protected function tearDown(): void
2631
{
32+
// Restore handlers altered during hidev execution
33+
if ($this->previousExceptionHandler !== null) {
34+
set_exception_handler($this->previousExceptionHandler);
35+
} else {
36+
restore_exception_handler();
37+
}
38+
2739
$this->tester = null;
2840
}
2941

tests/functional/Tester.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace hidev\tests\functional;
1212

1313
use Yii;
14+
use yii\base\InvalidConfigException;
1415
use yii\console\Application;
1516
use yii\console\Request;
1617

@@ -74,7 +75,8 @@ public function __destruct()
7475

7576
/**
7677
* Run hidev.
77-
* @param string $query
78+
* @param string|string[] $query
79+
* @throws InvalidConfigException
7880
*/
7981
public function hidev($query)
8082
{

tests/unit/console/CommonControllerTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,31 @@
1414

1515
class CommonControllerTest extends \PHPUnit\Framework\TestCase
1616
{
17-
/**
18-
* @var CommonController
19-
*/
20-
protected $object;
17+
protected CommonController $object;
2118

2219
protected $before = 'asd';
2320

2421
/**
2522
* Sets up the fixture, for example, opens a network connection.
2623
* This method is called before a test is executed.
2724
*/
28-
protected function setUp()
25+
protected function setUp(): void
2926
{
30-
$this->object = new CommonController('test', null);
27+
$this->object = new CommonController('test', null, [
28+
'request' => [
29+
'class' => \yii\console\Request::class,
30+
],
31+
'response' => [
32+
'class' => \yii\console\Response::class,
33+
],
34+
]);
3135
}
3236

3337
/**
3438
* Tears down the fixture, for example, closes a network connection.
3539
* This method is called after a test is executed.
3640
*/
37-
protected function tearDown()
41+
protected function tearDown(): void
3842
{
3943
}
4044

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hidev\tests\unit\exception;
6+
7+
use hidev\tests\unit\exception\stub\TestException;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class HasContextTest extends TestCase
11+
{
12+
public function testAddContext(): void
13+
{
14+
$e = new TestException('Error');
15+
16+
$e->addContext(['a' => 1]);
17+
$e->addContext(['b' => 2]);
18+
19+
$this->assertSame(['a' => 1, 'b' => 2], $e->getContext());
20+
}
21+
22+
public function testGetContext(): void
23+
{
24+
$e = new TestException('Error');
25+
$e->addContext(['foo' => 'bar']);
26+
27+
$this->assertSame(['foo' => 'bar'], $e->getContext());
28+
}
29+
30+
public function testMakeCreatesInstanceWithContext(): void
31+
{
32+
$e = TestException::make('Something happened', ['key' => 'value']);
33+
34+
$this->assertInstanceOf(TestException::class, $e);
35+
$this->assertSame('Something happened', $e->getMessage());
36+
$this->assertSame(['key' => 'value'], $e->getContext());
37+
}
38+
39+
public function testGetContextValueReturnsNestedValue(): void
40+
{
41+
$e = new TestException('Error');
42+
$e->addContext([
43+
'user' => [
44+
'id' => 15,
45+
'profile' => [
46+
'email' => 'test@example.com',
47+
],
48+
],
49+
]);
50+
51+
$this->assertSame(15, $e->getContextValue('user.id'));
52+
$this->assertSame('test@example.com', $e->getContextValue('user.profile.email'));
53+
}
54+
55+
public function testGetContextValueReturnsDefaultWhenMissing(): void
56+
{
57+
$e = new TestException('Error');
58+
$e->addContext(['foo' => 'bar']);
59+
60+
$this->assertSame('', $e->getContextValue('missing.key'));
61+
}
62+
}

0 commit comments

Comments
 (0)