1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Inspector\Test; |
6
|
|
|
|
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
|
|
|
class PHPUnitJSONReporter implements ResultPrinter |
20
|
|
|
{ |
21
|
|
|
public const FILENAME = 'phpunit-report.json'; |
22
|
|
|
public const ENVIRONMENT_VARIABLE_DIRECTORY_NAME = 'REPORTER_OUTPUT_PATH'; |
23
|
|
|
|
24
|
|
|
private array $data = []; |
25
|
|
|
private NamePrettifier $prettifier; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->prettifier = new NamePrettifier(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function printResult(TestResult $result): void |
33
|
|
|
{ |
34
|
|
|
$path = getenv(self::ENVIRONMENT_VARIABLE_DIRECTORY_NAME) ?: getcwd(); |
35
|
|
|
ksort($this->data); |
36
|
|
|
|
37
|
|
|
file_put_contents( |
38
|
|
|
$path . DIRECTORY_SEPARATOR . self::FILENAME, |
39
|
|
|
json_encode(array_values($this->data)) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function write(string $buffer): void |
44
|
|
|
{ |
45
|
|
|
$this->data = []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function addError(Test $test, Throwable $t, float $time): void |
49
|
|
|
{ |
50
|
|
|
$this->logErroredTest($test, $t); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function addWarning(Test $test, Warning $e, float $time): void |
54
|
|
|
{ |
55
|
|
|
$this->logErroredTest($test, $e); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function addFailure(Test $test, AssertionFailedError $e, float $time): void |
59
|
|
|
{ |
60
|
|
|
$this->logErroredTest($test, $e); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function addIncompleteTest(Test $test, Throwable $t, float $time): void |
64
|
|
|
{ |
65
|
|
|
$this->logErroredTest($test, $t); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function addRiskyTest(Test $test, Throwable $t, float $time): void |
69
|
|
|
{ |
70
|
|
|
$this->logErroredTest($test, $t); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function addSkippedTest(Test $test, Throwable $t, float $time): void |
74
|
|
|
{ |
75
|
|
|
$this->logErroredTest($test, $t); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function startTestSuite(TestSuite $suite): void |
79
|
|
|
{ |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function endTestSuite(TestSuite $suite): void |
83
|
|
|
{ |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function startTest(Test $test): void |
87
|
|
|
{ |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function endTest(Test $test, float $time): void |
91
|
|
|
{ |
92
|
|
|
if (!$test instanceof TestCase) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
if ($test->getStatus() !== BaseTestRunner::STATUS_PASSED) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$parsedName = $this->parseName($test); |
100
|
|
|
|
101
|
|
|
$this->data[$parsedName] = [ |
102
|
|
|
'file' => $this->parseFilename($test), |
103
|
|
|
'test' => $parsedName, |
104
|
|
|
'status' => 'ok', |
105
|
|
|
'stacktrace' => [], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function parseName(Test $test): string |
110
|
|
|
{ |
111
|
|
|
if ($test instanceof TestCase) { |
112
|
|
|
return $test::class . '::' . $test->getName(true); |
113
|
|
|
} |
114
|
|
|
return $this->prettifier->prettifyTestClass($test::class); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function parseFilename(Test $test): string |
118
|
|
|
{ |
119
|
|
|
$reflection = new ReflectionClass($test); |
120
|
|
|
|
121
|
|
|
return $reflection->getFileName(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function logErroredTest(Test $test, Throwable $t): void |
125
|
|
|
{ |
126
|
|
|
$parsedName = $this->parseName($test); |
127
|
|
|
|
128
|
|
|
$this->data[$parsedName] = [ |
129
|
|
|
'file' => $this->parseFilename($test), |
130
|
|
|
'test' => $parsedName, |
131
|
|
|
'status' => $t->getMessage(), |
132
|
|
|
'stacktrace' => $t->getTrace(), |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|