diff --git a/src/Database/Database.php b/src/Database/Database.php index 3f9f92ca9..d7f1a01b2 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4911,7 +4911,7 @@ public function getDocument(string $collection, string $id, array $queries = [], } if (empty($id)) { - return new Document(); + return $this->createDocumentInstance($collection, []); } $collection = $this->silent(fn () => $this->getCollection($collection)); diff --git a/tests/unit/EmptyDocumentTypeTest.php b/tests/unit/EmptyDocumentTypeTest.php new file mode 100644 index 000000000..787c0981f --- /dev/null +++ b/tests/unit/EmptyDocumentTypeTest.php @@ -0,0 +1,54 @@ +setDatabase('utopiaTests') + ->setNamespace('empty_type_' . \uniqid()); + $database->create(); + $database->createCollection('users'); + $database->setDocumentType('users', TypedUser::class); + + $empty = $database->getDocument('users', ''); + + $this->assertInstanceOf(TypedUser::class, $empty); + $this->assertTrue($empty->isEmpty()); + + $missing = $database->getDocument('users', 'nobody'); + + $this->assertInstanceOf(TypedUser::class, $missing); + $this->assertTrue($missing->isEmpty()); + } + + public function testEmptyIdOnAnUnmappedCollectionStaysAPlainDocument(): void + { + $database = new Database(new DatabaseMemory(), new Cache(new CacheMemory())); + + $empty = $database->getDocument('anything', ''); + + $this->assertSame(Document::class, $empty::class); + $this->assertTrue($empty->isEmpty()); + } +}