1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of PHPUnit. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Bergmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace EdmondsCommerce\PHPQA\PHPUnit\TestDox; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
14
|
|
|
use PHPUnit\Framework\Test; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use PHPUnit\Framework\TestResult; |
|
|
|
|
17
|
|
|
use PHPUnit\Framework\Warning; |
18
|
|
|
use PHPUnit\Runner\PhptTestCase; |
19
|
|
|
use PHPUnit\TextUI\ResultPrinter; |
20
|
|
|
use PHPUnit\Util\TestDox\NamePrettifier; |
21
|
|
|
use SebastianBergmann\Timer\Timer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This printer is for CLI output only. For the classes that output to file, html and xml, |
25
|
|
|
* please refer to the PHPUnit\Util\TestDox namespace |
26
|
|
|
*/ |
27
|
|
|
class CliTestDoxPrinter extends ResultPrinter |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var TestDoxTestResult |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
private $currentTestResult; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TestDoxTestResult |
36
|
|
|
*/ |
37
|
|
|
private $previousTestResult; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TestDoxTestResult[] |
41
|
|
|
*/ |
42
|
|
|
private $nonSuccessfulTestResults = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var NamePrettifier |
46
|
|
|
*/ |
47
|
|
|
private $prettifier; |
48
|
|
|
|
49
|
|
|
public function __construct( |
50
|
|
|
$out = null, |
51
|
|
|
bool $verbose = false, |
52
|
|
|
$colors = self::COLOR_DEFAULT, |
53
|
|
|
bool $debug = false, |
54
|
|
|
$numberOfColumns = 80, |
55
|
|
|
bool $reverse = false |
56
|
|
|
) { |
57
|
|
|
parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns, $reverse); |
58
|
|
|
|
59
|
|
|
$this->prettifier = new NamePrettifier(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function startTest(Test $test): void |
63
|
|
|
{ |
64
|
|
|
if (!$test instanceof TestCase && !$test instanceof PhptTestCase) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$class = \get_class($test); |
69
|
|
|
|
70
|
|
|
if ($test instanceof TestCase) { |
71
|
|
|
$annotations = $test->getAnnotations(); |
72
|
|
|
|
73
|
|
|
if (isset($annotations['class']['testdox'][0])) { |
74
|
|
|
$className = $annotations['class']['testdox'][0]; |
75
|
|
|
} else { |
76
|
|
|
$className = $this->prettifier->prettifyTestClass($class); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (isset($annotations['method']['testdox'][0])) { |
80
|
|
|
$testMethod = $annotations['method']['testdox'][0]; |
81
|
|
|
} else { |
82
|
|
|
$testMethod = $this->prettifier->prettifyTestMethod($test->getName(false)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$testMethod .= \substr($test->getDataSetAsString(false), 5); |
86
|
|
|
} elseif ($test instanceof PhptTestCase) { |
|
|
|
|
87
|
|
|
$className = $class; |
88
|
|
|
$testMethod = $test->getName(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->currentTestResult = new \EdmondsCommerce\PHPQA\PHPUnit\TestDox\TestResult( |
|
|
|
|
92
|
|
|
function (string $color, string $buffer) { |
93
|
|
|
return $this->formatWithColor($color, $buffer); |
94
|
|
|
}, |
95
|
|
|
$className, |
|
|
|
|
96
|
|
|
$testMethod |
|
|
|
|
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
parent::startTest($test); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function endTest(Test $test, float $time): void |
103
|
|
|
{ |
104
|
|
|
if (!$test instanceof TestCase && !$test instanceof PhptTestCase) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
parent::endTest($test, $time); |
109
|
|
|
|
110
|
|
|
$this->currentTestResult->setRuntime($time); |
111
|
|
|
|
112
|
|
|
$this->write($this->currentTestResult->toString($this->previousTestResult, $this->verbose)); |
113
|
|
|
|
114
|
|
|
$this->previousTestResult = $this->currentTestResult; |
115
|
|
|
|
116
|
|
|
if (!$this->currentTestResult->isTestSuccessful()) { |
117
|
|
|
$this->nonSuccessfulTestResults[] = $this->currentTestResult; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function addError(Test $test, \Throwable $t, float $time): void |
122
|
|
|
{ |
123
|
|
|
$this->currentTestResult->fail( |
124
|
|
|
$this->formatWithColor('fg-yellow', '✘'), |
125
|
|
|
(string)$t |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function addWarning(Test $test, Warning $e, float $time): void |
130
|
|
|
{ |
131
|
|
|
$this->currentTestResult->fail( |
132
|
|
|
$this->formatWithColor('fg-yellow', '✘'), |
133
|
|
|
(string)$e |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function addFailure(Test $test, AssertionFailedError $e, float $time): void |
138
|
|
|
{ |
139
|
|
|
$this->currentTestResult->fail( |
140
|
|
|
$this->formatWithColor('fg-red', '✘'), |
141
|
|
|
(string)$e |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function addIncompleteTest(Test $test, \Throwable $t, float $time): void |
146
|
|
|
{ |
147
|
|
|
$this->currentTestResult->fail( |
148
|
|
|
$this->formatWithColor('fg-yellow', '∅'), |
149
|
|
|
(string)$t, |
150
|
|
|
true |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function addRiskyTest(Test $test, \Throwable $t, float $time): void |
155
|
|
|
{ |
156
|
|
|
$this->currentTestResult->fail( |
157
|
|
|
$this->formatWithColor('fg-yellow', '☢'), |
158
|
|
|
(string)$t, |
159
|
|
|
true |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function addSkippedTest(Test $test, \Throwable $t, float $time): void |
164
|
|
|
{ |
165
|
|
|
$this->currentTestResult->fail( |
166
|
|
|
$this->formatWithColor('fg-yellow', '→'), |
167
|
|
|
(string)$t, |
168
|
|
|
true |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function writeProgress(string $progress): void |
173
|
|
|
{ |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function flush(): void |
177
|
|
|
{ |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function printResult(TestResult $result): void |
181
|
|
|
{ |
182
|
|
|
$this->printHeader(); |
183
|
|
|
|
184
|
|
|
$this->printNonSuccessfulTestsSummary($result->count()); |
185
|
|
|
|
186
|
|
|
$this->printFooter($result); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function printHeader(): void |
190
|
|
|
{ |
191
|
|
|
$this->write("\n".Timer::resourceUsage()."\n\n"); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
private function printNonSuccessfulTestsSummary(int $numberOfExecutedTests): void |
195
|
|
|
{ |
196
|
|
|
$numberOfNonSuccessfulTests = \count($this->nonSuccessfulTestResults); |
197
|
|
|
|
198
|
|
|
if ($numberOfNonSuccessfulTests === 0) { |
199
|
|
|
return; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (($numberOfNonSuccessfulTests / $numberOfExecutedTests) >= 0.7) { |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->write("Summary of non-successful tests:\n\n"); |
207
|
|
|
|
208
|
|
|
$previousTestResult = null; |
209
|
|
|
|
210
|
|
|
foreach ($this->nonSuccessfulTestResults as $testResult) { |
211
|
|
|
$this->write($testResult->toString($previousTestResult, $this->verbose)); |
212
|
|
|
|
213
|
|
|
$previousTestResult = $testResult; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: