4
4
5
5
/**
6
6
* A PHPUnit TestListener that exposes your slowest running tests by outputting
7
- * results directly to the console.
7
+ * results directly to the console or output file .
8
8
*/
9
9
class SpeedTrapListener implements \PHPUnit_Framework_TestListener
10
10
{
@@ -18,6 +18,27 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
18
18
*/
19
19
protected $ suites = 0 ;
20
20
21
+ /**
22
+ * Output path
23
+ *
24
+ * @var string
25
+ */
26
+ protected $ outPath ;
27
+
28
+ /**
29
+ * Output descriptor
30
+ *
31
+ * @var resource
32
+ */
33
+ protected $ out ;
34
+
35
+ /**
36
+ * If True, flush output after every write.
37
+ *
38
+ * @var boolean
39
+ */
40
+ protected $ forceFlush ;
41
+
21
42
/**
22
43
* Time in milliseconds at which a test will be considered "slow" and be
23
44
* reported by this listener.
@@ -48,6 +69,16 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
48
69
public function __construct (array $ options = array ())
49
70
{
50
71
$ this ->loadOptions ($ options );
72
+
73
+ $ this ->out = fopen ($ this ->outPath , 'wt ' );
74
+ }
75
+
76
+ /**
77
+ * Destruct the instance
78
+ */
79
+ public function __destruct ()
80
+ {
81
+ fclose ($ this ->out );
51
82
}
52
83
53
84
/**
@@ -266,7 +297,7 @@ protected function getHiddenCount()
266
297
*/
267
298
protected function renderHeader ()
268
299
{
269
- echo sprintf ("\n\nYou should really fix these slow tests (>%sms)... \n" , $ this ->slowThreshold );
300
+ $ this -> write ( sprintf ("\n\nYou should really fix these slow tests (>%sms)... \n" , $ this ->slowThreshold ) );
270
301
}
271
302
272
303
/**
@@ -281,7 +312,7 @@ protected function renderBody()
281
312
$ label = key ($ slowTests );
282
313
$ time = array_shift ($ slowTests );
283
314
284
- echo sprintf (" %s. %sms to run %s \n" , $ i , $ time , $ label );
315
+ $ this -> write ( sprintf (" %s. %sms to run %s \n" , $ i , $ time , $ label) );
285
316
}
286
317
}
287
318
@@ -291,7 +322,8 @@ protected function renderBody()
291
322
protected function renderFooter ()
292
323
{
293
324
if ($ hidden = $ this ->getHiddenCount ()) {
294
- echo sprintf ("...and there %s %s more above your threshold hidden from view " , $ hidden == 1 ? 'is ' : 'are ' , $ hidden );
325
+ $ this ->write (sprintf ("...and there %s %s more above your threshold hidden from view " ,
326
+ $ hidden == 1 ? 'is ' : 'are ' , $ hidden ));
295
327
}
296
328
}
297
329
@@ -302,6 +334,8 @@ protected function renderFooter()
302
334
*/
303
335
protected function loadOptions (array $ options )
304
336
{
337
+ $ this ->outPath = isset ($ options ['outPath ' ]) ? $ options ['outPath ' ] : 'php://stdout ' ;
338
+ $ this ->forceFlush = isset ($ options ['forceFlush ' ]) ? $ options ['forceFlush ' ] : false ;
305
339
$ this ->slowThreshold = isset ($ options ['slowThreshold ' ]) ? $ options ['slowThreshold ' ] : 500 ;
306
340
$ this ->reportLength = isset ($ options ['reportLength ' ]) ? $ options ['reportLength ' ] : 10 ;
307
341
}
@@ -328,4 +362,17 @@ protected function getSlowThreshold(\PHPUnit_Framework_TestCase $test)
328
362
329
363
return isset ($ ann ['method ' ]['slowThreshold ' ][0 ]) ? $ ann ['method ' ]['slowThreshold ' ][0 ] : $ this ->slowThreshold ;
330
364
}
365
+
366
+ /**
367
+ * Write text to output
368
+ *
369
+ * @param string $buffer
370
+ */
371
+ protected function write ($ buffer )
372
+ {
373
+ fwrite ($ this ->out , $ buffer );
374
+
375
+ if ($ this ->forceFlush )
376
+ fflush ($ this ->out );
377
+ }
331
378
}
0 commit comments