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 \EdmondsCommerce\PHPQA\PHPUnit\TestDox\TestResult |
31
|
|
|
*/ |
32
|
|
|
private $currentTestResult; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \EdmondsCommerce\PHPQA\PHPUnit\TestDox\TestResult |
36
|
|
|
*/ |
37
|
|
|
private $previousTestResult; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \EdmondsCommerce\PHPQA\PHPUnit\TestDox\TestResult[] |
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
|
|
|
$className = $testMethod = ''; |
68
|
|
|
|
69
|
|
|
$class = \get_class($test); |
70
|
|
|
|
71
|
|
|
if ($test instanceof TestCase) { |
72
|
|
|
$annotations = $test->getAnnotations(); |
73
|
|
|
|
74
|
|
|
if (isset($annotations['class']['testdox'][0])) { |
75
|
|
|
$className = $annotations['class']['testdox'][0]; |
76
|
|
|
} else { |
77
|
|
|
$className = $this->prettifier->prettifyTestClass($class); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (isset($annotations['method']['testdox'][0])) { |
81
|
|
|
$testMethod = $annotations['method']['testdox'][0]; |
82
|
|
|
} else { |
83
|
|
|
$testMethod = $this->prettifier->prettifyTestMethod((string)$test->getName(false)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$testMethod .= \substr($test->getDataSetAsString(false), 5); |
87
|
|
|
} elseif ($test instanceof PhptTestCase) { |
|
|
|
|
88
|
|
|
$className = $class; |
89
|
|
|
$testMethod = $test->getName(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->currentTestResult = new \EdmondsCommerce\PHPQA\PHPUnit\TestDox\TestResult( |
93
|
|
|
function (string $color, string $buffer) { |
94
|
|
|
return $this->formatWithColor($color, $buffer); |
95
|
|
|
}, |
96
|
|
|
$className, |
97
|
|
|
$testMethod |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
parent::startTest($test); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function endTest(Test $test, float $time): void |
104
|
|
|
{ |
105
|
|
|
if (!$test instanceof TestCase && !$test instanceof PhptTestCase) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
parent::endTest($test, $time); |
110
|
|
|
|
111
|
|
|
$this->currentTestResult->setRuntime($time); |
112
|
|
|
|
113
|
|
|
$this->write($this->currentTestResult->toString($this->previousTestResult, $this->verbose)); |
114
|
|
|
|
115
|
|
|
$this->previousTestResult = $this->currentTestResult; |
116
|
|
|
|
117
|
|
|
if (!$this->currentTestResult->isTestSuccessful()) { |
118
|
|
|
$this->nonSuccessfulTestResults[] = $this->currentTestResult; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function addError(Test $test, \Throwable $t, float $time): void |
123
|
|
|
{ |
124
|
|
|
$this->currentTestResult->fail( |
125
|
|
|
$this->formatWithColor('fg-yellow', '✘'), |
126
|
|
|
(string)$t |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function addWarning(Test $test, Warning $e, float $time): void |
131
|
|
|
{ |
132
|
|
|
$this->currentTestResult->fail( |
133
|
|
|
$this->formatWithColor('fg-yellow', '✘'), |
134
|
|
|
(string)$e |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function addFailure(Test $test, AssertionFailedError $e, float $time): void |
139
|
|
|
{ |
140
|
|
|
$this->currentTestResult->fail( |
141
|
|
|
$this->formatWithColor('fg-red', '✘'), |
142
|
|
|
(string)$e |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function addIncompleteTest(Test $test, \Throwable $t, float $time): void |
147
|
|
|
{ |
148
|
|
|
$this->currentTestResult->fail( |
149
|
|
|
$this->formatWithColor('fg-yellow', '∅'), |
150
|
|
|
(string)$t, |
151
|
|
|
true |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function addRiskyTest(Test $test, \Throwable $t, float $time): void |
156
|
|
|
{ |
157
|
|
|
$this->currentTestResult->fail( |
158
|
|
|
$this->formatWithColor('fg-yellow', '☢'), |
159
|
|
|
(string)$t, |
160
|
|
|
true |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function addSkippedTest(Test $test, \Throwable $t, float $time): void |
165
|
|
|
{ |
166
|
|
|
$this->currentTestResult->fail( |
167
|
|
|
$this->formatWithColor('fg-yellow', '→'), |
168
|
|
|
(string)$t, |
169
|
|
|
true |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function writeProgress(string $progress): void |
174
|
|
|
{ |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function flush(): void |
178
|
|
|
{ |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function printResult(TestResult $result): void |
182
|
|
|
{ |
183
|
|
|
$this->printHeader(); |
184
|
|
|
|
185
|
|
|
$this->printNonSuccessfulTestsSummary($result->count()); |
186
|
|
|
|
187
|
|
|
$this->printFooter($result); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
protected function printHeader(): void |
191
|
|
|
{ |
192
|
|
|
$this->write("\n".Timer::resourceUsage()."\n\n"); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
private function printNonSuccessfulTestsSummary(int $numberOfExecutedTests): void |
196
|
|
|
{ |
197
|
|
|
$numberOfNonSuccessfulTests = \count($this->nonSuccessfulTestResults); |
198
|
|
|
|
199
|
|
|
if ($numberOfNonSuccessfulTests === 0) { |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (($numberOfNonSuccessfulTests / $numberOfExecutedTests) >= 0.7) { |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->write("Summary of non-successful tests:\n\n"); |
208
|
|
|
|
209
|
|
|
$previousTestResult = null; |
210
|
|
|
|
211
|
|
|
foreach ($this->nonSuccessfulTestResults as $testResult) { |
212
|
|
|
$this->write($testResult->toString($previousTestResult, $this->verbose)); |
213
|
|
|
|
214
|
|
|
$previousTestResult = $testResult; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
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: