Skip to content

Commit

Permalink
Add Start-aware output handler
Browse files Browse the repository at this point in the history
  • Loading branch information
veproza committed Aug 25, 2018
1 parent a06179f commit d3f4ad2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Runner/StartAwareOutputHandler.php
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
56 changes: 56 additions & 0 deletions tests/Runner/Runner.start-aware-output.phpt
Original file line number Diff line number Diff line change
@@ -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}");
}
4 changes: 4 additions & 0 deletions tests/Runner/start-aware/pass.phptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

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

0 comments on commit d3f4ad2

Please sign in to comment.