diff --git a/README.md b/README.md index b1eaaf0..0ca6b94 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index ad7eaa8..e2f5802 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ } ], "require": { - "phpunit/phpunit": "^5.5" + "phpunit/phpunit": "^5.5|^6" }, "autoload": { "psr-4": { diff --git a/src/ImmediateExceptionPrinter.php b/src/ImmediateExceptionPrinter.php index 1466b50..049a8ad 100644 --- a/src/ImmediateExceptionPrinter.php +++ b/src/ImmediateExceptionPrinter.php @@ -1,147 +1,12 @@ 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; } } diff --git a/src/PhpUnit5Printer.php b/src/PhpUnit5Printer.php new file mode 100644 index 0000000..3738560 --- /dev/null +++ b/src/PhpUnit5Printer.php @@ -0,0 +1,38 @@ +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); + } +} diff --git a/src/PhpUnit6Printer.php b/src/PhpUnit6Printer.php new file mode 100644 index 0000000..adc427b --- /dev/null +++ b/src/PhpUnit6Printer.php @@ -0,0 +1,42 @@ +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); + } +} diff --git a/src/Printer.php b/src/Printer.php new file mode 100644 index 0000000..9108c80 --- /dev/null +++ b/src/Printer.php @@ -0,0 +1,174 @@ + 1000, + 'fg-yellow' => 200, + 'fg-green' => 0, + ]; + + /** + * Invoked when a test starts. + */ + protected function onStartTest() + { + $this->exception = $this->progress = null; + $this->lastColour = 'fg-green,bold'; + } + + /** + * Writes progress to the output buffer. + * + * @param string $progress + */ + protected function writeProgress($progress) + { + // Record progress so it can be written later instead of at start of line. + $this->progress = $progress; + + ++$this->numTestsRun; + } + + /** + * Invoked when coloured progress text is written. + * + * @param string $color PHPUnit colour name. + */ + protected function onWriteProgressWithColor($color) + { + $this->lastColour = $color; + } + + /** + * Invoked when a test ends. + * + * @param Test $test + * @param float $time Number of seconds elapsed. + */ + protected function onEndTest($test, $time) + { + $this->write(sprintf( + '%3d%% %s ', + floor($this->numTestsRun / $this->numTests * 100), + $this->progress + )); + $this->writeWithColor($this->lastColour, $this->describeTest($test), false); + $this->writePerformance($time); + + if ($this->exception) { + $this->writeExceptionTrace($this->exception); + } + } + + protected function describeTest($test) + { + if (class_exists(\PHPUnit_Util_Test::class)) { + return \PHPUnit_Util_Test::describe($test); + } + + return \PHPUnit\Util\Test::describe($test); + } + + /** + * Writes the test performance metric formatted as the number of milliseconds elapsed. + * + * @param float $time Number of seconds elapsed. + */ + protected function writePerformance($time) + { + $ms = round($time * 1000); + + foreach (self::$performanceThresholds as $colour => $threshold) { + if ($ms > $threshold) { + break; + } + } + + $this->writeWithColor($colour, " ($ms ms)"); + } + + /** + * Writes the specified exception to the output buffer. + * + * @param \Exception $exception Exception. + */ + protected function writeExceptionTrace(\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 + && !$exception instanceof 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); + } + } + + /** + * Invoked when an exception is thrown in the test runner. + * + * @param \Exception $e + */ + protected function onAddError(\Exception $e) + { + $this->writeProgressWithColor('fg-red,bold', 'E'); + + $this->exception = $e; + $this->lastTestFailed = true; + } + + /** + * Invoked when an assertion fails in the test runner. + * + * @param AssertionFailedError $e + */ + protected function onAddFailure($e) + { + $this->writeProgressWithColor('fg-red,bold', 'F'); + + $this->exception = $e; + $this->lastTestFailed = true; + } +} diff --git a/test/CapabilitiesTest.php b/test/CapabilitiesTest.php index 4429098..68baa09 100644 --- a/test/CapabilitiesTest.php +++ b/test/CapabilitiesTest.php @@ -1,7 +1,9 @@ markTestSkipped(); + self::markTestSkipped(); } public function testRisky() @@ -39,7 +41,7 @@ public function testRisky() public function testIncomplete() { - $this->markTestIncomplete(); + self::markTestIncomplete(); } /** diff --git a/test/functional/PHPUnit runner.php b/test/functional/PHPUnit runner.php index 36f48eb..a56bb0f 100644 --- a/test/functional/PHPUnit runner.php +++ b/test/functional/PHPUnit runner.php @@ -1,4 +1,8 @@