Skip to content

Commit 7bbbad1

Browse files
authored
Merge pull request #30 from johnkary/php7
Add PHP 7 features
2 parents dd06493 + a0d50cd commit 7bbbad1

File tree

4 files changed

+48
-59
lines changed

4 files changed

+48
-59
lines changed

UPGRADE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
UPGRADE FROM 1.x to 2.0
2+
=======================
3+
4+
### `JohnKary\PHPUnit\Listener\SpeedTrapListener` subclasses must implement scalar type hints
5+
6+
SpeedTrapListener was upgraded to support PHP 7 scalar type hints. Any
7+
subclass will need to update the overridden function signature:
8+
9+
* Declare strict types at the top of your subclass: `declare(strict_types=1);`
10+
* Update method signatures:
11+
12+
| Old signature | New signature |
13+
| -------- | --- |
14+
| `protected function isSlow($time, $slowThreshold)` | `protected function isSlow(int $time, int $slowThreshold) : bool`
15+
| `protected function addSlowTest(TestCase $test, $time)` | `protected function addSlowTest(TestCase $test, int $time)`
16+
| `protected function hasSlowTests()` | `protected function hasSlowTests() : bool`
17+
| `protected function toMilliseconds($time)` | `protected function toMilliseconds(float $time) : int`
18+
| `protected function makeLabel(TestCase $test)` | `protected function makeLabel(TestCase $test) : string`
19+
| `protected function getReportLength()` | `protected function getReportLength() : int`
20+
| `protected function getHiddenCount()` | `protected function getHiddenCount() : int`
21+
| `protected function getSlowThreshold(TestCase $test)` | `protected function getSlowThreshold(TestCase $test) : int`

src/JohnKary/PHPUnit/Listener/SpeedTrapListener.php

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace JohnKary\PHPUnit\Listener;
45

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};
117

128
/**
139
* A PHPUnit TestListener that exposes your slowest running tests by outputting
@@ -20,8 +16,6 @@ class SpeedTrapListener implements TestListener
2016
*
2117
* Increments as more suites are run, then decremented as they finish. All
2218
* suites have been run when returns to 0.
23-
*
24-
* @var int
2519
*/
2620
protected $suites = 0;
2721

@@ -42,19 +36,12 @@ class SpeedTrapListener implements TestListener
4236

4337
/**
4438
* 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
4941
*/
50-
protected $slow = array();
42+
protected $slow = [];
5143

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 = [])
5845
{
5946
$this->loadOptions($options);
6047
}
@@ -184,21 +171,17 @@ public function endTestSuite(TestSuite $suite)
184171
* Whether the given test execution time is considered slow.
185172
*
186173
* @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
189175
*/
190-
protected function isSlow($time, $slowThreshold)
176+
protected function isSlow(int $time, int $slowThreshold): bool
191177
{
192178
return $time >= $slowThreshold;
193179
}
194180

195181
/**
196182
* Stores a test as slow.
197-
*
198-
* @param TestCase $test
199-
* @param int $time Test execution time in milliseconds
200183
*/
201-
protected function addSlowTest(TestCase $test, $time)
184+
protected function addSlowTest(TestCase $test, int $time)
202185
{
203186
$label = $this->makeLabel($test);
204187

@@ -207,53 +190,41 @@ protected function addSlowTest(TestCase $test, $time)
207190

208191
/**
209192
* Whether at least one test has been considered slow.
210-
*
211-
* @return bool
212193
*/
213-
protected function hasSlowTests()
194+
protected function hasSlowTests(): bool
214195
{
215196
return !empty($this->slow);
216197
}
217198

218199
/**
219200
* Convert PHPUnit's reported test time (microseconds) to milliseconds.
220-
*
221-
* @param float $time
222-
* @return int
223201
*/
224-
protected function toMilliseconds($time)
202+
protected function toMilliseconds(float $time): int
225203
{
226204
return (int) round($time * 1000);
227205
}
228206

229207
/**
230208
* Label describing a test.
231-
*
232-
* @param TestCase $test
233-
* @return string
234209
*/
235-
protected function makeLabel(TestCase $test)
210+
protected function makeLabel(TestCase $test): string
236211
{
237212
return sprintf('%s:%s', get_class($test), $test->getName());
238213
}
239214

240215
/**
241216
* Calculate number of tests to include in slowness report.
242-
*
243-
* @return int
244217
*/
245-
protected function getReportLength()
218+
protected function getReportLength(): int
246219
{
247220
return min(count($this->slow), $this->reportLength);
248221
}
249222

250223
/**
251224
* Calculate number of slow tests to be hidden from the slowness report
252225
* due to list length.
253-
*
254-
* @return int
255226
*/
256-
protected function getHiddenCount()
227+
protected function getHiddenCount(): int
257228
{
258229
$total = count($this->slow);
259230
$showing = $this->getReportLength();
@@ -302,13 +273,11 @@ protected function renderFooter()
302273

303274
/**
304275
* Populate options into class internals.
305-
*
306-
* @param array $options
307276
*/
308277
protected function loadOptions(array $options)
309278
{
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;
312281
}
313282

314283
/**
@@ -323,14 +292,11 @@ protected function loadOptions(array $options)
323292
* \@slowThreshold 5000
324293
* public function testLongRunningProcess() {}
325294
* </code>
326-
*
327-
* @param TestCase $test
328-
* @return int
329295
*/
330-
protected function getSlowThreshold(TestCase $test)
296+
protected function getSlowThreshold(TestCase $test): int
331297
{
332298
$ann = $test->getAnnotations();
333299

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;
335301
}
336302
}

src/JohnKary/PHPUnit/Listener/Tests/ExceptionalTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace JohnKary\PHPUnit\Listener\Tests;
45

src/JohnKary/PHPUnit/Listener/Tests/SomeSlowTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace JohnKary\PHPUnit\Listener\Tests;
45

@@ -35,19 +36,19 @@ public function testLongEndToEndTest()
3536
/**
3637
* @dataProvider provideTime
3738
*/
38-
public function testWithDataProvider($time)
39+
public function testWithDataProvider(int $time)
3940
{
4041
$this->extendTime($time);
4142

4243
$this->assertTrue(true);
4344
}
4445
public function provideTime()
4546
{
46-
return array(
47-
'Rock' => array(800),
48-
'Chalk' => array(700),
49-
'Jayhawk' => array(600),
50-
);
47+
return [
48+
'Rock' => [800],
49+
'Chalk' => [700],
50+
'Jayhawk' => [600],
51+
];
5152
}
5253

5354
/**
@@ -79,7 +80,7 @@ public function testCanSetHigherSlowThreshold()
7980
/**
8081
* @param int $ms Number of additional microseconds to execute code
8182
*/
82-
private function extendTime($ms)
83+
private function extendTime(int $ms)
8384
{
8485
usleep($ms * 1000);
8586
}

0 commit comments

Comments
 (0)