Skip to content

Commit fdcda09

Browse files
authored
chore(Spanner): updates to support new Spanner emulator (#8677)
1 parent 92b3077 commit fdcda09

32 files changed

Lines changed: 418 additions & 457 deletions

.github/workflows/emulator-system-tests-spanner.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: 'ubuntu-24.04'
1919
services:
2020
emulator:
21-
image: gcr.io/cloud-spanner-emulator/emulator:1.5.34
21+
image: gcr.io/cloud-spanner-emulator/emulator:1.5.42
2222
ports:
2323
- 9010:9010
2424
- 9020:9020
@@ -41,7 +41,7 @@ jobs:
4141
- name: Setup PHP
4242
uses: shivammathur/setup-php@v2
4343
with:
44-
php-version: '8.1'
44+
php-version: '8.4'
4545
ini-values: grpc.enable_fork_support=1
4646
tools: pecl
4747
extensions: bcmath, grpc, pcntl

Core/src/Testing/System/SystemTestCase.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Google\Cloud\Storage\StorageClient;
2929
use Google\Cloud\Core\Testing\System\DeletionQueue;
3030
use PHPUnit\Framework\TestCase;
31-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
3231

3332
/**
3433
* SystemTestCase can be extended to implement system tests
@@ -287,11 +286,4 @@ public static function skipIfEmulatorUsed($reason = null)
287286
self::markTestSkipped($reason ?: 'This test is not supported by the emulator.');
288287
}
289288
}
290-
291-
protected static function getCacheItemPool()
292-
{
293-
return new FilesystemAdapter(
294-
directory: __DIR__ . '/../../../../.cache'
295-
);
296-
}
297289
}

Spanner/src/Result.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ private function setSnapshotOrTransaction(array $result): void
520520
[],
521521
$this->mapper
522522
);
523-
if (isset($result['precommitToken'])) {
523+
if (isset($result['precommitToken']['precommitToken'])) {
524524
// @TODO: Can we move this logic to the serializer or value mapper?
525525
$this->transaction->setPrecommitToken(
526526
(new MultiplexedSessionPrecommitToken())

Spanner/tests/System/AdminTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Google\Cloud\Core\Exception\FailedPreconditionException;
2121
use Google\Cloud\Core\LongRunning\LongRunningOperation;
22+
use Google\Cloud\Core\Testing\System\SystemTestCase;
2223
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
2324
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
2425
use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient;
@@ -31,8 +32,10 @@
3132
* @group spanner
3233
* @group admin
3334
*/
34-
class AdminTest extends SpannerTestCase
35+
class AdminTest extends SystemTestCase
3536
{
37+
use SystemTestCaseTrait;
38+
3639
/**
3740
* @beforeClass
3841
*/
@@ -123,15 +126,7 @@ public function testDatabase()
123126

124127
$this->assertInstanceOf(Database::class, current($database));
125128
$this->assertTrue($db->exists());
126-
127-
$expectedDatabaseDialect = DatabaseDialect::GOOGLE_STANDARD_SQL;
128-
129-
// TODO: Remove this, when the emulator supports PGSQL
130-
if ((bool) getenv('SPANNER_EMULATOR_HOST')) {
131-
$expectedDatabaseDialect = DatabaseDialect::DATABASE_DIALECT_UNSPECIFIED;
132-
}
133-
134-
$this->assertEquals($db->info()['databaseDialect'], $expectedDatabaseDialect);
129+
$this->assertEquals($db->info()['databaseDialect'], DatabaseDialect::GOOGLE_STANDARD_SQL);
135130

136131
$stmt = "CREATE TABLE Ids (\n" .
137132
" id INT64 NOT NULL,\n" .

Spanner/tests/System/BackupTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Google\Cloud\Core\Exception\BadRequestException;
2121
use Google\Cloud\Core\Exception\ConflictException;
2222
use Google\Cloud\Core\LongRunning\LongRunningOperation;
23+
use Google\Cloud\Core\Testing\System\SystemTestCase;
2324
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
2425
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig;
2526
use Google\Cloud\Spanner\Admin\Database\V1\EncryptionInfo\Type;
@@ -30,8 +31,10 @@
3031
/**
3132
* @group spanner
3233
*/
33-
class BackupTest extends SpannerTestCase
34+
class BackupTest extends SystemTestCase
3435
{
36+
use SystemTestCaseTrait;
37+
3538
const BACKUP_PREFIX = 'spanner_backup_';
3639

3740
protected static $backupId1;
@@ -47,7 +50,7 @@ class BackupTest extends SpannerTestCase
4750

4851
protected static $project;
4952

50-
private static $hasSetUp = false;
53+
private static $hasSetUpBackup = false;
5154

5255
/**
5356
* @beforeClass
@@ -59,7 +62,7 @@ public static function setUpTestFixtures(): void
5962
self::emulatorOnly();
6063

6164
self::setUpTestDatabase();
62-
if (self::$hasSetUp) {
65+
if (self::$hasSetUpBackup) {
6366
return;
6467
}
6568

@@ -112,7 +115,7 @@ public static function setUpTestFixtures(): void
112115
self::$backupId1 = uniqid(self::BACKUP_PREFIX);
113116
self::$backupId2 = uniqid('users-');
114117
self::$copyBackupId = uniqid('copy-');
115-
self::$hasSetUp = true;
118+
self::$hasSetUpBackup = true;
116119
}
117120

118121
public function testCreateBackup()

Spanner/tests/System/BatchTest.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Google\Cloud\Spanner\Tests\System;
1919

2020
use Google\Cloud\Core\Exception\ServiceException;
21+
use Google\Cloud\Core\Testing\System\SystemTestCase;
2122
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
2223
use Google\Cloud\Spanner\Batch\BatchClient;
2324
use Google\Cloud\Spanner\Batch\BatchSnapshot;
@@ -28,8 +29,9 @@
2829
* @group spanner
2930
* @group spanner-batch
3031
*/
31-
class BatchTest extends SpannerTestCase
32+
class BatchTest extends SystemTestCase
3233
{
34+
use SystemTestCaseTrait;
3335
use DatabaseRoleTrait;
3436

3537
private static $tableName;
@@ -56,26 +58,26 @@ public static function setUpTestFixtures(): void
5658
))->pollUntilComplete();
5759

5860
if (self::$database->info()['databaseDialect'] == DatabaseDialect::GOOGLE_STANDARD_SQL) {
59-
self::$database->updateDdlBatch([
60-
sprintf(
61-
'CREATE ROLE %s',
62-
self::$dbRole
63-
),
64-
sprintf(
65-
'CREATE ROLE %s',
66-
self::$restrictiveDbRole
67-
),
68-
sprintf(
61+
$statements = [
62+
sprintf('CREATE ROLE %s', self::$dbRole),
63+
sprintf('CREATE ROLE %s', self::$restrictiveDbRole),
64+
];
65+
66+
if (!self::isEmulatorUsed()) {
67+
$statements[] = sprintf(
6968
'GRANT SELECT(id) ON TABLE %s TO ROLE %s',
7069
self::$tableName,
7170
self::$restrictiveDbRole
72-
),
73-
sprintf(
74-
'GRANT SELECT ON TABLE %s TO ROLE %s',
75-
self::$tableName,
76-
self::$dbRole
77-
)
78-
])->pollUntilComplete();
71+
);
72+
}
73+
74+
$statements[] = sprintf(
75+
'GRANT SELECT ON TABLE %s TO ROLE %s',
76+
self::$tableName,
77+
self::$dbRole
78+
);
79+
80+
self::$database->updateDdlBatch($statements)->pollUntilComplete();
7981
}
8082

8183
self::seedTable();

Spanner/tests/System/BatchWriteTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
namespace Google\Cloud\Spanner\Tests\System;
1919

2020
use Google\Rpc\Code;
21+
use Google\Cloud\Core\Testing\System\SystemTestCase;
2122

2223
/**
2324
* @group spanner
2425
*/
25-
class BatchWriteTest extends SpannerTestCase
26+
class BatchWriteTest extends SystemTestCase
2627
{
28+
use SystemTestCaseTrait;
29+
2730
const TABLE_NAME = 'BatchWrites';
2831
/**
2932
* @beforeClass

Spanner/tests/System/DatabaseRoleTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function insertDbProvider()
4646
self::getDbWithRestrictiveRole(),
4747
[
4848
'id' => rand(1, 346464),
49-
'name' => uniqid(SpannerTestCase::TESTING_PREFIX),
49+
'name' => uniqid(self::TESTING_PREFIX),
5050
'birthday' => new Date(new \DateTime('2000-01-01'))
5151
],
5252
'PERMISSION_DENIED'
@@ -55,7 +55,7 @@ public function insertDbProvider()
5555
self::getDbWithRestrictiveRole(),
5656
[
5757
'id' => rand(1, 346464),
58-
'name' => uniqid(SpannerTestCase::TESTING_PREFIX)
58+
'name' => uniqid(self::TESTING_PREFIX)
5959
],
6060
null
6161
]

Spanner/tests/System/GeneratedAdminEmulatorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

20+
use Google\Cloud\Core\Testing\System\SystemTestCase;
2021
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
2122
use Google\Cloud\Spanner\Admin\Database\V1\CreateDatabaseRequest;
2223
use Google\Cloud\Spanner\Admin\Database\V1\GetDatabaseRequest;
@@ -28,8 +29,10 @@
2829
/**
2930
* @group spanner
3031
*/
31-
class GeneratedAdminEmulatorTest extends SpannerTestCase
32+
class GeneratedAdminEmulatorTest extends SystemTestCase
3233
{
34+
use SystemTestCaseTrait;
35+
3336
private static $projectId;
3437

3538
/**
@@ -42,9 +45,7 @@ public static function setUpTestFixtures(): void
4245

4346
public function testAdminClientEmulatorSupport()
4447
{
45-
if (!getenv('SPANNER_EMULATOR_HOST')) {
46-
self::markTestSkipped('This test is required to run only in the emulator.');
47-
}
48+
self::emulatorOnly();
4849

4950
$instanceId = uniqid(self::INSTANCE_NAME);
5051
$databaseId = uniqid(self::TESTING_PREFIX);

Spanner/tests/System/LargeReadTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717

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

20+
use Google\Cloud\Core\Testing\System\SystemTestCase;
2021
use Google\Cloud\Spanner\Bytes;
2122
use Google\Cloud\Spanner\KeySet;
2223

2324
/**
2425
* @group spanner
2526
* @group spanner-large-read
2627
*/
27-
class LargeReadTest extends SpannerTestCase
28+
class LargeReadTest extends SystemTestCase
2829
{
30+
use SystemTestCaseTrait;
31+
2932
private static $tableName;
3033
private static $row = [];
3134

0 commit comments

Comments
 (0)