|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Collision. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Nuno Maduro <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace NunoMaduro\Collision\Adapters\Phpunit; |
|
13
|
|
|
|
|
14
|
|
|
use NunoMaduro\Collision\Writer; |
|
15
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
|
16
|
|
|
use PHPUnit\Framework\ExceptionWrapper; |
|
17
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
20
|
|
|
use Symfony\Component\Console\Output\ConsoleSectionOutput; |
|
21
|
|
|
use Throwable; |
|
22
|
|
|
use Whoops\Exception\Inspector; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @internal |
|
26
|
|
|
*/ |
|
27
|
|
|
final class Style |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ConsoleOutput |
|
31
|
|
|
*/ |
|
32
|
|
|
private $output; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ConsoleSectionOutput |
|
36
|
|
|
*/ |
|
37
|
|
|
private $footer; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Style constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param ConsoleOutput $output |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function __construct(ConsoleOutput $output) |
|
45
|
|
|
{ |
|
46
|
3 |
|
$this->output = $output; |
|
47
|
|
|
|
|
48
|
3 |
|
$this->footer = $output->section(); |
|
49
|
3 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Prints the content similar too:. |
|
53
|
|
|
* |
|
54
|
|
|
* ``` |
|
55
|
|
|
* PASS Unit\ExampleTest |
|
56
|
|
|
* ✓ basic test |
|
57
|
|
|
* ``` |
|
58
|
|
|
* |
|
59
|
|
|
* @param State $state |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function writeCurrentRecap(State $state): void |
|
64
|
|
|
{ |
|
65
|
|
|
if (! $state->testCaseTestsCount()) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->footer->clear(); |
|
70
|
|
|
|
|
71
|
|
|
$this->output->writeln($this->titleLineFrom( |
|
72
|
|
|
$state->getTestCaseTitle() === 'FAIL' ? 'white' : 'black', |
|
73
|
|
|
$state->getTestCaseTitleColor(), |
|
74
|
|
|
$state->getTestCaseTitle(), |
|
75
|
|
|
$state->testCaseName |
|
76
|
|
|
)); |
|
77
|
|
|
|
|
78
|
|
|
$state->eachTestCaseTests(function (TestResult $testResult) { |
|
79
|
|
|
$this->output->writeln($this->testLineFrom( |
|
80
|
|
|
$testResult->color, |
|
81
|
|
|
$testResult->icon, |
|
82
|
|
|
$testResult->description, |
|
83
|
|
|
$testResult->warning |
|
84
|
|
|
)); |
|
85
|
|
|
}); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Prints the content similar too on the footer. Where |
|
90
|
|
|
* we are updating the current test. |
|
91
|
|
|
* |
|
92
|
|
|
* ``` |
|
93
|
|
|
* Runs Unit\ExampleTest |
|
94
|
|
|
* • basic test |
|
95
|
|
|
* ``` |
|
96
|
|
|
* |
|
97
|
|
|
* @param State $state |
|
98
|
|
|
* @param TestCase|null $testCase |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
public function updateFooter(State $state, TestCase $testCase = null): void |
|
103
|
|
|
{ |
|
104
|
|
|
$runs = []; |
|
105
|
|
|
|
|
106
|
|
|
if ($testCase) { |
|
107
|
|
|
$runs[] = $this->titleLineFrom( |
|
108
|
|
|
'black', |
|
109
|
|
|
'yellow', |
|
110
|
|
|
'RUNS', |
|
111
|
|
|
get_class($testCase) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$testResult = TestResult::fromTestCase($testCase, TestResult::RUNS); |
|
115
|
|
|
$runs[] = $this->testLineFrom( |
|
116
|
|
|
$testResult->color, |
|
117
|
|
|
$testResult->icon, |
|
118
|
|
|
$testResult->description |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$types = [TestResult::FAIL, TestResult::WARN, TestResult::RISKY, TestResult::INCOMPLETE, TestResult::SKIPPED, TestResult::PASS]; |
|
123
|
|
|
|
|
124
|
|
|
foreach ($types as $type) { |
|
125
|
|
|
if ($countTests = $state->countTestsInTestSuiteBy($type)) { |
|
126
|
|
|
$color = TestResult::makeColor($type); |
|
127
|
|
|
$tests[] = "<fg=$color;options=bold>$countTests $type</>"; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$pending = $state->suiteTotalTests - $state->testSuiteTestsCount(); |
|
132
|
|
|
if ($pending) { |
|
133
|
|
|
$tests[] = "\e[2m$pending pending\e[22m"; |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (! empty($tests)) { |
|
137
|
|
|
$this->footer->overwrite(array_merge($runs, [ |
|
138
|
|
|
'', |
|
139
|
|
|
sprintf( |
|
140
|
|
|
' <fg=white;options=bold>Tests: </><fg=default>%s</>', |
|
141
|
|
|
implode(', ', $tests) |
|
142
|
|
|
), |
|
143
|
|
|
])); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Writes the final recap. |
|
149
|
|
|
* |
|
150
|
|
|
* @param Timer $timer |
|
151
|
|
|
*/ |
|
152
|
|
|
public function writeRecap(Timer $timer): void |
|
153
|
|
|
{ |
|
154
|
|
|
$timeElapsed = number_format($timer->result(), 2, '.', ''); |
|
155
|
|
|
$this->footer->writeln( |
|
156
|
|
|
sprintf( |
|
157
|
|
|
' <fg=white;options=bold>Time: </><fg=default>%ss</>', |
|
158
|
|
|
$timeElapsed |
|
159
|
|
|
) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Displays the error using Collision's writer |
|
165
|
|
|
* and terminates with exit code === 1. |
|
166
|
|
|
* |
|
167
|
|
|
* @param Throwable $throwable |
|
168
|
|
|
* |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function writeError(State $state, Throwable $throwable) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->writeCurrentRecap($state); |
|
174
|
|
|
|
|
175
|
|
|
$this->updateFooter($state); |
|
176
|
|
|
|
|
177
|
|
|
$writer = (new Writer())->setOutput($this->output); |
|
178
|
|
|
|
|
179
|
|
|
if ($throwable instanceof AssertionFailedError) { |
|
180
|
|
|
$writer->showTitle(false); |
|
181
|
|
|
$this->output->write('', true); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$writer->ignoreFilesIn([ |
|
185
|
|
|
'/vendor\/phpunit\/phpunit\/src/', |
|
186
|
|
|
'/vendor\/mockery\/mockery/', |
|
187
|
|
|
'/vendor\/laravel\/framework\/src\/Illuminate\/Testing/', |
|
188
|
|
|
'/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Testing/', |
|
189
|
|
|
]); |
|
190
|
|
|
|
|
191
|
|
|
if ($throwable instanceof ExceptionWrapper && $throwable->getOriginalException() !== null) { |
|
192
|
|
|
$throwable = $throwable->getOriginalException(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$inspector = new Inspector($throwable); |
|
196
|
|
|
|
|
197
|
|
|
$writer->write($inspector); |
|
198
|
|
|
|
|
199
|
|
|
if ($throwable instanceof ExpectationFailedException && $comparisionFailure = $throwable->getComparisonFailure()) { |
|
200
|
|
|
$this->output->write($comparisionFailure->getDiff()); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
exit(1); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns the title contents. |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $fg |
|
210
|
|
|
* @param string $bg |
|
211
|
|
|
* @param string $title |
|
212
|
|
|
* @param string $testCaseName |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
|
|
private function titleLineFrom(string $fg, string $bg, string $title, string $testCaseName): string |
|
217
|
|
|
{ |
|
218
|
|
|
if (class_exists($testCaseName)) { |
|
219
|
|
|
$nameParts = explode('\\', $testCaseName); |
|
220
|
|
|
$highlightedPart = array_pop($nameParts); |
|
221
|
|
|
$nonHighlightedPart = implode('\\', $nameParts); |
|
222
|
|
|
$testCaseName = sprintf("\e[2m%s\e[22m<fg=white;options=bold>%s</>", "$nonHighlightedPart\\", $highlightedPart); |
|
223
|
|
|
} else if (file_exists($testCaseName)) { |
|
224
|
|
|
$testCaseName = substr($testCaseName, strlen((string) getcwd())+1); |
|
225
|
|
|
$nameParts = explode(DIRECTORY_SEPARATOR, $testCaseName); |
|
226
|
|
|
$highlightedPart = (string) array_pop($nameParts); |
|
227
|
|
|
$highlightedPart = substr($highlightedPart, 0, (int) strrpos($highlightedPart, ".")); |
|
228
|
|
|
$nonHighlightedPart = implode('\\', $nameParts); |
|
229
|
|
|
$testCaseName = sprintf("\e[2m%s\e[22m<fg=white;options=bold>%s</>", "$nonHighlightedPart\\", $highlightedPart); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return sprintf( |
|
233
|
|
|
"\n <fg=%s;bg=%s;options=bold> %s </><fg=default> %s</>", |
|
234
|
|
|
$fg, |
|
235
|
|
|
$bg, |
|
236
|
|
|
$title, |
|
237
|
|
|
$testCaseName |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Returns the test contents. |
|
243
|
|
|
* |
|
244
|
|
|
* @param string $fg |
|
245
|
|
|
* @param string $icon |
|
246
|
|
|
* @param string $description |
|
247
|
|
|
* |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
|
|
private function testLineFrom(string $fg, string $icon, string $description, string $warning = null): string |
|
251
|
|
|
{ |
|
252
|
|
|
if (! empty($warning)) { |
|
253
|
|
|
$warning = sprintf( |
|
254
|
|
|
' → %s', |
|
255
|
|
|
$warning |
|
256
|
|
|
); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
return sprintf( |
|
260
|
|
|
" <fg=%s;options=bold>%s</><fg=default> \e[2m%s\e[22m</><fg=yellow>%s</>", |
|
261
|
|
|
$fg, |
|
262
|
|
|
$icon, |
|
263
|
|
|
$description, |
|
264
|
|
|
$warning |
|
265
|
|
|
); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.