Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/0.53.x' into feat-not-found-exce…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
abnegate committed Nov 1, 2024
2 parents 40b804e + 19969d2 commit 89376bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
22 changes: 13 additions & 9 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4083,10 +4083,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 @@ -4104,10 +4104,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 @@ -4774,7 +4774,7 @@ private function deleteRestrict(
Query::equal($twoWayKey, [$document->getId()])
]);

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

Expand All @@ -4797,7 +4797,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 @@ -4841,7 +4841,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 @@ -5220,10 +5220,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 @@ -5233,6 +5233,10 @@ public function findOne(string $collection, array $queries = []): false|Document

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

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

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 @@ -4312,13 +4312,13 @@ public function testFindOne(): void
Query::orderAsc('name')
]);

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

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

public function testFindNull(): void
Expand Down Expand Up @@ -5556,7 +5556,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 @@ -6656,7 +6656,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 @@ -7132,7 +7132,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 @@ -7706,7 +7706,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 @@ -8139,7 +8139,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 @@ -8551,7 +8551,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 @@ -8928,7 +8928,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 @@ -9310,7 +9310,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 @@ -9692,7 +9692,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 @@ -9996,7 +9996,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 @@ -10018,7 +10018,7 @@ public function testSelectRelationshipAttributes(): void
Query::select(['name', '$id']),
]);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down

0 comments on commit 89376bd

Please sign in to comment.