77
88/**
99 * A PHPUnit TestListener that exposes your slowest running tests by outputting
10- * results directly to the console.
10+ * results directly to the console or output file. .
1111 */
1212class SpeedTrapListener implements TestListener
1313{
@@ -21,6 +21,27 @@ class SpeedTrapListener implements TestListener
2121 */
2222 protected $ suites = 0 ;
2323
24+ /**
25+ * Output path
26+ *
27+ * @var string
28+ */
29+ protected $ outPath ;
30+
31+ /**
32+ * Output descriptor
33+ *
34+ * @var resource
35+ */
36+ protected $ out ;
37+
38+ /**
39+ * If True, flush output after every write.
40+ *
41+ * @var boolean
42+ */
43+ protected $ forceFlush ;
44+
2445 /**
2546 * Test execution time (milliseconds) after which a test will be considered
2647 * "slow" and be included in the slowness report.
@@ -46,6 +67,16 @@ class SpeedTrapListener implements TestListener
4667 public function __construct (array $ options = [])
4768 {
4869 $ this ->loadOptions ($ options );
70+
71+ $ this ->out = fopen ($ this ->outPath , 'wt ' );
72+ }
73+
74+ /**
75+ * Destruct the instance
76+ */
77+ public function __destruct ()
78+ {
79+ fclose ($ this ->out );
4980 }
5081
5182 /**
@@ -169,7 +200,7 @@ protected function getHiddenCount(): int
169200 */
170201 protected function renderHeader ()
171202 {
172- echo sprintf ("\n\nYou should really fix these slow tests (>%sms)... \n" , $ this ->slowThreshold );
203+ $ this -> write ( sprintf ("\n\nYou should really fix these slow tests (>%sms)... \n" , $ this ->slowThreshold ) );
173204 }
174205
175206 /**
@@ -184,7 +215,7 @@ protected function renderBody()
184215 $ label = key ($ slowTests );
185216 $ time = array_shift ($ slowTests );
186217
187- echo sprintf (" %s. %sms to run %s \n" , $ i , $ time , $ label );
218+ $ this -> write ( sprintf (" %s. %sms to run %s \n" , $ i , $ time , $ label) );
188219 }
189220 }
190221
@@ -194,7 +225,8 @@ protected function renderBody()
194225 protected function renderFooter ()
195226 {
196227 if ($ hidden = $ this ->getHiddenCount ()) {
197- echo sprintf ("...and there %s %s more above your threshold hidden from view " , $ hidden == 1 ? 'is ' : 'are ' , $ hidden );
228+ $ this ->write (sprintf ("...and there %s %s more above your threshold hidden from view " ,
229+ $ hidden == 1 ? 'is ' : 'are ' , $ hidden ));
198230 }
199231 }
200232
@@ -203,6 +235,8 @@ protected function renderFooter()
203235 */
204236 protected function loadOptions (array $ options )
205237 {
238+ $ this ->outPath = $ options ['outPath ' ] ?? 'php://stdout ' ;
239+ $ this ->forceFlush = $ options ['forceFlush ' ] ?? false ;
206240 $ this ->slowThreshold = $ options ['slowThreshold ' ] ?? 500 ;
207241 $ this ->reportLength = $ options ['reportLength ' ] ?? 10 ;
208242 }
@@ -226,4 +260,17 @@ protected function getSlowThreshold(TestCase $test): int
226260
227261 return isset ($ ann ['method ' ]['slowThreshold ' ][0 ]) ? (int ) $ ann ['method ' ]['slowThreshold ' ][0 ] : $ this ->slowThreshold ;
228262 }
263+
264+ /**
265+ * Write text to output
266+ *
267+ * @param string $buffer
268+ */
269+ protected function write ($ buffer )
270+ {
271+ fwrite ($ this ->out , $ buffer );
272+
273+ if ($ this ->forceFlush )
274+ fflush ($ this ->out );
275+ }
229276}
0 commit comments