Permalink
Browse files

Runner: getResults() and getJobCount() logic moved to output handlers…

… (BC break)
  • Loading branch information...
milo committed Aug 3, 2017
1 parent e000563 commit 99d7980625edc31184077a9988c87c935056b153
@@ -201,10 +201,10 @@ private function createRunner()
if ($this->options['-o'] !== 'none') {
switch ($this->options['-o']) {
case 'tap':
$runner->outputHandlers[] = new Output\TapPrinter($runner);
$runner->outputHandlers[] = new Output\TapPrinter;
break;
case 'junit':
$runner->outputHandlers[] = new Output\JUnitPrinter($runner);
$runner->outputHandlers[] = new Output\JUnitPrinter;
break;
default:
$runner->outputHandlers[] = new Output\ConsolePrinter($runner, $this->options['-s']);
@@ -33,6 +33,12 @@ class ConsolePrinter implements Tester\Runner\OutputHandler
/** @var float */
private $time;
/** @var int */
private $count;
/** @var array */
private $results;
public function __construct(Runner $runner, $displaySkipped = false, $file = 'php://output')
{
@@ -44,6 +50,12 @@ public function __construct(Runner $runner, $displaySkipped = false, $file = 'ph
public function begin()
{
$this->count = 0;
$this->results = [
Test::PASSED => 0,
Test::SKIPPED => 0,
Test::FAILED => 0,
];
$this->time = -microtime(true);
fwrite($this->file, $this->runner->getInterpreter()->getShortInfo()
. ' | ' . $this->runner->getInterpreter()->getCommandLine()
@@ -53,11 +65,13 @@ public function begin()
public function prepare(Test $test)
{
$this->count++;
}
public function finish(Test $test)
{
$this->results[$test->getResult()]++;
$outputs = [
Test::PASSED => '.',
Test::SKIPPED => 's',
@@ -76,16 +90,14 @@ public function finish(Test $test)
public function end()
{
$jobCount = $this->runner->getJobCount();
$results = $this->runner->getResults();
$count = array_sum($results);
fwrite($this->file, !$jobCount ? "No tests found\n" :
$run = array_sum($this->results);
fwrite($this->file, !$this->count ? "No tests found\n" :
"\n\n" . $this->buffer . "\n"
. ($results[Test::FAILED] ? Dumper::color('white/red') . 'FAILURES!' : Dumper::color('white/green') . 'OK')
. " ($jobCount test" . ($jobCount > 1 ? 's' : '') . ', '
. ($results[Test::FAILED] ? $results[Test::FAILED] . ' failure' . ($results[Test::FAILED] > 1 ? 's' : '') . ', ' : '')
. ($results[Test::SKIPPED] ? $results[Test::SKIPPED] . ' skipped, ' : '')
. ($jobCount !== $count ? ($jobCount - $count) . ' not run, ' : '')
. ($this->results[Test::FAILED] ? Dumper::color('white/red') . 'FAILURES!' : Dumper::color('white/green') . 'OK')
. " ($this->count test" . ($this->count > 1 ? 's' : '') . ', '
. ($this->results[Test::FAILED] ? $this->results[Test::FAILED] . ' failure' . ($this->results[Test::FAILED] > 1 ? 's' : '') . ', ' : '')
. ($this->results[Test::SKIPPED] ? $this->results[Test::SKIPPED] . ' skipped, ' : '')
. ($this->count !== $run ? ($this->count - $run) . ' not run, ' : '')
. sprintf('%0.1f', $this->time + microtime(true)) . ' seconds)' . Dumper::color() . "\n");
$this->buffer = null;
@@ -8,7 +8,6 @@
namespace Tester\Runner\Output;
use Tester;
use Tester\Runner\Runner;
use Tester\Runner\Test;
@@ -17,9 +16,6 @@
*/
class JUnitPrinter implements Tester\Runner\OutputHandler
{
/** @var Runner */
private $runner;
/** @var resource */
private $file;
@@ -29,16 +25,23 @@ class JUnitPrinter implements Tester\Runner\OutputHandler
/** @var float */
private $startTime;
/** @var array */
private $results;
public function __construct(Runner $runner, $file = 'php://output')
public function __construct($file = 'php://output')
{
$this->runner = $runner;
$this->file = fopen($file, 'w');
}
public function begin()
{
$this->results = [
Test::PASSED => 0,
Test::SKIPPED => 0,
Test::FAILED => 0,
];
$this->startTime = microtime(true);
fwrite($this->file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n");
}
@@ -51,6 +54,7 @@ public function prepare(Test $test)
public function finish(Test $test)
{
$this->results[$test->getResult()]++;
$this->buffer .= "\t\t<testcase classname=\"" . htmlspecialchars($test->getSignature()) . '" name="' . htmlspecialchars($test->getSignature()) . '"';
switch ($test->getResult()) {
@@ -71,8 +75,7 @@ public function end()
{
$time = sprintf('%0.1f', microtime(true) - $this->startTime);
$output = $this->buffer;
$results = $this->runner->getResults();
$this->buffer = "\t<testsuite errors=\"{$results[Test::FAILED]}\" skipped=\"{$results[Test::SKIPPED]}\" tests=\"" . array_sum($results) . "\" time=\"$time\" timestamp=\"" . @date('Y-m-d\TH:i:s') . "\">\n";
$this->buffer = "\t<testsuite errors=\"{$this->results[Test::FAILED]}\" skipped=\"{$this->results[Test::SKIPPED]}\" tests=\"" . array_sum($this->results) . "\" time=\"$time\" timestamp=\"" . @date('Y-m-d\TH:i:s') . "\">\n";
$this->buffer .= $output;
$this->buffer .= "\t</testsuite>";
@@ -23,6 +23,12 @@ class Logger implements Tester\Runner\OutputHandler
/** @var resource */
private $file;
/** @var int */
private $count;
/** @var array */
private $results;
public function __construct(Runner $runner, $file = 'php://output')
{
@@ -33,6 +39,12 @@ public function __construct(Runner $runner, $file = 'php://output')
public function begin()
{
$this->count = 0;
$this->results = [
Test::PASSED => 0,
Test::SKIPPED => 0,
Test::FAILED => 0,
];
fwrite($this->file, 'PHP ' . $this->runner->getInterpreter()->getVersion()
. ' | ' . $this->runner->getInterpreter()->getCommandLine()
. " | {$this->runner->threadCount} threads\n\n");
@@ -41,11 +53,13 @@ public function begin()
public function prepare(Test $test)
{
$this->count++;
}
public function finish(Test $test)
{
$this->results[$test->getResult()]++;
$message = ' ' . str_replace("\n", "\n ", Tester\Dumper::removeColors(trim($test->message)));
$outputs = [
Test::PASSED => "-- OK: {$test->getSignature()}",
@@ -58,15 +72,13 @@ public function finish(Test $test)
public function end()
{
$jobCount = $this->runner->getJobCount();
$results = $this->runner->getResults();
$count = array_sum($results);
$run = array_sum($this->results);
fwrite($this->file,
($results[Test::FAILED] ? 'FAILURES!' : 'OK')
. " ($jobCount tests"
. ($results[Test::FAILED] ? ", {$results[Test::FAILED]} failures" : '')
. ($results[Test::SKIPPED] ? ", {$results[Test::SKIPPED]} skipped" : '')
. ($jobCount !== $count ? ', ' . ($jobCount - $count) . ' not run' : '')
($this->results[Test::FAILED] ? 'FAILURES!' : 'OK')
. " ($this->count tests"
. ($this->results[Test::FAILED] ? ", {$this->results[Test::FAILED]} failures" : '')
. ($this->results[Test::SKIPPED] ? ", {$this->results[Test::SKIPPED]} skipped" : '')
. ($this->count !== $run ? ', ' . ($this->count - $run) . ' not run' : '')
. ')'
);
}
@@ -8,7 +8,6 @@
namespace Tester\Runner\Output;
use Tester;
use Tester\Runner\Runner;
use Tester\Runner\Test;
@@ -17,22 +16,26 @@
*/
class TapPrinter implements Tester\Runner\OutputHandler
{
/** @var Runner */
private $runner;
/** @var resource */
private $file;
/** @var array */
private $results;
public function __construct(Runner $runner, $file = 'php://output')
public function __construct($file = 'php://output')
{
$this->runner = $runner;
$this->file = fopen($file, 'w');
}
public function begin()
{
$this->results = [
Test::PASSED => 0,
Test::SKIPPED => 0,
Test::FAILED => 0,
];
fwrite($this->file, "TAP version 13\n");
}
@@ -44,6 +47,7 @@ public function prepare(Test $test)
public function finish(Test $test)
{
$this->results[$test->getResult()]++;
$message = str_replace("\n", "\n# ", trim($test->message));
$outputs = [
Test::PASSED => "ok {$test->getSignature()}",
@@ -56,6 +60,6 @@ public function finish(Test $test)
public function end()
{
fwrite($this->file, '1..' . array_sum($this->runner->getResults()));
fwrite($this->file, '1..' . array_sum($this->results));
}
}
@@ -39,18 +39,15 @@ class Runner
/** @var Job[] */
private $jobs;
/** @var int */
private $jobCount;
/** @var array */
private $results;
/** @var bool */
private $interrupted;
/** @var string|null */
private $tempDir;
/** @var bool */
private $result;
/** @var array */
private $lastResults = [];
@@ -106,18 +103,17 @@ public function setTempDirectory($path)
*/
public function run()
{
$this->result = true;
$this->interrupted = false;
foreach ($this->outputHandlers as $handler) {
$handler->begin();
}
$this->results = [Test::PASSED => 0, Test::SKIPPED => 0, Test::FAILED => 0];
$this->jobs = $running = [];
foreach ($this->paths as $path) {
$this->findTests($path);
}
$this->jobCount = count($this->jobs) + array_sum($this->results);
if ($this->tempDir) {
usort($this->jobs, function (Job $a, Job $b) {
@@ -157,7 +153,8 @@ public function run()
foreach ($this->outputHandlers as $handler) {
$handler->end();
}
return !$this->results[Test::FAILED];
return $this->result;
}
@@ -198,16 +195,6 @@ public function addJob(Job $job)
}
/**
* Get count of jobs.
* @return int
*/
public function getJobCount()
{
return $this->jobCount;
}
/**
* @return void
*/
@@ -225,7 +212,8 @@ public function prepareTest(Test $test)
*/
public function finishTest(Test $test)
{
$this->results[$test->getResult()]++;
$this->result = $this->result && ($test->getResult() !== Test::FAILED);
foreach ($this->outputHandlers as $handler) {
$handler->finish(clone $test);
}
@@ -252,15 +240,6 @@ public function getInterpreter()
}
/**
* @return array
*/
public function getResults()
{
return $this->results;
}
/**
* @return void
*/
@@ -50,7 +50,7 @@ test(function () use ($interpreter) {
__DIR__ . '/stop-on-fail/pass.phptx',
];
Assert::notSame(0, $runner->run());
Assert::false($runner->run());
Assert::same([
[Test::FAILED, 'init-fail.phptx'],
[Test::FAILED, 'runtime-fail.phptx'],
@@ -69,7 +69,7 @@ test(function () use ($interpreter) {
__DIR__ . '/stop-on-fail/pass.phptx',
];
Assert::notSame(0, $runner->run());
Assert::false($runner->run());
Assert::same([
[Test::FAILED, 'init-fail.phptx'],
], $logger->results);
@@ -86,7 +86,7 @@ test(function () use ($interpreter) {
__DIR__ . '/stop-on-fail/pass.phptx',
];
Assert::notSame(0, $runner->run());
Assert::false($runner->run());
Assert::same([
[Test::FAILED, 'runtime-fail.phptx'],
], $logger->results);
@@ -25,9 +25,9 @@ $runner = new Runner(createInterpreter());
$runner->paths[] = __DIR__ . '/cases/*.phptx';
$runner->outputHandlers[] = new Output\ConsolePrinter($runner, false, $console = FileMock::create(''));
$runner->outputHandlers[] = new Output\ConsolePrinter($runner, true, $consoleWithSkipped = FileMock::create(''));
$runner->outputHandlers[] = new Output\JUnitPrinter($runner, $jUnit = FileMock::create(''));
$runner->outputHandlers[] = new Output\JUnitPrinter($jUnit = FileMock::create(''));
$runner->outputHandlers[] = new Output\Logger($runner, $logger = FileMock::create(''));
$runner->outputHandlers[] = new Output\TapPrinter($runner, $tap = FileMock::create(''));
$runner->outputHandlers[] = new Output\TapPrinter($tap = FileMock::create(''));
$runner->run();

0 comments on commit 99d7980

Please sign in to comment.