Skip to content

Commit

Permalink
Throw exception when $operator is illegal for the $value type #2
Browse files Browse the repository at this point in the history
  • Loading branch information
devmoath committed Dec 17, 2021
1 parent df8a652 commit 3ffedf1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/Jql.php
Expand Up @@ -71,7 +71,7 @@ public static function query(): self

public function where(string $column, string $operator, mixed $value, string $boolean = self::AND): self
{
$this->invalidBoolean($boolean);
$this->invalidBooleanOrOperator($boolean, $operator, $value);

return tap($this, fn() => $this->appendQuery("$column $operator {$this->quote($operator, $value)}", $boolean));
}
Expand Down Expand Up @@ -194,15 +194,26 @@ private function appendQuery(string $query, string $boolean = ''): void
/**
* @throws \InvalidArgumentException
*/
private function invalidBoolean(mixed $boolean): void
private function invalidBooleanOrOperator(mixed $boolean, string $operator, mixed $value): void
{
if (! in_array($boolean, [self::AND, self::OR])) {
throw new InvalidArgumentException(sprintf(
"Illegal boolean [%s] value. only [%s, %s] is acceptable",
'Illegal boolean [%s] value. only [%s, %s] is acceptable',
$boolean,
self::AND,
self::OR
));
}

if (! in_array($operator, [self::IN, self::NOT_IN, self::WAS_IN, self::WAS_NOT_IN]) && is_array($value)) {
throw new InvalidArgumentException(sprintf(
'Illegal operator [%s] value. only [%s, %s, %s, %s] is acceptable when $value type is array',
$operator,
self::IN,
self::NOT_IN,
self::WAS_IN,
self::WAS_NOT_IN,
));
}
}
}
27 changes: 25 additions & 2 deletions tests/JqlTest.php
Expand Up @@ -3,6 +3,7 @@
namespace DevMoath\JqlBuilder\Tests;

use DevMoath\JqlBuilder\Jql;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class JqlTest extends TestCase
Expand Down Expand Up @@ -39,7 +40,9 @@ public function it_can_generate_query_with_many_conditions_and_order_by(): void
->orderBy('created', Jql::ASC)
->getQuery();

self::assertSame("project = 'MY PROJECT' and issuetype = 'support' and status in ('wip', 'created') order by created asc", $query);
$expected = "project = 'MY PROJECT' and issuetype = 'support' and status in ('wip', 'created') order by created asc";

self::assertSame($expected, $query);
}

/** @test */
Expand Down Expand Up @@ -79,7 +82,7 @@ public function it_can_add_macro(): void
{
$builder = new Jql();

$builder::macro('whereCustom', function($value) {
$builder::macro('whereCustom', function ($value) {
/** @var Jql $this */
return $this->where('custom', Jql::EQUALS, $value);
});
Expand All @@ -89,4 +92,24 @@ public function it_can_add_macro(): void

self::assertSame("custom = '1'", $query);
}

/** @test */
public function it_can_throw_excption_when_invalid_boolean_passed(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Illegal boolean [=] value. only [and, or] is acceptable');
$this->expectExceptionCode(0);

Jql::query()->where('project', Jql::EQUALS, 'MY PROJECT', Jql::EQUALS);
}

/** @test */
public function it_can_throw_excption_when_invalid_operator_passed(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Illegal operator [=] value. only [in, not in, was in, was not in] is acceptable when $value type is array');
$this->expectExceptionCode(0);

Jql::query()->where('project', Jql::EQUALS, ['MY PROJECT']);
}
}

0 comments on commit 3ffedf1

Please sign in to comment.