Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
"require-dev": {
"codeception/codeception": "^5.0",
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^10.5",
"phpunit/phpunit": "10 - 12",
"rector/rector": "^2.1.1",
"roave/infection-static-analysis-plugin": "^1.30",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.15 || ^6.12",
"vimeo/psalm": "^5.22 || ^6.12",
"yiisoft/active-record": "dev-master",
"yiisoft/assets": "^5.1",
"yiisoft/csrf": "^2.0",
"yiisoft/db": "1.2 as dev-master",
"yiisoft/db-sqlite": "^1.0",
"yiisoft/db": "dev-master as 2.0.0",
"yiisoft/db-sqlite": "dev-master as 2.0.0",
"yiisoft/psr-dummy-provider": "^1.0",
"yiisoft/router-fastroute": "^4.0",
"yiisoft/test-support": "^3.0",
Expand Down Expand Up @@ -110,7 +110,7 @@
}
},
"scripts": {
"test": "phpunit --testdox --no-interaction",
"test": "phpunit --testdox",
"test-watch": "phpunit-watcher watch"
},
"config": {
Expand Down
3 changes: 2 additions & 1 deletion config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
declare(strict_types=1);

use Codeception\Extension;
use PHPUnit\Framework\Test;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
use Yiisoft\Yii\Debug\Api\Inspector\Command\CodeceptionCommand;
use Yiisoft\Yii\Debug\Api\Inspector\Command\PHPUnitCommand;
use Yiisoft\Yii\Debug\Api\Inspector\Command\PsalmCommand;

$testCommands = [];
if (class_exists(PHPUnit\Framework\Test::class)) {
if (interface_exists(Test::class)) {
$testCommands[PHPUnitCommand::COMMAND_NAME] = PHPUnitCommand::class;
}
if (class_exists(Extension::class)) {
Expand Down
13 changes: 7 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnDeprecation="true"
failOnNotice="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true">
Expand All @@ -21,9 +22,9 @@
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory>./src</directory>
</include>
</coverage>
</source>
</phpunit>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
errorLevel="4"
findUnusedBaselineEntry="true"
findUnusedCode="false"
ensureOverrideAttribute="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
2 changes: 1 addition & 1 deletion src/Inspector/Command/BashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function run(): CommandResponse

$processOutput = rtrim($process->getOutput());

if (!$process->getExitCode() > 1) {
if ($process->getExitCode() > 1) {
return new CommandResponse(
status: CommandResponse::STATUS_FAIL,
result: null,
Expand Down
2 changes: 1 addition & 1 deletion src/Inspector/Command/CodeceptionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function run(): CommandResponse
JSON_THROW_ON_ERROR
);

if (!$process->getExitCode() > 1) {
if ($process->getExitCode() > 1) {
return new CommandResponse(
status: CommandResponse::STATUS_FAIL,
result: null,
Expand Down
6 changes: 3 additions & 3 deletions src/Inspector/Command/PHPUnitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function run(): CommandResponse
$extension = PHPUnitJSONReporter::class;
$params = [
'vendor/bin/phpunit',
'--printer',
'--extension',
$extension,
'-vvv',
'--no-coverage -vvv',
];

$process = new Process($params);
Expand All @@ -59,7 +59,7 @@ public function run(): CommandResponse
JSON_THROW_ON_ERROR
);

if (!$process->getExitCode() > 1) {
if ($process->getExitCode() > 1) {
return new CommandResponse(
status: CommandResponse::STATUS_FAIL,
result: null,
Expand Down
2 changes: 1 addition & 1 deletion src/Inspector/Command/PsalmCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function run(): CommandResponse
JSON_THROW_ON_ERROR
);

if (!$process->getExitCode() > 1) {
if ($process->getExitCode() > 1) {
return new CommandResponse(
status: CommandResponse::STATUS_FAIL,
result: null,
Expand Down
177 changes: 88 additions & 89 deletions src/Inspector/Test/PHPUnitJSONReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,52 @@

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

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
use PHPUnit\Runner\BaseTestRunner;
use PHPUnit\TextUI\ResultPrinter;
use PHPUnit\Util\TestDox\NamePrettifier;
use ReflectionClass;
use Throwable;

/**
* @psalm-suppress InternalClass, InternalMethod, UndefinedClass
*/
class PHPUnitJSONReporter implements ResultPrinter
use PHPUnit\Event\Event;
use PHPUnit\Event\Test\ConsideredRisky;
use PHPUnit\Event\Test\Errored;
use PHPUnit\Event\Test\ErrorTriggered;
use PHPUnit\Event\Test\Failed;
use PHPUnit\Event\Test\Finished;
use PHPUnit\Event\Test\MarkedIncomplete;
use PHPUnit\Event\Test\NoticeTriggered;
use PHPUnit\Event\Test\Passed;
use PHPUnit\Event\Test\PhpNoticeTriggered;
use PHPUnit\Event\Test\PhpunitErrorTriggered;
use PHPUnit\Event\Test\PhpunitNoticeTriggered;
use PHPUnit\Event\Test\PhpunitWarningTriggered;
use PHPUnit\Event\Test\PhpWarningTriggered;
use PHPUnit\Event\Test\Skipped;
use PHPUnit\Event\Test\WarningTriggered;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;

class PHPUnitJSONReporter implements Extension
{
public const FILENAME = 'phpunit-report.json';
public const ENVIRONMENT_VARIABLE_DIRECTORY_NAME = 'REPORTER_OUTPUT_PATH';

private array $data = [];
private NamePrettifier $prettifier;

public function __construct()
{
$this->prettifier = new NamePrettifier();
}

public function printResult(TestResult $result): void
public function bootstrap(
Configuration $configuration,
Facade $facade,
ParameterCollection $parameters
): void {
$facade->registerTracer(new PHPUnitTracer($this));
}

public function __destruct()
{
$this->saveResult();
}

public function saveResult(): void
{
$path = getenv(self::ENVIRONMENT_VARIABLE_DIRECTORY_NAME) ?: getcwd();
ksort($this->data);
Expand All @@ -43,96 +60,78 @@
);
}

public function write(string $buffer): void
{
$this->data = [];
}

public function addError(Test $test, Throwable $t, float $time): void
{
$this->logErroredTest($test, $t);
}

public function addWarning(Test $test, Warning $e, float $time): void
{
$this->logErroredTest($test, $e);
}

public function addFailure(Test $test, AssertionFailedError $e, float $time): void
{
$this->logErroredTest($test, $e);
}

public function addIncompleteTest(Test $test, Throwable $t, float $time): void
{
$this->logErroredTest($test, $t);
}

public function addRiskyTest(Test $test, Throwable $t, float $time): void
{
$this->logErroredTest($test, $t);
}

public function addSkippedTest(Test $test, Throwable $t, float $time): void
{
$this->logErroredTest($test, $t);
}

public function startTestSuite(TestSuite $suite): void
public function logPassed(Passed|Finished $event): void
{
}

public function endTestSuite(TestSuite $suite): void
{
}

public function startTest(Test $test): void
{
}

public function endTest(Test $test, float $time): void
{
if (!$test instanceof TestCase) {
return;
}
if ($test->getStatus() !== BaseTestRunner::STATUS_PASSED) {
$parsedName = $event->test()->id();
if (array_key_exists($parsedName, $this->data)) {
return;
}

$parsedName = $this->parseName($test);

$this->data[$parsedName] = [
'file' => $this->parseFilename($test),
'file' => $event->test()->file(),
'test' => $parsedName,
'time' => $event->telemetryInfo()->durationSincePrevious()->asFloat(),
'status' => 'ok',
'stacktrace' => [],
];
}

private function parseName(Test $test): string
public function logErrored(Failed|MarkedIncomplete|Errored $event): void
{
if ($test instanceof TestCase) {
return $test::class . '::' . $test->getName(true);
$parsedName = $event->test()->id();
if (array_key_exists($parsedName, $this->data)) {
return;
}
return $this->prettifier->prettifyTestClass($test::class);
}

private function parseFilename(Test $test): string
{
$reflection = new ReflectionClass($test);

return $reflection->getFileName();
$this->data[$parsedName] = [
'file' => $event->test()->file(),
'test' => $parsedName,
'time' => $event->telemetryInfo()->durationSincePrevious()->asFloat(),
'status' => $event->throwable()->message(),
'stacktrace' => $event->throwable()->stackTrace(),
];
}

private function logErroredTest(Test $test, Throwable $t): void
public function logMessage(WarningTriggered|PhpWarningTriggered|PhpunitWarningTriggered|NoticeTriggered|PhpNoticeTriggered|PhpunitNoticeTriggered|ErrorTriggered|PhpunitErrorTriggered|Skipped|ConsideredRisky $event): void

Check failure on line 95 in src/Inspector/Test/PHPUnitJSONReporter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Inspector/Test/PHPUnitJSONReporter.php:95:32: UndefinedClass: Class, interface or enum named PHPUnit\Event\Test\PhpunitNoticeTriggered does not exist (see https://psalm.dev/019)

Check failure on line 95 in src/Inspector/Test/PHPUnitJSONReporter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedClass

src/Inspector/Test/PHPUnitJSONReporter.php:95:32: UndefinedClass: Class, interface or enum named PHPUnit\Event\Test\PhpunitNoticeTriggered does not exist (see https://psalm.dev/019)
{
$parsedName = $this->parseName($test);
$parsedName = $event->test()->id();
if (array_key_exists($parsedName, $this->data)) {
return;
}

$this->data[$parsedName] = [
'file' => $this->parseFilename($test),
'file' => $event->test()->file(),
'test' => $parsedName,
'status' => $t->getMessage(),
'stacktrace' => $t->getTrace(),
'time' => $event->telemetryInfo()->durationSincePrevious()->asFloat(),
'status' => $event->message(),
'stacktrace' => [],
];
}

public function log(Event $event)
{
if ($event instanceof Passed
|| $event instanceof Finished
) {
$this->logPassed($event);
} elseif ($event instanceof Failed
|| $event instanceof Errored
|| $event instanceof MarkedIncomplete
) {
$this->logErrored($event);
} elseif (
$event instanceof WarningTriggered
|| $event instanceof PhpWarningTriggered
|| $event instanceof PhpunitWarningTriggered
|| $event instanceof NoticeTriggered
|| $event instanceof PhpNoticeTriggered
|| $event instanceof PhpunitNoticeTriggered

Check failure on line 128 in src/Inspector/Test/PHPUnitJSONReporter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Inspector/Test/PHPUnitJSONReporter.php:128:34: UndefinedClass: Class, interface or enum named PHPUnit\Event\Test\PhpunitNoticeTriggered does not exist (see https://psalm.dev/019)

Check failure on line 128 in src/Inspector/Test/PHPUnitJSONReporter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedClass

src/Inspector/Test/PHPUnitJSONReporter.php:128:34: UndefinedClass: Class, interface or enum named PHPUnit\Event\Test\PhpunitNoticeTriggered does not exist (see https://psalm.dev/019)
|| $event instanceof ErrorTriggered
|| $event instanceof PhpunitErrorTriggered
|| $event instanceof Skipped
|| $event instanceof ConsideredRisky
) {
$this->logMessage($event);
}
}
}
23 changes: 23 additions & 0 deletions src/Inspector/Test/PHPUnitTracer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

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

use PHPUnit\Event\Event;
use PHPUnit\Event\Tracer\Tracer;

/**
* @psalm-suppress InternalClass, InternalMethod
*/
class PHPUnitTracer implements Tracer
{
public function __construct(private readonly PHPUnitJSONReporter $reporter)
{
}

public function trace(Event $event): void
{
$this->reporter->log($event);
}
}
Loading