From 1bff401a68eb3a62d888463d3b3529a83ab53428 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 10 Apr 2023 13:34:25 +0300 Subject: [PATCH 1/8] Empty values operators --- src/Database/Query.php | 26 +++++++++++++++ tests/Database/Base.php | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/src/Database/Query.php b/src/Database/Query.php index d92c6adec..07b6de811 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 operator requires values'); + } return new self(self::TYPE_EQUAL, $attribute, $values); } @@ -506,9 +510,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("lessThan operator can't be null"); + } return new self(self::TYPE_LESSER, $attribute, [$value]); } @@ -518,9 +526,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("LessThanEqual operator can't be null"); + } return new self(self::TYPE_LESSEREQUAL, $attribute, [$value]); } @@ -530,9 +542,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("GreaterThan operator can't be null"); + } return new self(self::TYPE_GREATER, $attribute, [$value]); } @@ -545,6 +561,9 @@ public static function greaterThan(string $attribute, mixed $value): self */ public static function greaterThanEqual(string $attribute, mixed$value): self { + if($value === null){ + Throw new Exception("GreaterThanEqual operator can't be null"); + } return new self(self::TYPE_GREATEREQUAL, $attribute, [$value]); } @@ -557,6 +576,9 @@ public static function greaterThanEqual(string $attribute, mixed$value): self */ public static function contains(string $attribute, array $values): self { + if(empty($values)){ + Throw new Exception('Contains operator requires values'); + } return new self(self::TYPE_CONTAINS, $attribute, $values); } @@ -579,9 +601,13 @@ 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 === '' || $value === null){ + Throw new Exception("Search operator can't be empty"); + } return new self(self::TYPE_SEARCH, $attribute, [$value]); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 4c0ed7382..89555c9be 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -815,6 +815,79 @@ 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 operator requires values', $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 operator can't be empty", $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("lessThan operator can't be null", $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("LessThanEqual operator can't be null", $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("GreaterThan operator can't be null", $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("GreaterThanEqual operator can't be null", $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 operator requires values', $e->getMessage()); + } + } /** * @depends testCreateDocument From 5ef3992250d8b1e542d885008f7c451e96b89e7c Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 10 Apr 2023 13:46:07 +0300 Subject: [PATCH 2/8] formatting and composer.lock --- composer.lock | 2 +- src/Database/Query.php | 28 ++++++++++++++-------------- tests/Database/Base.php | 11 ++++++++++- 3 files changed, 25 insertions(+), 16 deletions(-) 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 07b6de811..30af2989d 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -486,8 +486,8 @@ protected static function getMethodFromAlias(string $method): string */ public static function equal(string $attribute, array $values): self { - if(empty($values)){ - Throw new Exception('Equal operator requires values'); + if (empty($values)) { + throw new Exception('Equal operator requires values'); } return new self(self::TYPE_EQUAL, $attribute, $values); } @@ -514,8 +514,8 @@ public static function notEqual(string $attribute, mixed $value): self */ public static function lessThan(string $attribute, mixed $value): self { - if($value === null){ - Throw new Exception("lessThan operator can't be null"); + if ($value === null) { + throw new Exception("lessThan operator can't be null"); } return new self(self::TYPE_LESSER, $attribute, [$value]); } @@ -530,8 +530,8 @@ public static function lessThan(string $attribute, mixed $value): self */ public static function lessThanEqual(string $attribute, mixed $value): self { - if($value === null){ - Throw new Exception("LessThanEqual operator can't be null"); + if ($value === null) { + throw new Exception("LessThanEqual operator can't be null"); } return new self(self::TYPE_LESSEREQUAL, $attribute, [$value]); } @@ -546,8 +546,8 @@ public static function lessThanEqual(string $attribute, mixed $value): self */ public static function greaterThan(string $attribute, mixed $value): self { - if($value === null){ - Throw new Exception("GreaterThan operator can't be null"); + if ($value === null) { + throw new Exception("GreaterThan operator can't be null"); } return new self(self::TYPE_GREATER, $attribute, [$value]); } @@ -561,8 +561,8 @@ public static function greaterThan(string $attribute, mixed $value): self */ public static function greaterThanEqual(string $attribute, mixed$value): self { - if($value === null){ - Throw new Exception("GreaterThanEqual operator can't be null"); + if ($value === null) { + throw new Exception("GreaterThanEqual operator can't be null"); } return new self(self::TYPE_GREATEREQUAL, $attribute, [$value]); } @@ -576,8 +576,8 @@ public static function greaterThanEqual(string $attribute, mixed$value): self */ public static function contains(string $attribute, array $values): self { - if(empty($values)){ - Throw new Exception('Contains operator requires values'); + if (empty($values)) { + throw new Exception('Contains operator requires values'); } return new self(self::TYPE_CONTAINS, $attribute, $values); } @@ -605,8 +605,8 @@ public static function between(string $attribute, mixed $start, mixed $end): sel */ public static function search(string $attribute, mixed $value): self { - if($value === '' || $value === null){ - Throw new Exception("Search operator can't be empty"); + if ($value === '' || $value === null) { + throw new Exception("Search operator can't be empty"); } return new self(self::TYPE_SEARCH, $attribute, [$value]); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 89555c9be..02d51954c 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -817,7 +817,6 @@ public function testFulltextIndexWithInteger(): void public function testEmptyOperatorValues(): void { - try { static::getDatabase()->findOne('documents', [ Query::equal('string', []), @@ -838,6 +837,16 @@ public function testEmptyOperatorValues(): void $this->assertEquals("Search operator can't be empty", $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 operator can't be empty", $e->getMessage()); + } + try { static::getDatabase()->findOne('documents', [ Query::lessThan('string', null), From c79ab4704b0d6066e9c376915d6ef88c0ac98919 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 10 Apr 2023 15:07:29 +0300 Subject: [PATCH 3/8] formatting and throws --- src/Database/Query.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Database/Query.php b/src/Database/Query.php index 30af2989d..aaa7047b6 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -558,6 +558,7 @@ 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 { @@ -573,6 +574,7 @@ 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 { From 5b8afbd8523f261e821e2a28aaeb8da997e4c381 Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Mon, 10 Apr 2023 15:08:33 +0300 Subject: [PATCH 4/8] Update src/Database/Query.php Co-authored-by: Jake Barnby --- src/Database/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Query.php b/src/Database/Query.php index aaa7047b6..fe14a4a60 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -487,7 +487,7 @@ protected static function getMethodFromAlias(string $method): string public static function equal(string $attribute, array $values): self { if (empty($values)) { - throw new Exception('Equal operator requires values'); + throw new Exception('Equal queries require at least one value.'); } return new self(self::TYPE_EQUAL, $attribute, $values); } From 2ccebc9fa053b7688177d39ecc48a28979a06f36 Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Mon, 10 Apr 2023 15:08:47 +0300 Subject: [PATCH 5/8] Update src/Database/Query.php Co-authored-by: Jake Barnby --- src/Database/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Query.php b/src/Database/Query.php index fe14a4a60..e32dbba9b 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -515,7 +515,7 @@ public static function notEqual(string $attribute, mixed $value): self public static function lessThan(string $attribute, mixed $value): self { if ($value === null) { - throw new Exception("lessThan operator can't be null"); + throw new Exception('Less than queries cannot have a null value.'); } return new self(self::TYPE_LESSER, $attribute, [$value]); } From bf8e8d0c1c1f7643e26a6ae14d04e214e4981a11 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 10 Apr 2023 15:13:24 +0300 Subject: [PATCH 6/8] Text fixes --- src/Database/Query.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Database/Query.php b/src/Database/Query.php index e32dbba9b..c6982804e 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -531,7 +531,7 @@ public static function lessThan(string $attribute, mixed $value): self public static function lessThanEqual(string $attribute, mixed $value): self { if ($value === null) { - throw new Exception("LessThanEqual operator can't be null"); + throw new Exception('Less than equal queries cannot have a null value.'); } return new self(self::TYPE_LESSEREQUAL, $attribute, [$value]); } @@ -547,7 +547,7 @@ public static function lessThanEqual(string $attribute, mixed $value): self public static function greaterThan(string $attribute, mixed $value): self { if ($value === null) { - throw new Exception("GreaterThan operator can't be null"); + throw new Exception('Greater than queries cannot have a null value.'); } return new self(self::TYPE_GREATER, $attribute, [$value]); } @@ -560,10 +560,10 @@ public static function greaterThan(string $attribute, mixed $value): self * @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("GreaterThanEqual operator can't be null"); + throw new Exception('Greater than equal queries cannot have a null value.'); } return new self(self::TYPE_GREATEREQUAL, $attribute, [$value]); } @@ -579,7 +579,7 @@ public static function greaterThanEqual(string $attribute, mixed$value): self public static function contains(string $attribute, array $values): self { if (empty($values)) { - throw new Exception('Contains operator requires values'); + throw new Exception('Contains queries require at least one value.'); } return new self(self::TYPE_CONTAINS, $attribute, $values); } From 8d623476a0e5b997d7699cbf4354438a3b81aaee Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 10 Apr 2023 15:27:22 +0300 Subject: [PATCH 7/8] Text fixes #2 --- src/Database/Query.php | 13 +++++++++++-- tests/Database/Base.php | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/Database/Query.php b/src/Database/Query.php index c6982804e..7ccbef439 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -498,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]); } @@ -607,9 +611,14 @@ public static function between(string $attribute, mixed $start, mixed $end): sel */ public static function search(string $attribute, mixed $value): self { - if ($value === '' || $value === null) { - throw new Exception("Search operator can't be empty"); + 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 02d51954c..3b808a3e4 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -824,7 +824,17 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals('Equal operator requires values', $e->getMessage()); + $this->assertEquals('Equal queries require at least one 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 { @@ -834,7 +844,7 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals("Search operator can't be empty", $e->getMessage()); + $this->assertEquals('Search queries cannot have an empty value.', $e->getMessage()); } try { @@ -844,7 +854,17 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals("Search operator can't be empty", $e->getMessage()); + $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 { @@ -854,7 +874,7 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals("lessThan operator can't be null", $e->getMessage()); + $this->assertEquals('Less than queries cannot have a null value.', $e->getMessage()); } try { @@ -864,7 +884,7 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals("LessThanEqual operator can't be null", $e->getMessage()); + $this->assertEquals('Less than equal queries cannot have a null value.', $e->getMessage()); } try { @@ -874,7 +894,7 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals("GreaterThan operator can't be null", $e->getMessage()); + $this->assertEquals('Greater than queries cannot have a null value.', $e->getMessage()); } try { @@ -884,7 +904,7 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals("GreaterThanEqual operator can't be null", $e->getMessage()); + $this->assertEquals('Greater than equal queries cannot have a null value.', $e->getMessage()); } try { @@ -894,7 +914,7 @@ public function testEmptyOperatorValues(): void $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(Exception::class, $e); - $this->assertEquals('Contains operator requires values', $e->getMessage()); + $this->assertEquals('Contains queries require at least one value.', $e->getMessage()); } } From 2e56e84630179757e48c7cb296e52aa6ed63483a Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 10 Apr 2023 16:26:38 +0300 Subject: [PATCH 8/8] test in internal $id as well --- tests/Database/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 3b808a3e4..bc60f346e 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -829,7 +829,7 @@ public function testEmptyOperatorValues(): void try { static::getDatabase()->findOne('documents', [ - Query::search('string', null), + Query::search('$id', null), ]); $this->fail('Failed to throw exception'); } catch (Exception $e) {