Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make findOne return empty document instead of false #449

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4039,10 +4039,10 @@ private function updateDocumentRelationships(Document $collection, Document $old
}
if (
$oldValue?->getId() !== $value
&& $this->skipRelationships(fn () => $this->findOne($relatedCollection->getId(), [
&& !($this->skipRelationships(fn () => $this->findOne($relatedCollection->getId(), [
Query::select(['$id']),
Query::equal($twoWayKey, [$value]),
]))
]))->isEmpty())
) {
// Have to do this here because otherwise relations would be updated before the database can throw the unique violation
throw new DuplicateException('Document already has a related document');
Expand All @@ -4060,10 +4060,10 @@ private function updateDocumentRelationships(Document $collection, Document $old

if (
$oldValue?->getId() !== $value->getId()
&& $this->skipRelationships(fn () => $this->findOne($relatedCollection->getId(), [
&& !($this->skipRelationships(fn () => $this->findOne($relatedCollection->getId(), [
Query::select(['$id']),
Query::equal($twoWayKey, [$value->getId()]),
]))
]))->isEmpty())
) {
// Have to do this here because otherwise relations would be updated before the database can throw the unique violation
throw new DuplicateException('Document already has a related document');
Expand Down Expand Up @@ -4726,7 +4726,7 @@ private function deleteRestrict(
Query::equal($twoWayKey, [$document->getId()])
]);

if (!$related instanceof Document) {
if ($related->isEmpty()) {
return;
}

Expand All @@ -4749,7 +4749,7 @@ private function deleteRestrict(
Query::equal($twoWayKey, [$document->getId()])
]));

if ($related) {
if (!$related->isEmpty()) {
throw new RestrictedException('Cannot delete document because it has at least one related document.');
}
}
Expand Down Expand Up @@ -4793,7 +4793,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection
$related = $this->getDocument($relatedCollection->getId(), $value->getId(), [Query::select(['$id'])]);
}

if (!$related instanceof Document) {
if ($related->isEmpty()) {
return;
}

Expand Down Expand Up @@ -5172,10 +5172,10 @@ public function find(string $collection, array $queries = []): array
/**
* @param string $collection
* @param array<Query> $queries
* @return false|Document
* @return Document
* @throws DatabaseException
*/
public function findOne(string $collection, array $queries = []): false|Document
public function findOne(string $collection, array $queries = []): Document
{
$results = $this->silent(fn () => $this->find($collection, \array_merge([
Query::limit(1)
Expand All @@ -5185,7 +5185,11 @@ public function findOne(string $collection, array $queries = []): false|Document

$this->trigger(self::EVENT_DOCUMENT_FIND, $found);

return $found;
if (!$found) {
return new Document();
} else {
return $found;
}
}

/**
Expand Down
44 changes: 22 additions & 22 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4232,13 +4232,13 @@ public function testFindOne(): void
Query::orderAsc('name')
]);

$this->assertTrue($document instanceof Document);
$this->assertFalse($document->isEmpty());
$this->assertEquals('Frozen', $document->getAttribute('name'));

$document = $this->getDatabase()->findOne('movies', [
Query::offset(10)
]);
$this->assertEquals(false, $document);
$this->assertTrue($document->isEmpty());
}

public function testFindNull(): void
Expand Down Expand Up @@ -5475,7 +5475,7 @@ public function testRenameAttribute(): void

// Document should be there if adapter migrated properly
$document = $database->findOne('colors');
$this->assertTrue($document instanceof Document);
$this->assertFalse($document->isEmpty());
$this->assertEquals('black', $document->getAttribute('verbose'));
$this->assertEquals('#000000', $document->getAttribute('hex'));
$this->assertEquals(null, $document->getAttribute('name'));
Expand Down Expand Up @@ -6519,7 +6519,7 @@ public function testOneToOneOneWayRelationship(): void
Query::select(['*', 'library.name'])
]);

if (!$person instanceof Document) {
if ($person->isEmpty()) {
throw new Exception('Person not found');
}

Expand Down Expand Up @@ -6995,7 +6995,7 @@ public function testOneToOneTwoWayRelationship(): void
Query::select(['*', 'city.name'])
]);

if (!$country instanceof Document) {
if ($country->isEmpty()) {
throw new Exception('Country not found');
}

Expand Down Expand Up @@ -7569,7 +7569,7 @@ public function testOneToManyOneWayRelationship(): void
Query::select(['*', 'albums.name'])
]);

if (!$artist instanceof Document) {
if ($artist->isEmpty()) {
$this->fail('Artist not found');
}

Expand Down Expand Up @@ -8002,7 +8002,7 @@ public function testOneToManyTwoWayRelationship(): void
Query::select(['*', 'accounts.name'])
]);

if (!$customer instanceof Document) {
if ($customer->isEmpty()) {
throw new Exception('Customer not found');
}

Expand Down Expand Up @@ -8414,7 +8414,7 @@ public function testManyToOneOneWayRelationship(): void
Query::select(['*', 'movie.name'])
]);

if (!$review instanceof Document) {
if ($review->isEmpty()) {
throw new Exception('Review not found');
}

Expand Down Expand Up @@ -8791,7 +8791,7 @@ public function testManyToOneTwoWayRelationship(): void
Query::select(['*', 'store.name'])
]);

if (!$product instanceof Document) {
if ($product->isEmpty()) {
throw new Exception('Product not found');
}

Expand Down Expand Up @@ -9173,7 +9173,7 @@ public function testManyToManyOneWayRelationship(): void
Query::select(['*', 'songs.name'])
]);

if (!$playlist instanceof Document) {
if ($playlist->isEmpty()) {
throw new Exception('Playlist not found');
}

Expand Down Expand Up @@ -9555,7 +9555,7 @@ public function testManyToManyTwoWayRelationship(): void
Query::select(['*', 'classes.name'])
]);

if (!$student instanceof Document) {
if ($student->isEmpty()) {
throw new Exception('Student not found');
}

Expand Down Expand Up @@ -9859,7 +9859,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', 'models.name']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9881,7 +9881,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', '$id']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9896,7 +9896,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', '$internalId']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9911,7 +9911,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', '$collection']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9926,7 +9926,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', '$createdAt']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9941,7 +9941,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', '$updatedAt']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9956,7 +9956,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', '$permissions']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9972,7 +9972,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['*', 'models.year']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -9988,7 +9988,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['*', 'models.*']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -10005,7 +10005,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['models.*']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand All @@ -10021,7 +10021,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name']),
]);

if (!$make instanceof Document) {
if ($make->isEmpty()) {
throw new Exception('Make not found');
}

Expand Down
Loading