15
15
*/
16
16
class TestCase
17
17
{
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_]# ' ;
22
20
23
21
/** @var bool */
24
22
private $ handleErrors = false ;
@@ -36,24 +34,43 @@ public function run(): void
36
34
throw new \LogicException ('Calling TestCase::run($method) is deprecated. Use TestCase::runTest($method) instead. ' );
37
35
}
38
36
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 ) {
51
43
$ 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 ()) . '] ' ;
52
71
53
72
} else {
54
- foreach ($ methods as $ method ) {
55
- $ this ->runTest ($ method );
56
- }
73
+ $ this ->runTest ($ arg );
57
74
}
58
75
}
59
76
0 commit comments