10
10
*/
11
11
12
12
namespace {
13
+
13
14
if (!class_exists ('PHPUnit_Util_String ' )) {
14
15
15
16
/**
@@ -101,14 +102,19 @@ class JSON extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListe
101
102
*/
102
103
protected $ currentTestPass = true ;
103
104
105
+ /**
106
+ * @var array
107
+ */
108
+ protected $ logEvents = [];
109
+
104
110
/**
105
111
* An error occurred.
106
112
*
107
113
* @param \PHPUnit\Framework\Test $test
108
114
* @param \Throwable $e
109
115
* @param float $time
110
116
*/
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
112
118
{
113
119
$ this ->writeCase (
114
120
'error ' ,
@@ -128,7 +134,7 @@ public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $ti
128
134
* @param \PHPUnit\Framework\Warning $e
129
135
* @param float $time
130
136
*/
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
132
138
{
133
139
$ this ->writeCase (
134
140
'warning ' ,
@@ -148,8 +154,11 @@ public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\War
148
154
* @param \PHPUnit\Framework\AssertionFailedError $e
149
155
* @param float $time
150
156
*/
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 {
153
162
$ this ->writeCase (
154
163
'fail ' ,
155
164
$ time ,
@@ -168,7 +177,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
168
177
* @param Throwable $e
169
178
* @param float $time
170
179
*/
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
172
181
{
173
182
$ this ->writeCase (
174
183
'error ' ,
@@ -188,7 +197,7 @@ public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e,
188
197
* @param Throwable $e
189
198
* @param float $time
190
199
*/
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
192
201
{
193
202
$ this ->writeCase (
194
203
'error ' ,
@@ -208,7 +217,7 @@ public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float
208
217
* @param Throwable $e
209
218
* @param float $time
210
219
*/
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
212
221
{
213
222
$ this ->writeCase (
214
223
'error ' ,
@@ -226,12 +235,12 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, flo
226
235
*
227
236
* @param \PHPUnit\Framework\TestSuite $suite
228
237
*/
229
- public function startTestSuite (\PHPUnit \Framework \TestSuite $ suite ) : void
238
+ public function startTestSuite (\PHPUnit \Framework \TestSuite $ suite ): void
230
239
{
231
240
$ this ->currentTestSuiteName = $ suite ->getName ();
232
241
$ this ->currentTestName = '' ;
233
242
234
- $ this ->writeArray (
243
+ $ this ->addLogEvent (
235
244
[
236
245
'event ' => 'suiteStart ' ,
237
246
'suite ' => $ this ->currentTestSuiteName ,
@@ -245,27 +254,29 @@ public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
245
254
*
246
255
* @param \PHPUnit\Framework\TestSuite $suite
247
256
*/
248
- public function endTestSuite (\PHPUnit \Framework \TestSuite $ suite ) : void
257
+ public function endTestSuite (\PHPUnit \Framework \TestSuite $ suite ): void
249
258
{
250
259
$ this ->currentTestSuiteName = '' ;
251
260
$ this ->currentTestName = '' ;
261
+
262
+ $ this ->writeArray ($ this ->logEvents );
252
263
}
253
264
254
265
/**
255
266
* A test started.
256
267
*
257
268
* @param \PHPUnit\Framework\Test $test
258
269
*/
259
- public function startTest (\PHPUnit \Framework \Test $ test ) : void
270
+ public function startTest (\PHPUnit \Framework \Test $ test ): void
260
271
{
261
272
$ this ->currentTestName = \PHPUnit \Util \Test::describe ($ test );
262
273
$ this ->currentTestPass = true ;
263
274
264
- $ this ->writeArray (
275
+ $ this ->addLogEvent (
265
276
[
266
277
'event ' => 'testStart ' ,
267
278
'suite ' => $ this ->currentTestSuiteName ,
268
- 'test ' => $ this ->currentTestName
279
+ 'test ' => $ this ->currentTestName
269
280
]
270
281
);
271
282
}
@@ -276,7 +287,7 @@ public function startTest(\PHPUnit\Framework\Test $test) : void
276
287
* @param \PHPUnit\Framework\Test $test
277
288
* @param float $time
278
289
*/
279
- public function endTest (\PHPUnit \Framework \Test $ test , float $ time ) : void
290
+ public function endTest (\PHPUnit \Framework \Test $ test , float $ time ): void
280
291
{
281
292
if ($ this ->currentTestPass ) {
282
293
$ this ->writeCase ('pass ' , $ time , [], '' , $ test );
@@ -290,34 +301,44 @@ public function endTest(\PHPUnit\Framework\Test $test, float $time) : void
290
301
* @param string $message
291
302
* @param \PHPUnit\Framework\TestCase|null $test
292
303
*/
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
294
305
{
295
306
$ output = '' ;
296
307
// take care of TestSuite producing error (e.g. by running into exception) as TestSuite doesn't have hasOutput
297
308
if ($ test !== null && method_exists ($ test , 'hasOutput ' ) && $ test ->hasOutput ()) {
298
309
$ output = $ test ->getActualOutput ();
299
310
}
300
- $ this ->writeArray (
311
+ $ this ->addLogEvent (
301
312
[
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 ,
308
319
'message ' => \PHPUnit_Util_String::convertToUtf8 ($ message ),
309
- 'output ' => $ output ,
320
+ 'output ' => $ output ,
310
321
]
311
322
);
312
323
}
313
324
314
325
/**
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
316
337
*/
317
338
public function writeArray ($ buffer )
318
339
{
319
340
array_walk_recursive (
320
- $ buffer , function (&$ input ) {
341
+ $ buffer , function (&$ input ){
321
342
if (is_string ($ input )) {
322
343
$ input = \PHPUnit_Util_String::convertToUtf8 ($ input );
323
344
}
@@ -381,7 +402,7 @@ public function __construct($out = null)
381
402
* @param Throwable $e
382
403
* @param float $time
383
404
*/
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
385
406
{
386
407
$ this ->writeNotOk ($ test , 'Error ' );
387
408
}
@@ -393,7 +414,7 @@ public function addError(\PHPUnit\Framework\Test $test, \Throwable $e, float $ti
393
414
* @param \PHPUnit\Framework\Warning $e
394
415
* @param float $time
395
416
*/
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
397
418
{
398
419
$ this ->writeNotOk ($ test , 'Warning ' );
399
420
}
@@ -405,8 +426,11 @@ public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\War
405
426
* @param \PHPUnit\Framework\AssertionFailedError $e
406
427
* @param float $time
407
428
*/
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 {
410
434
$ this ->writeNotOk ($ test , 'Failure ' );
411
435
412
436
$ message = explode (
@@ -415,7 +439,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
415
439
);
416
440
417
441
$ diagnostic = [
418
- 'message ' => $ message [0 ],
442
+ 'message ' => $ message [0 ],
419
443
'severity ' => 'fail '
420
444
];
421
445
@@ -424,7 +448,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
424
448
425
449
if ($ cf !== null ) {
426
450
$ diagnostic ['data ' ] = [
427
- 'got ' => $ cf ->getActual (),
451
+ 'got ' => $ cf ->getActual (),
428
452
'expected ' => $ cf ->getExpected ()
429
453
];
430
454
}
@@ -447,7 +471,7 @@ public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Ass
447
471
* @param \Throwable $e
448
472
* @param float $time
449
473
*/
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
451
475
{
452
476
$ this ->writeNotOk ($ test , '' , 'TODO Incomplete Test ' );
453
477
}
@@ -459,7 +483,7 @@ public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $e,
459
483
* @param Throwable $e
460
484
* @param float $time
461
485
*/
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
463
487
{
464
488
$ this ->write (
465
489
sprintf (
@@ -479,7 +503,7 @@ public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $e, float
479
503
* @param Throwable $e
480
504
* @param float $time
481
505
*/
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
483
507
{
484
508
$ this ->write (
485
509
sprintf (
@@ -497,7 +521,7 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, flo
497
521
*
498
522
* @param \PHPUnit\Framework\TestSuite $suite
499
523
*/
500
- public function startTestSuite (\PHPUnit \Framework \TestSuite $ suite ) : void
524
+ public function startTestSuite (\PHPUnit \Framework \TestSuite $ suite ): void
501
525
{
502
526
$ this ->testSuiteLevel ++;
503
527
}
@@ -507,7 +531,7 @@ public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
507
531
*
508
532
* @param \PHPUnit\Framework\TestSuite $suite
509
533
*/
510
- public function endTestSuite (\PHPUnit \Framework \TestSuite $ suite ) : void
534
+ public function endTestSuite (\PHPUnit \Framework \TestSuite $ suite ): void
511
535
{
512
536
$ this ->testSuiteLevel --;
513
537
@@ -521,7 +545,7 @@ public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
521
545
*
522
546
* @param \PHPUnit\Framework\Test $test
523
547
*/
524
- public function startTest (\PHPUnit \Framework \Test $ test ) : void
548
+ public function startTest (\PHPUnit \Framework \Test $ test ): void
525
549
{
526
550
$ this ->testNumber ++;
527
551
$ this ->testSuccessful = true ;
@@ -533,7 +557,7 @@ public function startTest(\PHPUnit\Framework\Test $test) : void
533
557
* @param \PHPUnit\Framework\Test $test
534
558
* @param float $time
535
559
*/
536
- public function endTest (\PHPUnit \Framework \Test $ test , float $ time ) : void
560
+ public function endTest (\PHPUnit \Framework \Test $ test , float $ time ): void
537
561
{
538
562
if ($ this ->testSuccessful === true ) {
539
563
$ this ->write (
0 commit comments