Skip to content
Permalink
Browse files

Add Start-aware output handler

  • Loading branch information...
veproza committed Aug 25, 2018
1 parent a06179f commit d3f4ad2c500d2b927eb372ec2be9b2f594bec9bc
Showing with 91 additions and 0 deletions.
  1. +14 βˆ’0 src/Runner/Runner.php
  2. +16 βˆ’0 src/Runner/StartAwareOutputHandler.php
  3. +1 βˆ’0 src/tester.php
  4. +56 βˆ’0 tests/Runner/Runner.start-aware-output.phpt
  5. +4 βˆ’0 tests/Runner/start-aware/pass.phptx
@@ -121,6 +121,7 @@ public function run(): bool
$running[] = $job = array_shift($this->jobs);
$async = $this->threadCount > 1 && (count($running) + count($this->jobs) > 1);
$job->setEnvironmentVariable(Environment::THREAD, (string) array_shift($threads));
$this->startTest($job->getTest());
$job->run($async ? $job::RUN_ASYNC : 0);
}
@@ -191,6 +192,19 @@ public function prepareTest(Test $test): void
}
/**
* Writes to start-aware output handlers
*/
private function startTest(Test $test): void
{
foreach ($this->outputHandlers as $handler) {
if ($handler instanceof StartAwareOutputHandler) {
$handler->start($test);
}
}
}
/**
* Writes to output handlers.
*/
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Tester\Runner;
/**
* For output handlers that wish to output the time it took to perform a test
*/
interface StartAwareOutputHandler extends OutputHandler
{
/**
* Called when the test starts
* @param Test $test
*/
function start(Test $test): void;
}
@@ -15,6 +15,7 @@
require __DIR__ . '/Runner/CommandLine.php';
require __DIR__ . '/Runner/TestHandler.php';
require __DIR__ . '/Runner/OutputHandler.php';
require __DIR__ . '/Runner/StartAwareOutputHandler.php';
require __DIR__ . '/Runner/Output/Logger.php';
require __DIR__ . '/Runner/Output/TapPrinter.php';
require __DIR__ . '/Runner/Output/ConsolePrinter.php';
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
use Tester\Assert;
use Tester\Runner\Test;
require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../../src/Runner/Test.php';
require __DIR__ . '/../../src/Runner/TestHandler.php';
require __DIR__ . '/../../src/Runner/Runner.php';
require __DIR__ . '/../../src/Runner/OutputHandler.php';
require __DIR__ . '/../../src/Runner/StartAwareOutputHandler.php';
class TimeAwareLogger implements \Tester\Runner\StartAwareOutputHandler
{
/**
* @var float
*/
public $lastTimeStarted = NULL;
function start(Test $test): void
{
$this->lastTimeStarted = microtime(TRUE);
}
function begin(): void
{
}
function prepare(Test $test): void
{
}
function finish(Test $test): void
{
}
function end(): void
{
}
}
$start = microtime(TRUE);
$runner = new Tester\Runner\Runner(createInterpreter());
$runner->paths = [__DIR__ . "/start-aware/*.phptx"];
$runner->outputHandlers[] = $logger = new TimeAwareLogger;
$toleranceMicroSeconds = 1000;
$runner->run();
Assert::notSame(NULL, $logger->lastTimeStarted, "Test start time should be logged");
$difference = abs($start - $logger->lastTimeStarted);
if ($difference > $toleranceMicroSeconds) {
Assert::fail("Time {$logger->lastTimeStarted} should be within {$toleranceMicroSeconds} microseconds of $start, was {$difference}");
}
@@ -0,0 +1,4 @@
<?php

require __DIR__ . '/../../bootstrap.php';
Tester\Environment::$checkAssertions = false;

0 comments on commit d3f4ad2

Please sign in to comment.
You can’t perform that action at this time.