Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace echo to flushable stdout #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the test suite as normal. If one or more test executions exceed the slow

## 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.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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/SpeedTrapListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 TestListener
{
Expand All @@ -21,6 +21,27 @@ class SpeedTrapListener implements 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;

/**
* Test execution time (milliseconds) after which a test will be considered
* "slow" and be included in the slowness report.
Expand All @@ -46,6 +67,16 @@ class SpeedTrapListener implements TestListener
public function __construct(array $options = [])
{
$this->loadOptions($options);

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

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

/**
Expand Down Expand Up @@ -169,7 +200,7 @@ protected function getHiddenCount(): int
*/
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 @@ -184,7 +215,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 @@ -194,7 +225,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 @@ -203,6 +235,8 @@ protected function renderFooter()
*/
protected function loadOptions(array $options)
{
$this->outPath = $options['outPath'] ?? 'php://stdout';
$this->forceFlush = $options['forceFlush'] ?? false;
$this->slowThreshold = $options['slowThreshold'] ?? 500;
$this->reportLength = $options['reportLength'] ?? 10;
}
Expand All @@ -226,4 +260,17 @@ protected function getSlowThreshold(TestCase $test): int

return isset($ann['method']['slowThreshold'][0]) ? (int) $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);
}
}