4
4
5
5
class Runner
6
6
{
7
- private $ basePath ;
7
+ private $ testsDir ;
8
+ private $ samplesDir ;
8
9
private $ logger ;
9
- private $ services = [] ;
10
+ private $ tests ;
10
11
private $ namespace ;
11
12
12
- public function __construct ($ basePath , $ testNamespace )
13
+ public function __construct ($ samplesDir , $ testsDir , $ testNamespace )
13
14
{
14
- $ this ->basePath = $ basePath ;
15
+ $ this ->samplesDir = $ samplesDir ;
16
+ $ this ->testsDir = $ testsDir ;
15
17
$ this ->namespace = $ testNamespace ;
16
18
17
19
$ this ->logger = new DefaultLogger ();
18
- $ this ->assembleServicesFromSamples ();
20
+ $ this ->assembleTestFiles ();
19
21
}
20
22
21
- private function traverse ($ path )
23
+ private function traverse (string $ path ): \ DirectoryIterator
22
24
{
23
- return new \RecursiveDirectoryIterator ($ path, \FilesystemIterator:: SKIP_DOTS );
25
+ return new \DirectoryIterator ($ path );
24
26
}
25
27
26
- private function assembleServicesFromSamples ()
28
+ private function assembleTestFiles ()
27
29
{
28
- foreach ($ this ->traverse ($ this ->basePath ) as $ servicePath ) {
30
+ foreach ($ this ->traverse ($ this ->testsDir ) as $ servicePath ) {
29
31
if ($ servicePath ->isDir ()) {
30
- foreach ($ this ->traverse ($ servicePath ) as $ versionPath ) {
31
- $ this ->services [$ servicePath ->getBasename ()][] = $ versionPath ->getBasename ();
32
+ $ serviceBn = $ servicePath ->getBasename ();
33
+ foreach ($ this ->traverse ($ servicePath ->getPathname ()) as $ versionPath ) {
34
+ $ versionBn = $ versionPath ->getBasename ();
35
+ if ($ servicePath ->isDir () && $ versionBn [0 ] == 'v ' ) {
36
+ foreach ($ this ->traverse ($ versionPath ->getPathname ()) as $ testPath ) {
37
+ if (strpos ($ testPath ->getFilename (), 'Test.php ' )) {
38
+ $ testBn = strtolower (substr ($ testPath ->getBasename (), 0 , -8 ));
39
+ $ this ->tests [strtolower ($ serviceBn )][strtolower ($ versionBn )][] = $ testBn ;
40
+ }
41
+ }
42
+ }
32
43
}
33
44
}
34
45
}
35
46
}
36
47
37
48
private function getOpts ()
38
49
{
39
- $ opts = getopt ('s:v:t: ' , ['service: ' , 'version: ' , 'test:: ' , 'debug:: ' , 'help:: ' ]);
50
+ $ opts = getopt ('s:v:m: t: ' , ['service: ' , 'version: ' , ' module: : ' , 'test:: ' , 'debug:: ' , 'help:: ' ]);
40
51
41
52
$ getOpt = function (array $ keys , $ default ) use ($ opts ) {
53
+ $ value = $ default ;
42
54
foreach ($ keys as $ key ) {
43
55
if (isset ($ opts [$ key ])) {
44
- return $ opts [$ key ];
56
+ $ value = $ opts [$ key ];
57
+ break ;
45
58
}
46
59
}
47
- return $ default ;
60
+ return strtolower ( $ value ) ;
48
61
};
49
62
50
63
return [
51
64
$ getOpt (['s ' , 'service ' ], 'all ' ),
52
- $ getOpt (['n ' , 'version ' ], 'all ' ),
65
+ $ getOpt (['v ' , 'version ' ], 'all ' ),
66
+ $ getOpt (['m ' , 'module ' ], 'core ' ),
53
67
$ getOpt (['t ' , 'test ' ], '' ),
54
- isset ($ opts ['debug ' ]) ? (int ) $ opts ['debug ' ] : 0 ,
68
+ isset ($ opts ['debug ' ]) ? (int )$ opts ['debug ' ] : 0 ,
55
69
];
56
70
}
57
71
58
- private function getRunnableServices ($ service , $ version )
72
+ private function getRunnableServices ($ service , $ version, $ module )
59
73
{
60
- $ services = $ this ->services ;
74
+ $ tests = $ this ->tests ;
61
75
62
76
if ($ service != 'all ' ) {
63
- if (!isset ($ this ->services [$ service ])) {
64
- throw new \InvalidArgumentException (sprintf ("%s service does not exist " , $ service ));
77
+ if (!isset ($ tests [$ service ])) {
78
+ $ this ->logger ->critical (sprintf ("%s is not a valid service " , $ service ));
79
+ exit (1 );
65
80
}
66
81
67
- $ versions = ($ version == 'all ' ) ? $ this ->services [$ service ] : [$ version ];
68
- $ services = [$ service => $ versions ];
82
+ $ serviceArray = $ tests [$ service ];
83
+ $ tests = [$ service => $ serviceArray ];
84
+
85
+ if ($ version != 'all ' ) {
86
+ if (!isset ($ serviceArray [$ version ])) {
87
+ $ this ->logger ->critical (sprintf ("%s is not a valid version for the %s service " , $ version , $ service ));
88
+ exit (1 );
89
+ }
90
+
91
+ $ versionArray = $ serviceArray [$ version ];
92
+ if ($ module != 'core ' ) {
93
+ if (!in_array ($ module , $ serviceArray [$ version ])) {
94
+ $ this ->logger ->critical (sprintf ("%s is not a valid test class for the %s %s service " , $ module , $ version , $ service ));
95
+ exit (1 );
96
+ }
97
+ $ versionArray = [$ module ];
98
+ }
99
+
100
+ $ tests = [$ service => [$ version => $ versionArray ]];
101
+ }
69
102
}
70
103
71
- return $ services ;
104
+ return $ tests ;
72
105
}
73
106
74
107
/**
75
108
* @return TestInterface
76
109
*/
77
- private function getTest ($ serviceName , $ version , $ verbosity )
110
+ private function getTest ($ service , $ version, $ test , $ verbosity )
78
111
{
79
- $ className = sprintf ("%s \\%s \\%sTest " , $ this ->namespace , Utils::toCamelCase ($ serviceName ), ucfirst ($ version ));
112
+ $ className = sprintf ("%s \\%s \\%s \\ % sTest " , $ this ->namespace , Utils::toCamelCase ($ service ), $ version , ucfirst ($ test ));
80
113
81
114
if (!class_exists ($ className )) {
82
115
throw new \RuntimeException (sprintf ("%s does not exist " , $ className ));
83
116
}
84
117
85
- $ basePath = $ this ->basePath . DIRECTORY_SEPARATOR . $ serviceName . DIRECTORY_SEPARATOR . $ version ;
86
- $ smClass = sprintf ("%s \\SampleManager " , $ this ->namespace );
87
- $ class = new $ className ($ this ->logger , new $ smClass ($ basePath , $ verbosity ));
118
+ $ basePath = $ this ->samplesDir . DIRECTORY_SEPARATOR . $ service . DIRECTORY_SEPARATOR . $ version ;
119
+ $ smClass = sprintf ("%s \\SampleManager " , $ this ->namespace );
120
+ $ class = new $ className ($ this ->logger , new $ smClass ($ basePath , $ verbosity ), $ verbosity );
88
121
89
122
if (!($ class instanceof TestInterface)) {
90
123
throw new \RuntimeException (sprintf ("%s does not implement TestInterface " , $ className ));
@@ -95,19 +128,35 @@ private function getTest($serviceName, $version, $verbosity)
95
128
96
129
public function runServices ()
97
130
{
98
- list ($ serviceOpt , $ versionOpt , $ testMethodOpt , $ verbosityOpt ) = $ this ->getOpts ();
99
-
100
- foreach ($ this ->getRunnableServices ($ serviceOpt , $ versionOpt ) as $ serviceName => $ versions ) {
101
- foreach ($ versions as $ version ) {
102
- $ testRunner = $ this ->getTest ($ serviceName , $ version , $ verbosityOpt );
103
-
104
- if ($ testMethodOpt ) {
105
- $ testRunner ->runOneTest ($ testMethodOpt );
106
- } else {
107
- $ testRunner ->runTests ();
131
+ list ($ serviceOpt , $ versionOpt , $ moduleOpt , $ testMethodOpt , $ verbosityOpt ) = $ this ->getOpts ();
132
+
133
+ foreach ($ this ->getRunnableServices ($ serviceOpt , $ versionOpt , $ moduleOpt ) as $ serviceName => $ serviceArray ) {
134
+ foreach ($ serviceArray as $ versionName => $ versionArray ) {
135
+ foreach ($ versionArray as $ testName ) {
136
+
137
+ $ this ->logger ->info (str_repeat ('= ' , 49 ));
138
+ $ this ->logger ->info ("Starting %s %v %m integration test(s) " , [
139
+ '%s ' => $ serviceName ,
140
+ '%v ' => $ versionName ,
141
+ '%m ' => $ moduleOpt ,
142
+ ]);
143
+ $ this ->logger ->info (str_repeat ('= ' , 49 ));
144
+
145
+ $ testRunner = $ this ->getTest ($ serviceName , $ versionName , $ testName , $ verbosityOpt );
146
+
147
+ try {
148
+ if ($ testMethodOpt ) {
149
+ $ testRunner ->runOneTest ($ testMethodOpt );
150
+ } else {
151
+ $ testRunner ->runTests ();
152
+ }
153
+ } finally {
154
+ $ this ->logger ->info (str_repeat ('= ' , 11 ));
155
+ $ this ->logger ->info ('Cleaning up ' );
156
+ $ this ->logger ->info (str_repeat ('= ' , 11 ));
157
+ $ testRunner ->teardown ();
158
+ }
108
159
}
109
-
110
- $ testRunner ->teardown ();
111
160
}
112
161
}
113
162
}
0 commit comments