Skip to content

Commit 0bad4b6

Browse files
committed
Replace echo to flushable stdout - fix #19
Added outPath and forceFlush arguments FYI: This is useful, when a custom phpunit printer explicit flushes its results.
1 parent e87ceb0 commit 0bad4b6

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ Now run the test suite as normal. If one or more test executions exceed the slow
3434

3535
## Configuration
3636

37-
SpeedTrap has two configurable parameters:
37+
SpeedTrap has some configurable parameters:
3838

39+
* **outPath** - File path of the report output (Default: php://stdout)
40+
* **forceFlush** - Boolean flag for flushing after every written lines (Default: false)
3941
* **slowThreshold** - Number of milliseconds a test takes to execute before being considered "slow" (Default: 500ms)
4042
* **reportLength** - Number of slow tests included in the report (Default: 10 tests)
4143

@@ -49,6 +51,12 @@ These configuration parameters are set in `phpunit.xml` when adding the listener
4951
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
5052
<arguments>
5153
<array>
54+
<element key="outPath">
55+
<string>php://stderr</string>
56+
</element>
57+
<element key="forceFlush">
58+
<boolean>true</boolean>
59+
</element>
5260
<element key="slowThreshold">
5361
<integer>500</integer>
5462
</element>

phpunit.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
2323
<arguments>
2424
<array>
25+
<element key="outPath">
26+
<string>php://stderr</string>
27+
</element>
28+
<element key="forceFlush">
29+
<boolean>true</boolean>
30+
</element>
2531
<element key="slowThreshold">
2632
<integer>500</integer>
2733
</element>

src/SpeedTrapListener.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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
*/
1212
class 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

Comments
 (0)