Skip to content

Commit

Permalink
Merge pull request #309 from FriendsOfCake/issue-308
Browse files Browse the repository at this point in the history
Fix return value for Boolean::process().
  • Loading branch information
ADmad authored Feb 18, 2021
2 parents 2523851 + f128a1b commit 67aaee5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/Model/Filter/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,27 @@ public function process(): bool
$bool = false;
}

if ($bool !== null) {
if (!$this->manager()->getRepository() instanceof Table) {
foreach ($this->fields() as $field) {
$this->getQuery()->where([
$field => $bool,
]);
}

return true;
}
if ($bool === null) {
return false;
}

if ($this->manager()->getRepository() instanceof Table) {
$conditions = [];
foreach ($this->fields() as $field) {
$conditions[] = [$field => $bool];
}

$this->getQuery()->andWhere([$this->getConfig('mode') => $conditions]);

return true;
}

foreach ($this->fields() as $field) {
$this->getQuery()->where([
$field => $bool,
]);
}

return false;
return true;
}
}
6 changes: 4 additions & 2 deletions tests/TestCase/Model/Filter/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function testProcessWithFlagOn()
$filter = new Boolean('is_active', $manager);
$filter->setArgs(['is_active' => 'on']);
$filter->setQuery($articles->find());
$filter->process();
$result = $filter->process();

$this->assertTrue($result);
$this->assertRegExp(
'/WHERE Articles\.is_active = \:c0$/',
$filter->getQuery()->sql()
Expand Down Expand Up @@ -246,8 +247,9 @@ public function testProcessWithFlagInvalid()
$filter = new Boolean('is_active', $manager);
$filter->setArgs(['is_active' => 'neitherTruthyNorFalsy']);
$filter->setQuery($articles->find());
$filter->process();
$result = $filter->process();

$this->assertFalse($result);
$this->assertEmpty($filter->getQuery()->clause('where'));
$filter->getQuery()->sql();
$this->assertEmpty($filter->getQuery()->getValueBinder()->bindings());
Expand Down

0 comments on commit 67aaee5

Please sign in to comment.