Skip to content

Commit 0708838

Browse files
committed
TestCase: refactoring
1 parent 911c0e6 commit 0708838

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

src/Framework/TestCase.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
*/
1616
class TestCase
1717
{
18-
/** @internal */
19-
public const
20-
LIST_METHODS = 'nette-tester-list-methods',
21-
METHOD_PATTERN = '#^test[A-Z0-9_]#';
18+
public const LIST_METHODS = 'nette-tester-list-methods';
19+
private const METHOD_PATTERN = '#^test[A-Z0-9_]#';
2220

2321
/** @var bool */
2422
private $handleErrors = false;
@@ -36,24 +34,43 @@ public function run(): void
3634
throw new \LogicException('Calling TestCase::run($method) is deprecated. Use TestCase::runTest($method) instead.');
3735
}
3836

39-
$methods = array_values(preg_grep(self::METHOD_PATTERN, array_map(function (\ReflectionMethod $rm): string {
40-
return $rm->getName();
41-
}, (new \ReflectionObject($this))->getMethods())));
42-
43-
if (isset($_SERVER['argv']) && ($tmp = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv']))) {
44-
$method = reset($tmp);
45-
if ($method === self::LIST_METHODS) {
46-
Environment::$checkAssertions = false;
47-
header('Content-Type: text/plain');
48-
echo '[' . implode(',', $methods) . ']';
49-
return;
50-
}
37+
if (self::getCliArgument()) {
38+
$this->runFromCli();
39+
return;
40+
}
41+
42+
foreach ($this::findMethods() as $method) {
5143
$this->runTest($method);
44+
}
45+
}
46+
47+
48+
public static function findMethods(): array
49+
{
50+
$methods = (new \ReflectionClass(static::class))->getMethods();
51+
return array_values(preg_grep(self::METHOD_PATTERN, array_map(function (\ReflectionMethod $rm): string {
52+
return $rm->getName();
53+
}, $methods)));
54+
}
55+
56+
57+
public static function getCliArgument(): ?string
58+
{
59+
$args = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
60+
return reset($args) ?: null;
61+
}
62+
63+
64+
private function runFromCli(): void
65+
{
66+
$arg = self::getCliArgument();
67+
if ($arg === self::LIST_METHODS) {
68+
Environment::$checkAssertions = false;
69+
header('Content-Type: text/plain');
70+
echo '[' . implode(',', $this::findMethods()) . ']';
5271

5372
} else {
54-
foreach ($methods as $method) {
55-
$this->runTest($method);
56-
}
73+
$this->runTest($arg);
5774
}
5875
}
5976

0 commit comments

Comments
 (0)