Skip to content

Commit

Permalink
Refactor Snippet::DatastoreSessionHandlerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed May 9, 2024
1 parent 8f5db18 commit 2e9a749
Showing 1 changed file with 61 additions and 56 deletions.
117 changes: 61 additions & 56 deletions Datastore/tests/Snippet/DatastoreSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -129,36 +133,37 @@ 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,
'commit',
Argument::cetera()
)->shouldBeCalled()->will(fn () => trigger_error('oops!', E_USER_WARNING));


$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
]);
Expand Down

0 comments on commit 2e9a749

Please sign in to comment.