Skip to content

Commit d310de0

Browse files
committed
system tests and options refactor WIP
1 parent 70c301f commit d310de0

42 files changed

Lines changed: 292 additions & 249 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Core/src/LongRunning/LongRunningGapicConnection.php renamed to Core/src/LongRunning/LongRunningClientConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @internal
3535
*/
36-
class LongRunningGapicConnection implements LongRunningConnectionInterface
36+
class LongRunningClientConnection implements LongRunningConnectionInterface
3737
{
3838
use RequestProcessorTrait;
3939

Spanner/src/Backup.php

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

2020
use Closure;
2121
use DateTimeInterface;
22-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
22+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
2323
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2424
use Google\ApiCore\ValidationException;
2525
use Google\Cloud\Core\Exception\NotFoundException;
@@ -365,7 +365,7 @@ public function updateExpireTime(DateTimeInterface $newTimestamp, array $options
365365
public function resumeOperation($operationName, array $options = []): LongRunningOperation
366366
{
367367
return new LongRunningOperation(
368-
new LongRunningGapicConnection($this->databaseAdminClient, $this->serializer),
368+
new LongRunningClientConnection($this->databaseAdminClient, $this->serializer),
369369
$operationName,
370370
[
371371
[
@@ -438,7 +438,7 @@ private function backupResultFunction(): Closure
438438
{
439439
return function (array $backup) {
440440
$name = DatabaseAdminClient::parseName($backup['name']);
441-
return $this->instance->backup($name['name'], $backup);
441+
return $this->instance->backup($name['backup'], $backup);
442442
};
443443
}
444444
}

Spanner/src/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use Google\Cloud\Core\Iterator\ItemIterator;
3030
use Google\Cloud\Core\RequestHandler;
3131
use Google\Cloud\Core\Retry;
32-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
32+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
3333
use Google\Cloud\Core\LongRunning\LongRunningOperation;
3434
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
3535
use Google\Cloud\Spanner\Admin\Database\V1\CreateDatabaseRequest;
@@ -2365,7 +2365,7 @@ public function databaseOperations(array $options = []): ItemIterator
23652365
public function resumeOperation($operationName, array $options = []): LongRunningOperation
23662366
{
23672367
return new LongRunningOperation(
2368-
new LongRunningGapicConnection($this->databaseAdminClient, $this->serializer),
2368+
new LongRunningClientConnection($this->databaseAdminClient, $this->serializer),
23692369
$operationName,
23702370
[
23712371
[

Spanner/src/Instance.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
use Closure;
2121
use Google\Cloud\Core\LongRunning\LongRunningOperation;
22-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
22+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
2323
use Google\Cloud\Core\Exception\NotFoundException;
2424
use Google\Cloud\Core\Iam\IamManager;
2525
use Google\Cloud\Core\Iterator\ItemIterator;
@@ -84,6 +84,8 @@ class Instance
8484
*/
8585
private $projectName;
8686

87+
private bool $returnInt64AsObject;
88+
8789
/**
8890
* Create an object representing a Cloud Spanner instance.
8991
*
@@ -95,11 +97,7 @@ class Instance
9597
* @param Serializer $serializer The serializer instance to encode/decode messages.
9698
* @param string $projectId The project ID.
9799
* @param string $name The instance name or ID.
98-
* @param bool $returnInt64AsObject [optional] If true, 64 bit integers will be
99-
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit platform
100-
* compatibility. **Defaults to** false.
101-
* @param array $info [optional] A representation of the instance object.
102-
* @param array $options [optional]{
100+
* @param array $options {
103101
* Instance options
104102
*
105103
* @type array $directedReadOptions Directed read options.
@@ -108,7 +106,11 @@ class Instance
108106
* {@see \Google\Cloud\Spanner\V1\DirectedReadOptions\ReplicaSelection\Type} to set a value.
109107
* @type bool $routeToLeader Enable/disable Leader Aware Routing.
110108
* **Defaults to** `true` (enabled).
109+
* @type bool $returnInt64AsObject If true, 64 bit integers will be
110+
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit platform
111+
* compatibility. **Defaults to** false.
111112
* }
113+
* @param array $info A representation of the instance object.
112114
*/
113115
public function __construct(
114116
private GapicSpannerClient $spannerClient,
@@ -117,14 +119,14 @@ public function __construct(
117119
private Serializer $serializer,
118120
private string $projectId,
119121
private string $name,
120-
private bool $returnInt64AsObject = false,
122+
array $options = [],
121123
private array $info = [],
122-
array $options = []
123124
) {
124125
$this->name = $this->fullyQualifiedInstanceName($name, $projectId);
125126
$this->directedReadOptions = $options['directedReadOptions'] ?? [];
126127
$this->routeToLeader = $options['routeToLeader'] ?? true;
127128
$this->defaultQueryOptions = $options['defaultQueryOptions'] ?? [];
129+
$this->returnInt64AsObject = $options['returnInt64AsObject'] ?? false;
128130
$this->projectName = InstanceAdminClient::projectName($projectId);
129131
}
130132

@@ -819,7 +821,7 @@ public function createInstanceArray(
819821
public function resumeOperation($operationName, array $options = []): LongRunningOperation
820822
{
821823
return new LongRunningOperation(
822-
new LongRunningGapicConnection($this->instanceAdminClient, $this->serializer),
824+
new LongRunningClientConnection($this->instanceAdminClient, $this->serializer),
823825
$operationName,
824826
[
825827
[
@@ -881,13 +883,13 @@ private function instanceResultFunction(): Closure
881883
$this->serializer,
882884
$this->projectId,
883885
$name['instance'],
884-
$this->returnInt64AsObject,
885-
$result,
886886
[
887887
'directedReadOptions' => $this->directedReadOptions,
888888
'routeToLeader' => $this->routeToLeader,
889889
'defaultQueryOptions' => $this->defaultQueryOptions,
890-
]
890+
'returnInt64AsObject' => $this->returnInt64AsObject,
891+
],
892+
$result,
891893
);
892894
};
893895
}

Spanner/src/InstanceConfiguration.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Closure;
2121
use Google\ApiCore\ApiException;
2222
use Google\Cloud\Core\LongRunning\LongRunningOperation;
23-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
23+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
2424
use Google\ApiCore\ValidationException;
2525
use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient;
2626
use Google\Cloud\Spanner\Admin\Instance\V1\CreateInstanceConfigRequest;
@@ -280,10 +280,9 @@ public function update(array $options = [])
280280
[$data, $callOptions] = $this->splitOptionalArgs($options);
281281
$validateOnly = $data['validateOnly'] ?? false;
282282
unset($data['validateOnly']);
283-
$data += ['name' => $this->name];
284283

285284
$request = $this->serializer->decodeMessage(new UpdateInstanceConfigRequest(), [
286-
'instanceConfig' => $data,
285+
'instanceConfig' => $data + ['name' => $this->name],
287286
'updateMask' => $this->fieldMask($data),
288287
'validateOnly' => $validateOnly
289288
]);
@@ -338,7 +337,7 @@ public function delete(array $options = [])
338337
public function resumeOperation($operationName, array $options = [])
339338
{
340339
return new LongRunningOperation(
341-
new LongRunningGapicConnection($this->instanceAdminClient, $this->serializer),
340+
new LongRunningClientConnection($this->instanceAdminClient, $this->serializer),
342341
$operationName,
343342
[
344343
[

Spanner/src/Operation.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Operation
7777
/**
7878
* @param SpannerClient $spannerClient The Spanner client used to make requests.
7979
* @param Serializer $serializer The serializer instance to encode/decode messages.
80-
* @param array $config [optional] {
80+
* @param array $options {
8181
* Configuration options.
8282
*
8383
* @type bool $routeToLeader Enable/disable Leader Aware Routing.
@@ -91,12 +91,11 @@ class Operation
9191
public function __construct(
9292
private SpannerClient $spannerClient,
9393
private Serializer $serializer,
94-
$config = []
94+
array $options = []
9595
) {
9696
$this->mapper = new ValueMapper($options['returnInt64AsObject'] ?? false);
97-
$this->routeToLeader = $this->pluck('routeToLeader', $config, false) ?: true;
98-
$this->defaultQueryOptions =
99-
$this->pluck('defaultQueryOptions', $config, false) ?: [];
97+
$this->routeToLeader = $options['routeToLeader'] ?? true;
98+
$this->defaultQueryOptions = $options['defaultQueryOptions'] ?? [];
10099
}
101100

102101
/**
@@ -539,20 +538,21 @@ public function createTransaction(
539538
$res += [
540539
'id' => null
541540
];
541+
542+
// TODO: unravel this
543+
$transactionOptions = $options['transactionOptions'] ?? [];
544+
unset($options['transactionOptions']);
545+
542546
$options += [
543547
'tag' => null,
544-
'transactionOptions' => []
545-
];
546-
547-
$options['isRetry'] = $options['isRetry'] ?? false;
548+
'isRetry' => false,
549+
] + $transactionOptions;
548550

549551
return new Transaction(
550552
$this,
551553
$session,
552554
$res['id'],
553-
$options['isRetry'],
554-
$options['tag'],
555-
$options['transactionOptions'],
555+
$options,
556556
$this->mapper
557557
);
558558
}

Spanner/src/Result.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ class Result implements \IteratorAggregate
134134
* @param string $transactionContext The transaction's context.
135135
* @param ValueMapper $mapper Maps values.
136136
* @param ?RetrySettings $retrySettings {
137-
* Retry configuration options. Currently, only the `maxRetries` option is supported.
137+
* Retry configuration options. Currently, only the `maxRetries` option
138+
* is supported.
138139
*
139-
* @type int $maxRetries The maximum number of retry attempts before the operation fails.
140-
* Defaults to 3.
141-
* }
140+
* @type int $maxRetries The maximum number of retry attempts before the operation
141+
* fails. Defaults to 3.
142+
* }
142143
*/
143144
public function __construct(
144145
Operation $operation,

Spanner/src/SpannerClient.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Google\ApiCore\CredentialsWrapper;
2222
use Google\ApiCore\Middleware\MiddlewareInterface;
2323
use Google\Cloud\Core\LongRunning\LongRunningOperation;
24+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
2425
use Google\ApiCore\ValidationException;
2526
use Google\Auth\FetchAuthTokenInterface;
2627
use Google\Cloud\Core\ClientTrait;
@@ -39,6 +40,7 @@
3940
use Google\Cloud\Spanner\Middleware\SpannerMiddleware;
4041
use Google\Cloud\Spanner\Session\SessionPoolInterface;
4142
use Google\Cloud\Spanner\V1\Client\SpannerClient as GapicSpannerClient;
43+
use Google\LongRunning\Operation as OperationProto;
4244
use Google\Protobuf\Duration;
4345
use Psr\Cache\CacheItemPoolInterface;
4446
use Psr\Http\StreamInterface;
@@ -157,7 +159,7 @@ class SpannerClient
157159
* Create a Spanner client. Please note that this client requires
158160
* [the gRPC extension](https://cloud.google.com/php/grpc).
159161
*
160-
* @param array $config [optional] {
162+
* @param array $options {
161163
* Configuration Options.
162164
*
163165
* @type string $projectId The Google Cloud project ID.
@@ -206,63 +208,63 @@ class SpannerClient
206208
* }
207209
* @throws GoogleException If the gRPC extension is not enabled.
208210
*/
209-
public function __construct(array $config = [])
211+
public function __construct(array $options = [])
210212
{
211213
$emulatorHost = getenv('SPANNER_EMULATOR_HOST');
212214

213215
$this->requireGrpc();
214216
$scopes = [self::FULL_CONTROL_SCOPE, self::ADMIN_SCOPE];
215-
$config += [
217+
$options += [
216218
'returnInt64AsObject' => false,
217219
'projectIdRequired' => true,
218220
'hasEmulator' => (bool) $emulatorHost,
219221
'emulatorHost' => $emulatorHost,
220222
'queryOptions' => []
221223
];
222224

223-
$this->returnInt64AsObject = $config['returnInt64AsObject'];
224-
$this->directedReadOptions = $config['directedReadOptions'] ?? [];
225-
$this->routeToLeader = $config['routeToLeader'] ?? true;
226-
$this->defaultQueryOptions = $config['queryOptions'];
225+
$this->returnInt64AsObject = $options['returnInt64AsObject'];
226+
$this->directedReadOptions = $options['directedReadOptions'] ?? [];
227+
$this->routeToLeader = $options['routeToLeader'] ?? true;
228+
$this->defaultQueryOptions = $options['queryOptions'];
227229

228230
// Configure GAPIC client options
229-
$config = $this->buildClientOptions($config);
230-
if (isset($config['credentialsConfig']['scopes'])) {
231-
$config['credentialsConfig']['scopes'] = array_merge(
232-
$config['credentialsConfig']['scopes'],
231+
$options = $this->buildClientOptions($options);
232+
if (isset($options['credentialsConfig']['scopes'])) {
233+
$options['credentialsConfig']['scopes'] = array_merge(
234+
$options['credentialsConfig']['scopes'],
233235
$scopes
234236
);
235237
} else {
236-
$config['credentialsConfig']['scopes'] = $scopes;
238+
$options['credentialsConfig']['scopes'] = $scopes;
237239
}
238240

239241
if ($emulatorHost) {
240242
$emulatorConfig = $this->emulatorGapicConfig($emulatorHost);
241-
$config = array_merge(
242-
$config,
243+
$options = array_merge(
244+
$options,
243245
$emulatorConfig
244246
);
245247
} else {
246-
$config['credentials'] = $this->createCredentialsWrapper(
247-
$config['credentials'],
248-
$config['credentialsConfig'],
249-
$config['universeDomain']
248+
$options['credentials'] = $this->createCredentialsWrapper(
249+
$options['credentials'],
250+
$options['credentialsConfig'],
251+
$options['universeDomain']
250252
);
251253
}
252-
$this->projectId = $this->detectProjectId($config);
254+
$this->projectId = $this->detectProjectId($options);
253255
$this->serializer = new Serializer();
254256

255257
// Adds some defaults
256258
// gccl needs to be present for handwritten clients
257-
$clientConfig = $config += [
259+
$clientOptions = $options += [
258260
'libName' => 'gccl',
259261
'serializer' => $this->serializer,
260262
];
261-
$this->spannerClient = $config['gapicSpannerClient'] ?? new GapicSpannerClient($clientConfig);
262-
$this->instanceAdminClient = $config['gapicSpannerInstanceAdminClient']
263-
?? new InstanceAdminClient($clientConfig);
264-
$this->databaseAdminClient = $config['gapicSpannerDatabaseAdminClient']
265-
?? new DatabaseAdminClient($clientConfig);
263+
$this->spannerClient = $options['gapicSpannerClient'] ?? new GapicSpannerClient($clientOptions);
264+
$this->instanceAdminClient = $options['gapicSpannerInstanceAdminClient']
265+
?? new InstanceAdminClient($clientOptions);
266+
$this->databaseAdminClient = $options['gapicSpannerDatabaseAdminClient']
267+
?? new DatabaseAdminClient($clientOptions);
266268

267269
// Add the SpannerMiddleware, which wraps API Exceptions, and adds
268270
// Resource Prefix and LAR headers
@@ -499,9 +501,9 @@ public function instanceConfigOperations(array $options = [])
499501
[$this->instanceAdminClient, 'listInstanceConfigOperations'],
500502
$request,
501503
$callOptions + ['resource-prefix' => $this->projectName],
502-
function (Operation $operation) {
504+
function (OperationProto $operation) {
503505
return new LongRunningOperation(
504-
new LongRunningGapicConnection($this->databaseAdminClient, $this->serializer),
506+
new LongRunningClientConnection($this->databaseAdminClient, $this->serializer),
505507
$operation->getName(),
506508
[
507509
'type.googleapis.com/google.spanner.admin.instance.v1.ListInstanceConfigMetadata' =>
@@ -566,13 +568,13 @@ public function instance($name, array $instance = [])
566568
$this->serializer,
567569
$this->projectId,
568570
$name,
569-
$this->returnInt64AsObject,
570-
$instance,
571571
[
572572
'directedReadOptions' => $this->directedReadOptions,
573573
'routeToLeader' => $this->routeToLeader,
574-
'defaultQueryOptions' => $this->defaultQueryOptions
575-
]
574+
'defaultQueryOptions' => $this->defaultQueryOptions,
575+
'returnInt64AsObject' => $this->returnInt64AsObject,
576+
],
577+
$instance,
576578
);
577579
}
578580

0 commit comments

Comments
 (0)