|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Simplex\Quickstart\Shared\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
|
6
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
|
7
|
|
|
use PHPUnit\Framework\Test; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use PHPUnit\Framework\TestFailure; |
|
10
|
|
|
use PHPUnit\Framework\Warning; |
|
11
|
|
|
use PHPUnit\Runner\PhptTestCase; |
|
12
|
|
|
use PHPUnit\TextUI\ResultPrinter as PhpUnitResultPrinter; |
|
13
|
|
|
use SebastianBergmann\Comparator\ComparisonFailure; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @SuppressWarnings(PHPMD) |
|
17
|
|
|
*/ |
|
18
|
|
|
class TestResultPrinter extends PhpUnitResultPrinter |
|
19
|
|
|
{ |
|
20
|
|
|
const INDENT = ' '; |
|
21
|
|
|
|
|
22
|
|
|
public function endTest(Test $test, $time) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!$this->lastTestFailed) { |
|
25
|
|
|
$this->write(self::INDENT); |
|
26
|
|
|
$this->writeWithColor('fg-green', '✓ ', false); |
|
27
|
|
|
$this->printDescription($test, $time); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if ($test instanceof TestCase) { |
|
31
|
|
|
$this->numAssertions += $test->getNumAssertions(); |
|
32
|
|
|
} elseif ($test instanceof PhptTestCase) { |
|
33
|
|
|
$this->numAssertions++; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->lastTestFailed = false; |
|
37
|
|
|
|
|
38
|
|
|
if ($test instanceof TestCase) { |
|
39
|
|
|
if (!$test->hasExpectationOnOutput()) { |
|
40
|
|
|
$this->write($test->getActualOutput()); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function printDescription(Test $test, $time) |
|
46
|
|
|
{ |
|
47
|
|
|
$raw =\PHPUnit\Util\Test::describe($test); |
|
48
|
|
|
$parts = explode('::', $raw); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$this->writeWithColor( |
|
51
|
|
|
'fg-cyan, bold', |
|
52
|
|
|
$parts[0] . ': ', |
|
53
|
|
|
false |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
if (count($parts) === 1) { |
|
57
|
|
|
$this->write("\n"); |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$normalized = str_replace('_', ' ', $parts[1]); |
|
62
|
|
|
|
|
63
|
|
|
$this->writeWithColor( |
|
64
|
|
|
'fg-white', |
|
65
|
|
|
$normalized, |
|
66
|
|
|
false |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$this->writeWithColor( |
|
70
|
|
|
'fg-white', |
|
71
|
|
|
$this->getTimeString($time) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getTimeString($time) |
|
76
|
|
|
{ |
|
77
|
|
|
$ms = round($time * 1000); |
|
78
|
|
|
|
|
79
|
|
|
return ' ' . (string) $ms . 'ms'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
public function addError(Test $test, \Exception $e, $time) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$this->write(self::INDENT); |
|
85
|
|
|
$this->writeWithColor('fg-red', '✘ ', false); |
|
86
|
|
|
$this->printDescription($test, $time); |
|
87
|
|
|
|
|
88
|
|
|
$this->lastTestFailed = true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
public function addFailure(Test $test, AssertionFailedError $e, $time) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$this->write(self::INDENT); |
|
94
|
|
|
$this->writeWithColor('fg-red', '✘ ', false); |
|
95
|
|
|
$this->printDescription($test, $time); |
|
96
|
|
|
$this->lastTestFailed = true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function addWarning(Test $test, Warning $e, $time) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->lastTestFailed = true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function addIncompleteTest(Test $test, \Exception $e, $time) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->lastTestFailed = true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function addRiskyTest(Test $test, \Exception $e, $time) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->lastTestFailed = true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function addSkippedTest(Test $test, \Exception $e, $time) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->lastTestFailed = true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
protected function printDefectTrace(TestFailure $defect) |
|
120
|
|
|
{ |
|
121
|
|
|
$exception = $defect->thrownException(); |
|
122
|
|
|
|
|
123
|
|
|
$this->printException($exception); |
|
124
|
|
|
|
|
125
|
|
|
if (!$exception instanceof ExpectationFailedException) { |
|
126
|
|
|
|
|
127
|
|
|
$this->printGotoTip($exception); |
|
128
|
|
|
|
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$failure = $exception->getComparisonFailure(); |
|
133
|
|
|
|
|
134
|
|
|
if (null === $failure) { |
|
135
|
|
|
return; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$this->printComparison($failure); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
private function printException(\Exception $exception): void |
|
142
|
|
|
{ |
|
143
|
|
|
$this->write("\n"); |
|
144
|
|
|
|
|
145
|
|
|
$lines = explode("\n", (string) $exception); |
|
146
|
|
|
|
|
147
|
|
View Code Duplication |
foreach ($lines as $line) { |
|
|
|
|
|
|
148
|
|
|
$this->write(self::INDENT); |
|
149
|
|
|
$this->writeWithColor('fg-red, bg-red', ' ', false); |
|
150
|
|
|
$this->write(' '); |
|
151
|
|
|
$this->writeWithColor('fg-white', $line); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
while ($previous = $exception->getPrevious()) { |
|
155
|
|
|
$this->write("\nCaused by\n" . $previous); |
|
156
|
|
|
// @todo: format previous |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
protected function printGotoTip(\Exception $exception): void |
|
161
|
|
|
{ |
|
162
|
|
|
$file = new \SplFileInfo($exception->getFile()); |
|
163
|
|
|
$this->write(self::INDENT); |
|
164
|
|
|
$this->writeWithColor('fg-red, bg-red', ' ', false); |
|
165
|
|
|
$this->writeWithColor('fg-white, bold', ' Goto: ' . $file->getBasename() . ':' . $exception->getLine()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
private function printComparison(ComparisonFailure $failure): void |
|
169
|
|
|
{ |
|
170
|
|
|
$expected = $failure->getExpectedAsString(); |
|
171
|
|
|
if (empty($expected)) { |
|
172
|
|
|
$expected = $failure->getExpected(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$actual = $failure->getActualAsString(); |
|
176
|
|
|
if (empty($actual)) { |
|
177
|
|
|
$actual = $failure->getActual(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$this->write("\n"); |
|
181
|
|
|
|
|
182
|
|
|
$this->writeWithColor('fg-cyan, bold', self::INDENT . "Expected:"); |
|
183
|
|
|
|
|
184
|
|
|
$lines = explode("\n", $expected); |
|
185
|
|
View Code Duplication |
foreach ($lines as $line) { |
|
|
|
|
|
|
186
|
|
|
$this->write(self::INDENT); |
|
187
|
|
|
$this->writeWithColor('fg-red, bg-red', ' ', false); |
|
188
|
|
|
$this->write(' '); |
|
189
|
|
|
$this->writeWithColor('fg-white, bold', $line); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$this->write("\n"); |
|
193
|
|
|
|
|
194
|
|
|
$this->writeWithColor('fg-cyan, bold', self::INDENT . "Actual"); |
|
195
|
|
|
|
|
196
|
|
|
$lines = explode("\n", $actual); |
|
197
|
|
View Code Duplication |
foreach ($lines as $line) { |
|
|
|
|
|
|
198
|
|
|
$this->write(self::INDENT); |
|
199
|
|
|
$this->writeWithColor('fg-green, bg-green', ' ', false); |
|
200
|
|
|
$this->write(' '); |
|
201
|
|
|
$this->writeWithColor('fg-white, bold', $line); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|