1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Tests for query filters

This commit is contained in:
J. King 2022-04-29 20:53:05 -04:00
parent f51acb4264
commit d64dc751f9
2 changed files with 29 additions and 4 deletions

View file

@ -13,8 +13,7 @@ class QueryFilter {
protected $qWhereNot = []; // WHERE NOT clause components protected $qWhereNot = []; // WHERE NOT clause components
protected $tWhereNot = []; // WHERE NOT clause type bindings protected $tWhereNot = []; // WHERE NOT clause type bindings
protected $vWhereNot = []; // WHERE NOT clause binding values protected $vWhereNot = []; // WHERE NOT clause binding values
protected $filterRestrictive = true; // Whether to glue WHERE conditions with OR (false) or AND (true)
public $filterRestrictive = true;
public function setWhere(string $where, $types = null, $values = null): static { public function setWhere(string $where, $types = null, $values = null): static {
$this->qWhere[] = $where; $this->qWhere[] = $where;
@ -34,13 +33,18 @@ class QueryFilter {
return $this; return $this;
} }
public function setFilter(self $filter): static { public function setWhereGroup(self $filter): static {
$this->qWhere[] = "(".$filter->buildWhereBody().")"; $this->qWhere[] = "(".$filter->buildWhereBody().")";
$this->tWhere[] = $filter->getWhereTypes(); $this->tWhere[] = $filter->getWhereTypes();
$this->vWhere[] = $filter->getWhereValues(); $this->vWhere[] = $filter->getWhereValues();
return $this; return $this;
} }
public function setWhereRestrictive(bool $restrictive): static {
$this->filterRestrictive = $restrictive;
return $this;
}
protected function getWhereTypes(): array { protected function getWhereTypes(): array {
return ValueInfo::flatten([$this->tWhere, $this->tWhereNot]); return ValueInfo::flatten([$this->tWhere, $this->tWhereNot]);
} }

View file

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Misc; namespace JKingWeb\Arsse\TestCase\Misc;
use JKingWeb\Arsse\Misc\Query; use JKingWeb\Arsse\Misc\Query;
use JKingWeb\Arsse\Misc\QueryFilter;
/** /**
* @covers \JKingWeb\Arsse\Misc\Query * @covers \JKingWeb\Arsse\Misc\Query
@ -91,4 +92,24 @@ class TestQuery extends \JKingWeb\Arsse\Test\AbstractTest {
$this->assertSame(["datetime", "str", "bool"], $q->getTypes()); $this->assertSame(["datetime", "str", "bool"], $q->getTypes());
$this->assertSame([1, 3, 2], $q->getValues()); $this->assertSame([1, 3, 2], $q->getValues());
} }
public function testNestedWhereConditions(): void {
$q = new Query("SELECT *, ? as const from table", "datetime", 1);
$f = new QueryFilter;
$f->setWhere("a = ?", "str", "ook")->setWhere("b = c")->setWhere("c = ?", "int", 42);
$this->assertSame("a = ? AND b = c AND c = ?", (string) $f);
$this->assertSame(["str", "int"], $f->getTypes());
$this->assertSame(["ook", 42], $f->getValues());
$q->setWhereGroup($f);
$f->setWhereRestrictive(false);
$this->assertSame("a = ? OR b = c OR c = ?", (string) $f);
$q->setWhereGroup($f);
$this->assertSame("SELECT *, ? as const from table WHERE (a = ? AND b = c AND c = ?) AND (a = ? OR b = c OR c = ?)", $q->getQuery());
$this->assertSame(["datetime", "str", "int", "str", "int"], $q->getTypes());
$this->assertSame([1, "ook", 42, "ook", 42], $q->getValues());
$q->setWhereRestrictive(false);
$this->assertSame("SELECT *, ? as const from table WHERE (a = ? AND b = c AND c = ?) OR (a = ? OR b = c OR c = ?)", $q->getQuery());
$this->assertSame(["datetime", "str", "int", "str", "int"], $q->getTypes());
$this->assertSame([1, "ook", 42, "ook", 42], $q->getValues());
}
} }