Skip to content

Commit

Permalink
Replace echo to flushable stdout - fix #19
Browse files Browse the repository at this point in the history
Added outPath and forceFlush arguments

FYI:
This is useful, when a custom phpunit printer explicit flushes its results.
  • Loading branch information
andras-tim committed Mar 29, 2017
1 parent f7cfe17 commit 575f14e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ Now run your test suite as normal. If tests run that exceed the slowness thresho

## Configuration

SpeedTrap has two configurable parameters:
SpeedTrap has some configurable parameters:

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

Expand All @@ -49,6 +51,12 @@ These configuration parameters are set in `phpunit.xml` when adding the listener
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
<arguments>
<array>
<element key="outPath">
<string>php://stderr</string>
</element>
<element key="forceFlush">
<boolean>true</boolean>
</element>
<element key="slowThreshold">
<integer>500</integer>
</element>
Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
<arguments>
<array>
<element key="outPath">
<string>php://stderr</string>
</element>
<element key="forceFlush">
<boolean>true</boolean>
</element>
<element key="slowThreshold">
<integer>500</integer>
</element>
Expand Down
55 changes: 51 additions & 4 deletions src/JohnKary/PHPUnit/Listener/SpeedTrapListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* A PHPUnit TestListener that exposes your slowest running tests by outputting
* results directly to the console.
* results directly to the console or output file.
*/
class SpeedTrapListener implements \PHPUnit_Framework_TestListener
{
Expand All @@ -18,6 +18,27 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
*/
protected $suites = 0;

/**
* Output path
*
* @var string
*/
protected $outPath;

/**
* Output descriptor
*
* @var resource
*/
protected $out;

/**
* If True, flush output after every write.
*
* @var boolean
*/
protected $forceFlush;

/**
* Time in milliseconds at which a test will be considered "slow" and be
* reported by this listener.
Expand Down Expand Up @@ -48,6 +69,16 @@ class SpeedTrapListener implements \PHPUnit_Framework_TestListener
public function __construct(array $options = array())
{
$this->loadOptions($options);

$this->out = fopen($this->outPath, 'wt');
}

/**
* Destruct the instance
*/
public function __destruct()
{
fclose($this->out);
}

/**
Expand Down Expand Up @@ -266,7 +297,7 @@ protected function getHiddenCount()
*/
protected function renderHeader()
{
echo sprintf("\n\nYou should really fix these slow tests (>%sms)...\n", $this->slowThreshold);
$this->write(sprintf("\n\nYou should really fix these slow tests (>%sms)...\n", $this->slowThreshold));
}

/**
Expand All @@ -281,7 +312,7 @@ protected function renderBody()
$label = key($slowTests);
$time = array_shift($slowTests);

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

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

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

return isset($ann['method']['slowThreshold'][0]) ? $ann['method']['slowThreshold'][0] : $this->slowThreshold;
}

/**
* Write text to output
*
* @param string $buffer
*/
protected function write($buffer)
{
fwrite($this->out, $buffer);

if ($this->forceFlush)
fflush($this->out);
}
}

0 comments on commit 575f14e

Please sign in to comment.