diff --git a/src/phpunit5-loggers.php b/src/phpunit5-loggers.php index 02d8d09..554fddd 100644 --- a/src/phpunit5-loggers.php +++ b/src/phpunit5-loggers.php @@ -10,6 +10,7 @@ */ namespace { + if (!class_exists('PHPUnit_Util_String')) { /** @@ -99,6 +100,11 @@ class JSON extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListe */ protected $currentTestPass = true; + /** + * @var array + */ + protected $logEvents = []; + /** * An error occurred. * @@ -227,9 +233,9 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Exception $e, $ti public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) { $this->currentTestSuiteName = $suite->getName(); - $this->currentTestName = ''; + $this->currentTestName = ''; - $this->write( + $this->addLogEvent( [ 'event' => 'suiteStart', 'suite' => $this->currentTestSuiteName, @@ -246,7 +252,9 @@ public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) { $this->currentTestSuiteName = ''; - $this->currentTestName = ''; + $this->currentTestName = ''; + + $this->writeAll($this->logEvents); } /** @@ -259,7 +267,7 @@ public function startTest(\PHPUnit\Framework\Test $test) $this->currentTestName = \PHPUnit\Util\Test::describe($test); $this->currentTestPass = true; - $this->write( + $this->addLogEvent( [ 'event' => 'testStart', 'suite' => $this->currentTestSuiteName, @@ -295,7 +303,7 @@ protected function writeCase($status, $time, array $trace = [], $message = '', $ if ($test !== null && method_exists($test, 'hasOutput') && $test->hasOutput()) { $output = $test->getActualOutput(); } - $this->write( + $this->addLogEvent( [ 'event' => 'test', 'suite' => $this->currentTestSuiteName, @@ -310,9 +318,19 @@ protected function writeCase($status, $time, array $trace = [], $message = '', $ } /** - * @param string $buffer + * @param array $event_data + */ + protected function addLogEvent($event_data = []) + { + if (count($event_data)) { + array_push($this->logEvents, $event_data); + } + } + + /** + * @param array $buffer */ - public function write($buffer) + public function writeAll($buffer) { array_walk_recursive( $buffer, function (&$input) {