Skip to content

Commit 9207fe9

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 f7cfe17 commit 9207fe9

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 your test suite as normal. If tests run that exceed the slowness thresho
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.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
2727
<arguments>
2828
<array>
29+
<element key="outPath">
30+
<string>php://stderr</string>
31+
</element>
32+
<element key="forceFlush">
33+
<boolean>true</boolean>
34+
</element>
2935
<element key="slowThreshold">
3036
<integer>500</integer>
3137
</element>

src/JohnKary/PHPUnit/Listener/SpeedTrapListener.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* A PHPUnit TestListener that exposes your slowest running tests by outputting
7-
* results directly to the console.
7+
* results directly to the console or output file.
88
*/
99
class SpeedTrapListener implements \PHPUnit_Framework_TestListener
1010
{
@@ -18,6 +18,27 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
1818
*/
1919
protected $suites = 0;
2020

21+
/**
22+
* Output path
23+
*
24+
* @var string
25+
*/
26+
protected $outPath;
27+
28+
/**
29+
* Output descriptor
30+
*
31+
* @var resource
32+
*/
33+
protected $out;
34+
35+
/**
36+
* If True, flush output after every write.
37+
*
38+
* @var boolean
39+
*/
40+
protected $forceFlush;
41+
2142
/**
2243
* Time in milliseconds at which a test will be considered "slow" and be
2344
* reported by this listener.
@@ -48,6 +69,16 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
4869
public function __construct(array $options = array())
4970
{
5071
$this->loadOptions($options);
72+
73+
$this->out = fopen($this->outPath, 'wt');
74+
}
75+
76+
/**
77+
* Destruct the instance
78+
*/
79+
public function __destruct()
80+
{
81+
fclose($this->out);
5182
}
5283

5384
/**
@@ -266,7 +297,7 @@ protected function getHiddenCount()
266297
*/
267298
protected function renderHeader()
268299
{
269-
echo sprintf("\n\nYou should really fix these slow tests (>%sms)...\n", $this->slowThreshold);
300+
$this->write(sprintf("\n\nYou should really fix these slow tests (>%sms)...\n", $this->slowThreshold));
270301
}
271302

272303
/**
@@ -281,7 +312,7 @@ protected function renderBody()
281312
$label = key($slowTests);
282313
$time = array_shift($slowTests);
283314

284-
echo sprintf(" %s. %sms to run %s\n", $i, $time, $label);
315+
$this->write(sprintf(" %s. %sms to run %s\n", $i, $time, $label));
285316
}
286317
}
287318

@@ -291,7 +322,8 @@ protected function renderBody()
291322
protected function renderFooter()
292323
{
293324
if ($hidden = $this->getHiddenCount()) {
294-
echo sprintf("...and there %s %s more above your threshold hidden from view", $hidden == 1 ? 'is' : 'are', $hidden);
325+
$this->write(sprintf("...and there %s %s more above your threshold hidden from view",
326+
$hidden == 1 ? 'is' : 'are', $hidden));
295327
}
296328
}
297329

@@ -302,6 +334,8 @@ protected function renderFooter()
302334
*/
303335
protected function loadOptions(array $options)
304336
{
337+
$this->outPath = isset($options['outPath']) ? $options['outPath'] : 'php://stdout';
338+
$this->forceFlush = isset($options['forceFlush']) ? $options['forceFlush'] : false;
305339
$this->slowThreshold = isset($options['slowThreshold']) ? $options['slowThreshold'] : 500;
306340
$this->reportLength = isset($options['reportLength']) ? $options['reportLength'] : 10;
307341
}
@@ -328,4 +362,17 @@ protected function getSlowThreshold(\PHPUnit_Framework_TestCase $test)
328362

329363
return isset($ann['method']['slowThreshold'][0]) ? $ann['method']['slowThreshold'][0] : $this->slowThreshold;
330364
}
365+
366+
/**
367+
* Write text to output
368+
*
369+
* @param string $buffer
370+
*/
371+
protected function write($buffer)
372+
{
373+
fwrite($this->out, $buffer);
374+
375+
if ($this->forceFlush)
376+
fflush($this->out);
377+
}
331378
}

0 commit comments

Comments
 (0)