Skip to content

Commit

Permalink
Merge pull request #475 from utopia-php/fix-query-type-error
Browse files Browse the repository at this point in the history
Fix query type error
  • Loading branch information
abnegate authored Nov 6, 2024
2 parents 2ed56d0 + b7110d5 commit 6661edf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ public static function parseQuery(array $query): self
$attribute = $query['attribute'] ?? '';
$values = $query['values'] ?? [];

if (!\is_string($method)) {
throw new QueryException('Invalid query method. Must be a string, got ' . \gettype($method));
}

if (!self::isMethod($method)) {
throw new QueryException('Invalid query method: ' . $method);
}
Expand All @@ -262,6 +266,10 @@ public static function parseQuery(array $query): self
throw new QueryException('Invalid query attribute. Must be a string, got ' . \gettype($attribute));
}

if (!\is_array($values)) {
throw new QueryException('Invalid query values. Must be an array, got ' . \gettype($values));
}

if (\in_array($method, self::LOGICAL_TYPES)) {
foreach ($values as $index => $value) {
$values[$index] = self::parseQuery($value);
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,27 @@ public function testParse(): void
$this->assertEquals('actors', $queries[0]->getAttribute());
$this->assertEquals($json, '{"method":"or","values":[{"method":"equal","attribute":"actors","values":["Brad Pitt"]},{"method":"equal","attribute":"actors","values":["Johnny Depp"]}]}');

try {
Query::parse('{"method":["equal"],"attribute":["title"],"values":["test"]}');
$this->fail('Failed to throw exception');
} catch (QueryException $e) {
$this->assertEquals('Invalid query method. Must be a string, got array', $e->getMessage());
}

try {
Query::parse('{"method":"equal","attribute":["title"],"values":["test"]}');
$this->fail('Failed to throw exception');
} catch (QueryException $e) {
$this->assertEquals('Invalid query attribute. Must be a string, got array', $e->getMessage());
}

try {
Query::parse('{"method":"equal","attribute":"title","values":"test"}');
$this->fail('Failed to throw exception');
} catch (QueryException $e) {
$this->assertEquals('Invalid query values. Must be an array, got string', $e->getMessage());
}

try {
Query::parse('false');
$this->fail('Failed to throw exception');
Expand Down

0 comments on commit 6661edf

Please sign in to comment.