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

Commit f18ed63

Browse files
mycarrysunDavertMik
authored andcommitted
Track all event objects for a suite in JSON logger (7.0) (#34)
* Track all event objects for a suite in JSON logger * PSR2 formatting * PHPDoc: $buffer param for writeArray is an array
1 parent 6a62443 commit f18ed63

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

src/phpunit5-loggers.php

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
namespace {
13+
1314
if (!class_exists('PHPUnit_Util_String')) {
1415

1516
/**
@@ -101,14 +102,19 @@ class JSON extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListe
101102
*/
102103
protected $currentTestPass = true;
103104

105+
/**
106+
* @var array
107+
*/
108+
protected $logEvents = [];
109+
104110
/**
105111
* An error occurred.
106112
*
107113
* @param \PHPUnit\Framework\Test $test
108114
* @param \Throwable $e
109115
* @param float $time
110116
*/
111-
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
117+
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
112118
{
113119
$this->writeCase(
114120
'error',
@@ -128,7 +134,7 @@ public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $ti
128134
* @param \PHPUnit\Framework\Warning $e
129135
* @param float $time
130136
*/
131-
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time) : void
137+
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void
132138
{
133139
$this->writeCase(
134140
'warning',
@@ -148,8 +154,11 @@ public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\War
148154
* @param \PHPUnit\Framework\AssertionFailedError $e
149155
* @param float $time
150156
*/
151-
public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, float $time) : void
152-
{
157+
public function addFailure(
158+
\PHPUnit\Framework\Test $test,
159+
\PHPUnit\Framework\AssertionFailedError $e,
160+
float $time
161+
): void{
153162
$this->writeCase(
154163
'fail',
155164
$time,
@@ -168,7 +177,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
168177
* @param Throwable $e
169178
* @param float $time
170179
*/
171-
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
180+
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
172181
{
173182
$this->writeCase(
174183
'error',
@@ -188,7 +197,7 @@ public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e,
188197
* @param Throwable $e
189198
* @param float $time
190199
*/
191-
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
200+
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
192201
{
193202
$this->writeCase(
194203
'error',
@@ -208,7 +217,7 @@ public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float
208217
* @param Throwable $e
209218
* @param float $time
210219
*/
211-
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
220+
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
212221
{
213222
$this->writeCase(
214223
'error',
@@ -226,12 +235,12 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, flo
226235
*
227236
* @param \PHPUnit\Framework\TestSuite $suite
228237
*/
229-
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
238+
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void
230239
{
231240
$this->currentTestSuiteName = $suite->getName();
232241
$this->currentTestName = '';
233242

234-
$this->writeArray(
243+
$this->addLogEvent(
235244
[
236245
'event' => 'suiteStart',
237246
'suite' => $this->currentTestSuiteName,
@@ -245,27 +254,29 @@ public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
245254
*
246255
* @param \PHPUnit\Framework\TestSuite $suite
247256
*/
248-
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
257+
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void
249258
{
250259
$this->currentTestSuiteName = '';
251260
$this->currentTestName = '';
261+
262+
$this->writeArray($this->logEvents);
252263
}
253264

254265
/**
255266
* A test started.
256267
*
257268
* @param \PHPUnit\Framework\Test $test
258269
*/
259-
public function startTest(\PHPUnit\Framework\Test $test) : void
270+
public function startTest(\PHPUnit\Framework\Test $test): void
260271
{
261272
$this->currentTestName = \PHPUnit\Util\Test::describe($test);
262273
$this->currentTestPass = true;
263274

264-
$this->writeArray(
275+
$this->addLogEvent(
265276
[
266277
'event' => 'testStart',
267278
'suite' => $this->currentTestSuiteName,
268-
'test' => $this->currentTestName
279+
'test' => $this->currentTestName
269280
]
270281
);
271282
}
@@ -276,7 +287,7 @@ public function startTest(\PHPUnit\Framework\Test $test) : void
276287
* @param \PHPUnit\Framework\Test $test
277288
* @param float $time
278289
*/
279-
public function endTest(\PHPUnit\Framework\Test $test, float $time) : void
290+
public function endTest(\PHPUnit\Framework\Test $test, float $time): void
280291
{
281292
if ($this->currentTestPass) {
282293
$this->writeCase('pass', $time, [], '', $test);
@@ -290,34 +301,44 @@ public function endTest(\PHPUnit\Framework\Test $test, float $time) : void
290301
* @param string $message
291302
* @param \PHPUnit\Framework\TestCase|null $test
292303
*/
293-
protected function writeCase($status, float $time, array $trace = [], $message = '', $test = null) : void
304+
protected function writeCase($status, float $time, array $trace = [], $message = '', $test = null): void
294305
{
295306
$output = '';
296307
// take care of TestSuite producing error (e.g. by running into exception) as TestSuite doesn't have hasOutput
297308
if ($test !== null && method_exists($test, 'hasOutput') && $test->hasOutput()) {
298309
$output = $test->getActualOutput();
299310
}
300-
$this->writeArray(
311+
$this->addLogEvent(
301312
[
302-
'event' => 'test',
303-
'suite' => $this->currentTestSuiteName,
304-
'test' => $this->currentTestName,
305-
'status' => $status,
306-
'time' => $time,
307-
'trace' => $trace,
313+
'event' => 'test',
314+
'suite' => $this->currentTestSuiteName,
315+
'test' => $this->currentTestName,
316+
'status' => $status,
317+
'time' => $time,
318+
'trace' => $trace,
308319
'message' => \PHPUnit_Util_String::convertToUtf8($message),
309-
'output' => $output,
320+
'output' => $output,
310321
]
311322
);
312323
}
313324

314325
/**
315-
* @param string $buffer
326+
* @param array $event_data
327+
*/
328+
protected function addLogEvent($event_data = []): void
329+
{
330+
if (count($event_data)) {
331+
array_push($this->logEvents, $event_data);
332+
}
333+
}
334+
335+
/**
336+
* @param array $buffer
316337
*/
317338
public function writeArray($buffer)
318339
{
319340
array_walk_recursive(
320-
$buffer, function (&$input) {
341+
$buffer, function (&$input){
321342
if (is_string($input)) {
322343
$input = \PHPUnit_Util_String::convertToUtf8($input);
323344
}
@@ -381,7 +402,7 @@ public function __construct($out = null)
381402
* @param Throwable $e
382403
* @param float $time
383404
*/
384-
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
405+
public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
385406
{
386407
$this->writeNotOk($test, 'Error');
387408
}
@@ -393,7 +414,7 @@ public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $ti
393414
* @param \PHPUnit\Framework\Warning $e
394415
* @param float $time
395416
*/
396-
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time) : void
417+
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void
397418
{
398419
$this->writeNotOk($test, 'Warning');
399420
}
@@ -405,8 +426,11 @@ public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\War
405426
* @param \PHPUnit\Framework\AssertionFailedError $e
406427
* @param float $time
407428
*/
408-
public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, float $time) : void
409-
{
429+
public function addFailure(
430+
\PHPUnit\Framework\Test $test,
431+
\PHPUnit\Framework\AssertionFailedError $e,
432+
float $time
433+
): void{
410434
$this->writeNotOk($test, 'Failure');
411435

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

417441
$diagnostic = [
418-
'message' => $message[0],
442+
'message' => $message[0],
419443
'severity' => 'fail'
420444
];
421445

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

425449
if ($cf !== null) {
426450
$diagnostic['data'] = [
427-
'got' => $cf->getActual(),
451+
'got' => $cf->getActual(),
428452
'expected' => $cf->getExpected()
429453
];
430454
}
@@ -447,7 +471,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
447471
* @param \Throwable $e
448472
* @param float $time
449473
*/
450-
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
474+
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
451475
{
452476
$this->writeNotOk($test, '', 'TODO Incomplete Test');
453477
}
@@ -459,7 +483,7 @@ public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e,
459483
* @param Throwable $e
460484
* @param float $time
461485
*/
462-
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
486+
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
463487
{
464488
$this->write(
465489
sprintf(
@@ -479,7 +503,7 @@ public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float
479503
* @param Throwable $e
480504
* @param float $time
481505
*/
482-
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time) : void
506+
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, float $time): void
483507
{
484508
$this->write(
485509
sprintf(
@@ -497,7 +521,7 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, flo
497521
*
498522
* @param \PHPUnit\Framework\TestSuite $suite
499523
*/
500-
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
524+
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void
501525
{
502526
$this->testSuiteLevel++;
503527
}
@@ -507,7 +531,7 @@ public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
507531
*
508532
* @param \PHPUnit\Framework\TestSuite $suite
509533
*/
510-
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
534+
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void
511535
{
512536
$this->testSuiteLevel--;
513537

@@ -521,7 +545,7 @@ public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
521545
*
522546
* @param \PHPUnit\Framework\Test $test
523547
*/
524-
public function startTest(\PHPUnit\Framework\Test $test) : void
548+
public function startTest(\PHPUnit\Framework\Test $test): void
525549
{
526550
$this->testNumber++;
527551
$this->testSuccessful = true;
@@ -533,7 +557,7 @@ public function startTest(\PHPUnit\Framework\Test $test) : void
533557
* @param \PHPUnit\Framework\Test $test
534558
* @param float $time
535559
*/
536-
public function endTest(\PHPUnit\Framework\Test $test, float $time) : void
560+
public function endTest(\PHPUnit\Framework\Test $test, float $time): void
537561
{
538562
if ($this->testSuccessful === true) {
539563
$this->write(

0 commit comments

Comments
 (0)