|
1 | 1 | <?php |
2 | 2 | namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter; |
3 | 3 |
|
4 | | -class ImmediateExceptionPrinter extends \PHPUnit_TextUI_ResultPrinter |
5 | | -{ |
6 | | - /** |
7 | | - * The exception thrown by the last test. |
8 | | - * |
9 | | - * @var \Exception|null |
10 | | - */ |
11 | | - protected $exception; |
12 | | - |
13 | | - /** |
14 | | - * PHPUnit built-in progress indication character, e.g. E for error. |
15 | | - * |
16 | | - * @var string |
17 | | - */ |
18 | | - protected $progress; |
19 | | - |
20 | | - /** |
21 | | - * Last colour used by a progress indicator. |
22 | | - * |
23 | | - * @var string |
24 | | - */ |
25 | | - protected $lastColour; |
26 | | - |
27 | | - private static $performanceThresholds = [ |
28 | | - 'fg-red' => 1000, |
29 | | - 'fg-yellow' => 200, |
30 | | - 'fg-green' => 0, |
31 | | - ]; |
32 | | - |
33 | | - public function startTest(\PHPUnit_Framework_Test $test) |
34 | | - { |
35 | | - parent::startTest($test); |
36 | | - |
37 | | - $this->exception = $this->progress = null; |
38 | | - $this->lastColour = 'fg-green,bold'; |
39 | | - } |
40 | | - |
41 | | - protected function writeProgress($progress) |
| 4 | +if (class_exists(\PHPUnit_TextUI_ResultPrinter::class)) { |
| 5 | + class ImmediateExceptionPrinter extends PhpUnit5Printer |
42 | 6 | { |
43 | | - // Record progress so it can be written later instead of at start of line. |
44 | | - $this->progress = $progress; |
45 | | - |
46 | | - ++$this->numTestsRun; |
47 | 7 | } |
48 | | - |
49 | | - protected function writeProgressWithColor($color, $buffer) |
50 | | - { |
51 | | - parent::writeProgressWithColor($color, $buffer); |
52 | | - |
53 | | - $this->lastColour = $color; |
54 | | - } |
55 | | - |
56 | | - public function endTest(\PHPUnit_Framework_Test $test, $time) |
57 | | - { |
58 | | - parent::endTest($test, $time); |
59 | | - |
60 | | - $this->write(sprintf( |
61 | | - '%3d%% %s ', |
62 | | - floor($this->numTestsRun / $this->numTests * 100), |
63 | | - $this->progress |
64 | | - )); |
65 | | - $this->writeWithColor($this->lastColour, \PHPUnit_Util_Test::describe($test), false); |
66 | | - $this->writePerformance($time); |
67 | | - |
68 | | - if ($this->exception) { |
69 | | - $this->printExceptionTrace($this->exception); |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * Writes the test performance metric formatted as the number of milliseconds elapsed. |
75 | | - * |
76 | | - * @param float $time Number of seconds elapsed. |
77 | | - */ |
78 | | - protected function writePerformance($time) |
| 8 | +} else { |
| 9 | + class ImmediateExceptionPrinter extends PhpUnit6Printer |
79 | 10 | { |
80 | | - $ms = round($time * 1000); |
81 | | - |
82 | | - foreach (self::$performanceThresholds as $colour => $threshold) { |
83 | | - if ($ms > $threshold) { |
84 | | - break; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - $this->writeWithColor($colour, " ($ms ms)"); |
89 | | - } |
90 | | - |
91 | | - protected function printExceptionTrace(\Exception $exception) |
92 | | - { |
93 | | - $this->writeNewLine(); |
94 | | - |
95 | | - // Parse nested exception trace line by line. |
96 | | - foreach (explode("\n", $exception) as $line) { |
97 | | - // Print exception name and message. |
98 | | - if (!$exception instanceof \PHPUnit_Framework_AssertionFailedError |
99 | | - && false !== $pos = strpos($line, ': ') |
100 | | - ) { |
101 | | - $whitespace = str_repeat(' ', $pos + 2); |
102 | | - $this->writeWithColor('bg-red,fg-white', $whitespace); |
103 | | - |
104 | | - // Exception name. |
105 | | - $this->writeWithColor('bg-red,fg-white', sprintf(' %s ', substr($line, 0, $pos)), false); |
106 | | - // Exception message. |
107 | | - $this->writeWithColor('fg-red', substr($line, $pos + 1)); |
108 | | - |
109 | | - $this->writeWithColor('bg-red,fg-white', $whitespace); |
110 | | - |
111 | | - continue; |
112 | | - } |
113 | | - |
114 | | - $this->writeWithColor('fg-red', $line); |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - /** |
119 | | - * Called when an exception is thrown in the test runner. |
120 | | - * |
121 | | - * @param \PHPUnit_Framework_Test $test |
122 | | - * @param \Exception $e |
123 | | - * @param float $time |
124 | | - */ |
125 | | - public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
126 | | - { |
127 | | - $this->writeProgressWithColor('fg-red,bold', 'E'); |
128 | | - |
129 | | - $this->exception = $e; |
130 | | - $this->lastTestFailed = true; |
131 | | - } |
132 | | - |
133 | | - /** |
134 | | - * Called when an assertion fails in the test runner. |
135 | | - * |
136 | | - * @param \PHPUnit_Framework_Test $test |
137 | | - * @param \PHPUnit_Framework_AssertionFailedError $e |
138 | | - * @param float $time |
139 | | - */ |
140 | | - public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) |
141 | | - { |
142 | | - $this->writeProgressWithColor('fg-red,bold', 'F'); |
143 | | - |
144 | | - $this->exception = $e; |
145 | | - $this->lastTestFailed = true; |
146 | 11 | } |
147 | 12 | } |
0 commit comments