1
1
<?php
2
+ declare (strict_types=1 );
2
3
3
4
namespace JohnKary \PHPUnit \Listener ;
4
5
5
- use PHPUnit \Framework \AssertionFailedError ;
6
- use PHPUnit \Framework \TestSuite ;
7
- use PHPUnit \Framework \Test ;
8
- use PHPUnit \Framework \TestCase ;
9
- use PHPUnit \Framework \TestListener ;
10
- use PHPUnit \Framework \Warning ;
6
+ use PHPUnit \Framework \{AssertionFailedError , TestSuite , Test , TestCase , TestListener , Warning };
11
7
12
8
/**
13
9
* A PHPUnit TestListener that exposes your slowest running tests by outputting
@@ -20,8 +16,6 @@ class SpeedTrapListener implements TestListener
20
16
*
21
17
* Increments as more suites are run, then decremented as they finish. All
22
18
* suites have been run when returns to 0.
23
- *
24
- * @var int
25
19
*/
26
20
protected $ suites = 0 ;
27
21
@@ -42,19 +36,12 @@ class SpeedTrapListener implements TestListener
42
36
43
37
/**
44
38
* Collection of slow tests.
45
- * Keys are labels describing the test (string)
46
- * Values are total execution time of given test (int)
47
- *
48
- * @var array
39
+ * Keys (string) => Printable label describing the test
40
+ * Values (int) => Test execution time, in milliseconds
49
41
*/
50
- protected $ slow = array () ;
42
+ protected $ slow = [] ;
51
43
52
- /**
53
- * Construct a new instance.
54
- *
55
- * @param array $options
56
- */
57
- public function __construct (array $ options = array ())
44
+ public function __construct (array $ options = [])
58
45
{
59
46
$ this ->loadOptions ($ options );
60
47
}
@@ -184,21 +171,17 @@ public function endTestSuite(TestSuite $suite)
184
171
* Whether the given test execution time is considered slow.
185
172
*
186
173
* @param int $time Test execution time in milliseconds
187
- * @param int $slowThreshold Test execution time at which a test should be considered slow (milliseconds)
188
- * @return bool
174
+ * @param int $slowThreshold Test execution time at which a test should be considered slow, in milliseconds
189
175
*/
190
- protected function isSlow ($ time , $ slowThreshold )
176
+ protected function isSlow (int $ time , int $ slowThreshold ): bool
191
177
{
192
178
return $ time >= $ slowThreshold ;
193
179
}
194
180
195
181
/**
196
182
* Stores a test as slow.
197
- *
198
- * @param TestCase $test
199
- * @param int $time Test execution time in milliseconds
200
183
*/
201
- protected function addSlowTest (TestCase $ test , $ time )
184
+ protected function addSlowTest (TestCase $ test , int $ time )
202
185
{
203
186
$ label = $ this ->makeLabel ($ test );
204
187
@@ -207,53 +190,41 @@ protected function addSlowTest(TestCase $test, $time)
207
190
208
191
/**
209
192
* Whether at least one test has been considered slow.
210
- *
211
- * @return bool
212
193
*/
213
- protected function hasSlowTests ()
194
+ protected function hasSlowTests (): bool
214
195
{
215
196
return !empty ($ this ->slow );
216
197
}
217
198
218
199
/**
219
200
* Convert PHPUnit's reported test time (microseconds) to milliseconds.
220
- *
221
- * @param float $time
222
- * @return int
223
201
*/
224
- protected function toMilliseconds ($ time )
202
+ protected function toMilliseconds (float $ time ): int
225
203
{
226
204
return (int ) round ($ time * 1000 );
227
205
}
228
206
229
207
/**
230
208
* Label describing a test.
231
- *
232
- * @param TestCase $test
233
- * @return string
234
209
*/
235
- protected function makeLabel (TestCase $ test )
210
+ protected function makeLabel (TestCase $ test ): string
236
211
{
237
212
return sprintf ('%s:%s ' , get_class ($ test ), $ test ->getName ());
238
213
}
239
214
240
215
/**
241
216
* Calculate number of tests to include in slowness report.
242
- *
243
- * @return int
244
217
*/
245
- protected function getReportLength ()
218
+ protected function getReportLength (): int
246
219
{
247
220
return min (count ($ this ->slow ), $ this ->reportLength );
248
221
}
249
222
250
223
/**
251
224
* Calculate number of slow tests to be hidden from the slowness report
252
225
* due to list length.
253
- *
254
- * @return int
255
226
*/
256
- protected function getHiddenCount ()
227
+ protected function getHiddenCount (): int
257
228
{
258
229
$ total = count ($ this ->slow );
259
230
$ showing = $ this ->getReportLength ();
@@ -302,13 +273,11 @@ protected function renderFooter()
302
273
303
274
/**
304
275
* Populate options into class internals.
305
- *
306
- * @param array $options
307
276
*/
308
277
protected function loadOptions (array $ options )
309
278
{
310
- $ this ->slowThreshold = isset ( $ options ['slowThreshold ' ]) ? $ options [ ' slowThreshold ' ] : 500 ;
311
- $ this ->reportLength = isset ( $ options ['reportLength ' ]) ? $ options [ ' reportLength ' ] : 10 ;
279
+ $ this ->slowThreshold = $ options ['slowThreshold ' ] ?? 500 ;
280
+ $ this ->reportLength = $ options ['reportLength ' ] ?? 10 ;
312
281
}
313
282
314
283
/**
@@ -323,14 +292,11 @@ protected function loadOptions(array $options)
323
292
* \@slowThreshold 5000
324
293
* public function testLongRunningProcess() {}
325
294
* </code>
326
- *
327
- * @param TestCase $test
328
- * @return int
329
295
*/
330
- protected function getSlowThreshold (TestCase $ test )
296
+ protected function getSlowThreshold (TestCase $ test ): int
331
297
{
332
298
$ ann = $ test ->getAnnotations ();
333
299
334
- return isset ($ ann ['method ' ]['slowThreshold ' ][0 ]) ? $ ann ['method ' ]['slowThreshold ' ][0 ] : $ this ->slowThreshold ;
300
+ return isset ($ ann ['method ' ]['slowThreshold ' ][0 ]) ? ( int ) $ ann ['method ' ]['slowThreshold ' ][0 ] : $ this ->slowThreshold ;
335
301
}
336
302
}
0 commit comments