Skip to content

Commit

Permalink
Refactor SnippetTest::DatastoreClientTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed May 9, 2024
1 parent 38a9e7d commit 8f5db18
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 48 deletions.
13 changes: 11 additions & 2 deletions Core/src/Testing/DatastoreOperationRefreshTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
trait DatastoreOperationRefreshTrait
{
private static Serializer $_serializer;

/**
* Refresh the operation property of a given stubbed class.
*
Expand Down Expand Up @@ -120,13 +122,20 @@ private function mockSendRequest($methodName, $params, $returnValue, $shouldBeCa
$prophecy->willReturn($returnValue);
}

/**
* @return Serializer
*/
private function getSerializer()
{
if (isset(self::$_serializer)) {
return self::$_serializer;
}

if (isset($this->serializer)) {
return $this->serializer;
return self::$_serializer = $this->serializer;
}

return new Serializer([], [
return self::$_serializer = new Serializer([], [
'google.protobuf.Value' => function ($v) {
return $this->flattenValue($v);
},
Expand Down
114 changes: 68 additions & 46 deletions Datastore/tests/Snippet/DatastoreClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
use Google\Cloud\Datastore\Query\QueryInterface;
use Google\Cloud\Datastore\ReadOnlyTransaction;
use Google\Cloud\Datastore\Transaction;
use Google\Cloud\Datastore\V1\BeginTransactionRequest;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\LookupRequest;
use Google\Cloud\Datastore\V1\RunAggregationQueryRequest;
use Google\Cloud\Datastore\V1\RunQueryRequest;
use Google\Cloud\Datastore\V1\TransactionOptions;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

Expand Down Expand Up @@ -346,16 +351,21 @@ public function testAllocateIdsBatch()
$this->assertEquals(Key::STATE_NAMED, $res->returnVal()[0]->state());
}

/** @group current */
public function testTransaction()
{
$snippet = $this->snippetFromMethod(DatastoreClient::class, 'transaction');
$snippet->addLocal('datastore', $this->client);

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'beginTransaction',
['transactionOptions' => ['readWrite' => []]],
['transaction' => 'foo']
);
Argument::that(function ($req) {
$data = $this->getSerializer()->encodeMessage($req);
return isset($data['transactionOptions']['readWrite']);
}),
Argument::cetera()
)->willReturn(['transaction' => 'foo'])->shouldBeCalled();

$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -369,11 +379,17 @@ public function testReadOnlyTransaction()
{
$snippet = $this->snippetFromMethod(DatastoreClient::class, 'readOnlyTransaction');
$snippet->addLocal('datastore', $this->client);
$this->mockSendRequest(

$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'beginTransaction',
['transactionOptions' => ['readOnly' => []]],
['transaction' => 'foo']
);
Argument::that(function ($req) {
$data = $this->getSerializer()->encodeMessage($req);
return isset($data['transactionOptions']['readOnly']);
}),
Argument::cetera()
)->willReturn(['transaction' => 'foo'])->shouldBeCalled();

$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
]);
Expand Down Expand Up @@ -533,9 +549,12 @@ public function testLookup()
$snippet = $this->snippetFromMethod(DatastoreClient::class, 'lookup');
$snippet->addLocal('datastore', $this->client);

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'lookup',
[],
Argument::type(LookupRequest::class),
Argument::cetera()
)->willReturn(
[
'found' => [
[
Expand All @@ -554,7 +573,7 @@ public function testLookup()
]
]
]
);
)->shouldBeCalled();

$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -575,9 +594,12 @@ public function testLookupBatch()
$snippet = $this->snippetFromMethod(DatastoreClient::class, 'lookupBatch');
$snippet->addLocal('datastore', $this->client);

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'lookup',
[],
Argument::type(LookupRequest::class),
Argument::cetera()
)->willReturn(
[
'found' => [
[
Expand Down Expand Up @@ -610,7 +632,7 @@ public function testLookupBatch()
]
]
]
);
)->shouldBeCalled();

$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down Expand Up @@ -668,9 +690,12 @@ public function testRunQuery()
$query->queryKey()->willReturn('query');
$snippet->addLocal('query', $query->reveal());

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'runQuery',
[],
Argument::type(RunQueryRequest::class),
Argument::cetera()
)->willReturn(
[
'batch' => [
'entityResults' => [
Expand All @@ -690,9 +715,8 @@ public function testRunQuery()
]
]
]
],
0
);
]
)->shouldBeCalled();

$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -718,9 +742,12 @@ public function testRunAggregationQuery()
$query->queryObject()->willReturn([]);
$snippet->addLocal('query', $query->reveal());

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'runAggregationQuery',
[],
Argument::type(RunAggregationQueryRequest::class),
Argument::cetera()
)->willReturn(
[
'batch' => [
'aggregationResults' => [
Expand All @@ -732,9 +759,8 @@ public function testRunAggregationQuery()
],
'readTime' => (new \DateTime)->format('Y-m-d\TH:i:s') .'.000001Z'
]
],
0
);
]
)->shouldBeCalled();

$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down Expand Up @@ -781,31 +807,27 @@ private function allocateIdsRequestHandlerMock()
]);
}

private function validateTransactionOptions($type, array $options = [])
private function validateTransactionOptions($data, $type, array $options = [])
{
return Argument::that(function ($args) use ($type, $options) {
if (!isset($args['transactionOptions'])) {
echo 'missing opts';
return false;
}
if (!array_key_exists($type, $args['transactionOptions'])) {
echo 'missing key';
return false;
}
if (!isset($data['transactionOptions'])) {
echo 'missing opts';
return false;
}
if (!array_key_exists($type, $data['transactionOptions'])) {
echo 'missing key';
return false;
}

if (!empty((array) $options)) {
return $options === $args['transactionOptions'][$type];
} else {
if (is_array($args['transactionOptions'][$type]) and
isset($args['transactionOptions'][$type]['readTime'])) {
return true;
}
return is_object($args['transactionOptions'][$type])
&& empty((array) $args['transactionOptions'][$type]);
if (!empty((array) $options)) {
return $options === $data['transactionOptions'][$type];
} else {
if (is_array($data['transactionOptions'][$type]) and
isset($data['transactionOptions'][$type]['readTime'])) {
return true;
}

return true;
});
return is_object($data['transactionOptions'][$type])
&& empty((array) $data['transactionOptions'][$type]);
}
}

private function mockSendRequestForCommit($mutationType, $returnValue)
Expand Down

0 comments on commit 8f5db18

Please sign in to comment.