44
55class Runner
66{
7- private $ basePath ;
7+ private $ testsDir ;
8+ private $ samplesDir ;
89 private $ logger ;
9- private $ services = [] ;
10+ private $ tests ;
1011 private $ namespace ;
1112
12- public function __construct ($ basePath , $ testNamespace )
13+ public function __construct ($ samplesDir , $ testsDir , $ testNamespace )
1314 {
14- $ this ->basePath = $ basePath ;
15+ $ this ->samplesDir = $ samplesDir ;
16+ $ this ->testsDir = $ testsDir ;
1517 $ this ->namespace = $ testNamespace ;
1618
1719 $ this ->logger = new DefaultLogger ();
18- $ this ->assembleServicesFromSamples ();
20+ $ this ->assembleTestFiles ();
1921 }
2022
21- private function traverse ($ path )
23+ private function traverse (string $ path ): \ DirectoryIterator
2224 {
23- return new \RecursiveDirectoryIterator ($ path, \FilesystemIterator:: SKIP_DOTS );
25+ return new \DirectoryIterator ($ path );
2426 }
2527
26- private function assembleServicesFromSamples ()
28+ private function assembleTestFiles ()
2729 {
28- foreach ($ this ->traverse ($ this ->basePath ) as $ servicePath ) {
30+ foreach ($ this ->traverse ($ this ->testsDir ) as $ servicePath ) {
2931 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+ }
3243 }
3344 }
3445 }
3546 }
3647
3748 private function getOpts ()
3849 {
39- $ opts = getopt ('s:v:t: ' , ['service: ' , 'version: ' , 'test:: ' , 'debug:: ' , 'help:: ' ]);
50+ $ opts = getopt ('s:v:m: t: ' , ['service: ' , 'version: ' , ' module: : ' , 'test:: ' , 'debug:: ' , 'help:: ' ]);
4051
4152 $ getOpt = function (array $ keys , $ default ) use ($ opts ) {
53+ $ value = $ default ;
4254 foreach ($ keys as $ key ) {
4355 if (isset ($ opts [$ key ])) {
44- return $ opts [$ key ];
56+ $ value = $ opts [$ key ];
57+ break ;
4558 }
4659 }
47- return $ default ;
60+ return strtolower ( $ value ) ;
4861 };
4962
5063 return [
5164 $ getOpt (['s ' , 'service ' ], 'all ' ),
52- $ getOpt (['n ' , 'version ' ], 'all ' ),
65+ $ getOpt (['v ' , 'version ' ], 'all ' ),
66+ $ getOpt (['m ' , 'module ' ], 'core ' ),
5367 $ getOpt (['t ' , 'test ' ], '' ),
54- isset ($ opts ['debug ' ]) ? (int ) $ opts ['debug ' ] : 0 ,
68+ isset ($ opts ['debug ' ]) ? (int )$ opts ['debug ' ] : 0 ,
5569 ];
5670 }
5771
58- private function getRunnableServices ($ service , $ version )
72+ private function getRunnableServices ($ service , $ version, $ module )
5973 {
60- $ services = $ this ->services ;
74+ $ tests = $ this ->tests ;
6175
6276 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 );
6580 }
6681
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+ }
69102 }
70103
71- return $ services ;
104+ return $ tests ;
72105 }
73106
74107 /**
75108 * @return TestInterface
76109 */
77- private function getTest ($ serviceName , $ version , $ verbosity )
110+ private function getTest ($ service , $ version, $ test , $ verbosity )
78111 {
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 ));
80113
81114 if (!class_exists ($ className )) {
82115 throw new \RuntimeException (sprintf ("%s does not exist " , $ className ));
83116 }
84117
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 );
88121
89122 if (!($ class instanceof TestInterface)) {
90123 throw new \RuntimeException (sprintf ("%s does not implement TestInterface " , $ className ));
@@ -95,19 +128,35 @@ private function getTest($serviceName, $version, $verbosity)
95128
96129 public function runServices ()
97130 {
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+ }
108159 }
109-
110- $ testRunner ->teardown ();
111160 }
112161 }
113162 }
0 commit comments