Skip to content

Instantly share code, notes, and snippets.

@Petah
Created June 29, 2020 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Petah/6d466d8e491f90ae99ffe0fc7195e687 to your computer and use it in GitHub Desktop.
Save Petah/6d466d8e491f90ae99ffe0fc7195e687 to your computer and use it in GitHub Desktop.
<?php
use PHPUnit\Framework\TestListener;
class OutputListener implements TestListener
{
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
echo PHP_EOL;
echo PHP_EOL . 'ERROR: ' . get_class($test) . ' ' . $test->getName();
echo PHP_EOL;
echo PHP_EOL . $e->getMessage();
echo PHP_EOL;
echo PHP_EOL . $this->exceptionTraceToString($e);
echo PHP_EOL;
}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
echo PHP_EOL;
echo PHP_EOL . 'FAILURE: ' . get_class($test) . ' ' . $test->getName();
echo PHP_EOL;
echo PHP_EOL . $e->getMessage();
echo PHP_EOL;
}
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function startTest(PHPUnit_Framework_Test $test)
{
echo PHP_EOL . get_class($test) . ' ' . $test->getName();
}
public function endTest(PHPUnit_Framework_Test $test, $time)
{
}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
private function exceptionTraceToString($exception)
{
$trace = [];
foreach ($exception->getSerializableTrace() as $line) {
$text = '';
if (isset($line['line'])) {
$text .= '#' . $line['line'];
}
$text .= ' ';
if (isset($line['class'])) {
$text .= $line['class'];
}
if (isset($line['type'])) {
$text .= $line['type'];
}
if (isset($line['function'])) {
$text .= $line['function'];
}
$text .= ' ';
if (isset($line['file'])) {
$text .= $line['file'];
}
$trace[] = trim($text);
}
$trace = implode(PHP_EOL, $trace);
return $trace;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
...
<listeners>
<listener class="OutputListener" />
</listeners>
...
</phpunit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment