44
55/**
66 * 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 .
88 */
99class SpeedTrapListener implements \PHPUnit_Framework_TestListener
1010{
@@ -18,6 +18,27 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
1818 */
1919 protected $ suites = 0 ;
2020
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+
2142 /**
2243 * Time in milliseconds at which a test will be considered "slow" and be
2344 * reported by this listener.
@@ -48,6 +69,16 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
4869 public function __construct (array $ options = array ())
4970 {
5071 $ 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 );
5182 }
5283
5384 /**
@@ -266,7 +297,7 @@ protected function getHiddenCount()
266297 */
267298 protected function renderHeader ()
268299 {
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 ) );
270301 }
271302
272303 /**
@@ -281,7 +312,7 @@ protected function renderBody()
281312 $ label = key ($ slowTests );
282313 $ time = array_shift ($ slowTests );
283314
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) );
285316 }
286317 }
287318
@@ -291,7 +322,8 @@ protected function renderBody()
291322 protected function renderFooter ()
292323 {
293324 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 ));
295327 }
296328 }
297329
@@ -302,6 +334,8 @@ protected function renderFooter()
302334 */
303335 protected function loadOptions (array $ options )
304336 {
337+ $ this ->outPath = isset ($ options ['outPath ' ]) ? $ options ['outPath ' ] : 'php://stdout ' ;
338+ $ this ->forceFlush = isset ($ options ['forceFlush ' ]) ? $ options ['forceFlush ' ] : false ;
305339 $ this ->slowThreshold = isset ($ options ['slowThreshold ' ]) ? $ options ['slowThreshold ' ] : 500 ;
306340 $ this ->reportLength = isset ($ options ['reportLength ' ]) ? $ options ['reportLength ' ] : 10 ;
307341 }
@@ -328,4 +362,17 @@ protected function getSlowThreshold(\PHPUnit_Framework_TestCase $test)
328362
329363 return isset ($ ann ['method ' ]['slowThreshold ' ][0 ]) ? $ ann ['method ' ]['slowThreshold ' ][0 ] : $ this ->slowThreshold ;
330364 }
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+ }
331378}
0 commit comments