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 (7.0) #34

Merged
merged 3 commits 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
100 changes: 62 additions & 38 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 @@ -101,14 +102,19 @@ class JSON extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListe
*/
protected $currentTestPass = true;

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

/**
* An error occurred.
*
* @param \PHPUnit\Framework\Test $test
* @param \Throwable $e
* @param float $time
*/
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->writeCase(
'error',
Expand All @@ -128,7 +134,7 @@ public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $ti
* @param \PHPUnit\Framework\Warning $e
* @param float $time
*/
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time) : void
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void
{
$this->writeCase(
'warning',
Expand All @@ -148,8 +154,11 @@ public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\War
* @param \PHPUnit\Framework\AssertionFailedError $e
* @param float $time
*/
public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, float $time) : void
{
public function addFailure(
\PHPUnit\Framework\Test $test,
\PHPUnit\Framework\AssertionFailedError $e,
float $time
): void{
$this->writeCase(
'fail',
$time,
Expand All @@ -168,7 +177,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
* @param Throwable $e
* @param float $time
*/
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->writeCase(
'error',
Expand All @@ -188,7 +197,7 @@ public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e,
* @param Throwable $e
* @param float $time
*/
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->writeCase(
'error',
Expand All @@ -208,7 +217,7 @@ public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float
* @param Throwable $e
* @param float $time
*/
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->writeCase(
'error',
Expand All @@ -226,12 +235,12 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, flo
*
* @param \PHPUnit\Framework\TestSuite $suite
*/
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void
{
$this->currentTestSuiteName = $suite->getName();
$this->currentTestName = '';

$this->writeArray(
$this->addLogEvent(
[
'event' => 'suiteStart',
'suite' => $this->currentTestSuiteName,
Expand All @@ -245,27 +254,29 @@ public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
*
* @param \PHPUnit\Framework\TestSuite $suite
*/
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void
{
$this->currentTestSuiteName = '';
$this->currentTestName = '';

$this->writeArray($this->logEvents);
}

/**
* A test started.
*
* @param \PHPUnit\Framework\Test $test
*/
public function startTest(\PHPUnit\Framework\Test $test) : void
public function startTest(\PHPUnit\Framework\Test $test): void
{
$this->currentTestName = \PHPUnit\Util\Test::describe($test);
$this->currentTestPass = true;

$this->writeArray(
$this->addLogEvent(
[
'event' => 'testStart',
'suite' => $this->currentTestSuiteName,
'test' => $this->currentTestName
'test' => $this->currentTestName
]
);
}
Expand All @@ -276,7 +287,7 @@ public function startTest(\PHPUnit\Framework\Test $test) : void
* @param \PHPUnit\Framework\Test $test
* @param float $time
*/
public function endTest(\PHPUnit\Framework\Test $test, float $time) : void
public function endTest(\PHPUnit\Framework\Test $test, float $time): void
{
if ($this->currentTestPass) {
$this->writeCase('pass', $time, [], '', $test);
Expand All @@ -290,34 +301,44 @@ public function endTest(\PHPUnit\Framework\Test $test, float $time) : void
* @param string $message
* @param \PHPUnit\Framework\TestCase|null $test
*/
protected function writeCase($status, float $time, array $trace = [], $message = '', $test = null) : void
protected function writeCase($status, float $time, array $trace = [], $message = '', $test = null): void
{
$output = '';
// take care of TestSuite producing error (e.g. by running into exception) as TestSuite doesn't have hasOutput
if ($test !== null && method_exists($test, 'hasOutput') && $test->hasOutput()) {
$output = $test->getActualOutput();
}
$this->writeArray(
$this->addLogEvent(
[
'event' => 'test',
'suite' => $this->currentTestSuiteName,
'test' => $this->currentTestName,
'status' => $status,
'time' => $time,
'trace' => $trace,
'event' => 'test',
'suite' => $this->currentTestSuiteName,
'test' => $this->currentTestName,
'status' => $status,
'time' => $time,
'trace' => $trace,
'message' => \PHPUnit_Util_String::convertToUtf8($message),
'output' => $output,
'output' => $output,
]
);
}

/**
* @param string $buffer
* @param array $event_data
*/
protected function addLogEvent($event_data = []): void
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 writeArray($buffer)
{
array_walk_recursive(
$buffer, function (&$input) {
$buffer, function (&$input){
if (is_string($input)) {
$input = \PHPUnit_Util_String::convertToUtf8($input);
}
Expand Down Expand Up @@ -381,7 +402,7 @@ public function __construct($out = null)
* @param Throwable $e
* @param float $time
*/
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->writeNotOk($test, 'Error');
}
Expand All @@ -393,7 +414,7 @@ public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $ti
* @param \PHPUnit\Framework\Warning $e
* @param float $time
*/
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time) : void
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void
{
$this->writeNotOk($test, 'Warning');
}
Expand All @@ -405,8 +426,11 @@ public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\War
* @param \PHPUnit\Framework\AssertionFailedError $e
* @param float $time
*/
public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, float $time) : void
{
public function addFailure(
\PHPUnit\Framework\Test $test,
\PHPUnit\Framework\AssertionFailedError $e,
float $time
): void{
$this->writeNotOk($test, 'Failure');

$message = explode(
Expand All @@ -415,7 +439,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
);

$diagnostic = [
'message' => $message[0],
'message' => $message[0],
'severity' => 'fail'
];

Expand All @@ -424,7 +448,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass

if ($cf !== null) {
$diagnostic['data'] = [
'got' => $cf->getActual(),
'got' => $cf->getActual(),
'expected' => $cf->getExpected()
];
}
Expand All @@ -447,7 +471,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
* @param \Throwable $e
* @param float $time
*/
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->writeNotOk($test, '', 'TODO Incomplete Test');
}
Expand All @@ -459,7 +483,7 @@ public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e,
* @param Throwable $e
* @param float $time
*/
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->write(
sprintf(
Expand All @@ -479,7 +503,7 @@ public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float
* @param Throwable $e
* @param float $time
*/
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
{
$this->write(
sprintf(
Expand All @@ -497,7 +521,7 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, flo
*
* @param \PHPUnit\Framework\TestSuite $suite
*/
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void
{
$this->testSuiteLevel++;
}
Expand All @@ -507,7 +531,7 @@ public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
*
* @param \PHPUnit\Framework\TestSuite $suite
*/
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void
{
$this->testSuiteLevel--;

Expand All @@ -521,7 +545,7 @@ public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
*
* @param \PHPUnit\Framework\Test $test
*/
public function startTest(\PHPUnit\Framework\Test $test) : void
public function startTest(\PHPUnit\Framework\Test $test): void
{
$this->testNumber++;
$this->testSuccessful = true;
Expand All @@ -533,7 +557,7 @@ public function startTest(\PHPUnit\Framework\Test $test) : void
* @param \PHPUnit\Framework\Test $test
* @param float $time
*/
public function endTest(\PHPUnit\Framework\Test $test, float $time) : void
public function endTest(\PHPUnit\Framework\Test $test, float $time): void
{
if ($this->testSuccessful === true) {
$this->write(
Expand Down