Skip to content

Commit

Permalink
Added PHPUnit 6 support.
Browse files Browse the repository at this point in the history
Split ImmediateExceptionPrinter into PhpUnit6Printer and PhpUnit5Printer backed by Printer trait.
  • Loading branch information
Bilge committed Mar 11, 2017
1 parent 65bbdc9 commit 5bd117e
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 145 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Not shown: the normal test summary output you're used to seeing at the end of a
## Requirements

* PHP 5.6 or newer.
* [PHPUnit][PHPUnit] 5.5 or newer. PHPUnit 6 not yet supported.
* [PHPUnit][PHPUnit] 5.5 or newer.

## Testing

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
],
"require": {
"phpunit/phpunit": "^5.5"
"phpunit/phpunit": "^5.5|^6"
},
"autoload": {
"psr-4": {
Expand Down
143 changes: 4 additions & 139 deletions src/ImmediateExceptionPrinter.php
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;
}
}
38 changes: 38 additions & 0 deletions src/PhpUnit5Printer.php
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);
}
}
42 changes: 42 additions & 0 deletions src/PhpUnit6Printer.php
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);
}
}
Loading

0 comments on commit 5bd117e

Please sign in to comment.