Skip to content

Commit

Permalink
@testcase is not needed WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 8, 2019
1 parent 660ca7e commit eacdb43
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
4 changes: 0 additions & 4 deletions demo/runner.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/**
* @testCase
*/

declare(strict_types=1);

require __DIR__ . '/../src/bootstrap.php';
Expand Down
27 changes: 27 additions & 0 deletions demo2/runner.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../src/bootstrap.php';


class MyTest extends Tester\TestCase
{
public function testMe1()
{
Tester\Assert::true(true);
echo __FUNCTION__ . ',';
}


public function testMe2()
{
echo __FUNCTION__ . ',';
}
}


(new Tester\TestCaseRunner)
->run(new MyTest);

// this could be the recommended way to run single test, that will replace @testcase
37 changes: 21 additions & 16 deletions src/Framework/TestCaseRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
class TestCaseRunner
{
private const LIST_METHODS = 'nette-tester-list-methods';

/** @var array */
private $classes = [];

Expand All @@ -38,43 +36,50 @@ public function findTests(string $fileMask): self
}


public function run(): void
public function run(TestCase $test = null): void
{
if ($this->runFromCli()) {
if ($this->runFromCli($test)) {
return;
}

foreach ($this->classes as $class) {
$test = $this->createInstance($class);
} elseif ($test) {
$test->run();

} else {
foreach ($this->classes as $class) {
$test = $this->createInstance($class);
$test->run();
}
}
}


private function runFromCli(): bool
private function runFromCli(TestCase $test = null): bool
{
$args = preg_filter('#--method=([\w:-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
$arg = reset($args);
if (!$arg) {
return false;

} elseif ($arg === self::LIST_METHODS) {
if ($arg) {
[$class, $method] = explode('::', $arg);
$test = $test ?: $this->createInstance($class);
$test->runTest($method);
return true;

} elseif (getenv(Environment::RUNNER)) {
Environment::$checkAssertions = false;
$methods = [];
foreach ($this->classes as $class) {
$classes = $test ? [get_class($test)] : $this->classes;
foreach ($classes as $class) {
foreach ($class::findMethods() as $method) {
$methods[] = $class . '::' . $method;
}
}
header('Content-Type: text/plain');
echo '[' . implode(',', $methods) . ']';
exit(Runner\Job::CODE_TESTCASE);

} else {
[$class, $method] = explode('::', $arg);
$test = $this->createInstance($class);
$test->runTest($method);
return false;
}
return true;
}


Expand Down
1 change: 1 addition & 0 deletions src/Runner/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Job
CODE_OK = 0,
CODE_SKIP = 177,
CODE_FAIL = 178,
CODE_TESTCASE = 179,
CODE_ERROR = 255;

/** waiting time between process activity check in microseconds */
Expand Down
20 changes: 17 additions & 3 deletions src/Runner/TestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,30 @@ private function initiateTestCase(Test $test, $foo, PhpInterpreter $interpreter)

private function assessExitCode(Job $job, $code): ?Test
{
$test = $job->getTest();
$code = (int) $code;
if ($job->getExitCode() === Job::CODE_SKIP) {
$message = preg_match('#.*Skipped:\n(.*?)\z#s', $output = $job->getTest()->stdout, $m)
$message = preg_match('#.*Skipped:\n(.*?)\z#s', $output = $test->stdout, $m)
? $m[1]
: $output;
return $job->getTest()->withResult(Test::SKIPPED, trim($message));
return $test->withResult(Test::SKIPPED, trim($message));

} elseif ($job->getExitCode() === Job::CODE_TESTCASE) {
$methods = TestCase::parseOutput($test->stdout);
if ($methods === null) {
return $test->withResult(Test::FAILED, "Cannot read TestCaseRunner output in file '{$test->getFile()}'.");
} elseif (!$methods) {
return $test->withResult(Test::SKIPPED, "TestCaseRunner in file '{$test->getFile()}' does not contain any test.");
}
foreach ($methods as $method) {
$testVariety = $test->withArguments(['method' => $method]);
$this->runner->prepareTest($testVariety);
$this->runner->addJob(new Job($testVariety, $this->runner->getInterpreter(), $this->runner->getEnvironmentVariables()));
}

} elseif ($job->getExitCode() !== $code) {
$message = $job->getExitCode() !== Job::CODE_FAIL ? "Exited with error code {$job->getExitCode()} (expected $code)" : '';
return $job->getTest()->withResult(Test::FAILED, trim($message . "\n" . $job->getTest()->stdout));
return $test->withResult(Test::FAILED, trim($message . "\n" . $test->stdout));
}
return null;
}
Expand Down

0 comments on commit eacdb43

Please sign in to comment.