Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Track all event objects for a suite in JSON logger (6.0) #35

Merged
merged 1 commit into from
Jun 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/phpunit5-loggers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

namespace {

if (!class_exists('PHPUnit_Util_String')) {

/**
Expand Down Expand Up @@ -99,6 +100,11 @@ class JSON extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListe
*/
protected $currentTestPass = true;

/**
* @var array
*/
protected $logEvents = [];

/**
* An error occurred.
*
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -310,9 +318,19 @@ protected function writeCase($status, $time, array $trace = [], $message = '', $
}

/**
* @param string $buffer
* @param array $event_data
*/
protected function addLogEvent($event_data = [])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used a class prop to store all events so we can write all the events properly JSON encoded at the end of the suite

{
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) {
Expand Down