-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathSystemTestCase.php
More file actions
289 lines (266 loc) · 8.68 KB
/
SystemTestCase.php
File metadata and controls
289 lines (266 loc) · 8.68 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Core\Testing\System;
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\BigQuery\Dataset;
use Google\Cloud\Core\Exception\BadRequestException;
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Core\ExponentialBackoff;
use Google\Cloud\PubSub\PubSubClient;
use Google\Cloud\PubSub\Topic;
use Google\Cloud\Storage\Bucket;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\Testing\System\DeletionQueue;
use PHPUnit\Framework\TestCase;
/**
* SystemTestCase can be extended to implement system tests
*
* @experimental
* @internal
*/
abstract class SystemTestCase extends TestCase
{
protected static $deletionQueue;
private static $emulatedClasses = [];
private static $emulatedClassPrefixes = [];
/**
* Set up the deletion queue
*
* @experimental
* @internal
*/
public static function setupQueue()
{
if (!self::$deletionQueue) {
self::$deletionQueue = new DeletionQueue;
}
}
/**
* Process the deletion queue
*
* @experimental
* @internal
*/
public static function processQueue()
{
self::$deletionQueue->process();
}
/**
* Create a random integer ID for test entities.
*
* @return int
*
* @experimental
* @internal
*/
public static function randId()
{
return rand(1, 9999999);
}
/**
* Create a bucket and enqueue it for deletion.
*
* This method provides a means of creating a bucket with pre-configured
* flush+delete functionality. Use in place of `StorageClient::createBucket()`.
*
* When inserting objects into a bucket created with this method, you do NOT need
* to enqueue those objects for deletion or concern yourself with order of
* operations.
*
* @param StorageClient $client
* @param string $bucketName
* @param array $options
* @return Bucket
* @throws GoogleException
*
* @experimental
* @internal
*/
public static function createBucket(StorageClient $client, $bucketName, array $options = [])
{
$backoff = new ExponentialBackoff(8, function ($ex) {
return !($ex instanceof BadRequestException);
});
$bucket = $backoff->execute(function () use ($client, $bucketName, $options) {
return $client->createBucket($bucketName, $options);
});
self::$deletionQueue->add(function () use ($bucket) {
foreach ($bucket->objects() as $object) {
$object->delete();
}
$bucket->delete();
});
return $bucket;
}
/**
* Create a dataset and enqueue it for deletion.
*
* This method provides a means of creating a dataset with pre-configured
* flush+delete functionality. Use in place of `BigQueryClient::createDataset()`.
*
* When inserting tables into a dataset created with this method, you do NOT need
* to enqueue those tables for deletion or concern yourself with order of
* operations.
*
* @param BigQueryClient $client
* @param string $datasetName
* @param array $options
* @return Dataset
*
* @experimental
* @internal
*/
public static function createDataset(BigQueryClient $client, $datasetName, array $options = [])
{
$dataset = $client->createDataset($datasetName, $options);
self::$deletionQueue->add(function () use ($dataset) {
$dataset->delete(['deleteContents' => true]);
});
return $dataset;
}
/**
* Create a topic and enqueue it for deletion.
*
* This method provides a means of creating a topic with pre-configured
* flush+delete functionality. Use in place of `PubSubClient::createTopic()`.
*
* When inserting subscriptions into a topic created with this method, you do NOT need
* to enqueue those subscriptions for deletion or concern yourself with order of
* operations.
*
* @param PubSubClient $client
* @param string $topicName
* @param array $options
* @return Topic
* @throws GoogleException
*
* @experimental
* @internal
*/
public static function createTopic(PubSubClient $client, $topicName, array $options = [])
{
$backoff = new ExponentialBackoff(8);
$topic = $backoff->execute(function () use ($client, $topicName, $options) {
return $client->createTopic($topicName, $options);
});
self::$deletionQueue->add(function () use ($topic) {
foreach ($topic->subscriptions() as $subscription) {
$subscription->delete();
}
$topic->delete();
});
return $topic;
}
/**
* Set "using emulator" flag for single test case.
*
* Should be called in `setUpBeforeClass()` method. This will allow to
* skip tests that are not supported by emulator.
*
* Example:
* ```
* self::setUsingEmulator(getenv('FOOBAR_EMULATOR_HOST'));
* ```
*
* @param bool $enabled Whether emulator is detected. **Defaults to** `true`.
*/
public static function setUsingEmulator($enabled = true)
{
self::$emulatedClasses[get_called_class()] = (bool)$enabled;
}
/**
* Set "using emulator" flag for test cases with specified fully-qualified name prefix.
*
* Should be called in `setUpBeforeClass()` method. This will allow to
* skip tests that are not supported by emulator.
*
* Example:
* ```
* // Set flag for called class namespace.
* self::setUsingEmulatorForClassPrefix(getenv('FOOBAR_EMULATOR_HOST'));
* ```
*
* ```
* // Set flag for some other namespace.
* self::setUsingEmulatorForClassPrefix(getenv('FOOBAR_EMULATOR_HOST'), 'Foobar\\Tests\\System\\Admin\\');
* ```
*
* @param bool $enabled Whether emulator is detected. **Defaults to** `true`.
* @param string|null $prefix Fully-qualified class name prefix. **Defaults to** called class namespace.
*/
public static function setUsingEmulatorForClassPrefix($enabled = true, $prefix = null)
{
if (!isset($prefix)) {
$className = get_called_class();
$prefix = substr($className, 0, strrpos($className, '\\') + 1);
}
self::$emulatedClassPrefixes[$prefix] = (bool)$enabled;
}
/**
* Returns `true` when "using emulator" flag is set either for called class name or its
* fully-qualified name prefix or `false` otherwise.
*
* Example:
* ```
* $transports = [['grpc']];
* if (!self::isEmulatorUsed()) {
* $transports[] = ['rest'];
* }
* ```
*
* @return bool
*/
public static function isEmulatorUsed()
{
$className = get_called_class();
if (!isset(self::$emulatedClasses[$className])) {
$prefix = substr($className, 0, strrpos($className, '\\') + 1);
$isEmulated = false;
foreach (self::$emulatedClassPrefixes as $key => $flag) {
if (strpos($prefix, $key) === 0) {
$isEmulated = $flag;
break;
}
}
self::$emulatedClasses[$className] = $isEmulated;
}
return self::$emulatedClasses[$className];
}
/**
* Skips current test (when called from test method) or entire test case (when called from `setUpBeforeClass()`)
* if "using emulator" flag is set either for called class name or its fully-qualified name prefix.
*
* Example:
* ```
* // Use default reason.
* self::skipIfEmulatorUsed();
* ```
*
* ```
* // Use custom reason.
* self::skipIfEmulatorUsed('Administration functions are not supported by emulator.');
* ```
*
* @param string|null $reason Message explaining reason for skipping this test.
*/
public static function skipIfEmulatorUsed($reason = null)
{
if (self::isEmulatorUsed()) {
self::markTestSkipped($reason ?: 'This test is not supported by the emulator.');
}
}
}