7
7
8
8
/**
9
9
* 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. .
11
11
*/
12
12
class SpeedTrapListener implements TestListener
13
13
{
@@ -21,6 +21,27 @@ class SpeedTrapListener implements TestListener
21
21
*/
22
22
protected $ suites = 0 ;
23
23
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
+
24
45
/**
25
46
* Test execution time (milliseconds) after which a test will be considered
26
47
* "slow" and be included in the slowness report.
@@ -46,6 +67,16 @@ class SpeedTrapListener implements TestListener
46
67
public function __construct (array $ options = [])
47
68
{
48
69
$ 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 );
49
80
}
50
81
51
82
/**
@@ -169,7 +200,7 @@ protected function getHiddenCount(): int
169
200
*/
170
201
protected function renderHeader ()
171
202
{
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 ) );
173
204
}
174
205
175
206
/**
@@ -184,7 +215,7 @@ protected function renderBody()
184
215
$ label = key ($ slowTests );
185
216
$ time = array_shift ($ slowTests );
186
217
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) );
188
219
}
189
220
}
190
221
@@ -194,7 +225,8 @@ protected function renderBody()
194
225
protected function renderFooter ()
195
226
{
196
227
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 ));
198
230
}
199
231
}
200
232
@@ -203,6 +235,8 @@ protected function renderFooter()
203
235
*/
204
236
protected function loadOptions (array $ options )
205
237
{
238
+ $ this ->outPath = $ options ['outPath ' ] ?? 'php://stdout ' ;
239
+ $ this ->forceFlush = $ options ['forceFlush ' ] ?? false ;
206
240
$ this ->slowThreshold = $ options ['slowThreshold ' ] ?? 500 ;
207
241
$ this ->reportLength = $ options ['reportLength ' ] ?? 10 ;
208
242
}
@@ -226,4 +260,17 @@ protected function getSlowThreshold(TestCase $test): int
226
260
227
261
return isset ($ ann ['method ' ]['slowThreshold ' ][0 ]) ? (int ) $ ann ['method ' ]['slowThreshold ' ][0 ] : $ this ->slowThreshold ;
228
262
}
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
+ }
229
276
}
0 commit comments