Skip to content

Commit 24cfefa

Browse files
committed
@testcase is not needed WIP
1 parent a0aa35d commit 24cfefa

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

demo/runner.phpt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php
22

3-
/**
4-
* @testCase
5-
*/
6-
73
declare(strict_types=1);
84

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

demo2/runner.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require __DIR__ . '/../src/bootstrap.php';
6+
7+
8+
class MyTest extends Tester\TestCase
9+
{
10+
public function testMe1()
11+
{
12+
Tester\Assert::true(true);
13+
echo __FUNCTION__ . ',';
14+
}
15+
16+
17+
public function testMe2()
18+
{
19+
echo __FUNCTION__ . ',';
20+
}
21+
}
22+
23+
24+
(new Tester\TestCaseRunner)
25+
->run(new MyTest);
26+
27+
// this could be the recommended way to run single test, that will replace @testcase

src/Framework/TestCaseRunner.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
class TestCaseRunner
1717
{
18-
private const LIST_METHODS = 'nette-tester-list-methods';
19-
2018
/** @var array */
2119
private $classes = [];
2220

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

4038

41-
public function run(): void
39+
public function run(TestCase $test = null): void
4240
{
43-
if ($this->runFromCli()) {
41+
if ($this->runFromCli($test)) {
4442
return;
45-
}
4643

47-
foreach ($this->classes as $class) {
48-
$test = $this->createInstance($class);
44+
} elseif ($test) {
4945
$test->run();
46+
47+
} else {
48+
foreach ($this->classes as $class) {
49+
$test = $this->createInstance($class);
50+
$test->run();
51+
}
5052
}
5153
}
5254

5355

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

61-
} elseif ($arg === self::LIST_METHODS) {
61+
if ($arg) {
62+
[$class, $method] = explode('::', $arg);
63+
$test = $test ?: $this->createInstance($class);
64+
$test->runTest($method);
65+
return true;
66+
67+
} elseif (getenv(Environment::RUNNER)) {
6268
Environment::$checkAssertions = false;
6369
$methods = [];
64-
foreach ($this->classes as $class) {
70+
$classes = $test ? [get_class($test)] : $this->classes;
71+
foreach ($classes as $class) {
6572
foreach ($class::findMethods() as $method) {
6673
$methods[] = $class . '::' . $method;
6774
}
6875
}
6976
header('Content-Type: text/plain');
7077
echo '[' . implode(',', $methods) . ']';
78+
exit(Runner\Job::CODE_TESTCASE);
7179

7280
} else {
73-
[$class, $method] = explode('::', $arg);
74-
$test = $this->createInstance($class);
75-
$test->runTest($method);
81+
return false;
7682
}
77-
return true;
7883
}
7984

8085

src/Runner/Job.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Job
2222
CODE_OK = 0,
2323
CODE_SKIP = 177,
2424
CODE_FAIL = 178,
25+
CODE_TESTCASE = 179,
2526
CODE_ERROR = 255;
2627

2728
/** waiting time between process activity check in microseconds */

src/Runner/TestHandler.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,29 @@ private function initiateTestCase(Test $test, $foo, PhpInterpreter $interpreter)
178178

179179
private function assessExitCode(Job $job, $code): ?Test
180180
{
181+
$test = $job->getTest();
181182
$code = (int) $code;
182183
if ($job->getExitCode() === Job::CODE_SKIP) {
183-
$message = preg_match('#.*Skipped:\n(.*?)\z#s', $output = $job->getTest()->stdout, $m)
184+
$message = preg_match('#.*Skipped:\n(.*?)\z#s', $output = $test->stdout, $m)
184185
? $m[1]
185186
: $output;
186-
return $job->getTest()->withResult(Test::SKIPPED, trim($message));
187+
return $test->withResult(Test::SKIPPED, trim($message));
188+
189+
} elseif ($job->getExitCode() === Job::CODE_TESTCASE) {
190+
if (!preg_match('#\[([^[]*)]#', (string) strrchr($test->stdout, '['), $m)) {
191+
return $test->withResult(Test::FAILED, "Cannot list TestCase methods in file '{$test->getFile()}'. Do you call TestCase::run() in it?");
192+
} elseif (!strlen($m[1])) {
193+
return $test->withResult(Test::SKIPPED, "TestCase in file '{$test->getFile()}' does not contain test methods.");
194+
}
195+
foreach (explode(',', $m[1]) as $method) {
196+
$testVariety = $test->withArguments(['method' => $method]);
197+
$this->runner->prepareTest($testVariety);
198+
$this->runner->addJob(new Job($testVariety, $this->runner->getInterpreter(), $this->runner->getEnvironmentVariables()));
199+
}
187200

188201
} elseif ($job->getExitCode() !== $code) {
189202
$message = $job->getExitCode() !== Job::CODE_FAIL ? "Exited with error code {$job->getExitCode()} (expected $code)" : '';
190-
return $job->getTest()->withResult(Test::FAILED, trim($message . "\n" . $job->getTest()->stdout));
203+
return $test->withResult(Test::FAILED, trim($message . "\n" . $test->stdout));
191204
}
192205
return null;
193206
}

0 commit comments

Comments
 (0)