diff --git a/composer.lock b/composer.lock index 2058675c3..9a5de9cf4 100644 --- a/composer.lock +++ b/composer.lock @@ -2673,5 +2673,5 @@ "ext-redis": "*", "ext-mongodb": "*" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/src/Database/Query.php b/src/Database/Query.php index d92c6adec..7ccbef439 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -482,9 +482,13 @@ protected static function getMethodFromAlias(string $method): string * @param string $attribute * @param array $values * @return Query + * @throws Exception */ public static function equal(string $attribute, array $values): self { + if (empty($values)) { + throw new Exception('Equal queries require at least one value.'); + } return new self(self::TYPE_EQUAL, $attribute, $values); } @@ -494,9 +498,13 @@ public static function equal(string $attribute, array $values): self * @param string $attribute * @param mixed $value * @return Query + * @throws Exception */ public static function notEqual(string $attribute, mixed $value): self { + if ($value === null) { + throw new Exception('Not equal queries cannot have a null value, Please use Is not null operator.'); + } return new self(self::TYPE_NOTEQUAL, $attribute, [$value]); } @@ -506,9 +514,13 @@ public static function notEqual(string $attribute, mixed $value): self * @param string $attribute * @param mixed $value * @return Query + * @throws Exception */ public static function lessThan(string $attribute, mixed $value): self { + if ($value === null) { + throw new Exception('Less than queries cannot have a null value.'); + } return new self(self::TYPE_LESSER, $attribute, [$value]); } @@ -518,9 +530,13 @@ public static function lessThan(string $attribute, mixed $value): self * @param string $attribute * @param mixed $value * @return Query + * @throws Exception */ public static function lessThanEqual(string $attribute, mixed $value): self { + if ($value === null) { + throw new Exception('Less than equal queries cannot have a null value.'); + } return new self(self::TYPE_LESSEREQUAL, $attribute, [$value]); } @@ -530,9 +546,13 @@ public static function lessThanEqual(string $attribute, mixed $value): self * @param string $attribute * @param mixed $value * @return Query + * @throws Exception */ public static function greaterThan(string $attribute, mixed $value): self { + if ($value === null) { + throw new Exception('Greater than queries cannot have a null value.'); + } return new self(self::TYPE_GREATER, $attribute, [$value]); } @@ -542,9 +562,13 @@ public static function greaterThan(string $attribute, mixed $value): self * @param string $attribute * @param mixed $value * @return Query + * @throws Exception */ - public static function greaterThanEqual(string $attribute, mixed$value): self + public static function greaterThanEqual(string $attribute, mixed $value): self { + if ($value === null) { + throw new Exception('Greater than equal queries cannot have a null value.'); + } return new self(self::TYPE_GREATEREQUAL, $attribute, [$value]); } @@ -554,9 +578,13 @@ public static function greaterThanEqual(string $attribute, mixed$value): self * @param string $attribute * @param array $values * @return Query + * @throws Exception */ public static function contains(string $attribute, array $values): self { + if (empty($values)) { + throw new Exception('Contains queries require at least one value.'); + } return new self(self::TYPE_CONTAINS, $attribute, $values); } @@ -579,9 +607,18 @@ public static function between(string $attribute, mixed $start, mixed $end): sel * @param string $attribute * @param mixed $value * @return Query + * @throws Exception */ public static function search(string $attribute, mixed $value): self { + if ($value === null) { + throw new Exception('Search queries cannot have a null value.'); + } + + if ($value === '') { + throw new Exception('Search queries cannot have an empty value.'); + } + return new self(self::TYPE_SEARCH, $attribute, [$value]); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 4c0ed7382..bc60f346e 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -815,6 +815,108 @@ public function testFulltextIndexWithInteger(): void static::getDatabase()->createIndex('documents', 'fulltext_integer', Database::INDEX_FULLTEXT, ['string','integer']); } + public function testEmptyOperatorValues(): void + { + try { + static::getDatabase()->findOne('documents', [ + Query::equal('string', []), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Equal queries require at least one value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::search('$id', null), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Search queries cannot have a null value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::search('string', ''), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Search queries cannot have an empty value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::search('string', null), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Search queries cannot have a null value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::notEqual('string', null), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Not equal queries cannot have a null value, Please use Is not null operator.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::lessThan('string', null), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Less than queries cannot have a null value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::lessThanEqual('string', null), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Less than equal queries cannot have a null value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::greaterThan('string', null), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Greater than queries cannot have a null value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::greaterThanEqual('string', null), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Greater than equal queries cannot have a null value.', $e->getMessage()); + } + + try { + static::getDatabase()->findOne('documents', [ + Query::contains('string', []), + ]); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(Exception::class, $e); + $this->assertEquals('Contains queries require at least one value.', $e->getMessage()); + } + } /** * @depends testCreateDocument