-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split ImmediateExceptionPrinter into PhpUnit6Printer and PhpUnit5Printer backed by Printer trait.
- Loading branch information
Showing
8 changed files
with
270 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
} | ||
], | ||
"require": { | ||
"phpunit/phpunit": "^5.5" | ||
"phpunit/phpunit": "^5.5|^6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,12 @@ | ||
<?php | ||
namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter; | ||
|
||
class ImmediateExceptionPrinter extends \PHPUnit_TextUI_ResultPrinter | ||
{ | ||
/** | ||
* The exception thrown by the last test. | ||
* | ||
* @var \Exception|null | ||
*/ | ||
protected $exception; | ||
|
||
/** | ||
* PHPUnit built-in progress indication character, e.g. E for error. | ||
* | ||
* @var string | ||
*/ | ||
protected $progress; | ||
|
||
/** | ||
* Last colour used by a progress indicator. | ||
* | ||
* @var string | ||
*/ | ||
protected $lastColour; | ||
|
||
private static $performanceThresholds = [ | ||
'fg-red' => 1000, | ||
'fg-yellow' => 200, | ||
'fg-green' => 0, | ||
]; | ||
|
||
public function startTest(\PHPUnit_Framework_Test $test) | ||
{ | ||
parent::startTest($test); | ||
|
||
$this->exception = $this->progress = null; | ||
$this->lastColour = 'fg-green,bold'; | ||
} | ||
|
||
protected function writeProgress($progress) | ||
if (class_exists(\PHPUnit_TextUI_ResultPrinter::class)) { | ||
class ImmediateExceptionPrinter extends PhpUnit5Printer | ||
{ | ||
// Record progress so it can be written later instead of at start of line. | ||
$this->progress = $progress; | ||
|
||
++$this->numTestsRun; | ||
} | ||
|
||
protected function writeProgressWithColor($color, $buffer) | ||
{ | ||
parent::writeProgressWithColor($color, $buffer); | ||
|
||
$this->lastColour = $color; | ||
} | ||
|
||
public function endTest(\PHPUnit_Framework_Test $test, $time) | ||
{ | ||
parent::endTest($test, $time); | ||
|
||
$this->write(sprintf( | ||
'%3d%% %s ', | ||
floor($this->numTestsRun / $this->numTests * 100), | ||
$this->progress | ||
)); | ||
$this->writeWithColor($this->lastColour, \PHPUnit_Util_Test::describe($test), false); | ||
$this->writePerformance($time); | ||
|
||
if ($this->exception) { | ||
$this->printExceptionTrace($this->exception); | ||
} | ||
} | ||
|
||
/** | ||
* Writes the test performance metric formatted as the number of milliseconds elapsed. | ||
* | ||
* @param float $time Number of seconds elapsed. | ||
*/ | ||
protected function writePerformance($time) | ||
} else { | ||
class ImmediateExceptionPrinter extends PhpUnit6Printer | ||
{ | ||
$ms = round($time * 1000); | ||
|
||
foreach (self::$performanceThresholds as $colour => $threshold) { | ||
if ($ms > $threshold) { | ||
break; | ||
} | ||
} | ||
|
||
$this->writeWithColor($colour, " ($ms ms)"); | ||
} | ||
|
||
protected function printExceptionTrace(\Exception $exception) | ||
{ | ||
$this->writeNewLine(); | ||
|
||
// Parse nested exception trace line by line. | ||
foreach (explode("\n", $exception) as $line) { | ||
// Print exception name and message. | ||
if (!$exception instanceof \PHPUnit_Framework_AssertionFailedError | ||
&& false !== $pos = strpos($line, ': ') | ||
) { | ||
$whitespace = str_repeat(' ', $pos + 2); | ||
$this->writeWithColor('bg-red,fg-white', $whitespace); | ||
|
||
// Exception name. | ||
$this->writeWithColor('bg-red,fg-white', sprintf(' %s ', substr($line, 0, $pos)), false); | ||
// Exception message. | ||
$this->writeWithColor('fg-red', substr($line, $pos + 1)); | ||
|
||
$this->writeWithColor('bg-red,fg-white', $whitespace); | ||
|
||
continue; | ||
} | ||
|
||
$this->writeWithColor('fg-red', $line); | ||
} | ||
} | ||
|
||
/** | ||
* Called when an exception is thrown in the test runner. | ||
* | ||
* @param \PHPUnit_Framework_Test $test | ||
* @param \Exception $e | ||
* @param float $time | ||
*/ | ||
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) | ||
{ | ||
$this->writeProgressWithColor('fg-red,bold', 'E'); | ||
|
||
$this->exception = $e; | ||
$this->lastTestFailed = true; | ||
} | ||
|
||
/** | ||
* Called when an assertion fails in the test runner. | ||
* | ||
* @param \PHPUnit_Framework_Test $test | ||
* @param \PHPUnit_Framework_AssertionFailedError $e | ||
* @param float $time | ||
*/ | ||
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) | ||
{ | ||
$this->writeProgressWithColor('fg-red,bold', 'F'); | ||
|
||
$this->exception = $e; | ||
$this->lastTestFailed = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter; | ||
|
||
class PhpUnit5Printer extends \PHPUnit_TextUI_ResultPrinter | ||
{ | ||
use Printer; | ||
|
||
public function startTest(\PHPUnit_Framework_Test $test) | ||
{ | ||
parent::startTest($test); | ||
|
||
$this->onStartTest(); | ||
} | ||
|
||
protected function writeProgressWithColor($color, $buffer) | ||
{ | ||
parent::writeProgressWithColor($color, $buffer); | ||
|
||
$this->onWriteProgressWithColor($color); | ||
} | ||
|
||
public function endTest(\PHPUnit_Framework_Test $test, $time) | ||
{ | ||
parent::endTest($test, $time); | ||
|
||
$this->onEndTest($test, $time); | ||
} | ||
|
||
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) | ||
{ | ||
$this->onAddError($e); | ||
} | ||
|
||
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) | ||
{ | ||
$this->onAddFailure($e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter; | ||
|
||
use PHPUnit\Framework\AssertionFailedError; | ||
use PHPUnit\Framework\Test; | ||
use PHPUnit\TextUI\ResultPrinter; | ||
|
||
class PhpUnit6Printer extends ResultPrinter | ||
{ | ||
use Printer; | ||
|
||
public function startTest(Test $test) | ||
{ | ||
parent::startTest($test); | ||
|
||
$this->onStartTest(); | ||
} | ||
|
||
protected function writeProgressWithColor($color, $buffer) | ||
{ | ||
parent::writeProgressWithColor($color, $buffer); | ||
|
||
$this->onWriteProgressWithColor($color); | ||
} | ||
|
||
public function endTest(Test $test, $time) | ||
{ | ||
parent::endTest($test, $time); | ||
|
||
$this->onEndTest($test, $time); | ||
} | ||
|
||
public function addError(Test $test, \Exception $e, $time) | ||
{ | ||
$this->onAddError($e); | ||
} | ||
|
||
public function addFailure(Test $test, AssertionFailedError $e, $time) | ||
{ | ||
$this->onAddFailure($e); | ||
} | ||
} |
Oops, something went wrong.