Skip to content

Commit ed830ec

Browse files
authored
Merge pull request #305 from utopia-php/fix-empty-check
Fix `isEmpty` returning true if $id is not selected
2 parents 98dc6a9 + 708d2c1 commit ed830ec

File tree

8 files changed

+92
-120
lines changed

8 files changed

+92
-120
lines changed

src/Database/Database.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,12 +2238,13 @@ public function getDocument(string $collection, string $id, array $queries = [])
22382238
}
22392239

22402240
$document = $this->adapter->getDocument($collection->getId(), $id, $queries);
2241-
$document->setAttribute('$collection', $collection->getId());
22422241

22432242
if ($document->isEmpty()) {
22442243
return $document;
22452244
}
22462245

2246+
$document->setAttribute('$collection', $collection->getId());
2247+
22472248
if ($collection->getId() !== self::METADATA) {
22482249
if (!$validator->isValid([
22492250
...$collection->getRead(),
@@ -4072,7 +4073,10 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu
40724073
}
40734074
$node = $this->casting($collection, $node);
40744075
$node = $this->decode($collection, $node, $selections);
4075-
$node->setAttribute('$collection', $collection->getId());
4076+
4077+
if (!$node->isEmpty()) {
4078+
$node->setAttribute('$collection', $collection->getId());
4079+
}
40764080
}
40774081

40784082
$results = $this->applyNestedQueries($results, $nestedQueries, $relationships);

src/Database/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public function findAndRemove(string $key, $find, string $subject = ''): bool
371371
*/
372372
public function isEmpty(): bool
373373
{
374-
return empty($this->getId());
374+
return !\count($this);
375375
}
376376

377377
/**

tests/Database/Adapter/MariaDBTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,4 @@ public static function getDatabase(): Database
5858

5959
return self::$database = $database;
6060
}
61-
62-
public static function killDatabase(): void
63-
{
64-
self::$database = null;
65-
}
6661
}

tests/Database/Adapter/MongoDBTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,4 @@ public function testKeywords(): void
9595
{
9696
$this->assertTrue(true);
9797
}
98-
99-
public static function killDatabase(): void
100-
{
101-
self::$database = null;
102-
}
10398
}

tests/Database/Adapter/MySQLTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,4 @@ public static function getDatabase(): Database
6969

7070
return self::$database = $database;
7171
}
72-
73-
public static function killDatabase(): void
74-
{
75-
self::$database = null;
76-
}
7772
}

tests/Database/Adapter/PostgresTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,4 @@ public static function getDatabase(): Database
5656

5757
return self::$database = $database;
5858
}
59-
60-
public static function killDatabase(): void
61-
{
62-
self::$database = null;
63-
}
6459
}

tests/Database/Adapter/SQLiteTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,4 @@ public static function getDatabase(): Database
6666

6767
return self::$database = $database;
6868
}
69-
70-
public static function killDatabase(): void
71-
{
72-
self::$database = null;
73-
}
7469
}

tests/Database/Base.php

Lines changed: 85 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ abstract class Base extends TestCase
3535
*/
3636
abstract protected static function getDatabase(): Database;
3737

38-
abstract protected static function killDatabase(): void;
39-
4038
/**
4139
* @return string
4240
*/
@@ -1056,7 +1054,8 @@ public function testGetDocumentSelect(Document $document): Document
10561054
Query::select(['string', 'integer']),
10571055
]);
10581056

1059-
$this->assertNotEmpty(true, $document->getId());
1057+
$this->assertEmpty($document->getId());
1058+
$this->assertFalse($document->isEmpty());
10601059
$this->assertIsString($document->getAttribute('string'));
10611060
$this->assertEquals('text📝', $document->getAttribute('string'));
10621061
$this->assertIsInt($document->getAttribute('integer'));
@@ -11691,6 +11690,89 @@ public function testLabels(): void
1169111690
$this->assertCount(1, $documents);
1169211691
}
1169311692

11693+
public function testEmptyOperatorValues(): void
11694+
{
11695+
try {
11696+
static::getDatabase()->findOne('documents', [
11697+
Query::equal('string', []),
11698+
]);
11699+
$this->fail('Failed to throw exception');
11700+
} catch (Exception $e) {
11701+
$this->assertInstanceOf(Exception::class, $e);
11702+
$this->assertEquals('Invalid query: Equal queries require at least one value.', $e->getMessage());
11703+
}
11704+
11705+
try {
11706+
static::getDatabase()->findOne('documents', [
11707+
Query::search('string', null),
11708+
]);
11709+
$this->fail('Failed to throw exception');
11710+
} catch (Exception $e) {
11711+
$this->assertInstanceOf(Exception::class, $e);
11712+
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11713+
}
11714+
11715+
try {
11716+
static::getDatabase()->findOne('documents', [
11717+
Query::notEqual('string', []),
11718+
]);
11719+
$this->fail('Failed to throw exception');
11720+
} catch (Exception $e) {
11721+
$this->assertInstanceOf(Exception::class, $e);
11722+
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11723+
}
11724+
11725+
try {
11726+
static::getDatabase()->findOne('documents', [
11727+
Query::lessThan('string', []),
11728+
]);
11729+
$this->fail('Failed to throw exception');
11730+
} catch (Exception $e) {
11731+
$this->assertInstanceOf(Exception::class, $e);
11732+
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11733+
}
11734+
11735+
try {
11736+
static::getDatabase()->findOne('documents', [
11737+
Query::lessThanEqual('string', []),
11738+
]);
11739+
$this->fail('Failed to throw exception');
11740+
} catch (Exception $e) {
11741+
$this->assertInstanceOf(Exception::class, $e);
11742+
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11743+
}
11744+
11745+
try {
11746+
static::getDatabase()->findOne('documents', [
11747+
Query::greaterThan('string', []),
11748+
]);
11749+
$this->fail('Failed to throw exception');
11750+
} catch (Exception $e) {
11751+
$this->assertInstanceOf(Exception::class, $e);
11752+
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11753+
}
11754+
11755+
try {
11756+
static::getDatabase()->findOne('documents', [
11757+
Query::greaterThanEqual('string', []),
11758+
]);
11759+
$this->fail('Failed to throw exception');
11760+
} catch (Exception $e) {
11761+
$this->assertInstanceOf(Exception::class, $e);
11762+
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11763+
}
11764+
11765+
try {
11766+
static::getDatabase()->findOne('documents', [
11767+
Query::contains('string', []),
11768+
]);
11769+
$this->fail('Failed to throw exception');
11770+
} catch (Exception $e) {
11771+
$this->assertInstanceOf(Exception::class, $e);
11772+
$this->assertEquals('Invalid query: Contains queries require at least one value.', $e->getMessage());
11773+
}
11774+
}
11775+
1169411776
public function testEvents(): void
1169511777
{
1169611778
Authorization::skip(function () {
@@ -11782,93 +11864,4 @@ public function testEvents(): void
1178211864
$database->delete('hellodb');
1178311865
});
1178411866
}
11785-
11786-
public function testEmptyOperatorValues(): void
11787-
{
11788-
try {
11789-
static::getDatabase()->findOne('documents', [
11790-
Query::equal('string', []),
11791-
]);
11792-
$this->fail('Failed to throw exception');
11793-
} catch (Exception $e) {
11794-
$this->assertInstanceOf(Exception::class, $e);
11795-
$this->assertEquals('Invalid query: Equal queries require at least one value.', $e->getMessage());
11796-
}
11797-
11798-
try {
11799-
static::getDatabase()->findOne('documents', [
11800-
Query::search('string', null),
11801-
]);
11802-
$this->fail('Failed to throw exception');
11803-
} catch (Exception $e) {
11804-
$this->assertInstanceOf(Exception::class, $e);
11805-
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11806-
}
11807-
11808-
try {
11809-
static::getDatabase()->findOne('documents', [
11810-
Query::notEqual('string', []),
11811-
]);
11812-
$this->fail('Failed to throw exception');
11813-
} catch (Exception $e) {
11814-
$this->assertInstanceOf(Exception::class, $e);
11815-
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11816-
}
11817-
11818-
try {
11819-
static::getDatabase()->findOne('documents', [
11820-
Query::lessThan('string', []),
11821-
]);
11822-
$this->fail('Failed to throw exception');
11823-
} catch (Exception $e) {
11824-
$this->assertInstanceOf(Exception::class, $e);
11825-
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11826-
}
11827-
11828-
try {
11829-
static::getDatabase()->findOne('documents', [
11830-
Query::lessThanEqual('string', []),
11831-
]);
11832-
$this->fail('Failed to throw exception');
11833-
} catch (Exception $e) {
11834-
$this->assertInstanceOf(Exception::class, $e);
11835-
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11836-
}
11837-
11838-
try {
11839-
static::getDatabase()->findOne('documents', [
11840-
Query::greaterThan('string', []),
11841-
]);
11842-
$this->fail('Failed to throw exception');
11843-
} catch (Exception $e) {
11844-
$this->assertInstanceOf(Exception::class, $e);
11845-
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11846-
}
11847-
11848-
try {
11849-
static::getDatabase()->findOne('documents', [
11850-
Query::greaterThanEqual('string', []),
11851-
]);
11852-
$this->fail('Failed to throw exception');
11853-
} catch (Exception $e) {
11854-
$this->assertInstanceOf(Exception::class, $e);
11855-
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
11856-
}
11857-
11858-
try {
11859-
static::getDatabase()->findOne('documents', [
11860-
Query::contains('string', []),
11861-
]);
11862-
$this->fail('Failed to throw exception');
11863-
} catch (Exception $e) {
11864-
$this->assertInstanceOf(Exception::class, $e);
11865-
$this->assertEquals('Invalid query: Contains queries require at least one value.', $e->getMessage());
11866-
}
11867-
}
11868-
11869-
public function testLast(): void
11870-
{
11871-
$this->expectNotToPerformAssertions();
11872-
static::killDatabase();
11873-
}
1187411867
}

0 commit comments

Comments
 (0)