From cee1539e8bdd218dcca4f281b4f291783476c69a Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Mon, 1 Apr 2024 06:11:27 +0000 Subject: [PATCH] Update lookup rpc + unit + snippet tests --- .../DatastoreOperationRefreshTrait.php | 2 - Datastore/src/Operation.php | 61 ++++- .../tests/Snippet/DatastoreClientTest.php | 20 +- .../Snippet/DatastoreSessionHandlerTest.php | 48 ++-- .../tests/Snippet/ReadOnlyTransactionTest.php | 29 ++- Datastore/tests/Snippet/TransactionTest.php | 20 +- Datastore/tests/Unit/DatastoreClientTest.php | 118 ++++++---- Datastore/tests/Unit/OperationTest.php | 219 +++++++++++------- Datastore/tests/Unit/TransactionTest.php | 122 ++++++---- 9 files changed, 401 insertions(+), 238 deletions(-) diff --git a/Core/src/Testing/DatastoreOperationRefreshTrait.php b/Core/src/Testing/DatastoreOperationRefreshTrait.php index dc0035a52d7b..dd6793bf5c30 100644 --- a/Core/src/Testing/DatastoreOperationRefreshTrait.php +++ b/Core/src/Testing/DatastoreOperationRefreshTrait.php @@ -141,9 +141,7 @@ private function mockSendRequest($methodName, $params, $returnValue, $shouldBeCa return false; } $data = $serializer->encodeMessage($arg); - // $z = array_replace_recursive($data, $params) == $data; return array_replace_recursive($data, $params) == $data; - // return array_merge($params, $data) == $data; }), Argument::cetera() ); diff --git a/Datastore/src/Operation.php b/Datastore/src/Operation.php index a3ae3a5285a9..80cd0fa30ce1 100644 --- a/Datastore/src/Operation.php +++ b/Datastore/src/Operation.php @@ -31,6 +31,9 @@ use Google\Cloud\Datastore\V1\AllocateIdsRequest; use Google\Cloud\Datastore\V1\BeginTransactionRequest; use Google\Cloud\Datastore\V1\Client\DatastoreClient; +use Google\Cloud\Datastore\V1\Key\PathElement; +use Google\Cloud\Datastore\V1\LookupRequest; +use Google\Cloud\Datastore\V1\PartitionId; use Google\Cloud\Datastore\V1\QueryResultBatch\MoreResultsType; use Google\Cloud\Datastore\V1\TransactionOptions; @@ -453,11 +456,22 @@ public function lookup(array $keys, array $options = []) $serviceKeys[] = $key->keyObject(); }); - $res = $this->connection->lookup($options + $this->readOptions($options) + [ + list($data, $optionalArgs) = $this->splitOptionalArgs($options, ['transaction', 'className', 'sort', 'readTime', 'readConsistency']); + $data += $this->readOptions($options) + [ 'projectId' => $this->projectId, 'databaseId' => $this->databaseId, - 'keys' => $serviceKeys, - ]); + 'keys' => $this->keysList($serviceKeys), + ]; + + $request = $this->serializer->decodeMessage(new LookupRequest(), $data); + $x = $this->serializer->encodeMessage($request); + + $res = $this->requestHandler->sendRequest( + DatastoreClient::class, + 'lookup', + $request, + $optionalArgs + ); $result = []; if (isset($res['found'])) { @@ -929,4 +943,45 @@ private function sortEntities(array $entities, array $keys) return $ret; } + + /** + * Convert a list of keys to a list of {@see Google\Cloud\Datastore\V1\Key}. + * + * @param array[] $keys + * @return Key[] + */ + private function keysList(array $keys) + { + $out = []; + foreach ($keys as $key) { + $local = []; + + if (isset($key['partitionId'])) { + $p = $this->arrayFilterRemoveNull([ + 'project_id' => isset($key['partitionId']['projectId']) + ? $key['partitionId']['projectId'] + : null, + 'namespace_id' => isset($key['partitionId']['namespaceId']) + ? $key['partitionId']['namespaceId'] + : null, + 'database_id' => isset($key['partitionId']['databaseId']) + ? $key['partitionId']['databaseId'] + : null, + ]); + + $local['partition_id'] = new PartitionId($p); + } + + $local['path'] = []; + if (isset($key['path'])) { + foreach ($key['path'] as $element) { + $local['path'][] = new PathElement($element); + } + } + + $out[] = $local; + } + + return $out; + } } diff --git a/Datastore/tests/Snippet/DatastoreClientTest.php b/Datastore/tests/Snippet/DatastoreClientTest.php index 9e61b90fc4ad..7939beb36460 100644 --- a/Datastore/tests/Snippet/DatastoreClientTest.php +++ b/Datastore/tests/Snippet/DatastoreClientTest.php @@ -562,9 +562,10 @@ public function testLookup() $snippet = $this->snippetFromMethod(DatastoreClient::class, 'lookup'); $snippet->addLocal('datastore', $this->client); - $this->connection->lookup(Argument::any()) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'lookup', + [], + [ 'found' => [ [ 'entity' => [ @@ -581,7 +582,8 @@ public function testLookup() ] ] ] - ]); + ] + ); $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -602,9 +604,10 @@ public function testLookupBatch() $snippet = $this->snippetFromMethod(DatastoreClient::class, 'lookupBatch'); $snippet->addLocal('datastore', $this->client); - $this->connection->lookup(Argument::any()) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'lookup', + [], + [ 'found' => [ [ 'entity' => [ @@ -635,7 +638,8 @@ public function testLookupBatch() ] ] ] - ]); + ] + ); $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT diff --git a/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php b/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php index ada15639d968..b00886e949c1 100644 --- a/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php +++ b/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php @@ -72,14 +72,22 @@ public function testClass() $snippet = $this->snippetFromClass(DatastoreSessionHandler::class); $snippet->replace('$datastore = new DatastoreClient();', ''); - $this->connection->lookup(Argument::allOf( - Argument::withEntry('transaction', self::TRANSACTION), - Argument::that(function ($args) { - return $args['keys'][0]['partitionId']['namespaceId'] === 'sessions' - && $args['keys'][0]['path'][0]['kind'] === 'PHPSESSID' - && isset($args['keys'][0]['path'][0]['name']); - }) - ))->shouldBeCalled()->willReturn([]); + $this->mockSendRequest( + 'lookup', + [ + 'keys' => [ + [ + 'partitionId' => ['namespaceId' => 'sessions'], + 'path' => [ + ['kind' => 'PHPSESSID'] + ] + ] + ], + 'readOptions' => ['transaction' => self::TRANSACTION] + ], + [], + 0 + ); $this->mockSendRequest( 'beginTransaction', @@ -119,14 +127,22 @@ public function testClassErrorHandler() $snippet = $this->snippetFromClass(DatastoreSessionHandler::class, 1); $snippet->replace('$datastore = new DatastoreClient();', ''); - $this->connection->lookup(Argument::allOf( - Argument::withEntry('transaction', self::TRANSACTION), - Argument::that(function ($args) { - return $args['keys'][0]['partitionId']['namespaceId'] === 'sessions' - && $args['keys'][0]['path'][0]['kind'] === 'PHPSESSID' - && isset($args['keys'][0]['path'][0]['name']); - }) - ))->shouldBeCalled()->willReturn([]); + $this->mockSendRequest( + 'lookup', + [ + 'keys' => [ + [ + 'partitionId' => ['namespaceId' => 'sessions'], + 'path' => [ + ['kind' => 'PHPSESSID'] + ] + ] + ], + 'readOptions' => ['transaction' => self::TRANSACTION] + ], + [], + 0 + ); $this->mockSendRequest( 'beginTransaction', diff --git a/Datastore/tests/Snippet/ReadOnlyTransactionTest.php b/Datastore/tests/Snippet/ReadOnlyTransactionTest.php index b4bd041eae12..a6a5e0c458f3 100644 --- a/Datastore/tests/Snippet/ReadOnlyTransactionTest.php +++ b/Datastore/tests/Snippet/ReadOnlyTransactionTest.php @@ -131,9 +131,12 @@ public function testClassRollback() ['transaction' => 'foo'], 0 ); - $this->connection->lookup(Argument::any()) - ->shouldBeCalled() - ->willReturn([]); + $this->mockSendRequest( + 'lookup', + [], + [], + 0 + ); $this->connection->rollback(Argument::any()) ->shouldBeCalled(); @@ -157,9 +160,10 @@ public function testLookup() $snippet->addLocal('datastore', $this->client); $snippet->addLocal('transaction', $this->transaction); - $this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION)) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['transaction' => self::TRANSACTION]], + [ 'found' => [ [ 'entity' => [ @@ -176,7 +180,8 @@ public function testLookup() ] ] ] - ]); + ] + ); $this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -192,9 +197,10 @@ public function testLookupBatch() $snippet->addLocal('datastore', $this->client); $snippet->addLocal('transaction', $this->transaction); - $this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION)) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['transaction' => self::TRANSACTION]], + [ 'found' => [ [ 'entity' => [ @@ -225,7 +231,8 @@ public function testLookupBatch() ] ] ] - ]); + ] + ); $this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT diff --git a/Datastore/tests/Snippet/TransactionTest.php b/Datastore/tests/Snippet/TransactionTest.php index 3f00669260a5..bdaa68fb709d 100644 --- a/Datastore/tests/Snippet/TransactionTest.php +++ b/Datastore/tests/Snippet/TransactionTest.php @@ -320,9 +320,10 @@ public function testLookup() $snippet->addLocal('datastore', $this->client); $snippet->addLocal('transaction', $this->transaction); - $this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION)) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['transaction' => self::TRANSACTION]], + [ 'found' => [ [ 'entity' => [ @@ -339,7 +340,8 @@ public function testLookup() ] ] ] - ]); + ] + ); $this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -355,9 +357,10 @@ public function testLookupBatch() $snippet->addLocal('datastore', $this->client); $snippet->addLocal('transaction', $this->transaction); - $this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION)) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['transaction' => self::TRANSACTION]], + [ 'found' => [ [ 'entity' => [ @@ -388,7 +391,8 @@ public function testLookupBatch() ] ] ] - ]); + ] + ); $this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT diff --git a/Datastore/tests/Unit/DatastoreClientTest.php b/Datastore/tests/Unit/DatastoreClientTest.php index eb3d55bc3786..82170cc6a34a 100644 --- a/Datastore/tests/Unit/DatastoreClientTest.php +++ b/Datastore/tests/Unit/DatastoreClientTest.php @@ -248,11 +248,11 @@ public function allocateIdProvider() */ public function testTransaction($method, $type, $key) { - $this->mockSendRequest( + $this->mockSendRequest( 'beginTransaction', [ 'projectId' => self::PROJECT, - 'transactionOptions' => ['readTime' => []] + 'transactionOptions' => [($method == 'transaction' ? 'readWrite' : 'readOnly') => []] ], ['transaction' => self::TRANSACTION] ); @@ -260,7 +260,6 @@ public function testTransaction($method, $type, $key) $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT ]); - $res = $this->client->$method(); $this->assertInstanceOf($type, $res); } @@ -486,15 +485,18 @@ public function testLookup() { $key = $this->client->key('Person', 'John'); - $this->connection->lookup( - Argument::withEntry('keys', [$key->keyObject()]) - )->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($key) + $this->mockSendRequest( + 'lookup', + ['keys' => [$key->keyObject()]], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($key) + ] ] - ] - ]); + ], + 0 + ); $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -510,15 +512,18 @@ public function testLookupMissing() { $key = $this->client->key('Person', 'John'); - $this->connection->lookup( - Argument::withEntry('keys', [$key->keyObject()]) - )->shouldBeCalled()->willReturn([ - 'missing' => [ - [ - 'entity' => $this->entityArray($key) + $this->mockSendRequest( + 'lookup', + ['keys' => [$key->keyObject()]], + [ + 'missing' => [ + [ + 'entity' => $this->entityArray($key) + ] ] - ] - ]); + ], + 0 + ); $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -533,23 +538,26 @@ public function testLookupBatch() { $key = $this->client->key('Person', 'John'); - $this->connection->lookup( - Argument::withEntry('keys', [$key->keyObject()]) - )->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($key) - ] - ], - 'missing' => [ - [ - 'entity' => $this->entityArray($key) + $this->mockSendRequest( + 'lookup', + ['keys' => [$key->keyObject()]], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($key) + ] + ], + 'missing' => [ + [ + 'entity' => $this->entityArray($key) + ] + ], + 'deferred' => [ + $key->keyObject() ] ], - 'deferred' => [ - $key->keyObject() - ] - ]); + 0 + ); $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -567,15 +575,18 @@ public function testLookupBatchWithReadTime() $key = $this->client->key('Person', 'John'); $time = new Timestamp(new \DateTime()); - $this->connection->lookup( - Argument::withEntry('readTime', $time) - )->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($key) + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['readTime' => $time]], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($key) + ] ] - ] - ]); + ], + 0 + ); $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -590,16 +601,21 @@ public function testLookupWithReadTime() $key = $this->client->key('Person', 'John'); $time = new Timestamp(new \DateTime()); - $this->connection->lookup(Argument::allOf( - Argument::withEntry('keys', [$key->keyObject()]), - Argument::withEntry('readTime', $time) - ))->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($key) + $this->mockSendRequest( + 'lookup', + [ + 'keys' => [$key->keyObject()], + 'readOptions' => ['readTime' => $time] + ], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($key) + ] ] - ] - ]); + ], + 0 + ); $this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT diff --git a/Datastore/tests/Unit/OperationTest.php b/Datastore/tests/Unit/OperationTest.php index 9782dcc59af5..9de9dae9f782 100644 --- a/Datastore/tests/Unit/OperationTest.php +++ b/Datastore/tests/Unit/OperationTest.php @@ -31,6 +31,7 @@ use Google\Cloud\Datastore\Query\GqlQuery; use Google\Cloud\Datastore\Query\Query; use Google\Cloud\Datastore\Query\QueryInterface; +use Google\Cloud\Datastore\V1\LookupRequest; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Prophecy\Argument; @@ -281,9 +282,12 @@ public function testLookup() { $key = $this->operation->key('foo', 'Bar'); - $this->connection->lookup(Argument::type('array')) - ->shouldBeCalled() - ->willReturn([]); + $this->mockSendRequest( + 'lookup', + [], + [], + 0 + ); $this->operation->___setProperty('connection', $this->connection->reveal()); @@ -295,9 +299,11 @@ public function testLookup() public function testLookupFound() { $body = json_decode(file_get_contents(Fixtures::ENTITY_BATCH_LOOKUP_FIXTURE()), true); - $this->connection->lookup(Argument::any())->willReturn([ - 'found' => $body, - ]); + $this->mockSendRequest( + 'lookup', + [], + ['found' => $body,] + ); $this->operation->___setProperty('connection', $this->connection->reveal()); @@ -317,9 +323,12 @@ public function testLookupFound() public function testLookupMissing() { $body = json_decode(file_get_contents(Fixtures::ENTITY_BATCH_LOOKUP_FIXTURE()), true); - $this->connection->lookup(Argument::any())->willReturn([ - 'missing' => $body, - ]); + + $this->mockSendRequest( + 'lookup', + [], + ['missing' => $body,] + ); $this->operation->___setProperty('connection', $this->connection->reveal()); @@ -337,9 +346,12 @@ public function testLookupMissing() public function testLookupDeferred() { $body = json_decode(file_get_contents(Fixtures::ENTITY_BATCH_LOOKUP_FIXTURE()), true); - $this->connection->lookup(Argument::any())->willReturn([ - 'deferred' => [$body[0]['entity']['key']], - ]); + + $this->mockSendRequest( + 'lookup', + [], + ['deferred' => [$body[0]['entity']['key']]] + ); $this->operation->___setProperty('connection', $this->connection->reveal()); @@ -354,9 +366,13 @@ public function testLookupDeferred() public function testLookupWithReadOptionsFromTransaction() { - $this->connection->lookup(Argument::withKey('readOptions'))->shouldBeCalled()->willReturn([]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['transaction' => 'foo']], + [], + 0 + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $k = new Key('test-project', [ 'path' => [['kind' => 'kind', 'id' => '123']], @@ -367,24 +383,32 @@ public function testLookupWithReadOptionsFromTransaction() public function testLookupWithReadOptionsFromReadConsistency() { - $this->connection->lookup(Argument::withKey('readOptions'))->shouldBeCalled()->willReturn([]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['readConsistency' => 123]], + [], + 0 + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $k = new Key('test-project', [ 'path' => [['kind' => 'kind', 'id' => '123']], ]); - $this->operation->lookup([$k], ['readConsistency' => 'foo']); + $this->operation->lookup([$k], ['readConsistency' => 123]); } public function testLookupWithoutReadOptions() { - $this->connection->lookup(Argument::that(function ($args) { - return !isset($args['readOptions']); - }))->shouldBeCalled()->willReturn([]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->requestHandler->sendRequest( + Argument::any(), + Argument::any(), + Argument::that(function (LookupRequest $arg) { + return !$arg->hasReadOptions(); + }), + Argument::cetera() + )->shouldBeCalled()->willReturn([]); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $k = new Key('test-project', [ 'path' => [['kind' => 'kind', 'id' => '123']], @@ -404,11 +428,12 @@ public function testLookupWithSort() ]); } - $this->connection->lookup(Argument::any())->willReturn([ - 'found' => $data['entities'], - ]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + [], + ['found' => $data['entities']] + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $res = $this->operation->lookup($keys, [ 'sort' => true, @@ -432,12 +457,15 @@ public function testLookupWithoutSort() ]); } - $this->connection->lookup(Argument::any())->willReturn([ - 'found' => $data['entities'], - 'missing' => $data['missing'], - ]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + [], + [ + 'found' => $data['entities'], + 'missing' => $data['missing'], + ] + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $res = $this->operation->lookup($keys); @@ -464,11 +492,12 @@ public function testLookupWithSortAndMissingKey() ]); } - $this->connection->lookup(Argument::any())->willReturn([ - 'found' => $data['entities'], - ]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + [], + ['found' => $data['entities'],] + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $res = $this->operation->lookup($keys, [ 'sort' => true, @@ -854,12 +883,12 @@ public function testMapEntityResult() { $res = json_decode(file_get_contents(Fixtures::ENTITY_RESULT_FIXTURE()), true); - $this->connection->lookup(Argument::type('array')) - ->willReturn([ - 'found' => $res, - ]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + [], + ['found' => $res,] + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $key = $this->operation->key('Person', 12345); @@ -875,12 +904,12 @@ public function testMapEntityResultWithoutProperties() { $res = json_decode(file_get_contents(Fixtures::ENTITY_RESULT_NO_PROPERTIES_FIXTURE()), true); - $this->connection->lookup(Argument::type('array')) - ->willReturn([ - 'found' => $res, - ]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + [], + ['found' => $res,] + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $key = $this->operation->key('Person', 12345); @@ -895,12 +924,12 @@ public function testMapEntityResultArrayOfClassNames() { $res = json_decode(file_get_contents(Fixtures::ENTITY_RESULT_FIXTURE()), true); - $this->connection->lookup(Argument::type('array')) - ->willReturn([ - 'found' => $res, - ]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + [], + ['found' => $res,] + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $key = $this->operation->key('Person', 12345); @@ -919,12 +948,12 @@ public function testMapEntityResultArrayOfClassNamesMissingKindMapItem() $res = json_decode(file_get_contents(Fixtures::ENTITY_RESULT_FIXTURE()), true); - $this->connection->lookup(Argument::type('array')) - ->willReturn([ - 'found' => $res, - ]); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + [], + ['found' => $res,] + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $key = $this->operation->key('Person', 12345); @@ -937,13 +966,13 @@ public function testMapEntityResultArrayOfClassNamesMissingKindMapItem() public function testTransactionInReadOptions() { - $this->connection->lookup(Argument::that(function ($arg) { - return isset($arg['readOptions']['transaction']); - })) - ->willReturn([]) - ->shouldBeCalled(); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['transaction' => '1234']], + [], + 0 + ); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $key = $this->operation->key('Person', 12345); $this->operation->lookup([$key], [ @@ -953,13 +982,15 @@ public function testTransactionInReadOptions() public function testNonTransactionalReadOptions() { - $this->connection->lookup(Argument::that(function ($arg) { - return !isset($arg['readOptions']['transaction']); - })) - ->willReturn([]) - ->shouldBeCalled(); - - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->requestHandler->sendRequest( + Argument::any(), + Argument::any(), + Argument::that(function (LookupRequest $arg) { + return (!$arg->hasReadOptions()) || (!$arg->getReadOptions()->hasTransaction()); + }), + Argument::cetera() + )->shouldBeCalled()->willReturn([]); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $key = $this->operation->key('Person', 12345); $this->operation->lookup([$key]); @@ -967,15 +998,18 @@ public function testNonTransactionalReadOptions() public function testReadConsistencyInReadOptions() { - $this->connection->lookup(Argument::withEntry('readOptions', ['readConsistency' => 'test'])) - ->willReturn([]) - ->shouldBeCalled(); + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['readConsistency' => 123]], + [], + 0 + ); - $this->operation->___setProperty('connection', $this->connection->reveal()); + $this->operation->___setProperty('requestHandler', $this->requestHandler->reveal()); $key = $this->operation->key('Person', 12345); $this->operation->lookup([$key], [ - 'readConsistency' => 'test', + 'readConsistency' => 123, ]); } @@ -1021,12 +1055,19 @@ public function testAllocateIdsWithDatabaseIdOverride() public function testLookupWithDatabaseIdOverride() { - $this->connection - ->lookup( - Argument::withEntry('databaseId', 'otherDatabaseId') - ) - ->shouldBeCalledTimes(1) - ->willReturn([]); + // $this->connection + // ->lookup( + // Argument::withEntry('databaseId', 'otherDatabaseId') + // ) + // ->shouldBeCalledTimes(1) + // ->willReturn([]); + + $this->mockSendRequest( + 'lookup', + ['databaseId' => 'otherDatabaseId'], + [], + 1 + ); $this->operation->lookup( [], diff --git a/Datastore/tests/Unit/TransactionTest.php b/Datastore/tests/Unit/TransactionTest.php index fc3a55412df1..2d4646f1cb55 100644 --- a/Datastore/tests/Unit/TransactionTest.php +++ b/Datastore/tests/Unit/TransactionTest.php @@ -110,16 +110,21 @@ public function setUp(): void */ public function testLookup(callable $transaction) { - $this->connection->lookup(Argument::allOf( - Argument::withEntry('transaction', self::TRANSACTION), - Argument::withEntry('keys', [$this->key->keyObject()]) - ))->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($this->key) + $this->mockSendRequest( + 'lookup', + [ + 'readOptions' => ['transaction' => self::TRANSACTION], + 'keys' => [$this->key->keyObject()], + ], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($this->key) + ] ] - ] - ]); + ], + 0 + ); $transaction = $transaction(); $this->refreshOperation($transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ @@ -134,17 +139,25 @@ public function testLookup(callable $transaction) public function testLookupWithReadTime() { $time = new Timestamp(new \DateTime()); - $this->connection->lookup(Argument::allOf( - Argument::withEntry('transaction', self::TRANSACTION), - Argument::withEntry('keys', [$this->key->keyObject()]), - Argument::withEntry('readTime', $time) - ))->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($this->key) + + $this->mockSendRequest( + 'lookup', + [ + 'readOptions' => [ + 'readTime' => $time, + 'transaction' => self::TRANSACTION, + ], + 'keys' => [$this->key->keyObject()], + ], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($this->key) + ] ] - ] - ]); + ], + 0 + ); $transaction = $this->readOnly; $this->refreshOperation($transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ @@ -160,15 +173,18 @@ public function testLookupWithReadTime() */ public function testLookupMissing(callable $transaction) { - $this->connection->lookup( - Argument::withEntry('keys', [$this->key->keyObject()]) - )->shouldBeCalled()->willReturn([ - 'missing' => [ - [ - 'entity' => $this->entityArray($this->key) + $this->mockSendRequest( + 'lookup', + ['keys' => [$this->key->keyObject()]], + [ + 'missing' => [ + [ + 'entity' => $this->entityArray($this->key) + ] ] - ] - ]); + ], + 0 + ); $transaction = $transaction(); $this->refreshOperation($transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ @@ -184,23 +200,26 @@ public function testLookupMissing(callable $transaction) */ public function testLookupBatch(callable $transaction) { - $this->connection->lookup( - Argument::withEntry('keys', [$this->key->keyObject()]) - )->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($this->key) - ] - ], - 'missing' => [ - [ - 'entity' => $this->entityArray($this->key) + $this->mockSendRequest( + 'lookup', + ['keys' => [$this->key->keyObject()]], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($this->key) + ] + ], + 'missing' => [ + [ + 'entity' => $this->entityArray($this->key) + ] + ], + 'deferred' => [ + $this->key->keyObject() ] ], - 'deferred' => [ - $this->key->keyObject() - ] - ]); + 0 + ); $transaction = $transaction(); $this->refreshOperation($transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [ @@ -219,15 +238,18 @@ public function testLookupBatch(callable $transaction) public function testLookupBatchWithReadTime(callable $transaction) { $time = new Timestamp(new \DateTime()); - $this->connection->lookup( - Argument::withEntry('readTime', $time) - )->shouldBeCalled()->willReturn([ - 'found' => [ - [ - 'entity' => $this->entityArray($this->key) + $this->mockSendRequest( + 'lookup', + ['readOptions' => ['readTime' => $time]], + [ + 'found' => [ + [ + 'entity' => $this->entityArray($this->key) + ] ] - ] - ]); + ], + 0 + ); $transaction = $transaction(); $this->refreshOperation($transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [