Skip to content

Commit d3f4ad2

Browse files
committed
Add Start-aware output handler
1 parent a06179f commit d3f4ad2

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

src/Runner/Runner.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function run(): bool
121121
$running[] = $job = array_shift($this->jobs);
122122
$async = $this->threadCount > 1 && (count($running) + count($this->jobs) > 1);
123123
$job->setEnvironmentVariable(Environment::THREAD, (string) array_shift($threads));
124+
$this->startTest($job->getTest());
124125
$job->run($async ? $job::RUN_ASYNC : 0);
125126
}
126127

@@ -191,6 +192,19 @@ public function prepareTest(Test $test): void
191192
}
192193

193194

195+
/**
196+
* Writes to start-aware output handlers
197+
*/
198+
private function startTest(Test $test): void
199+
{
200+
foreach ($this->outputHandlers as $handler) {
201+
if ($handler instanceof StartAwareOutputHandler) {
202+
$handler->start($test);
203+
}
204+
}
205+
}
206+
207+
194208
/**
195209
* Writes to output handlers.
196210
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tester\Runner;
5+
6+
/**
7+
* For output handlers that wish to output the time it took to perform a test
8+
*/
9+
interface StartAwareOutputHandler extends OutputHandler
10+
{
11+
/**
12+
* Called when the test starts
13+
* @param Test $test
14+
*/
15+
function start(Test $test): void;
16+
}

src/tester.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require __DIR__ . '/Runner/CommandLine.php';
1616
require __DIR__ . '/Runner/TestHandler.php';
1717
require __DIR__ . '/Runner/OutputHandler.php';
18+
require __DIR__ . '/Runner/StartAwareOutputHandler.php';
1819
require __DIR__ . '/Runner/Output/Logger.php';
1920
require __DIR__ . '/Runner/Output/TapPrinter.php';
2021
require __DIR__ . '/Runner/Output/ConsolePrinter.php';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tester\Assert;
6+
use Tester\Runner\Test;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
require __DIR__ . '/../../src/Runner/Test.php';
10+
require __DIR__ . '/../../src/Runner/TestHandler.php';
11+
require __DIR__ . '/../../src/Runner/Runner.php';
12+
require __DIR__ . '/../../src/Runner/OutputHandler.php';
13+
require __DIR__ . '/../../src/Runner/StartAwareOutputHandler.php';
14+
15+
class TimeAwareLogger implements \Tester\Runner\StartAwareOutputHandler
16+
{
17+
/**
18+
* @var float
19+
*/
20+
public $lastTimeStarted = NULL;
21+
22+
function start(Test $test): void
23+
{
24+
$this->lastTimeStarted = microtime(TRUE);
25+
}
26+
27+
function begin(): void
28+
{
29+
}
30+
31+
function prepare(Test $test): void
32+
{
33+
}
34+
35+
function finish(Test $test): void
36+
{
37+
}
38+
39+
function end(): void
40+
{
41+
}
42+
}
43+
44+
$start = microtime(TRUE);
45+
$runner = new Tester\Runner\Runner(createInterpreter());
46+
$runner->paths = [__DIR__ . "/start-aware/*.phptx"];
47+
$runner->outputHandlers[] = $logger = new TimeAwareLogger;
48+
49+
$toleranceMicroSeconds = 1000;
50+
$runner->run();
51+
52+
Assert::notSame(NULL, $logger->lastTimeStarted, "Test start time should be logged");
53+
$difference = abs($start - $logger->lastTimeStarted);
54+
if ($difference > $toleranceMicroSeconds) {
55+
Assert::fail("Time {$logger->lastTimeStarted} should be within {$toleranceMicroSeconds} microseconds of $start, was {$difference}");
56+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
require __DIR__ . '/../../bootstrap.php';
4+
Tester\Environment::$checkAssertions = false;

0 commit comments

Comments
 (0)