|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Inspector\Test; |
|
6
|
|
|
|
|
7
|
|
|
use Codeception\Event\FailEvent; |
|
8
|
|
|
use Codeception\Event\PrintResultEvent; |
|
9
|
|
|
use Codeception\Event\TestEvent; |
|
10
|
|
|
use Codeception\Events; |
|
11
|
|
|
use Codeception\Extension; |
|
12
|
|
|
use Codeception\Test\Descriptor; |
|
13
|
|
|
use ReflectionClass; |
|
14
|
|
|
use ReflectionObject; |
|
15
|
|
|
|
|
16
|
|
|
final class CodeceptionJSONReporter extends Extension |
|
17
|
|
|
{ |
|
18
|
|
|
protected array $config = [ |
|
19
|
|
|
'output-path' => __DIR__, |
|
20
|
|
|
]; |
|
21
|
|
|
private array $data = []; |
|
22
|
|
|
public const FILENAME = 'codeception-report.json'; |
|
23
|
|
|
|
|
24
|
|
|
public function _initialize(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->_reconfigure(['settings' => ['silent' => true]]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static array $events = [ |
|
30
|
|
|
Events::TEST_SUCCESS => 'success', |
|
31
|
|
|
Events::TEST_FAIL => 'fail', |
|
32
|
|
|
Events::TEST_ERROR => 'error', |
|
33
|
|
|
Events::RESULT_PRINT_AFTER => 'all', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
public function success(TestEvent $event): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->data[] = [ |
|
39
|
|
|
'file' => $this->getTestFilename($event), |
|
40
|
|
|
'test' => $this->getTestName($event), |
|
41
|
|
|
'time' => $event->getTime(), |
|
42
|
|
|
'status' => 'ok', |
|
43
|
|
|
'stacktrace' => [], |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function fail(FailEvent $event): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->data[] = [ |
|
50
|
|
|
'file' => $this->getTestFilename($event), |
|
51
|
|
|
'test' => $this->getTestName($event), |
|
52
|
|
|
'time' => $event->getTime(), |
|
53
|
|
|
'status' => 'fail', |
|
54
|
|
|
'stacktrace' => $event->getFail()->getTrace(), |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function error(FailEvent $event): void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->data[] = [ |
|
61
|
|
|
'file' => $this->getTestFilename($event), |
|
62
|
|
|
'test' => $this->getTestName($event), |
|
63
|
|
|
'time' => $event->getTime(), |
|
64
|
|
|
'status' => 'error', |
|
65
|
|
|
'stacktrace' => $event->getFail()->getTrace(), |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function all(PrintResultEvent $event): void |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
file_put_contents( |
|
72
|
|
|
$this->config['output-path'] . DIRECTORY_SEPARATOR . self::FILENAME, |
|
73
|
|
|
json_encode($this->data, JSON_THROW_ON_ERROR) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function getTestName(TestEvent $event): string |
|
78
|
|
|
{ |
|
79
|
|
|
return Descriptor::getTestFullName($event->getTest()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function getTestFilename(TestEvent $event): string |
|
83
|
|
|
{ |
|
84
|
|
|
$test = new ReflectionObject($event->getTest()); |
|
85
|
|
|
if ($test->hasProperty('testClass') && $test->hasProperty('testMethod')) { |
|
86
|
|
|
$class = $test->getProperty('testClass')->getValue($event->getTest()); |
|
87
|
|
|
$method = $test->getProperty('testMethod')->getValue($event->getTest()); |
|
88
|
|
|
|
|
89
|
|
|
$classReflection = new ReflectionClass($class); |
|
90
|
|
|
$methodReflection = $classReflection->getMethod($method); |
|
91
|
|
|
|
|
92
|
|
|
return $classReflection->getFileName() . ':' . $methodReflection->getStartLine(); |
|
93
|
|
|
} |
|
94
|
|
|
return Descriptor::getTestFileName($event->getTest()); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.