Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,13 @@ protected static function getMethodFromAlias(string $method): string
* @param string $attribute
* @param array<mixed> $values
* @return Query
* @throws Exception
*/
public static function equal(string $attribute, array $values): self
{
if (empty($values)) {
throw new Exception('Equal operator requires values');
Comment thread
fogelito marked this conversation as resolved.
Outdated
}
return new self(self::TYPE_EQUAL, $attribute, $values);
}

Expand All @@ -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");
Comment thread
fogelito marked this conversation as resolved.
Outdated
}
return new self(self::TYPE_LESSER, $attribute, [$value]);
}

Expand All @@ -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]);
}

Expand All @@ -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]);
}

Expand All @@ -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]);
}

Expand All @@ -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);
}

Expand All @@ -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]);
}

Expand Down
82 changes: 82 additions & 0 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,88 @@ 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::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),
]);
$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
Expand Down