Skip to content

Commit

Permalink
fix(Firestore): SnapshotTrait getSnapshot method (#5807)
Browse files Browse the repository at this point in the history
* Decoded the API format timestamp received in batchGetDocument() method to Google\Protobuf\Timestamp using Serialize:decodeMessage().
* Added system and unit test for this new change.
  • Loading branch information
yash30201 authored Jan 25, 2023
1 parent ab60987 commit cfc8fef
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Firestore/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Google\Cloud\Firestore\V1\TransactionOptions;
use Google\Cloud\Firestore\V1\TransactionOptions\ReadWrite;
use Google\Cloud\Firestore\FirestoreClient as ManualFirestoreClient;
use Google\Protobuf\Timestamp as ProtobufTimestamp;

/**
* A gRPC connection to Cloud Firestore via GAPIC.
Expand Down Expand Up @@ -121,6 +122,12 @@ public function __construct(array $config = [])
*/
public function batchGetDocuments(array $args)
{
if (isset($args['readTime'])) {
$args['readTime'] = $this->serializer->decodeMessage(
new ProtobufTimestamp(),
$args['readTime']
);
}
return $this->send([$this->firestore, 'batchGetDocuments'], [
$this->pluck('database', $args),
$this->pluck('documents', $args),
Expand Down
12 changes: 12 additions & 0 deletions Firestore/tests/System/DocumentAndCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Google\Cloud\Firestore\Tests\System;

use Google\Cloud\Core\Timestamp;
use Google\Cloud\Firestore\CollectionReference;
use Google\Cloud\Firestore\DocumentReference;

Expand Down Expand Up @@ -44,6 +45,17 @@ public function testCreate()
$this->assertEquals(['firstName' => 'Kate'], $document->snapshot()->data());
}

public function testSnapshotWithReadTime()
{
$readTime = new Timestamp(new \DateTimeImmutable());
$snapshotData = $this->document->snapshot([
'readTime' => $readTime
])->data();

$this->assertEquals('John', $snapshotData['firstName']);
$this->assertEquals('USA', $snapshotData['country']);
}

public function testUpdate()
{
$this->assertEquals('John', $this->document->snapshot()['firstName']);
Expand Down
42 changes: 42 additions & 0 deletions Firestore/tests/Unit/Connection/GrpcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Google\Cloud\Firestore\V1\TransactionOptions_ReadWrite;
use Google\Cloud\Firestore\V1\Value;
use Google\Cloud\Firestore\V1\Write;
use Google\Protobuf\Timestamp as ProtobufTimestamp;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Prophecy\Argument;

Expand Down Expand Up @@ -86,6 +87,47 @@ public function testBatchGetDocuments()
$this->sendAndAssert('batchGetDocuments', $args, $expected);
}

public function testBatchGetDocumentsWithReadTime()
{
$documents = [
sprintf(
'projects/%s/databases/%s/documents/%s',
self::PROJECT,
self::DATABASE,
'a/b'
),
sprintf(
'projects/%s/databases/%s/documents/%s',
self::PROJECT,
self::DATABASE,
'a/c'
)
];

$args = [
'database' => sprintf(
'projects/%s/databases/%s',
self::PROJECT,
self::DATABASE
),
'documents' => $documents,
'readTime' => [
'seconds' => (int) 202320232,
'nanos' => (int) 0
]
];

$protobufTimestamp = new ProtobufTimestamp();
$protobufTimestamp->setSeconds($args['readTime']['seconds']);
$expected = [
$args['database'],
$args['documents'],
$this->header() + ['readTime' => $protobufTimestamp]
];

$this->sendAndAssert('batchGetDocuments', $args, $expected);
}

public function testBeginTransaction()
{
$args = [
Expand Down

0 comments on commit cfc8fef

Please sign in to comment.