From 2e9a749e27c1291078227b187d491fbfd4c5abd4 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Thu, 9 May 2024 08:32:12 +0000 Subject: [PATCH] Refactor Snippet::DatastoreSessionHandlerTest --- .../Snippet/DatastoreSessionHandlerTest.php | 117 +++++++++--------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php b/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php index a2237f9ae91b..23dc83cc66ec 100644 --- a/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php +++ b/Datastore/tests/Snippet/DatastoreSessionHandlerTest.php @@ -26,6 +26,7 @@ use Google\Cloud\Datastore\DatastoreSessionHandler; use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient; use Google\Cloud\Datastore\V1\CommitRequest\Mode; +use Google\Cloud\Datastore\V1\TransactionOptions; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -66,50 +67,53 @@ public function setUp(): void $this->requestHandler = $this->prophesize(RequestHandler::class); } + /** @group current */ public function testClass() { $snippet = $this->snippetFromClass(DatastoreSessionHandler::class); $snippet->replace('$datastore = new DatastoreClient();', ''); - $this->mockSendRequest( + $this->requestHandler->sendRequest( + V1DatastoreClient::class, 'lookup', - [ - 'keys' => [ - [ - 'partitionId' => ['namespaceId' => 'sessions'], - 'path' => [ - ['kind' => 'PHPSESSID'] - ] - ] - ], - 'readOptions' => ['transaction' => self::TRANSACTION] - ], - [], - 0 - ); - - $this->mockSendRequest( + Argument::that(function ($req) { + $data = $this->getSerializer()->encodeMessage($req); + + return isset($data['readOptions']['transaction']) + && $data['keys'][0]['partitionId']['namespaceId'] === 'sessions' + && $data['keys'][0]['path'][0]['kind'] === 'PHPSESSID' + && isset($data['keys'][0]['path'][0]['name']); + }), + Argument::cetera() + )->shouldBeCalled()->willReturn(); + + $this->requestHandler->sendRequest( + V1DatastoreClient::class, 'beginTransaction', - ['transactionOptions' => ['readWrite' => []]], - ['transaction' => self::TRANSACTION] - ); + Argument::that(function ($req) { + $data = $this->getSerializer()->encodeMessage($req); + return isset($data['transactionOptions']['readWrite']); + }), + Argument::cetera() + )->shouldBeCalled() + ->willReturn(['transaction' => self::TRANSACTION]); - $this->mockSendRequest( + $this->requestHandler->sendRequest( + V1DatastoreClient::class, 'commit', - [ - 'transaction' => self::TRANSACTION, - 'mode' => Mode::TRANSACTIONAL, - 'mutations' => [['upsert' => [ - 'key' => [ - 'path' => [['kind' => 'PHPSESSID']], - 'partitionId' => ['namespaceId' => 'sessions'] - ], - 'properties' => ['data' => ['stringValue' => 'name|'.serialize('Bob')]] - ]]] - ], - [], - 0 - ); + Argument::that(function ($req) { + $data = $this->getSerializer()->encodeMessage($req); + + return isset($data['transaction']) + && isset($data['mode']) + && $data['mutations'][0]['upsert']['key']['partitionId']['namespaceId'] === 'sessions' + && $data['mutations'][0]['upsert']['key']['path'][0]['kind'] === 'PHPSESSID' + && isset($data['mutations'][0]['upsert']['key']['path'][0]['name']) + && $data['mutations'][0]['upsert']['properties']['data']['stringValue'] === 'name|'.serialize('Bob') + && isset($data['mutations'][0]['upsert']['properties']['t']); + }), + Argument::cetera() + )->shouldBeCalled()->willReturn([]); $this->refreshOperation($this->client, $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT @@ -129,28 +133,30 @@ public function testClassErrorHandler() $snippet = $this->snippetFromClass(DatastoreSessionHandler::class, 1); $snippet->replace('$datastore = new DatastoreClient();', ''); - $this->mockSendRequest( + $this->requestHandler->sendRequest( + V1DatastoreClient::class, 'lookup', - [ - 'keys' => [ - [ - 'partitionId' => ['namespaceId' => 'sessions'], - 'path' => [ - ['kind' => 'PHPSESSID'] - ] - ] - ], - 'readOptions' => ['transaction' => self::TRANSACTION] - ], - [], - 0 - ); - - $this->mockSendRequest( + Argument::that(function ($req) { + $data = $this->getSerializer()->encodeMessage($req); + + return isset($data['readOptions']['transaction']) + && $data['keys'][0]['partitionId']['namespaceId'] === 'sessions' + && $data['keys'][0]['path'][0]['kind'] === 'PHPSESSID' + && isset($data['keys'][0]['path'][0]['name']); + }), + Argument::cetera() + )->shouldBeCalled()->willReturn(); + + $this->requestHandler->sendRequest( + V1DatastoreClient::class, 'beginTransaction', - ['transactionOptions' => ['readWrite' => []]], - ['transaction' => self::TRANSACTION] - ); + Argument::that(function ($req) { + $data = $this->getSerializer()->encodeMessage($req); + return isset($data['transactionOptions']['readWrite']); + }), + Argument::cetera() + )->shouldBeCalled() + ->willReturn(['transaction' => self::TRANSACTION]); $this->requestHandler->sendRequest( V1DatastoreClient::class, @@ -158,7 +164,6 @@ public function testClassErrorHandler() Argument::cetera() )->shouldBeCalled()->will(fn () => trigger_error('oops!', E_USER_WARNING)); - $this->refreshOperation($this->client, $this->requestHandler->reveal(), [ 'projectId' => self::PROJECT ]);