From eacdb436fc5155cb222fc2a7dc5da288fb29e108 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 7 Feb 2019 20:55:13 +0100 Subject: [PATCH] @testcase is not needed WIP --- demo/runner.phpt | 4 ---- demo2/runner.phpt | 27 +++++++++++++++++++++++ src/Framework/TestCaseRunner.php | 37 ++++++++++++++++++-------------- src/Runner/Job.php | 1 + src/Runner/TestHandler.php | 20 ++++++++++++++--- 5 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 demo2/runner.phpt diff --git a/demo/runner.phpt b/demo/runner.phpt index 71252fb4..38767ec1 100644 --- a/demo/runner.phpt +++ b/demo/runner.phpt @@ -1,9 +1,5 @@ run(new MyTest); + +// this could be the recommended way to run single test, that will replace @testcase diff --git a/src/Framework/TestCaseRunner.php b/src/Framework/TestCaseRunner.php index d223b293..48b1c116 100644 --- a/src/Framework/TestCaseRunner.php +++ b/src/Framework/TestCaseRunner.php @@ -15,8 +15,6 @@ */ class TestCaseRunner { - private const LIST_METHODS = 'nette-tester-list-methods'; - /** @var array */ private $classes = []; @@ -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; } diff --git a/src/Runner/Job.php b/src/Runner/Job.php index 1ab8a218..43504413 100644 --- a/src/Runner/Job.php +++ b/src/Runner/Job.php @@ -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 */ diff --git a/src/Runner/TestHandler.php b/src/Runner/TestHandler.php index 8b0c2583..d9b9884e 100644 --- a/src/Runner/TestHandler.php +++ b/src/Runner/TestHandler.php @@ -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; }