Skip to content

Commit 7d828cc

Browse files
authored
Refactors bigtable tests to use base trait (GoogleCloudPlatform#999)
1 parent ec4e6c7 commit 7d828cc

File tree

6 files changed

+195
-177
lines changed

6 files changed

+195
-177
lines changed

bigtable/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"require": {
33
"google/cloud-bigtable": "^1.0.0",
44
"psr/cache": "^1.0"
5+
},
6+
"autoload-dev": {
7+
"psr-4": {
8+
"Google\\Cloud\\Samples\\Bigtable\\Tests\\": "test"
9+
}
510
}
611
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Google\Cloud\Samples\Bigtable\Tests;
20+
21+
use Exception;
22+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
23+
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
24+
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
25+
use Google\Cloud\Bigtable\Admin\V2\Table;
26+
use Google\Cloud\Bigtable\BigtableClient;
27+
use Google\Cloud\TestUtils\TestTrait;
28+
use Google\Cloud\TestUtils\ExponentialBackoffTrait;
29+
30+
trait BigtableTestTrait
31+
{
32+
use TestTrait;
33+
use ExponentialBackoffTrait;
34+
35+
private static $instanceAdminClient;
36+
private static $tableAdminClient;
37+
private static $bigtableClient;
38+
private static $instanceId;
39+
private static $tableId;
40+
41+
public static function setUpBigtableVars()
42+
{
43+
self::checkProjectEnvVarBeforeClass();
44+
self::$instanceAdminClient = new BigtableInstanceAdminClient();
45+
self::$tableAdminClient = new BigtableTableAdminClient();
46+
self::$bigtableClient = new BigtableClient([
47+
'projectId' => self::$projectId,
48+
]);
49+
}
50+
51+
public static function createDevInstance($instanceIdPrefix)
52+
{
53+
$instanceId = uniqid($instanceIdPrefix);
54+
$output = self::runSnippet('create_dev_instance', [
55+
self::$projectId,
56+
$instanceId,
57+
$instanceId,
58+
]);
59+
60+
// Verify the instance was created successfully
61+
if (false !== strpos($output, 'Error: ')) {
62+
throw new Exception('Error creating instance: ' . $output);
63+
}
64+
65+
return $instanceId;
66+
}
67+
68+
public static function createTable($tableIdPrefix, $columns = [])
69+
{
70+
$tableId = uniqid($tableIdPrefix);
71+
72+
$formattedParent = self::$tableAdminClient
73+
->instanceName(self::$projectId, self::$instanceId);
74+
75+
$columns = $columns ?: ['stats_summary'];
76+
$table = (new Table())->setColumnFamilies(array_combine(
77+
$columns,
78+
array_fill(0, count($columns), new ColumnFamily)
79+
));
80+
81+
self::$tableAdminClient->createtable(
82+
$formattedParent,
83+
$tableId,
84+
$table
85+
);
86+
87+
return $tableId;
88+
}
89+
90+
public static function deleteBigtableInstance()
91+
{
92+
$instanceName = self::$instanceAdminClient->instanceName(
93+
self::$projectId,
94+
self::$instanceId
95+
);
96+
self::$instanceAdminClient->deleteInstance($instanceName);
97+
}
98+
}

bigtable/test/bigtableTest.php

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,55 @@
22

33
namespace Google\Cloud\Samples\Bigtable\Tests;
44

5-
use Google\Cloud\Bigtable\BigtableClient;
6-
use PHPUnit\Framework\TestCase;
7-
8-
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
9-
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
10-
use Google\Cloud\TestUtils\ExponentialBackoffTrait;
11-
use Google\Cloud\Bigtable\Admin\V2\Table\View;
12-
use Google\Cloud\TestUtils\TestTrait;
135
use Google\ApiCore\ApiException;
6+
use Google\Cloud\Bigtable\Admin\V2\Table\View;
7+
use PHPUnit\Framework\TestCase;
148

159
final class BigtableTest extends TestCase
1610
{
17-
use TestTrait, ExponentialBackoffTrait;
11+
use BigtableTestTrait;
1812

1913
const INSTANCE_ID_PREFIX = 'php-instance-';
2014
const CLUSTER_ID_PREFIX = 'php-cluster-';
2115
const TABLE_ID_PREFIX = 'php-table-';
22-
private static $instanceAdminClient;
23-
private static $tableAdminClient;
24-
private static $bigtableClient;
25-
private static $instanceId;
16+
2617
private static $clusterId;
2718

2819
public static function setUpBeforeClass()
2920
{
30-
self::checkProjectEnvVarBeforeClass();
31-
self::$instanceAdminClient = new BigtableInstanceAdminClient();
32-
self::$tableAdminClient = new BigtableTableAdminClient();
33-
self::$bigtableClient = new BigtableClient([
34-
'projectId' => self::$projectId,
35-
]);
36-
37-
self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
38-
self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX);
39-
40-
self::create_production_instance(self::$projectId, self::$instanceId, self::$clusterId);
21+
self::setUpBigtableVars();
4122
}
4223

4324
public function setUp()
4425
{
4526
$this->useResourceExhaustedBackoff();
4627
}
4728

29+
public function testCreateProductionInstance()
30+
{
31+
self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
32+
self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX);
33+
34+
$content = self::runSnippet('create_production_instance', [
35+
self::$projectId,
36+
self::$instanceId,
37+
self::$clusterId
38+
]);
39+
40+
$instanceName = self::$instanceAdminClient->instanceName(
41+
self::$projectId,
42+
self::$instanceId
43+
);
44+
45+
$this->checkInstance($instanceName);
46+
}
47+
48+
/**
49+
* @depends testCreateProductionInstance
50+
*/
4851
public function testCreateAndDeleteCluster()
4952
{
53+
// Create a new cluster as last cluster in an instance cannot be deleted
5054
$clusterId = uniqid(self::CLUSTER_ID_PREFIX);
5155

5256
$content = self::runSnippet('create_cluster', [
@@ -57,7 +61,11 @@ public function testCreateAndDeleteCluster()
5761
]);
5862
$array = explode(PHP_EOL, $content);
5963

60-
$clusterName = self::$instanceAdminClient->clusterName(self::$projectId, self::$instanceId, $clusterId);
64+
$clusterName = self::$instanceAdminClient->clusterName(
65+
self::$projectId,
66+
self::$instanceId,
67+
$clusterId
68+
);
6169

6270
$this->checkCluster($clusterName);
6371

@@ -95,23 +103,9 @@ public function testCreateDevInstance()
95103
$this->cleanInstance(self::$projectId, $instanceId);
96104
}
97105

98-
public function testCreateProductionInstance()
99-
{
100-
$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
101-
$clusterId = uniqid(self::CLUSTER_ID_PREFIX);
102-
103-
$content = self::runSnippet('create_production_instance', [
104-
self::$projectId,
105-
$instanceId,
106-
$clusterId
107-
]);
108-
109-
$instanceName = self::$instanceAdminClient->instanceName(self::$projectId, $instanceId);
110-
111-
$this->checkInstance($instanceName);
112-
$this->cleanInstance(self::$projectId, $instanceId);
113-
}
114-
106+
/**
107+
* @depends testCreateProductionInstance
108+
*/
115109
public function testListInstances()
116110
{
117111
$content = self::runSnippet('list_instance', [
@@ -125,6 +119,9 @@ public function testListInstances()
125119
$this->assertContains(self::$instanceId, $array);
126120
}
127121

122+
/**
123+
* @depends testCreateProductionInstance
124+
*/
128125
public function testListTable()
129126
{
130127
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -141,6 +138,9 @@ public function testListTable()
141138
$this->assertContains('projects/' . self::$projectId . '/instances/' . self::$instanceId . '/tables/' . $tableId, $array);
142139
}
143140

141+
/**
142+
* @depends testCreateProductionInstance
143+
*/
144144
public function testListColumnFamilies()
145145
{
146146
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -166,6 +166,9 @@ public function testListColumnFamilies()
166166
$this->assertContains('{"gcRule":{"union":{"rules":[{"maxNumVersions":2},{"maxAge":"432000s"}]}}}', $array);
167167
}
168168

169+
/**
170+
* @depends testCreateProductionInstance
171+
*/
169172
public function testListInstanceClusters()
170173
{
171174
$content = self::runSnippet('list_instance_clusters', [
@@ -179,7 +182,10 @@ public function testListInstanceClusters()
179182
$this->assertContains('projects/' . self::$projectId . '/instances/' . self::$instanceId . '/clusters/' . self::$clusterId, $array);
180183
}
181184

182-
public function testcreate_table()
185+
/**
186+
* @depends testCreateProductionInstance
187+
*/
188+
public function testCreateTable()
183189
{
184190
$tableId = uniqid(self::TABLE_ID_PREFIX);
185191

@@ -194,6 +200,9 @@ public function testcreate_table()
194200
$this->checkTable($tableName);
195201
}
196202

203+
/**
204+
* @depends testCreateProductionInstance
205+
*/
197206
public function testCreateFamilyGcUnion()
198207
{
199208
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -226,6 +235,9 @@ public function testCreateFamilyGcUnion()
226235
$this->checkRule($tableName, 'cf3', $gcRuleCompare);
227236
}
228237

238+
/**
239+
* @depends testCreateProductionInstance
240+
*/
229241
public function testCreateFamilyGcNested()
230242
{
231243
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -267,6 +279,9 @@ public function testCreateFamilyGcNested()
267279
$this->checkRule($tableName, 'cf5', $gcRuleCompare);
268280
}
269281

282+
/**
283+
* @depends testCreateProductionInstance
284+
*/
270285
public function testCreateFamilyGcMaxVersions()
271286
{
272287
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -290,6 +305,9 @@ public function testCreateFamilyGcMaxVersions()
290305
$this->checkRule($tableName, 'cf2', $gcRuleCompare);
291306
}
292307

308+
/**
309+
* @depends testCreateProductionInstance
310+
*/
293311
public function testCreateFamilyGcMaxAge()
294312
{
295313
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -313,6 +331,9 @@ public function testCreateFamilyGcMaxAge()
313331
$this->checkRule($tableName, 'cf1', $gcRuleCompare);
314332
}
315333

334+
/**
335+
* @depends testCreateProductionInstance
336+
*/
316337
public function testCreateFamilyGcIntersection()
317338
{
318339
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -345,6 +366,9 @@ public function testCreateFamilyGcIntersection()
345366
$this->checkRule($tableName, 'cf4', $gcRuleCompare);
346367
}
347368

369+
/**
370+
* @depends testCreateProductionInstance
371+
*/
348372
public function testDeleteTable()
349373
{
350374
$tableId = uniqid(self::TABLE_ID_PREFIX);
@@ -369,6 +393,9 @@ public function testDeleteTable()
369393
}
370394
}
371395

396+
/**
397+
* @depends testCreateProductionInstance
398+
*/
372399
public function testHelloWorld()
373400
{
374401
$this->requireGrpc();
@@ -395,15 +422,9 @@ public function testHelloWorld()
395422
$this->assertContains(sprintf('Deleted %s table.', $tableId), $array);
396423
}
397424

398-
private static function create_production_instance($projectId, $instanceId, $clusterId)
399-
{
400-
$content = self::runSnippet('create_production_instance', [
401-
$projectId,
402-
$instanceId,
403-
$clusterId
404-
]);
405-
}
406-
425+
/**
426+
* @depends testCreateProductionInstance
427+
*/
407428
public function testDeleteInstance()
408429
{
409430
$instanceName = self::$instanceAdminClient->instanceName(self::$projectId, self::$instanceId);

0 commit comments

Comments
 (0)