Skip to content

Commit ab54955

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

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

src/Framework/TestCase.php

Lines changed: 32 additions & 20 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,25 +34,39 @@ 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 ($this->runFromCli()) {
38+
return;
39+
}
40+
41+
foreach ($this::findMethods() as $method) {
5142
$this->runTest($method);
43+
}
44+
}
5245

53-
} else {
54-
foreach ($methods as $method) {
55-
$this->runTest($method);
56-
}
46+
47+
public static function findMethods(): array
48+
{
49+
$methods = (new \ReflectionClass(static::class))->getMethods();
50+
return array_values(preg_grep(self::METHOD_PATTERN, array_map(function (\ReflectionMethod $rm): string {
51+
return $rm->getName();
52+
}, $methods)));
53+
}
54+
55+
56+
private function runFromCli(): bool
57+
{
58+
$args = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
59+
$arg = reset($args);
60+
if (!$arg) {
61+
return false;
62+
} elseif ($arg === self::LIST_METHODS) {
63+
Environment::$checkAssertions = false;
64+
header('Content-Type: text/plain');
65+
echo '[' . implode(',', $this::findMethods()) . ']';
66+
} elseif ($arg) {
67+
$this->runTest($arg);
5768
}
69+
return true;
5870
}
5971

6072

0 commit comments

Comments
 (0)