diff --git a/README.md b/README.md index 184f10d..cf0f0dd 100644 --- a/README.md +++ b/README.md @@ -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) @@ -49,6 +51,12 @@ These configuration parameters are set in `phpunit.xml` when adding the listener + + php://stderr + + + true + 500 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a37276d..2c719b7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -22,6 +22,12 @@ + + php://stderr + + + true + 500 diff --git a/src/SpeedTrapListener.php b/src/SpeedTrapListener.php index d3d532c..4f1a36e 100644 --- a/src/SpeedTrapListener.php +++ b/src/SpeedTrapListener.php @@ -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 { @@ -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. @@ -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); } /** @@ -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)); } /** @@ -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)); } } @@ -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)); } } @@ -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; } @@ -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); + } }