Skip to content

Commit

Permalink
TestHandler, Runner & Job uses Test
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed May 2, 2017
1 parent a34637b commit 65ea0f8
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 213 deletions.
4 changes: 2 additions & 2 deletions src/Runner/CliTester.php
Expand Up @@ -48,9 +48,9 @@ public function run()
$this->createPhpInterpreter();

if ($this->options['--info']) {
$job = new Job(__DIR__ . '/info.php', $this->interpreter);
$job = new Job(new Test(__DIR__ . '/info.php'), $this->interpreter);
$job->run();
echo $job->getOutput();
echo $job->getTest()->stdout;
return;
}

Expand Down
73 changes: 25 additions & 48 deletions src/Runner/Job.php
Expand Up @@ -29,11 +29,8 @@ class Job
RUN_ASYNC = 1,
RUN_COLLECT_ERRORS = 2;

/** @var string test file */
private $file;

/** @var string[] test arguments */
private $args;
/** @var Test */
private $test;

/** @var string[] environment variables for test */
private $envVars;
Expand Down Expand Up @@ -63,15 +60,14 @@ class Job
private $exitCode = self::CODE_NONE;


/**
* @param string test file name
* @return void
*/
public function __construct($testFile, PhpInterpreter $interpreter, array $args = NULL, array $envVars = NULL)
public function __construct(Test $test, PhpInterpreter $interpreter, array $envVars = NULL)
{
$this->file = (string) $testFile;
if ($test->getResult() !== Test::PREPARED) {
throw new \LogicException("Test {$test->getName()} already has result {$test->getResult()}.");
}

$this->test = $test;
$this->interpreter = $interpreter;
$this->args = (array) $args;
$this->envVars = (array) $envVars;
}

Expand Down Expand Up @@ -108,16 +104,25 @@ public function run($flags = NULL)
putenv("$name=$value");
}

$args = [];
foreach ($this->test->getArguments() as $value) {
if (is_array($value)) {
$args[] = Helpers::escapeArg("--$value[0]=$value[1]");
} else {
$args[] = Helpers::escapeArg($value);
}
}

$this->proc = proc_open(
$this->interpreter->getCommandLine()
. ' -d register_argc_argv=on ' . Helpers::escapeArg($this->file) . ' ' . implode(' ', $this->args),
. ' -d register_argc_argv=on ' . Helpers::escapeArg($this->test->getFile()) . ' ' . implode(' ', $args),
[
['pipe', 'r'],
['pipe', 'w'],
['pipe', 'w'],
],
$pipes,
dirname($this->file),
dirname($this->test->getFile()),
NULL,
['bypass_shell' => TRUE]
);
Expand Down Expand Up @@ -182,27 +187,19 @@ public function isRunning()
}
}
}
return FALSE;
}


/**
* Returns test file path.
* @return string
*/
public function getFile()
{
return $this->file;
$this->test->stdout = $this->output;
$this->test->stderr = $this->errorOutput;
return FALSE;
}


/**
* Returns script arguments.
* @return string[]
* @return Test
*/
public function getArguments()
public function getTest()
{
return $this->args;
return $this->test;
}


Expand All @@ -216,26 +213,6 @@ public function getExitCode()
}


/**
* Returns test output.
* @return string
*/
public function getOutput()
{
return $this->output;
}


/**
* Returns test error output.
* @return string|NULL
*/
public function getErrorOutput()
{
return $this->errorOutput;
}


/**
* Returns output headers.
* @return string[]
Expand Down
17 changes: 9 additions & 8 deletions src/Runner/Output/ConsolePrinter.php
Expand Up @@ -10,6 +10,7 @@
use Tester;
use Tester\Dumper;
use Tester\Runner\Runner;
use Tester\Runner\Test;


/**
Expand Down Expand Up @@ -49,16 +50,16 @@ public function begin()
public function result($testName, $result, $message)
{
$outputs = [
Runner::PASSED => '.',
Runner::SKIPPED => 's',
Runner::FAILED => Dumper::color('white/red', 'F'),
Test::PASSED => '.',
Test::SKIPPED => 's',
Test::FAILED => Dumper::color('white/red', 'F'),
];
echo $outputs[$result];

$message = ' ' . str_replace("\n", "\n ", trim($message)) . "\n\n";
if ($result === Runner::FAILED) {
if ($result === Test::FAILED) {
$this->buffer .= Dumper::color('red', "-- FAILED: $testName") . "\n$message";
} elseif ($result === Runner::SKIPPED && $this->displaySkipped) {
} elseif ($result === Test::SKIPPED && $this->displaySkipped) {
$this->buffer .= "-- Skipped: $testName\n$message";
}
}
Expand All @@ -71,10 +72,10 @@ public function end()
$count = array_sum($results);
echo !$jobCount ? "No tests found\n" :
"\n\n" . $this->buffer . "\n"
. ($results[Runner::FAILED] ? Dumper::color('white/red') . 'FAILURES!' : Dumper::color('white/green') . 'OK')
. ($results[Test::FAILED] ? Dumper::color('white/red') . 'FAILURES!' : Dumper::color('white/green') . 'OK')
. " ($jobCount test" . ($jobCount > 1 ? 's' : '') . ", "
. ($results[Runner::FAILED] ? $results[Runner::FAILED] . ' failure' . ($results[Runner::FAILED] > 1 ? 's' : '') . ', ' : '')
. ($results[Runner::SKIPPED] ? $results[Runner::SKIPPED] . ' skipped, ' : '')
. ($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, ' : '')
. sprintf('%0.1f', $this->time + microtime(TRUE)) . ' seconds)' . Dumper::color() . "\n";

Expand Down
7 changes: 4 additions & 3 deletions src/Runner/Output/JUnitPrinter.php
Expand Up @@ -9,6 +9,7 @@

use Tester;
use Tester\Runner\Runner;
use Tester\Runner\Test;


/**
Expand Down Expand Up @@ -47,13 +48,13 @@ public function result($testName, $result, $message)
$this->buffer .= "\t\t<testcase classname=\"" . htmlspecialchars($testName) . '" name="' . htmlspecialchars($testName) . '"';

switch ($result) {
case Runner::FAILED:
case Test::FAILED:
$this->buffer .= ">\n\t\t\t<failure message=\"" . htmlspecialchars($message) . "\"/>\n\t\t</testcase>\n";
break;
case Runner::SKIPPED:
case Test::SKIPPED:
$this->buffer .= ">\n\t\t\t<skipped/>\n\t\t</testcase>\n";
break;
case Runner::PASSED:
case Test::PASSED:
$this->buffer .= "/>\n";
break;
}
Expand Down
13 changes: 7 additions & 6 deletions src/Runner/Output/Logger.php
Expand Up @@ -9,6 +9,7 @@

use Tester;
use Tester\Runner\Runner;
use Tester\Runner\Test;


/**
Expand Down Expand Up @@ -42,9 +43,9 @@ public function result($testName, $result, $message)
{
$message = ' ' . str_replace("\n", "\n ", Tester\Dumper::removeColors(trim($message)));
$outputs = [
Runner::PASSED => "-- OK: $testName",
Runner::SKIPPED => "-- Skipped: $testName\n$message",
Runner::FAILED => "-- FAILED: $testName\n$message",
Test::PASSED => "-- OK: $testName",
Test::SKIPPED => "-- Skipped: $testName\n$message",
Test::FAILED => "-- FAILED: $testName\n$message",
];
fwrite($this->file, $outputs[$result] . "\n\n");
}
Expand All @@ -56,10 +57,10 @@ public function end()
$results = $this->runner->getResults();
$count = array_sum($results);
fwrite($this->file,
($results[Runner::FAILED] ? 'FAILURES!' : 'OK')
($results[Test::FAILED] ? 'FAILURES!' : 'OK')
. " ($jobCount tests"
. ($results[Runner::FAILED] ? ", {$results[Runner::FAILED]} failures" : '')
. ($results[Runner::SKIPPED] ? ", {$results[Runner::SKIPPED]} skipped" : '')
. ($results[Test::FAILED] ? ", {$results[Test::FAILED]} failures" : '')
. ($results[Test::SKIPPED] ? ", {$results[Test::SKIPPED]} skipped" : '')
. ($jobCount !== $count ? ', ' . ($jobCount - $count) . ' not run' : '')
. ')'
);
Expand Down
7 changes: 4 additions & 3 deletions src/Runner/Output/TapPrinter.php
Expand Up @@ -9,6 +9,7 @@

use Tester;
use Tester\Runner\Runner;
use Tester\Runner\Test;


/**
Expand Down Expand Up @@ -40,9 +41,9 @@ public function result($testName, $result, $message)
{
$message = str_replace("\n", "\n# ", trim($message));
$outputs = [
Runner::PASSED => "ok $testName",
Runner::SKIPPED => "ok $testName #skip $message",
Runner::FAILED => "not ok $testName\n# $message",
Test::PASSED => "ok $testName",
Test::SKIPPED => "ok $testName #skip $message",
Test::FAILED => "not ok $testName\n# $message",
];
fwrite($this->file, $outputs[$result] . "\n");
}
Expand Down
17 changes: 6 additions & 11 deletions src/Runner/Runner.php
Expand Up @@ -15,11 +15,6 @@
*/
class Runner
{
const
PASSED = 1,
SKIPPED = 2,
FAILED = 3;

/** @var string[] paths to test files/directories */
public $paths = [];

Expand Down Expand Up @@ -93,7 +88,7 @@ public function run()
$handler->begin();
}

$this->results = [self::PASSED => 0, self::SKIPPED => 0, self::FAILED => 0];
$this->results = [Test::PASSED => 0, Test::SKIPPED => 0, Test::FAILED => 0];
$this->jobs = $running = [];
foreach ($this->paths as $path) {
$this->findTests($path);
Expand Down Expand Up @@ -132,7 +127,7 @@ public function run()
foreach ($this->outputHandlers as $handler) {
$handler->end();
}
return !$this->results[self::FAILED];
return !$this->results[Test::FAILED];
}


Expand Down Expand Up @@ -187,14 +182,14 @@ public function getJobCount()
* Writes to output handlers.
* @return void
*/
public function writeResult($testName, $result, $message = NULL)
public function writeResult(Test $test)
{
$this->results[$result]++;
$this->results[$test->getResult()]++;
foreach ($this->outputHandlers as $handler) {
$handler->result($testName, $result, $message);
$handler->result($test->getName(30), $test->getResult(), $test->message);
}

if ($this->stopOnFail && $result === self::FAILED) {
if ($this->stopOnFail && $test->getResult() === Test::FAILED) {
$this->interrupted = TRUE;
}
}
Expand Down

0 comments on commit 65ea0f8

Please sign in to comment.