Skip to content

Commit 5bd117e

Browse files
committed
Added PHPUnit 6 support.
Split ImmediateExceptionPrinter into PhpUnit6Printer and PhpUnit5Printer backed by Printer trait.
1 parent 65bbdc9 commit 5bd117e

File tree

8 files changed

+270
-145
lines changed

8 files changed

+270
-145
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Not shown: the normal test summary output you're used to seeing at the end of a
5151
## Requirements
5252

5353
* PHP 5.6 or newer.
54-
* [PHPUnit][PHPUnit] 5.5 or newer. PHPUnit 6 not yet supported.
54+
* [PHPUnit][PHPUnit] 5.5 or newer.
5555

5656
## Testing
5757

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
],
1010
"require": {
11-
"phpunit/phpunit": "^5.5"
11+
"phpunit/phpunit": "^5.5|^6"
1212
},
1313
"autoload": {
1414
"psr-4": {

src/ImmediateExceptionPrinter.php

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,12 @@
11
<?php
22
namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter;
33

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
426
{
43-
// Record progress so it can be written later instead of at start of line.
44-
$this->progress = $progress;
45-
46-
++$this->numTestsRun;
477
}
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
7910
{
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;
14611
}
14712
}

src/PhpUnit5Printer.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter;
3+
4+
class PhpUnit5Printer extends \PHPUnit_TextUI_ResultPrinter
5+
{
6+
use Printer;
7+
8+
public function startTest(\PHPUnit_Framework_Test $test)
9+
{
10+
parent::startTest($test);
11+
12+
$this->onStartTest();
13+
}
14+
15+
protected function writeProgressWithColor($color, $buffer)
16+
{
17+
parent::writeProgressWithColor($color, $buffer);
18+
19+
$this->onWriteProgressWithColor($color);
20+
}
21+
22+
public function endTest(\PHPUnit_Framework_Test $test, $time)
23+
{
24+
parent::endTest($test, $time);
25+
26+
$this->onEndTest($test, $time);
27+
}
28+
29+
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
30+
{
31+
$this->onAddError($e);
32+
}
33+
34+
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
35+
{
36+
$this->onAddFailure($e);
37+
}
38+
}

src/PhpUnit6Printer.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter;
3+
4+
use PHPUnit\Framework\AssertionFailedError;
5+
use PHPUnit\Framework\Test;
6+
use PHPUnit\TextUI\ResultPrinter;
7+
8+
class PhpUnit6Printer extends ResultPrinter
9+
{
10+
use Printer;
11+
12+
public function startTest(Test $test)
13+
{
14+
parent::startTest($test);
15+
16+
$this->onStartTest();
17+
}
18+
19+
protected function writeProgressWithColor($color, $buffer)
20+
{
21+
parent::writeProgressWithColor($color, $buffer);
22+
23+
$this->onWriteProgressWithColor($color);
24+
}
25+
26+
public function endTest(Test $test, $time)
27+
{
28+
parent::endTest($test, $time);
29+
30+
$this->onEndTest($test, $time);
31+
}
32+
33+
public function addError(Test $test, \Exception $e, $time)
34+
{
35+
$this->onAddError($e);
36+
}
37+
38+
public function addFailure(Test $test, AssertionFailedError $e, $time)
39+
{
40+
$this->onAddFailure($e);
41+
}
42+
}

0 commit comments

Comments
 (0)