Skip to content

Commit b8e27ef

Browse files
committed
Fix base64 encoding
1 parent e49033a commit b8e27ef

9 files changed

Lines changed: 53 additions & 57 deletions

File tree

Datastore/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/Unit/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
33
<coverage>
44
<include>
55
<directory suffix=".php">src</directory>

Datastore/src/DatastoreClient.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ class DatastoreClient
149149
* @type bool $returnInt64AsObject If true, 64 bit integers will be
150150
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit
151151
* platform compatibility. **Defaults to** false.
152-
* @type Google\Cloud\Datastore\V1\Client\DatastoreClient $datastoreClient A client that is of
153-
* type {@see \Google\Cloud\Datastore\V1\Client\DatastoreClient}
152+
* @type GapicDatastoreClient $datastoreClient A client that is of
153+
* type {@see GapicDatastoreClient}
154154
* }
155155
* @throws \InvalidArgumentException
156156
*/
@@ -555,11 +555,6 @@ public function allocateIds(array $keys, array $options = [])
555555
*/
556556
public function transaction(array $options = [])
557557
{
558-
if (isset($options['transactionOptions']['previousTransaction'])) {
559-
$options['transactionOptions']['previousTransaction'] = base64_encode(
560-
$options['transactionOptions']['previousTransaction']
561-
);
562-
}
563558
$transaction = $this->operation->beginTransaction([
564559
// if empty, force request to encode as {} rather than [].
565560
'readWrite' => $this->pluck('transactionOptions', $options, false) ?: (object) []

Datastore/src/Operation.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
use Google\Cloud\Datastore\V1\Client\DatastoreClient;
3333
use Google\Cloud\Datastore\V1\CommitRequest;
3434
use Google\Cloud\Datastore\V1\CommitRequest\Mode;
35-
use Google\Cloud\Datastore\V1\Key as GrpcKey;
35+
use Google\Cloud\Datastore\V1\EntityResult;
36+
use Google\Cloud\Datastore\V1\Key as ProtobufKey;
3637
use Google\Cloud\Datastore\V1\LookupRequest;
3738
use Google\Cloud\Datastore\V1\Mutation;
3839
use Google\Cloud\Datastore\V1\ReadOptions;
@@ -41,6 +42,7 @@
4142
use Google\Cloud\Datastore\V1\RunAggregationQueryRequest;
4243
use Google\Cloud\Datastore\V1\RunQueryRequest;
4344
use Google\Cloud\Datastore\V1\TransactionOptions;
45+
use Google\Protobuf\RepeatedField;
4446
use Google\Protobuf\Timestamp as ProtobufTimestamp;
4547
use InvalidArgumentException;
4648

@@ -310,10 +312,10 @@ public function beginTransaction($transactionOptions, array $options = [])
310312
$protoTransactionOptions = new TransactionOptions();
311313
$protoTransactionOptions->mergeFromJsonString(json_encode($transactionOptions));
312314

313-
$beginTransactionRequest = new BeginTransactionRequest();
314-
$beginTransactionRequest->setProjectId($this->projectId);
315-
$beginTransactionRequest->setDatabaseId($options['databaseId'] ?? $this->databaseId);
316-
$beginTransactionRequest->setTransactionOptions($protoTransactionOptions);
315+
$beginTransactionRequest = (new BeginTransactionRequest())
316+
->setProjectId($this->projectId)
317+
->setDatabaseId($options['databaseId'] ?? $this->databaseId)
318+
->setTransactionOptions($protoTransactionOptions);
317319

318320
$res = $this->gapicClient->beginTransaction($beginTransactionRequest, $options);
319321

@@ -353,19 +355,19 @@ public function allocateIds(array $keys, array $options = [])
353355

354356
$serviceKeys = [];
355357
foreach ($keys as $key) {
356-
$keyMessage = new GrpcKey();
358+
$keyMessage = new protobufKey();
357359
$keyMessage->mergeFromJsonString(json_encode($key->keyObject()));
358360
$serviceKeys[] = $keyMessage;
359361
}
360362

361-
$request = new AllocateIdsRequest();
362-
$request->setProjectId($this->projectId);
363-
$request->setDatabaseId($options['databaseId'] ?? $this->databaseId);
364-
$request->setKeys($serviceKeys);
363+
$request = (new AllocateIdsRequest())
364+
->setProjectId($this->projectId)
365+
->setDatabaseId($options['databaseId'] ?? $this->databaseId)
366+
->setKeys($serviceKeys);
365367

366368
$allocateIdsResponse = $this->gapicClient->allocateIds($request, $options);
367369

368-
/** @var GrpcKey $responseKey */
370+
/** @var protobufKey $responseKey */
369371
foreach ($allocateIdsResponse->getKeys() as $index => $responseKey) {
370372
$path = $responseKey->getPath();
371373

@@ -425,17 +427,16 @@ public function lookup(array $keys, array $options = [])
425427
));
426428
}
427429

428-
$grpcKey = new GrpcKey();
429-
$grpcKey->mergeFromJsonString(json_encode($key->keyObject()));
430-
$serviceKeys[] = $grpcKey;
430+
$protobufKey = new protobufKey();
431+
$protobufKey->mergeFromJsonString(json_encode($key->keyObject()));
432+
$serviceKeys[] = $protobufKey;
431433
});
432434

433-
$lookupRequest = new LookupRequest();
434-
$lookupRequest->setDatabaseId($options['databaseId'] ?? $this->databaseId);
435-
$lookupRequest->setProjectId($this->projectId);
436-
$lookupRequest->setKeys($serviceKeys);
437-
438-
$lookupRequest->setReadOptions($this->createReadOptions($options));
435+
$lookupRequest = (new LookupRequest())
436+
->setDatabaseId($options['databaseId'] ?? $this->databaseId)
437+
->setProjectId($this->projectId)
438+
->setKeys($serviceKeys)
439+
->setReadOptions($this->createReadOptions($options));
439440

440441
$lookupResponse = $this->gapicClient->lookup($lookupRequest, $options);
441442

@@ -445,7 +446,7 @@ public function lookup(array $keys, array $options = [])
445446
'deferred' => [],
446447
];
447448

448-
/** @var GrpcEntity $found */
449+
/** @var protoEntity $found */
449450
foreach ($lookupResponse->getFound() as $found) {
450451
$result['found'][] = $this->mapEntityResult(
451452
$this->serializer->encodeMessage($found), $options['className']
@@ -456,15 +457,15 @@ public function lookup(array $keys, array $options = [])
456457
$result['found'] = $this->sortEntities($result['found'], $keys);
457458
}
458459

459-
/** @var GrpcEntity $missing */
460+
/** @var entityResult $missing*/
460461
foreach ($lookupResponse->getMissing() as $missing) {
461462
$result['missing'][] = $this->key(
462463
$missing->getEntity()->getKey()->getPath(),
463464
$missing->getEntity()->getKey()->getPartitionId()
464465
);
465466
}
466467

467-
/** @var GrpcKey $deferred */
468+
/** @var protobufKey $deferred */
468469
foreach ($lookupResponse->getDeferred() as $deferred) {
469470
$result['deferred'][] = $this->key(
470471
$deferred->getPath(),
@@ -615,6 +616,8 @@ function (array $entityResult) use ($options) {
615616
* [ReadConsistency](https://cloud.google.com/datastore/reference/rest/v1/ReadOptions#ReadConsistency).
616617
* @type string $databaseId ID of the database to which the entities belong.
617618
* @type Timestamp $readTime Reads entities as they were at the given timestamp.
619+
* @type ExplainOptions $explainOptions An ExplainOptions object to get the execution stats
620+
* {@see ExplainOptions}
618621
* }
619622
* @return AggregationQueryResult
620623
*/
@@ -692,11 +695,12 @@ public function commit(array $mutations, array $options = [])
692695
$protoMutations[] = $protoMutation;
693696
}
694697

695-
$commitRequest = new CommitRequest();
696-
$commitRequest->setMutations($protoMutations);
697-
$commitRequest->setDatabaseId($options['databaseId']);
698-
$commitRequest->setProjectId($this->projectId);
699-
$commitRequest->setMode($transactionMode);
698+
$commitRequest = (new CommitRequest())
699+
->setMutations($protoMutations)
700+
->setDatabaseId($options['databaseId'])
701+
->setProjectId($this->projectId)
702+
->setMode($transactionMode);
703+
700704
if ($transactionMode === Mode::TRANSACTIONAL) {
701705
$commitRequest->setTransaction(base64_decode($options['transaction']));
702706
}
@@ -793,10 +797,10 @@ public function mutation(
793797
*/
794798
public function rollback($transactionId)
795799
{
796-
$rollbackRequest = new RollbackRequest();
797-
$rollbackRequest->setProjectId($this->projectId);
798-
$rollbackRequest->setDatabaseId($this->databaseId);
799-
$rollbackRequest->setTransaction(base64_decode($transactionId));
800+
$rollbackRequest = (new RollbackRequest())
801+
->setProjectId($this->projectId)
802+
->setDatabaseId($this->databaseId)
803+
->setTransaction(base64_decode($transactionId));
800804

801805
$this->gapicClient->rollback($rollbackRequest);
802806
}
@@ -885,7 +889,7 @@ private function mapEntityResult(array $result, $class)
885889
}
886890

887891
return $this->entity($key, $properties, [
888-
'cursor' => (isset($result['cursor']) && $result['cursor'] !== '')
892+
'cursor' => !empty($result['cursor'])
889893
? $result['cursor']
890894
: null,
891895
'baseVersion' => (isset($result['version']))
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

3+
use DG\BypassFinals;
34
use Google\Cloud\Core\Testing\TestHelpers;
45
use Google\Cloud\Datastore\Tests\System\DatastoreTestCase;
56

67
TestHelpers::requireKeyfile('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH');
78
TestHelpers::systemTestShutdown(function () {
89
DatastoreTestCase::tearDownFixtures();
9-
});
10+
});

Datastore/tests/Unit/DatastoreClientTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
use Google\Cloud\Datastore\V1\LookupRequest;
5050
use Google\Cloud\Datastore\V1\LookupResponse;
5151
use Prophecy\PhpUnit\ProphecyTrait;
52-
use DG\BypassFinals;
5352
use Google\Cloud\Datastore\V1\AllocateIdsRequest;
5453
use Google\Cloud\Datastore\V1\AllocateIdsResponse;
5554
use Google\Cloud\Datastore\V1\BeginTransactionRequest;
@@ -81,10 +80,6 @@ class DatastoreClientTest extends TestCase
8180

8281
public function setUp(): void
8382
{
84-
if (class_exists(BypassFinals::class)) {
85-
BypassFinals::enable(true);
86-
}
87-
8883
$this->gapicClient = $this->prophesize(GapicClient::class);
8984
$this->client = new DatastoreClient([
9085
'projectId' => self::PROJECT,
@@ -323,7 +318,7 @@ public function testTransactionWithOptions()
323318
->getReadWrite()
324319
->getPreviousTransaction();
325320
$this->assertNotEmpty($previousTransaction);
326-
$this->assertEquals($previousTransaction, 'previousId');
321+
$this->assertEquals(base64_decode('previousId'), $previousTransaction);
327322
return true;
328323
}), [])->shouldBeCalled(1)->willReturn(
329324
$response

Datastore/tests/Unit/OperationTest.php

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

1818
namespace Google\Cloud\Datastore\Tests\Unit;
1919

20-
use DG\BypassFinals;
2120
use Google\Cloud\Core\Testing\TestHelpers;
2221
use Google\Cloud\Core\Timestamp;
2322
use Google\Cloud\Datastore\Entity;
@@ -68,9 +67,6 @@ class OperationTest extends TestCase
6867

6968
public function setUp(): void
7069
{
71-
if (class_exists(BypassFinals::class)) {
72-
BypassFinals::enable(true);
73-
}
7470
$this->gapicClient = $this->prophesize(DatastoreClient::class);
7571
$this->operation = TestHelpers::stub(Operation::class, [
7672
$this->gapicClient->reveal(),

Datastore/tests/Unit/ProtoEncodeTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ public static function generateProto(string $message, array $data): Message
3636

3737
return $message;
3838
}
39-
}
39+
}

Datastore/tests/Unit/TransactionTest.php

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

1818
namespace Google\Cloud\Datastore\Tests\Unit;
1919

20-
use DG\BypassFinals;
2120
use Google\Cloud\Core\Testing\DatastoreOperationRefreshTrait;
2221
use Google\Cloud\Core\Testing\TestHelpers;
2322
use Google\Cloud\Core\Timestamp;
@@ -74,9 +73,6 @@ class TransactionTest extends TestCase
7473

7574
public function setUp(): void
7675
{
77-
if (class_exists(BypassFinals::class)) {
78-
BypassFinals::enable(true);
79-
}
8076
$this->gapicClient = $this->prophesize(DatastoreClient::class);
8177

8278
$this->operation = new Operation(

Datastore/tests/Unit/bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use DG\BypassFinals;
4+
5+
BypassFinals::allowPaths([
6+
'*/src/V1/Client/*',
7+
]);
8+
9+
BypassFinals::enable(true);

0 commit comments

Comments
 (0)