-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathTestCase.php
More file actions
123 lines (107 loc) · 3.9 KB
/
TestCase.php
File metadata and controls
123 lines (107 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace OpenStack\Sample;
use InvalidArgumentException;
use OpenStack\Common\Service\AbstractService;
use OpenStack\OpenStack;
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
private $namePrefix = 'phptest_';
/** @var \OpenStack\Sample\SampleManager */
protected $sampleManager;
/** @var array<string, \OpenStack\Common\Service\AbstractService> */
protected $cachedServices = [];
public function __construct()
{
parent::__construct();
$this->sampleManager = new SampleManager(
__DIR__ . '/../../samples',
intval(getenv('SAMPLE_VERBOSITY'))
);
}
protected function getOpenStack(array $options = []): OpenStack
{
return new OpenStack($this->getAuthOpts($options));
}
/**
* @param class-string<T> $serviceType
* @return T
* @template T of \OpenStack\Common\Service\AbstractService
*
* @noinspection PhpFullyQualifiedNameUsageInspection
*/
protected function getCachedService(string $serviceType): AbstractService
{
if (isset($this->cachedServices[$serviceType])) {
return $this->cachedServices[$serviceType];
}
switch ($serviceType) {
case \OpenStack\BlockStorage\v2\Service::class:
$service = $this->getOpenStack()->blockStorageV2();
break;
case \OpenStack\BlockStorage\v3\Service::class:
$service = $this->getOpenStack()->blockStorageV3(['catalogName' => 'cinder', 'catalogType' => 'block-storage']);
break;
case \OpenStack\Compute\v2\Service::class:
$service = $this->getOpenStack()->computeV2();
break;
case \OpenStack\Identity\v2\Service::class:
$service = $this->getOpenStack()->identityV2();
break;
case \OpenStack\Identity\v3\Service::class:
$service = $this->getOpenStack()->identityV3();
break;
case \OpenStack\Images\v2\Service::class:
$service = $this->getOpenStack()->imagesV2();
break;
case \OpenStack\Networking\v2\Service::class:
$service = $this->getOpenStack()->networkingV2();
break;
case \OpenStack\ObjectStore\v1\Service::class:
$service = $this->getOpenStack()->objectStoreV1();
break;
default:
throw new InvalidArgumentException("Unknown service type: $serviceType");
}
$this->cachedServices[$serviceType] = $service;
return $this->cachedServices[$serviceType];
}
protected function getAuthOpts(array $options = []): array
{
return array_merge(
[
'authUrl' => getenv('OS_AUTH_URL'),
'region' => getenv('OS_REGION_NAME'),
'user' => [
'id' => getenv('OS_USER_ID'),
'password' => getenv('OS_PASSWORD'),
],
'scope' => [
'project' => [
'id' => getenv('OS_PROJECT_ID'),
],
],
],
$options
);
}
/**
* Creates random string
*/
protected function randomStr($length = 5): string
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charsLen = strlen($chars);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $chars[rand(0, $charsLen - 1)];
}
return $this->namePrefix . $randomString;
}
/**
* Creates a sample file from a template. It must be included to be executed.
*/
protected function sampleFile(string $path, array $replacements = []): string
{
return $this->sampleManager->write($path, $replacements);
}
}