Skip to content

Commit 87b777a

Browse files
author
Jamie Hannaford
committed
Add better integration tests
1 parent 4a4aec0 commit 87b777a

File tree

15 files changed

+594
-331
lines changed

15 files changed

+594
-331
lines changed

tests/integration/Runner.php

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,120 @@
44

55
class 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
}

tests/integration/SampleManager.php

Lines changed: 0 additions & 104 deletions
This file was deleted.

tests/integration/TestCase.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OpenCloud\Integration;
44

5+
use OpenCloud\Common\Resource\Deletable;
56
use Psr\Log\LoggerInterface;
67

78
abstract class TestCase extends \PHPUnit_Framework_TestCase implements TestInterface
@@ -10,6 +11,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase implements TestInter
1011
private $startPoint;
1112
private $lastPoint;
1213
private $sampleManager;
14+
private $namePrefix = 'phptest_';
1315

1416
public function __construct(LoggerInterface $logger, SampleManagerInterface $sampleManager)
1517
{
@@ -75,7 +77,7 @@ protected function randomStr($length = 5)
7577
$randomString .= $chars[rand(0, $charsLen - 1)];
7678
}
7779

78-
return 'phptest_' . $randomString;
80+
return $this->namePrefix . $randomString;
7981
}
8082

8183
private function formatMinDifference($duration)
@@ -112,4 +114,21 @@ protected function sampleFile(array $replacements, $path)
112114
{
113115
return $this->sampleManager->write($path, $replacements);
114116
}
117+
118+
protected function getBaseClient()
119+
{
120+
return eval($this->sampleManager->getConnectionStr());
121+
}
122+
123+
protected function deleteItems(\Generator $items)
124+
{
125+
foreach ($items as $item) {
126+
if ($item instanceof Deletable
127+
&& property_exists($item, 'name')
128+
&& strpos($item->name, $this->namePrefix) === 0
129+
) {
130+
$item->delete();
131+
}
132+
}
133+
}
115134
}

0 commit comments

Comments
 (0)