Skip to content

Commit

Permalink
Refactor all the remaining unit and snippets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed May 10, 2024
1 parent c0e2644 commit 87ff887
Show file tree
Hide file tree
Showing 17 changed files with 668 additions and 1,254 deletions.
47 changes: 0 additions & 47 deletions Core/src/Testing/DatastoreOperationRefreshTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,53 +75,6 @@ public function refreshOperation($stub, RequestHandler $requestHandler, array $o
return $stub;
}

/**
* Helper method for Unit and Snippet test classes. This mocks the
* $requestHandler class property present in the Test Class with
* given arguments.
*
* @param string $methodName The method name to mock in RequestHandler::sendRequest
* @param array<string, mixed> $params The parameters to look for in the
* array equivalent of rpc request.
* @param mixed $returnValue The value to be returned by sendRequest mock.
* @param null|int $shouldBeCalledTimes Adds a shouldBeCalled prophecy. Defaults to `null`, implying nothing is added.
* [
* `0` => `shouldBeCalled`,
* Non zero positive integer $x => `shouldBeCalledTimes($x)`
* ]
*/
private function mockSendRequest($methodName, $params, $returnValue, $shouldBeCalledTimes = null)
{
$serializer = $this->getSerializer();

$prophecy = $this->requestHandler->sendRequest(
DatastoreClient::class,
$methodName,
Argument::that(function ($arg) use ($methodName, $params, $serializer) {
$requestName = ucfirst($methodName . 'Request');
$x = explode('\\', get_class($arg));
$argName = end($x);

if ($requestName != $argName) {
return false;
}
$data = $serializer->encodeMessage($arg);
return array_replace_recursive($data, $params) == $data;
}),
Argument::cetera()
);

if (!is_null($shouldBeCalledTimes)) {
if ($shouldBeCalledTimes == 0) {
$prophecy->shouldBeCalled();
} else {
$prophecy->shouldBeCalledTimes($shouldBeCalledTimes);
}
}

$prophecy->willReturn($returnValue);
}

/**
* @return Serializer
*/
Expand Down
2 changes: 1 addition & 1 deletion Datastore/tests/Snippet/DatastoreClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ private function validateTransactionOptions($data, $type, array $options = [])
}

if (!empty((array) $options)) {
return $options === $data['transactionOptions'][$type];
return $options == $data['transactionOptions'][$type];
} else {
if (is_array($data['transactionOptions'][$type]) and
isset($data['transactionOptions'][$type]['readTime'])) {
Expand Down
14 changes: 7 additions & 7 deletions Datastore/tests/Snippet/DatastoreSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function testClass()
$data = $this->getSerializer()->encodeMessage($req);

return isset($data['readOptions']['transaction'])
&& $data['keys'][0]['partitionId']['namespaceId'] === 'sessions'
&& $data['keys'][0]['path'][0]['kind'] === 'PHPSESSID'
&& $data['keys'][0]['partitionId']['namespaceId'] == 'sessions'
&& $data['keys'][0]['path'][0]['kind'] == 'PHPSESSID'
&& isset($data['keys'][0]['path'][0]['name']);
}),
Argument::cetera()
Expand All @@ -106,10 +106,10 @@ public function testClass()

return isset($data['transaction'])
&& isset($data['mode'])
&& $data['mutations'][0]['upsert']['key']['partitionId']['namespaceId'] === 'sessions'
&& $data['mutations'][0]['upsert']['key']['path'][0]['kind'] === 'PHPSESSID'
&& $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')
&& $data['mutations'][0]['upsert']['properties']['data']['stringValue'] == 'name|'.serialize('Bob')
&& isset($data['mutations'][0]['upsert']['properties']['t']);
}),
Argument::cetera()
Expand Down Expand Up @@ -140,8 +140,8 @@ public function testClassErrorHandler()
$data = $this->getSerializer()->encodeMessage($req);

return isset($data['readOptions']['transaction'])
&& $data['keys'][0]['partitionId']['namespaceId'] === 'sessions'
&& $data['keys'][0]['path'][0]['kind'] === 'PHPSESSID'
&& $data['keys'][0]['partitionId']['namespaceId'] == 'sessions'
&& $data['keys'][0]['path'][0]['kind'] == 'PHPSESSID'
&& isset($data['keys'][0]['path'][0]['name']);
}),
Argument::cetera()
Expand Down
20 changes: 1 addition & 19 deletions Datastore/tests/Snippet/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class EntityTest extends SnippetTestCase
private $options;
private $entity;
private $key;
private $serializer;
private $requestHandler;

public function setUp(): void
Expand All @@ -68,23 +67,6 @@ public function setUp(): void
$this->entity = new Entity($this->key, [], $this->options);

$this->requestHandler = $this->prophesize(RequestHandler::class);

$this->serializer = new Serializer([], [
'google.protobuf.Value' => function ($v) {
return $this->flattenValue($v);
},
'google.protobuf.Timestamp' => function ($v) {
return $this->formatTimestampFromApi($v);
}
], [], [
'google.protobuf.Timestamp' => function ($v) {
if (is_string($v)) {
$dt = new \DateTime($v);
return ['seconds' => $dt->format('U')];
}
return $v;
}
]);
}

public function testClass()
Expand Down Expand Up @@ -141,7 +123,7 @@ public function testClassEntityType()

$operation = new Operation(
$this->requestHandler->reveal(),
$this->serializer,
$this->getSerializer(),
'example_project',
'foo',
new EntityMapper('example_project', false, false)
Expand Down
41 changes: 15 additions & 26 deletions Datastore/tests/Snippet/Query/AggregationQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use Google\Cloud\Datastore\Operation;
use Google\Cloud\Datastore\Query\AggregationQuery;
use Google\Cloud\Datastore\Query\AggregationQueryResult;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\RunAggregationQueryRequest;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

Expand All @@ -43,33 +45,16 @@ class AggregationQueryTest extends SnippetTestCase
private $datastore;
private $operation;
private $requestHandler;
private $serializer;

public function setUp(): void
{
$mapper = new EntityMapper('my-awesome-project', true, false);
$this->datastore = TestHelpers::stub(DatastoreClient::class, [], ['operation']);
$this->requestHandler = $this->prophesize(RequestHandler::class);
$this->serializer = new Serializer([], [
'google.protobuf.Value' => function ($v) {
return $this->flattenValue($v);
},
'google.protobuf.Timestamp' => function ($v) {
return $this->formatTimestampFromApi($v);
}
], [], [
'google.protobuf.Timestamp' => function ($v) {
if (is_string($v)) {
$dt = new \DateTime($v);
return ['seconds' => $dt->format('U')];
}
return $v;
}
]);

$this->operation = TestHelpers::stub(Operation::class, [
$this->requestHandler->reveal(),
$this->serializer,
$this->getSerializer(),
'my-awesome-project',
'',
$mapper
Expand All @@ -78,9 +63,12 @@ public function setUp(): void

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

$this->operation->___setProperty('requestHandler', $this->requestHandler->reveal());
Expand All @@ -111,9 +98,12 @@ public function testClass()

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

$this->operation->___setProperty('requestHandler', $this->requestHandler->reveal());
Expand Down
31 changes: 9 additions & 22 deletions Datastore/tests/Snippet/Query/GqlQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Google\Cloud\Datastore\EntityMapper;
use Google\Cloud\Datastore\Operation;
use Google\Cloud\Datastore\Query\GqlQuery;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\RunQueryRequest;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

Expand All @@ -44,31 +46,14 @@ class GqlQueryTest extends SnippetTestCase
private $datastore;
private $operation;
private $requestHandler;
private $serializer;

public function setUp(): void
{
$this->datastore = TestHelpers::stub(DatastoreClient::class, [], ['operation']);
$this->requestHandler = $this->prophesize(RequestHandler::class);
$this->serializer = new Serializer([], [
'google.protobuf.Value' => function ($v) {
return $this->flattenValue($v);
},
'google.protobuf.Timestamp' => function ($v) {
return $this->formatTimestampFromApi($v);
}
], [], [
'google.protobuf.Timestamp' => function ($v) {
if (is_string($v)) {
$dt = new \DateTime($v);
return ['seconds' => $dt->format('U')];
}
return $v;
}
]);
$this->operation = TestHelpers::stub(Operation::class, [
$this->requestHandler->reveal(),
$this->serializer,
$this->getSerializer(),
'my-awesome-project',
'',
new EntityMapper('my-awesome-project', true, false)
Expand All @@ -77,9 +62,12 @@ public function setUp(): void

public function testClass()
{
$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'runQuery',
[],
Argument::type(RunQueryRequest::class),
Argument::cetera()
)->shouldBeCalled()->willReturn(
[
'batch' => [
'entityResults' => [
Expand All @@ -97,8 +85,7 @@ public function testClass()
]
]
]
],
0
]
);

$this->operation->___setProperty('requestHandler', $this->requestHandler->reveal());
Expand Down
31 changes: 9 additions & 22 deletions Datastore/tests/Snippet/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Google\Cloud\Datastore\Operation;
use Google\Cloud\Datastore\Query\Filter;
use Google\Cloud\Datastore\Query\Query;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\RunQueryRequest;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

Expand All @@ -46,33 +48,16 @@ class QueryTest extends SnippetTestCase
private $operation;
private $query;
private $requestHandler;
private $serializer;

public function setUp(): void
{
$mapper = new EntityMapper('my-awesome-project', true, false);

$this->datastore = TestHelpers::stub(DatastoreClient::class, [], ['operation']);
$this->requestHandler = $this->prophesize(RequestHandler::class);
$this->serializer = new Serializer([], [
'google.protobuf.Value' => function ($v) {
return $this->flattenValue($v);
},
'google.protobuf.Timestamp' => function ($v) {
return $this->formatTimestampFromApi($v);
}
], [], [
'google.protobuf.Timestamp' => function ($v) {
if (is_string($v)) {
$dt = new \DateTime($v);
return ['seconds' => $dt->format('U')];
}
return $v;
}
]);
$this->operation = TestHelpers::stub(Operation::class, [
$this->requestHandler->reveal(),
$this->serializer,
$this->getSerializer(),
'my-awesome-project',
'',
$mapper
Expand All @@ -83,9 +68,12 @@ public function setUp(): void

public function testClass()
{
$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'runQuery',
[],
Argument::type(RunQueryRequest::class),
Argument::cetera()
)->shouldBeCalled()->willReturn(
[
'batch' => [
'entityResults' => [
Expand All @@ -104,8 +92,7 @@ public function testClass()
],
'moreResults' => 'no'
]
],
0
]
);

$this->operation->___setProperty('requestHandler', $this->requestHandler->reveal());
Expand Down
Loading

0 comments on commit 87ff887

Please sign in to comment.