Skip to content

Commit 460536f

Browse files
committed
add proto tests
1 parent 4e2746c commit 460536f

File tree

3 files changed

+155
-79
lines changed

3 files changed

+155
-79
lines changed

spanner/composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"require": {
33
"google/cloud-spanner": "^1.97"
4+
},
5+
"autoload": {
6+
"psr-4": {
7+
"GPBMetadata\\": "generated/GPBMetadata",
8+
"Testing\\": "generated/Testing"
9+
}
410
}
511
}

spanner/src/query_data_with_struct_proto_columns.php

-79
This file was deleted.

spanner/test/spannerProtoTest.php

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Google LLC
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\Samples\Spanner;
19+
20+
use Google\Cloud\Spanner\SpannerClient;
21+
use Google\Cloud\Spanner\Instance;
22+
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
23+
use Google\Cloud\TestUtils\TestTrait;
24+
use PHPUnitRetry\RetryTrait;
25+
use PHPUnit\Framework\TestCase;
26+
27+
/**
28+
* @retryAttempts 3
29+
* @retryDelayMethod exponentialBackoff
30+
*/
31+
class spannerProtoTest extends TestCase
32+
{
33+
use TestTrait {
34+
TestTrait::runFunctionSnippet as traitRunFunctionSnippet;
35+
}
36+
37+
use RetryTrait, EventuallyConsistentTestTrait;
38+
39+
/** @var string $instanceId */
40+
protected static $instanceId;
41+
42+
/** @var string $databaseId */
43+
protected static $databaseId;
44+
45+
/** @var Instance $instance */
46+
protected static $instance;
47+
48+
public static function setUpBeforeClass(): void
49+
{
50+
self::checkProjectEnvVars();
51+
52+
if (!extension_loaded('grpc')) {
53+
self::markTestSkipped('Must enable grpc extension.');
54+
}
55+
56+
$spanner = new SpannerClient([
57+
'projectId' => self::$projectId,
58+
]);
59+
60+
self::$instanceId = 'proto-test-' . time() . rand();
61+
self::$databaseId = 'proto-db-' . time() . rand();
62+
self::$instance = $spanner->instance(self::$instanceId);
63+
64+
// Create the instance for testing
65+
$operation = $spanner->createInstance(
66+
$spanner->instanceConfiguration('regional-us-central1'),
67+
self::$instanceId,
68+
[
69+
'displayName' => 'Proto Test Instance',
70+
'nodeCount' => 1,
71+
'labels' => [
72+
'cloud_spanner_samples' => true,
73+
]
74+
]
75+
);
76+
$operation->pollUntilComplete();
77+
}
78+
79+
public function testCreateDatabaseWithProtoColumns()
80+
{
81+
$output = $this->runAdminFunctionSnippet('create_database_with_proto_columns', [
82+
self::$projectId,
83+
self::$instanceId,
84+
self::$databaseId
85+
]);
86+
87+
$this->assertStringContainsString('Waiting for operation to complete...', $output);
88+
$this->assertStringContainsString(sprintf('Created database %s on instance %s', self::$databaseId, self::$instanceId), $output);
89+
}
90+
91+
/**
92+
* @depends testCreateDatabaseWithProtoColumns
93+
*/
94+
public function testInsertDataWithProtoColumns()
95+
{
96+
$output = $this->runFunctionSnippet('insert_data_with_proto_columns', [
97+
self::$instanceId,
98+
self::$databaseId,
99+
1 // User ID
100+
]);
101+
102+
$this->assertEquals('Inserted data.' . PHP_EOL, $output);
103+
}
104+
105+
/**
106+
* @depends testInsertDataWithProtoColumns
107+
*/
108+
public function testQueryDataWithProtoColumns()
109+
{
110+
$output = $this->runFunctionSnippet('query_data_with_proto_columns', [
111+
self::$instanceId,
112+
self::$databaseId,
113+
1 // User ID
114+
]);
115+
116+
$this->assertStringContainsString('User:', $output);
117+
$this->assertStringContainsString('Test User 1', $output);
118+
$this->assertStringContainsString('Book:', $output);
119+
$this->assertStringContainsString('testing.data.Book', $output);
120+
}
121+
122+
private function runFunctionSnippet($sampleName, $params = [])
123+
{
124+
return $this->traitRunFunctionSnippet(
125+
$sampleName,
126+
array_values($params) ?: [self::$instanceId, self::$databaseId]
127+
);
128+
}
129+
130+
private function runAdminFunctionSnippet($sampleName, $params = [])
131+
{
132+
return $this->traitRunFunctionSnippet(
133+
$sampleName,
134+
array_values($params) ?: [self::$projectId, self::$instanceId, self::$databaseId]
135+
);
136+
}
137+
138+
public static function tearDownAfterClass(): void
139+
{
140+
if (self::$instance->exists()) {
141+
// Clean up database
142+
$database = self::$instance->database(self::$databaseId);
143+
if ($database->exists()) {
144+
$database->drop();
145+
}
146+
self::$instance->delete();
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)