Skip to content

Commit e143018

Browse files
committed
adapt to PhpUnit 10+
1 parent a24093b commit e143018

File tree

6 files changed

+129
-103
lines changed

6 files changed

+129
-103
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@
6363
"require-dev": {
6464
"codeception/codeception": "^5.0",
6565
"maglnet/composer-require-checker": "^4.2",
66-
"phpunit/phpunit": "^10.5",
66+
"phpunit/phpunit": "10 - 12",
6767
"rector/rector": "^2.1.1",
6868
"roave/infection-static-analysis-plugin": "^1.30",
6969
"spatie/phpunit-watcher": "^1.23",
70-
"vimeo/psalm": "^5.15 || ^6.12",
70+
"vimeo/psalm": "^5.22 || ^6.12",
7171
"yiisoft/active-record": "dev-master",
7272
"yiisoft/assets": "^5.1",
7373
"yiisoft/csrf": "^2.0",
74-
"yiisoft/db": "1.2 as dev-master",
75-
"yiisoft/db-sqlite": "^1.0",
74+
"yiisoft/db": "dev-master as 2.0.0",
75+
"yiisoft/db-sqlite": "dev-master as 2.0.0",
7676
"yiisoft/psr-dummy-provider": "^1.0",
7777
"yiisoft/router-fastroute": "^4.0",
7878
"yiisoft/test-support": "^3.0",
@@ -110,7 +110,7 @@
110110
}
111111
},
112112
"scripts": {
113-
"test": "phpunit --testdox --no-interaction",
113+
"test": "phpunit --testdox",
114114
"test-watch": "phpunit-watcher watch"
115115
},
116116
"config": {

config/params.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
declare(strict_types=1);
44

55
use Codeception\Extension;
6+
use PHPUnit\Framework\Test;
67
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
78
use Yiisoft\Yii\Debug\Api\Inspector\Command\CodeceptionCommand;
89
use Yiisoft\Yii\Debug\Api\Inspector\Command\PHPUnitCommand;
910
use Yiisoft\Yii\Debug\Api\Inspector\Command\PsalmCommand;
1011

1112
$testCommands = [];
12-
if (class_exists(PHPUnit\Framework\Test::class)) {
13+
if (interface_exists(Test::class)) {
1314
$testCommands[PHPUnitCommand::COMMAND_NAME] = PHPUnitCommand::class;
1415
}
1516
if (class_exists(Extension::class)) {

phpunit.xml.dist

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
<phpunit bootstrap="vendor/autoload.php"
44
colors="true"
5-
verbose="true"
65
failOnRisky="true"
76
failOnWarning="true"
8-
convertErrorsToExceptions="true"
9-
convertNoticesToExceptions="true"
10-
convertWarningsToExceptions="true"
7+
failOnDeprecation="true"
8+
failOnNotice="true"
9+
displayDetailsOnTestsThatTriggerDeprecations="true"
10+
beStrictAboutCoverageMetadata="true"
11+
beStrictAboutOutputDuringTests="true"
1112
stopOnFailure="false"
1213
executionOrder="random"
1314
resolveDependencies="true">
@@ -21,9 +22,9 @@
2122
</testsuite>
2223
</testsuites>
2324

24-
<coverage>
25+
<source>
2526
<include>
2627
<directory>./src</directory>
2728
</include>
28-
</coverage>
29+
</source>
2930
</phpunit>

src/Inspector/Command/PHPUnitCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public function run(): CommandResponse
3939
$extension = PHPUnitJSONReporter::class;
4040
$params = [
4141
'vendor/bin/phpunit',
42-
'--printer',
42+
'--extension',
4343
$extension,
44-
'-vvv',
44+
'--no-coverage -vvv',
4545
];
4646

4747
$process = new Process($params);

src/Inspector/Test/PHPUnitJSONReporter.php

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,135 +4,135 @@
44

55
namespace Yiisoft\Yii\Debug\Api\Inspector\Test;
66

7-
use PHPUnit\Framework\AssertionFailedError;
8-
use PHPUnit\Framework\Test;
9-
use PHPUnit\Framework\TestCase;
10-
use PHPUnit\Framework\TestResult;
11-
use PHPUnit\Framework\TestSuite;
12-
use PHPUnit\Framework\Warning;
13-
use PHPUnit\Runner\BaseTestRunner;
14-
use PHPUnit\TextUI\ResultPrinter;
15-
use PHPUnit\Util\TestDox\NamePrettifier;
16-
use ReflectionClass;
17-
use Throwable;
18-
19-
/**
20-
* @psalm-suppress InternalClass, InternalMethod, UndefinedClass
21-
*/
22-
class PHPUnitJSONReporter implements ResultPrinter
7+
use PHPUnit\Event\Event;
8+
use PHPUnit\Event\Test\ConsideredRisky;
9+
use PHPUnit\Event\Test\Errored;
10+
use PHPUnit\Event\Test\ErrorTriggered;
11+
use PHPUnit\Event\Test\Failed;
12+
use PHPUnit\Event\Test\Finished;
13+
use PHPUnit\Event\Test\MarkedIncomplete;
14+
use PHPUnit\Event\Test\NoticeTriggered;
15+
use PHPUnit\Event\Test\Passed;
16+
use PHPUnit\Event\Test\PhpNoticeTriggered;
17+
use PHPUnit\Event\Test\PhpunitErrorTriggered;
18+
use PHPUnit\Event\Test\PhpunitNoticeTriggered;
19+
use PHPUnit\Event\Test\PhpunitWarningTriggered;
20+
use PHPUnit\Event\Test\PhpWarningTriggered;
21+
use PHPUnit\Event\Test\Skipped;
22+
use PHPUnit\Event\Test\WarningTriggered;
23+
use PHPUnit\Runner\Extension\Extension;
24+
use PHPUnit\Runner\Extension\Facade;
25+
use PHPUnit\Runner\Extension\ParameterCollection;
26+
use PHPUnit\TextUI\Configuration\Configuration;
27+
28+
class PHPUnitJSONReporter implements Extension
2329
{
2430
public const FILENAME = 'phpunit-report.json';
2531
public const ENVIRONMENT_VARIABLE_DIRECTORY_NAME = 'REPORTER_OUTPUT_PATH';
2632

2733
private array $data = [];
28-
private NamePrettifier $prettifier;
2934

3035
public function __construct()
3136
{
32-
$this->prettifier = new NamePrettifier();
3337
}
3438

35-
public function printResult(TestResult $result): void
39+
public function bootstrap(
40+
Configuration $configuration,
41+
Facade $facade,
42+
ParameterCollection $parameters
43+
): void
3644
{
37-
$path = getenv(self::ENVIRONMENT_VARIABLE_DIRECTORY_NAME) ?: getcwd();
38-
ksort($this->data);
39-
40-
file_put_contents(
41-
$path . DIRECTORY_SEPARATOR . self::FILENAME,
42-
json_encode(array_values($this->data), JSON_THROW_ON_ERROR)
43-
);
44-
}
45-
46-
public function write(string $buffer): void
47-
{
48-
$this->data = [];
49-
}
50-
51-
public function addError(Test $test, Throwable $t, float $time): void
52-
{
53-
$this->logErroredTest($test, $t);
54-
}
55-
56-
public function addWarning(Test $test, Warning $e, float $time): void
57-
{
58-
$this->logErroredTest($test, $e);
59-
}
60-
61-
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
62-
{
63-
$this->logErroredTest($test, $e);
64-
}
65-
66-
public function addIncompleteTest(Test $test, Throwable $t, float $time): void
67-
{
68-
$this->logErroredTest($test, $t);
69-
}
70-
71-
public function addRiskyTest(Test $test, Throwable $t, float $time): void
72-
{
73-
$this->logErroredTest($test, $t);
74-
}
75-
76-
public function addSkippedTest(Test $test, Throwable $t, float $time): void
77-
{
78-
$this->logErroredTest($test, $t);
45+
$facade->registerTracer(new PHPUnitTracer($this));
7946
}
8047

81-
public function startTestSuite(TestSuite $suite): void
48+
public function __destruct()
8249
{
50+
$this->saveResult();
8351
}
8452

85-
public function endTestSuite(TestSuite $suite): void
53+
public function saveResult(): void
8654
{
87-
}
55+
$path = getenv(self::ENVIRONMENT_VARIABLE_DIRECTORY_NAME) ?: getcwd();
56+
ksort($this->data);
8857

89-
public function startTest(Test $test): void
90-
{
58+
file_put_contents(
59+
$path . DIRECTORY_SEPARATOR . self::FILENAME,
60+
json_encode(array_values($this->data), JSON_THROW_ON_ERROR)
61+
);
9162
}
9263

93-
public function endTest(Test $test, float $time): void
64+
public function logPassed(Passed|Finished $event): void
9465
{
95-
if (!$test instanceof TestCase) {
66+
$parsedName = $event->test()->id();
67+
if (array_key_exists($parsedName, $this->data)) {
9668
return;
9769
}
98-
if ($test->getStatus() !== BaseTestRunner::STATUS_PASSED) {
99-
return;
100-
}
101-
102-
$parsedName = $this->parseName($test);
10370

10471
$this->data[$parsedName] = [
105-
'file' => $this->parseFilename($test),
72+
'file' => $event->test()->file(),
10673
'test' => $parsedName,
74+
'time' => $event->telemetryInfo()->durationSincePrevious()->asFloat(),
10775
'status' => 'ok',
10876
'stacktrace' => [],
10977
];
11078
}
11179

112-
private function parseName(Test $test): string
80+
public function logErrored(Failed|MarkedIncomplete|Errored $event): void
11381
{
114-
if ($test instanceof TestCase) {
115-
return $test::class . '::' . $test->getName(true);
82+
$parsedName = $event->test()->id();
83+
if (array_key_exists($parsedName, $this->data)) {
84+
return;
11685
}
117-
return $this->prettifier->prettifyTestClass($test::class);
118-
}
119-
120-
private function parseFilename(Test $test): string
121-
{
122-
$reflection = new ReflectionClass($test);
12386

124-
return $reflection->getFileName();
87+
$this->data[$parsedName] = [
88+
'file' => $event->test()->file(),
89+
'test' => $parsedName,
90+
'time' => $event->telemetryInfo()->durationSincePrevious()->asFloat(),
91+
'status' => $event->throwable()->message(),
92+
'stacktrace' => $event->throwable()->stackTrace(),
93+
];
12594
}
12695

127-
private function logErroredTest(Test $test, Throwable $t): void
96+
public function logMessage(WarningTriggered|PhpWarningTriggered|PhpunitWarningTriggered|NoticeTriggered|PhpNoticeTriggered|PhpunitNoticeTriggered|ErrorTriggered|PhpunitErrorTriggered|Skipped|ConsideredRisky $event): void
12897
{
129-
$parsedName = $this->parseName($test);
98+
$parsedName = $event->test()->id();
99+
if (array_key_exists($parsedName, $this->data)) {
100+
return;
101+
}
130102

131103
$this->data[$parsedName] = [
132-
'file' => $this->parseFilename($test),
104+
'file' => $event->test()->file(),
133105
'test' => $parsedName,
134-
'status' => $t->getMessage(),
135-
'stacktrace' => $t->getTrace(),
106+
'time' => $event->telemetryInfo()->durationSincePrevious()->asFloat(),
107+
'status' => $event->message(),
108+
'stacktrace' => [],
136109
];
137110
}
111+
112+
public function log(Event $event)
113+
{
114+
if ($event instanceof Passed
115+
|| $event instanceof Finished
116+
) {
117+
$this->logPassed($event);
118+
} elseif ($event instanceof Failed
119+
|| $event instanceof Errored
120+
|| $event instanceof MarkedIncomplete
121+
) {
122+
$this->logErrored($event);
123+
} elseif (
124+
$event instanceof WarningTriggered
125+
|| $event instanceof PhpWarningTriggered
126+
|| $event instanceof PhpunitWarningTriggered
127+
|| $event instanceof NoticeTriggered
128+
|| $event instanceof PhpNoticeTriggered
129+
|| $event instanceof PhpunitNoticeTriggered
130+
|| $event instanceof ErrorTriggered
131+
|| $event instanceof PhpunitErrorTriggered
132+
|| $event instanceof Skipped
133+
|| $event instanceof ConsideredRisky
134+
) {
135+
$this->logMessage($event);
136+
}
137+
}
138138
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Yii\Debug\Api\Inspector\Test;
6+
7+
use PHPUnit\Event\Event;
8+
use PHPUnit\Event\Tracer\Tracer;
9+
10+
/**
11+
* @psalm-suppress InternalClass, InternalMethod
12+
*/
13+
class PHPUnitTracer implements Tracer
14+
{
15+
public function __construct(private readonly PHPUnitJSONReporter $reporter)
16+
{
17+
}
18+
19+
public function trace(Event $event): void
20+
{
21+
$this->reporter->log($event);
22+
23+
}
24+
}

0 commit comments

Comments
 (0)