Skip to content

Commit d38dbfd

Browse files
committed
add SystemTestCaseTrait, skip setting up batch tests on emulator
1 parent f918e7c commit d38dbfd

4 files changed

Lines changed: 135 additions & 143 deletions

File tree

Spanner/tests/System/PgBatchTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ class PgBatchTest extends SpannerPgTestCase
3232
use DatabaseRoleTrait;
3333

3434
private static $tableName;
35-
private static $isSetup = false;
35+
private static $hasSetupBatch = false;
3636

3737
/**
3838
* @beforeClass
3939
*/
4040
public static function setUpTestFixtures(): void
4141
{
42-
if (self::$isSetup) {
42+
// skip setting up fixutres for the emulator as there's only one test which does not
43+
// suppport the emulator.
44+
// NOTE: remove this if emulator tests are adde
45+
self::skipEmulatorTests();
46+
47+
if (self::$hasSetupBatch) {
4348
return;
4449
}
4550
self::setUpTestDatabase();
@@ -77,7 +82,7 @@ public static function setUpTestFixtures(): void
7782
}
7883

7984
self::seedTable();
80-
self::$isSetup = true;
85+
self::$hasSetupBatch = true;
8186
}
8287

8388
/**

Spanner/tests/System/SpannerPgTestCase.php

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@
1717

1818
namespace Google\Cloud\Spanner\Tests\System;
1919

20-
use Google\Cloud\Spanner;
20+
use Google\Cloud\Core\Testing\System\SystemTestCase;
2121
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
2222

2323
/**
2424
* @group spanner
2525
* @group spanner-postgres
2626
*/
27-
abstract class SpannerPgTestCase extends SpannerTestCase
27+
abstract class SpannerPgTestCase extends SystemTestCase
2828
{
29+
use SystemTestCaseTrait;
30+
2931
protected static function setUpTestDatabase(): void
3032
{
3133
if (self::$hasSetUp) {
3234
return;
3335
}
3436

35-
self::getClient();
36-
37-
self::$instance = self::$client->instance(self::INSTANCE_NAME);
37+
self::$instance = self::getClient()->instance(self::INSTANCE_NAME);
3838

3939
self::$dbName = uniqid(self::TESTING_PREFIX);
4040

@@ -81,37 +81,4 @@ protected static function setUpTestDatabase(): void
8181

8282
self::$hasSetUp = true;
8383
}
84-
85-
protected static function getClient()
86-
{
87-
if (self::$client) {
88-
return self::$client;
89-
}
90-
91-
$keyFilePath = getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH');
92-
93-
$clientConfig = [
94-
'keyFilePath' => $keyFilePath,
95-
'cacheItemPool' => self::getCacheItemPool(),
96-
];
97-
98-
$serviceAddress = getenv('SPANNER_SERVICE_ADDRESS');
99-
if ($serviceAddress) {
100-
$gapicConfig = [
101-
'serviceAddress' => $serviceAddress
102-
];
103-
104-
$clientConfig['gapicSpannerClient'] = new Spanner\V1\SpannerClient($gapicConfig);
105-
$clientConfig['gapicSpannerDatabaseAdminClient'] =
106-
new Spanner\Admin\Database\V1\DatabaseAdminClient($gapicConfig);
107-
$clientConfig['gapicSpannerInstanceAdminClient'] =
108-
new Spanner\Admin\Instance\V1\InstanceAdminClient($gapicConfig);
109-
110-
echo 'Using Service Address: ' . $serviceAddress . PHP_EOL;
111-
}
112-
113-
self::$client = new SpannerClient($clientConfig);
114-
115-
return self::$client;
116-
}
11784
}

Spanner/tests/System/SpannerTestCase.php

Lines changed: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,22 @@
1818
namespace Google\Cloud\Spanner\Tests\System;
1919

2020
use Google\Cloud\Core\Testing\System\SystemTestCase;
21-
use Google\Cloud\Spanner;
2221
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
23-
use Google\Cloud\Spanner\SpannerClient;
2422

2523
/**
2624
* @group spanner
2725
*/
2826
abstract class SpannerTestCase extends SystemTestCase
2927
{
30-
const TESTING_PREFIX = 'gcloud_testing_';
31-
const INSTANCE_NAME = 'google-cloud-php-system-tests';
32-
33-
const TEST_TABLE_NAME = 'Users';
34-
const TEST_INDEX_NAME = 'uniqueIndex';
35-
36-
const DATABASE_ROLE = 'Reader';
37-
const RESTRICTIVE_DATABASE_ROLE = 'RestrictiveReader';
38-
39-
protected static $client;
40-
protected static $instance;
41-
protected static $database;
42-
protected static $database2;
43-
protected static $dbName;
44-
protected static $hasSetUp = false;
28+
use SystemTestCaseTrait;
4529

4630
protected static function setUpTestDatabase(): void
4731
{
4832
if (self::$hasSetUp) {
4933
return;
50-
}
51-
52-
self::getClient();
34+
}
5335

54-
self::$instance = self::$client->instance(self::INSTANCE_NAME);
36+
self::$instance = self::getClient()->instance(self::INSTANCE_NAME);
5537

5638
if (!self::$dbName = getenv('GOOGLE_CLOUD_SPANNER_TEST_DATABASE')) {
5739
self::$dbName = uniqid(self::TESTING_PREFIX);
@@ -96,85 +78,4 @@ protected static function setUpTestDatabase(): void
9678
self::$database2 = self::getDatabaseInstance(self::$dbName);
9779
self::$hasSetUp = true;
9880
}
99-
100-
protected static function getClient()
101-
{
102-
if (self::$client) {
103-
return self::$client;
104-
}
105-
106-
$keyFilePath = getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH');
107-
108-
$clientConfig = [
109-
'keyFilePath' => $keyFilePath,
110-
'cacheItemPool' => self::getCacheItemPool(),
111-
];
112-
113-
$serviceAddress = getenv('SPANNER_SERVICE_ADDRESS');
114-
if ($serviceAddress) {
115-
$gapicConfig = [
116-
'serviceAddress' => $serviceAddress
117-
];
118-
119-
$clientConfig['gapicSpannerClient'] = new Spanner\V1\Client\SpannerClient($gapicConfig);
120-
$clientConfig['gapicSpannerDatabaseAdminClient'] =
121-
new Spanner\Admin\Database\V1\Client\DatabaseAdminClient($gapicConfig);
122-
$clientConfig['gapicSpannerInstanceAdminClient'] =
123-
new Spanner\Admin\Instance\V1\Client\InstanceAdminClient($gapicConfig);
124-
125-
echo 'Using Service Address: ' . $serviceAddress . PHP_EOL;
126-
}
127-
128-
self::$client = new SpannerClient($clientConfig);
129-
130-
return self::$client;
131-
}
132-
133-
public static function getDatabaseInstance($dbName, $options = [])
134-
{
135-
return self::getClient()->connect(self::INSTANCE_NAME, $dbName, $options);
136-
}
137-
138-
public static function getDatabaseFromInstance($instance, $dbName, $options = [])
139-
{
140-
$instance = self::$client->instance($instance);
141-
return $instance->database($dbName, $options);
142-
}
143-
144-
public static function skipEmulatorTests()
145-
{
146-
if (self::isEmulatorUsed()) {
147-
self::markTestSkipped('This test is not supported by the emulator.');
148-
}
149-
}
150-
151-
public static function emulatorOnly()
152-
{
153-
if (!self::isEmulatorUsed()) {
154-
self::markTestSkipped('This test is only supported by the emulator.');
155-
}
156-
}
157-
158-
public static function isEmulatorUsed(): bool
159-
{
160-
return (bool) getenv('SPANNER_EMULATOR_HOST');
161-
}
162-
163-
public static function getDbWithReaderRole()
164-
{
165-
return self::getDatabaseFromInstance(
166-
self::INSTANCE_NAME,
167-
self::$dbName,
168-
['databaseRole' => self::DATABASE_ROLE]
169-
);
170-
}
171-
172-
public static function getDbWithRestrictiveRole()
173-
{
174-
return self::getDatabaseFromInstance(
175-
self::INSTANCE_NAME,
176-
self::$dbName,
177-
['databaseRole' => self::RESTRICTIVE_DATABASE_ROLE]
178-
);
179-
}
18081
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Spanner\Tests\System;
19+
20+
use Google\Cloud\Spanner;
21+
use Google\Cloud\Spanner\SpannerClient;
22+
23+
trait SystemTestCaseTrait
24+
{
25+
const TESTING_PREFIX = 'gcloud_testing_';
26+
const INSTANCE_NAME = 'google-cloud-php-system-tests';
27+
28+
const TEST_TABLE_NAME = 'Users';
29+
const TEST_INDEX_NAME = 'uniqueIndex';
30+
31+
const DATABASE_ROLE = 'Reader';
32+
const RESTRICTIVE_DATABASE_ROLE = 'RestrictiveReader';
33+
34+
protected static $client;
35+
protected static $instance;
36+
protected static $database;
37+
protected static $database2;
38+
protected static $dbName;
39+
protected static $hasSetUp = false;
40+
41+
protected static function getClient()
42+
{
43+
if (self::$client) {
44+
return self::$client;
45+
}
46+
47+
$keyFilePath = getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH');
48+
49+
$clientConfig = [
50+
'keyFilePath' => $keyFilePath,
51+
'cacheItemPool' => self::getCacheItemPool(),
52+
];
53+
54+
$serviceAddress = getenv('SPANNER_SERVICE_ADDRESS');
55+
if ($serviceAddress) {
56+
$gapicConfig = [
57+
'serviceAddress' => $serviceAddress
58+
];
59+
60+
$clientConfig['gapicSpannerClient'] = new Spanner\V1\Client\SpannerClient($gapicConfig);
61+
$clientConfig['gapicSpannerDatabaseAdminClient'] =
62+
new Spanner\Admin\Database\V1\Client\DatabaseAdminClient($gapicConfig);
63+
$clientConfig['gapicSpannerInstanceAdminClient'] =
64+
new Spanner\Admin\Instance\V1\Client\InstanceAdminClient($gapicConfig);
65+
66+
echo 'Using Service Address: ' . $serviceAddress . PHP_EOL;
67+
}
68+
69+
return self::$client = new SpannerClient($clientConfig);
70+
}
71+
72+
public static function getDatabaseInstance($dbName, $options = [])
73+
{
74+
return self::getClient()->connect(self::INSTANCE_NAME, $dbName, $options);
75+
}
76+
77+
public static function getDatabaseFromInstance($instance, $dbName, $options = [])
78+
{
79+
$instance = self::getClient()->instance($instance);
80+
return $instance->database($dbName, $options);
81+
}
82+
83+
public static function skipEmulatorTests()
84+
{
85+
if (self::isEmulatorUsed()) {
86+
self::markTestSkipped('This test is not supported by the emulator.');
87+
}
88+
}
89+
90+
public static function emulatorOnly()
91+
{
92+
if (!self::isEmulatorUsed()) {
93+
self::markTestSkipped('This test is only supported by the emulator.');
94+
}
95+
}
96+
97+
public static function isEmulatorUsed(): bool
98+
{
99+
return (bool) getenv('SPANNER_EMULATOR_HOST');
100+
}
101+
102+
public static function getDbWithReaderRole()
103+
{
104+
return self::getDatabaseFromInstance(
105+
self::INSTANCE_NAME,
106+
self::$dbName,
107+
['databaseRole' => self::DATABASE_ROLE]
108+
);
109+
}
110+
111+
public static function getDbWithRestrictiveRole()
112+
{
113+
return self::getDatabaseFromInstance(
114+
self::INSTANCE_NAME,
115+
self::$dbName,
116+
['databaseRole' => self::RESTRICTIVE_DATABASE_ROLE]
117+
);
118+
}
119+
}

0 commit comments

Comments
 (0)