From 493c82688930d35e402392653defe3dc84d7397a Mon Sep 17 00:00:00 2001 From: Eric Druid Date: Wed, 18 Sep 2019 10:57:17 +0200 Subject: [PATCH 1/4] Fix notice when listing non-existant documents (#13) * Fix notice when listing non-existant documents When listing documents with the parameter showMissing=true, non-existing documents (that has sub-objects) will show up but will not have createTime, updateTime of fields columns. This triggers an "Undefined index" notice which some frameworks (laravel for instance) escalates to an ErrorException. * Handle empty collections when listing documents --- src/FirestoreDatabaseResource.php | 10 +++++++--- src/FirestoreDocument.php | 10 ++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/FirestoreDatabaseResource.php b/src/FirestoreDatabaseResource.php index 3662a74..50ccc96 100644 --- a/src/FirestoreDatabaseResource.php +++ b/src/FirestoreDatabaseResource.php @@ -60,9 +60,13 @@ public function listDocuments($collection, array $parameters = [], array $option $response = $this->client->request('GET', 'documents/' . FirestoreHelper::normalizeCollection($collection), $options, $parameters); - $documents = array_map(function($doc) { - return new FirestoreDocument($doc); - }, $response['documents']); + if (isset($response['documents'])) { + $documents = array_map(function($doc) { + return new FirestoreDocument($doc); + }, $response['documents']); + } else { + $documents = []; + } return array_merge($response, [ 'documents' => $documents, diff --git a/src/FirestoreDocument.php b/src/FirestoreDocument.php index ea9ee0c..713bfa7 100644 --- a/src/FirestoreDocument.php +++ b/src/FirestoreDocument.php @@ -36,11 +36,13 @@ public function __construct($object = null, $databaseResource = null) { if (null !== $object) { $this->name = $object['name']; - $this->createTime = $object['createTime']; - $this->updateTime = $object['updateTime']; + $this->createTime = isset($object['createTime']) ? $object['createTime'] : null; + $this->updateTime = isset($object['updateTime']) ? $object['updateTime'] : null; - foreach ($object['fields'] as $fieldName => $value) { - $this->fields[ $fieldName ] = $value; + if (isset($object['fields'])) { + foreach ($object['fields'] as $fieldName => $value) { + $this->fields[ $fieldName ] = $value; + } } } From d231e1b7b0560a3b7331d95f6456b6a7437cf1df Mon Sep 17 00:00:00 2001 From: Petter Blomberg Date: Wed, 18 Sep 2019 10:58:26 +0200 Subject: [PATCH 2/4] Store http response code in UnhandledRequestError (#12) This allows the user to deal with errors in a more informed way - for example, if the response code is 502 or 503, it might be a good idea to wait 15-30 seconds and try again. --- src/Exceptions/UnhandledRequestError.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exceptions/UnhandledRequestError.php b/src/Exceptions/UnhandledRequestError.php index 21db0a3..972a1c9 100644 --- a/src/Exceptions/UnhandledRequestError.php +++ b/src/Exceptions/UnhandledRequestError.php @@ -7,6 +7,6 @@ class UnhandledRequestError extends \Exception public function __construct($code, $response) { $message = 'The request failed with the error: '.$code.'. Response: '.$response; - parent::__construct($message); + parent::__construct($message, $code); } } From 25bd6f06bf9e84cd75b10aeb0feb7245a43e4661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Gramary=20Barbosa?= Date: Sat, 4 Apr 2020 20:23:32 +0200 Subject: [PATCH 3/4] Fill class @method phpdoc to avoid lint warnings using FirestoreClient (#17) --- src/FirestoreClient.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/FirestoreClient.php b/src/FirestoreClient.php index e8d820e..fdc5fc1 100644 --- a/src/FirestoreClient.php +++ b/src/FirestoreClient.php @@ -9,6 +9,15 @@ use MrShan0\PHPFirestore\Handlers\RequestErrorHandler; use MrShan0\PHPFirestore\Helpers\FirestoreHelper; +/** + * @method array listDocuments($collection, array $parameters = [], array $options = []) + * @method FirestoreDocument getDocument($documentPath, array $parameters = [], array $options = []) + * @method array getBatchDocuments(array $documentsId, array $parameters = [], array $options = []) + * @method FirestoreDocument addDocument($collection, $payload, $documentId = null, array $parameters = [], array $options = []) + * @method FirestoreDocument updateDocument($documentPath, $payload, $documentExists = null, array $parameters = [], array $options = []) + * @method FirestoreDocument setDocument($documentPath, $payload, $documentExists = null, array $parameters = [], array $options = []) + * @method boolean deleteDocument($document, array $options = []) + */ class FirestoreClient { /** From 811c574d29aa09f0504ad3076503cc34fc209a97 Mon Sep 17 00:00:00 2001 From: Eric Druid Date: Sat, 4 Apr 2020 22:10:10 +0200 Subject: [PATCH 4/4] Add a few tests for FirestoreDocument and fix odd behaviour of getCreatedTime (#15) Added few test cases. --- src/FirestoreDocument.php | 8 +++ tests/FirestoreDocumentTest.php | 118 ++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 tests/FirestoreDocumentTest.php diff --git a/src/FirestoreDocument.php b/src/FirestoreDocument.php index 713bfa7..c234c85 100644 --- a/src/FirestoreDocument.php +++ b/src/FirestoreDocument.php @@ -78,6 +78,10 @@ public function getAbsoluteName() */ public function getCreatedTime() { + if (is_null($this->createTime)) { + return null; + } + return new DateTime($this->createTime); } @@ -86,6 +90,10 @@ public function getCreatedTime() */ public function getUpdatedTime() { + if (is_null($this->updateTime)) { + return null; + } + return new DateTime($this->updateTime); } diff --git a/tests/FirestoreDocumentTest.php b/tests/FirestoreDocumentTest.php new file mode 100644 index 0000000..f824ddb --- /dev/null +++ b/tests/FirestoreDocumentTest.php @@ -0,0 +1,118 @@ + 'projects/database/(default)/documents/collection/item', + ] + ); + + $this->assertSame('projects/database/(default)/documents/collection/item', $document->getAbsoluteName()); + } + + + /** @test */ + public function get_absolute_name_returns_null_if_no_document_provided() + { + $document = new FirestoreDocument(); + + $this->assertNull($document->getAbsoluteName()); + } + + + /** @test */ + public function get_created_time_returns_created_time() + { + $document = new FirestoreDocument( + [ + 'name' => 'projects/database/(default)/documents/collection/item', + 'createTime' => '2018-12-24T15:00:00.123456Z', + 'updateTime' => '2018-12-24T16:00:00.123456Z', + 'fields' => [ + 'myKey' => [ + 'stringValue' => 'my value' + ] + ], + ] + ); + + $this->assertEquals(new \DateTime('2018-12-24T15:00:00.123456Z'), $document->getCreatedTime()); + } + + + /** @test */ + public function get_updated_time_returns_updated_time() + { + $document = new FirestoreDocument( + [ + 'name' => 'projects/database/(default)/documents/collection/item', + 'createTime' => '2018-12-24T15:00:00.123456Z', + 'updateTime' => '2018-12-24T16:00:00.123456Z', + 'fields' => [ + 'myKey' => [ + 'stringValue' => 'my value' + ] + ], + ] + ); + + $this->assertEquals(new \DateTime('2018-12-24T16:00:00.123456Z'), $document->getUpdatedTime()); + } + + + /** @test */ + public function get_created_time_returns_null_if_document_does_not_exist() + { + $document = new FirestoreDocument( + [ + 'name' => 'projects/database/(default)/documents/collection/item', + ] + ); + + $this->assertNull($document->getCreatedTime()); + } + + + /** @test */ + public function get_updated_time_returns_null_if_document_does_not_exist() + { + $document = new FirestoreDocument( + [ + 'name' => 'projects/database/(default)/documents/collection/item', + ] + ); + + $this->assertNull($document->getUpdatedTime()); + } + + + /** @test */ + public function get_name_returns_name() + { + $document = new FirestoreDocument( + [ + 'name' => 'projects/database/(default)/documents/collection/item', + ] + ); + + $this->assertSame('projects/database/(default)/documents/collection/item', $document->getName()); + } + + + /** @test */ + public function get_name_returns_null_if_no_document_provided() + { + $document = new FirestoreDocument(); + + $this->assertNull($document->getName()); + } +}