4
4
5
5
class Runner
6
6
{
7
+ private $ basePath ;
7
8
private $ logger ;
8
9
private $ services = [];
9
10
10
- public function __construct ()
11
+ public function __construct ($ basePath )
11
12
{
13
+ $ this ->basePath = $ basePath ;
12
14
$ this ->logger = new DefaultLogger ();
13
15
$ this ->assembleServicesFromSamples ();
14
16
}
@@ -20,9 +22,7 @@ private function traverse($path)
20
22
21
23
private function assembleServicesFromSamples ()
22
24
{
23
- $ path = dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'samples ' ;
24
-
25
- foreach ($ this ->traverse ($ path ) as $ servicePath ) {
25
+ foreach ($ this ->traverse ($ this ->basePath ) as $ servicePath ) {
26
26
if ($ servicePath ->isDir ()) {
27
27
foreach ($ this ->traverse ($ servicePath ) as $ versionPath ) {
28
28
$ this ->services [$ servicePath ->getBasename ()][] = $ versionPath ->getBasename ();
@@ -33,85 +33,80 @@ private function assembleServicesFromSamples()
33
33
34
34
private function getOpts ()
35
35
{
36
- $ opts = getopt ('s:v:t: ' , [
37
- 'service: ' ,
38
- 'version: ' ,
39
- 'test:: ' ,
40
- 'debug:: ' ,
41
- 'help:: ' ,
42
- ]);
36
+ $ opts = getopt ('s:v:t: ' , ['service: ' , 'version: ' , 'test:: ' , 'debug:: ' , 'help:: ' ]);
37
+
38
+ $ getOpt = function (array $ keys , $ default ) use ($ opts ) {
39
+ foreach ($ keys as $ key ) {
40
+ if (isset ($ opts [$ key ])) {
41
+ return $ opts [$ key ];
42
+ }
43
+ }
44
+ return $ default ;
45
+ };
43
46
44
47
return [
45
- $ this -> getOpt ($ opts , ['s ' , 'service ' ], 'all ' ),
46
- $ this -> getOpt ($ opts , ['n ' , 'version ' ], 'all ' ),
47
- $ this -> getOpt ($ opts , ['t ' , 'test ' ], '' ),
48
+ $ getOpt (['s ' , 'service ' ], 'all ' ),
49
+ $ getOpt (['n ' , 'version ' ], 'all ' ),
50
+ $ getOpt (['t ' , 'test ' ], '' ),
48
51
isset ($ opts ['debug ' ]) ? (int ) $ opts ['debug ' ] : 0 ,
49
52
];
50
53
}
51
54
52
- private function getOpt (array $ opts , array $ keys , $ default )
53
- {
54
- foreach ($ keys as $ key ) {
55
- if (isset ($ opts [$ key ])) {
56
- return $ opts [$ key ];
57
- }
58
- }
59
-
60
- return $ default ;
61
- }
62
-
63
55
private function getRunnableServices ($ service , $ version )
64
56
{
65
57
$ services = $ this ->services ;
66
58
67
59
if ($ service != 'all ' ) {
68
- if (!isset ($ this ->services [strtolower ($ service )])) {
69
- $ this ->logger ->emergency ('{service} service does not exist ' , ['{service} ' => $ service ]);
70
- exit ;
71
- }
72
-
73
- if ($ version == 'all ' ) {
74
- $ versions = $ this ->services [strtolower ($ service )];
75
- } else {
76
- $ versions = [$ version ];
60
+ if (!isset ($ this ->services [$ service ])) {
61
+ throw new \InvalidArgumentException (sprintf ("%s service does not exist " , $ service ));
77
62
}
78
63
64
+ $ versions = ($ version == 'all ' ) ? $ this ->services [$ service ] : [$ version ];
79
65
$ services = [$ service => $ versions ];
80
66
}
81
67
82
68
return $ services ;
83
69
}
84
70
85
- private function toCamelCase ($ word , $ separator = '_ ' )
71
+ /**
72
+ * @return TestInterface
73
+ */
74
+ private function getTest ($ serviceName , $ version , $ verbosity )
86
75
{
87
- return str_replace ($ separator , '' , ucwords ($ word , $ separator ));
76
+ $ namespace = (new \ReflectionClass ($ this ))->getNamespaceName ();
77
+ $ className = sprintf ("%s \\%s \\%sTest " , $ namespace , Utils::toCamelCase ($ serviceName ), ucfirst ($ version ));
78
+
79
+ if (!class_exists ($ className )) {
80
+ throw new \RuntimeException (sprintf ("%s does not exist " , $ className ));
81
+ }
82
+
83
+ $ basePath = $ this ->basePath . DIRECTORY_SEPARATOR . $ serviceName . DIRECTORY_SEPARATOR . $ version ;
84
+ $ smClass = sprintf ("%s \\SampleManager " , $ namespace );
85
+ $ class = new $ className ($ this ->logger , new $ smClass ($ basePath , $ verbosity ));
86
+
87
+ if (!($ class instanceof TestInterface)) {
88
+ throw new \RuntimeException (sprintf ("%s does not implement TestInterface " , $ className ));
89
+ }
90
+
91
+ return $ class ;
88
92
}
89
93
90
94
public function runServices ()
91
95
{
92
- list ($ serviceOpt , $ versionOpt , $ testMethodOpt , $ verbosity ) = $ this ->getOpts ();
93
-
94
- $ services = $ this ->getRunnableServices ($ serviceOpt , $ versionOpt , $ testMethodOpt );
96
+ list ($ serviceOpt , $ versionOpt , $ testMethodOpt , $ verbosityOpt ) = $ this ->getOpts ();
95
97
96
- foreach ($ services as $ serviceName => $ versions ) {
98
+ foreach ($ this -> getRunnableServices ( $ serviceOpt , $ versionOpt ) as $ serviceName => $ versions ) {
97
99
foreach ($ versions as $ version ) {
98
- $ class = sprintf ("%s \\%s \\%sTest " , __NAMESPACE__ , $ this ->toCamelCase ($ serviceName ), ucfirst ($ version ));
99
- $ testRunner = new $ class ($ this ->logger , $ verbosity );
100
+ $ testRunner = $ this ->getTest ($ serviceName , $ version , $ verbosityOpt );
100
101
101
- if ($ testMethodOpt && method_exists ($ testRunner , $ testMethodOpt )) {
102
- $ testRunner ->startTimer ();
103
- $ testRunner ->$ testMethodOpt ();
102
+ if ($ testMethodOpt ) {
103
+ $ testRunner ->runOneTest ($ testMethodOpt );
104
104
} else {
105
105
$ testRunner ->runTests ();
106
106
}
107
107
108
- $ testRunner ->deletePaths ();
108
+ $ testRunner ->teardown ();
109
109
}
110
110
}
111
111
}
112
112
}
113
-
114
- require_once dirname (dirname (__DIR__ )) . '/vendor/autoload.php ' ;
115
-
116
- $ runner = new Runner ();
117
- $ runner ->runServices ();
0 commit comments