Skip to content

Commit 158534d

Browse files
author
Jamie Hannaford
committed
refactor integration tests
1 parent 5a802dd commit 158534d

File tree

9 files changed

+216
-201
lines changed

9 files changed

+216
-201
lines changed

tests/integration/ObjectStore/V1Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class V1Test extends TestCase
1414
/**
1515
* @return \OpenStack\ObjectStore\v1\Service
1616
*/
17-
private function getService()
17+
protected function getService()
1818
{
1919
if (null === $this->service) {
2020
$this->service = (new OpenStack())->objectStoreV1(Utils::getAuthOpts());
@@ -57,7 +57,7 @@ public function accountMetadata()
5757
$this->logStep('Getting account metadata');
5858
/** @var array $metadata */
5959
require_once $this->sampleFile($replacements, 'account/get_metadata.php');
60-
$this->assertEquals([
60+
$this->assertArraySubset([
6161
'Foo' => $replacements['{val_1}'],
6262
'Bar' => $replacements['{val_2}'],
6363
], $metadata);

tests/integration/Runner.php

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
class Runner
66
{
7+
private $basePath;
78
private $logger;
89
private $services = [];
910

10-
public function __construct()
11+
public function __construct($basePath)
1112
{
13+
$this->basePath = $basePath;
1214
$this->logger = new DefaultLogger();
1315
$this->assembleServicesFromSamples();
1416
}
@@ -20,9 +22,7 @@ private function traverse($path)
2022

2123
private function assembleServicesFromSamples()
2224
{
23-
$path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'samples';
24-
25-
foreach ($this->traverse($path) as $servicePath) {
25+
foreach ($this->traverse($this->basePath) as $servicePath) {
2626
if ($servicePath->isDir()) {
2727
foreach ($this->traverse($servicePath) as $versionPath) {
2828
$this->services[$servicePath->getBasename()][] = $versionPath->getBasename();
@@ -33,85 +33,80 @@ private function assembleServicesFromSamples()
3333

3434
private function getOpts()
3535
{
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+
};
4346

4447
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'], ''),
4851
isset($opts['debug']) ? (int) $opts['debug'] : 0,
4952
];
5053
}
5154

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-
6355
private function getRunnableServices($service, $version)
6456
{
6557
$services = $this->services;
6658

6759
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));
7762
}
7863

64+
$versions = ($version == 'all') ? $this->services[$service] : [$version];
7965
$services = [$service => $versions];
8066
}
8167

8268
return $services;
8369
}
8470

85-
private function toCamelCase($word, $separator = '_')
71+
/**
72+
* @return TestInterface
73+
*/
74+
private function getTest($serviceName, $version, $verbosity)
8675
{
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;
8892
}
8993

9094
public function runServices()
9195
{
92-
list($serviceOpt, $versionOpt, $testMethodOpt, $verbosity) = $this->getOpts();
93-
94-
$services = $this->getRunnableServices($serviceOpt, $versionOpt, $testMethodOpt);
96+
list ($serviceOpt, $versionOpt, $testMethodOpt, $verbosityOpt) = $this->getOpts();
9597

96-
foreach ($services as $serviceName => $versions) {
98+
foreach ($this->getRunnableServices($serviceOpt, $versionOpt) as $serviceName => $versions) {
9799
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);
100101

101-
if ($testMethodOpt && method_exists($testRunner, $testMethodOpt)) {
102-
$testRunner->startTimer();
103-
$testRunner->$testMethodOpt();
102+
if ($testMethodOpt) {
103+
$testRunner->runOneTest($testMethodOpt);
104104
} else {
105105
$testRunner->runTests();
106106
}
107107

108-
$testRunner->deletePaths();
108+
$testRunner->teardown();
109109
}
110110
}
111111
}
112112
}
113-
114-
require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';
115-
116-
$runner = new Runner();
117-
$runner->runServices();

tests/integration/SampleManager.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace OpenStack\Integration;
4+
5+
class SampleManager implements SampleManagerInterface
6+
{
7+
protected $basePath;
8+
protected $paths = [];
9+
protected $verbosity;
10+
11+
public function __construct($basePath, $verbosity)
12+
{
13+
$this->basePath = $basePath;
14+
$this->verbosity = $verbosity;
15+
}
16+
17+
public function deletePaths()
18+
{
19+
if (!empty($this->paths)) {
20+
foreach ($this->paths as $path) {
21+
unlink($path);
22+
}
23+
}
24+
}
25+
26+
protected function getGlobalReplacements()
27+
{
28+
return [
29+
'{userId}' => getenv('OS_USER_ID'),
30+
'{username}' => getenv('OS_USERNAME'),
31+
'{password}' => getenv('OS_PASSWORD'),
32+
'{domainId}' => getenv('OS_DOMAIN_ID'),
33+
'{authUrl}' => getenv('OS_AUTH_URL'),
34+
'{tenantId}' => getenv('OS_TENANT_ID'),
35+
'{region}' => getenv('OS_REGION'),
36+
'{projectId}' => getenv('OS_PROJECT_ID'),
37+
'{projectName}' => getenv('OS_PROJECT_NAME'),
38+
];
39+
}
40+
41+
protected function getConnectionTemplate()
42+
{
43+
if ($this->verbosity === 1) {
44+
$subst = <<<'EOL'
45+
use OpenStack\Integration\DefaultLogger;
46+
use OpenStack\Integration\Utils;
47+
use GuzzleHttp\MessageFormatter;
48+
49+
$options = [
50+
'debugLog' => true,
51+
'logger' => new DefaultLogger(),
52+
'messageFormatter' => new MessageFormatter(),
53+
];
54+
$openstack = new OpenStack\OpenStack(Utils::getAuthOpts($options));
55+
EOL;
56+
} elseif ($this->verbosity === 2) {
57+
$subst = <<<'EOL'
58+
use OpenStack\Integration\DefaultLogger;
59+
use OpenStack\Integration\Utils;
60+
use GuzzleHttp\MessageFormatter;
61+
62+
$options = [
63+
'debugLog' => true,
64+
'logger' => new DefaultLogger(),
65+
'messageFormatter' => new MessageFormatter(MessageFormatter::DEBUG),
66+
];
67+
$openstack = new OpenStack\OpenStack(Utils::getAuthOpts($options));
68+
EOL;
69+
} else {
70+
$subst = <<<'EOL'
71+
use OpenStack\Integration\Utils;
72+
73+
$openstack = new OpenStack\OpenStack(Utils::getAuthOpts());
74+
EOL;
75+
}
76+
77+
return $subst;
78+
}
79+
80+
public function write($path, array $replacements)
81+
{
82+
$replacements = array_merge($this->getGlobalReplacements(), $replacements);
83+
84+
$sampleFile = rtrim($this->basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path;
85+
86+
if (!file_exists($sampleFile) || !is_readable($sampleFile)) {
87+
throw new \RuntimeException(sprintf("%s either does not exist or is not readable", $sampleFile));
88+
}
89+
90+
$content = strtr(file_get_contents($sampleFile), $replacements);
91+
$content = str_replace("'vendor/'", "'" . dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . "vendor'", $content);
92+
93+
$subst = $this->getConnectionTemplate();
94+
$content = preg_replace('/\([^)]+\)/', '', $content, 1);
95+
$content = str_replace('$openstack = new OpenStack\OpenStack;', $subst, $content);
96+
97+
$tmp = tempnam(sys_get_temp_dir(), 'openstack');
98+
file_put_contents($tmp, $content);
99+
100+
$this->paths[] = $tmp;
101+
102+
return $tmp;
103+
}
104+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace OpenStack\Integration;
4+
5+
interface SampleManagerInterface
6+
{
7+
public function write($path, array $replacements);
8+
9+
public function deletePaths();
10+
}

0 commit comments

Comments
 (0)