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,25 +34,39 @@ 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 ($ this ->runFromCli ()) {
38
+ return ;
39
+ }
40
+
41
+ foreach ($ this ::findMethods () as $ method ) {
51
42
$ this ->runTest ($ method );
43
+ }
44
+ }
52
45
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 );
57
68
}
69
+ return true ;
58
70
}
59
71
60
72
0 commit comments