Skip to content

Commit

Permalink
#15 when no where filter is used, do not replace inner joins by left …
Browse files Browse the repository at this point in the history
…joins

- moved if (count($logicExp->getParts()) === 0)
#14 when no where filter is used, don't apply
- added unit test
  • Loading branch information
metaclass-nl committed Sep 20, 2022
1 parent d96209b commit e316cc9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Filter/FilterLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
}
}

// Only add where and replace inner joins if there is any filter logic to apply.
if (count($logicExp->getParts()) === 0) {
return;
}

if ($this->innerJoinsLeft) {
$this->replaceInnerJoinsByLeftJoins($queryBuilder);
}

// only add where if parts is filled.
if (count($logicExp->getParts()) === 0) {
return;
}

// if $existingWhere empty it does not matter how applied
// if combinator == AND no problem
// if $filterWhere empty use andWhere
Expand Down
57 changes: 52 additions & 5 deletions tests/Filter/FilterLogicWithAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ public function setUp(): void
self::assertNotNull($this->filterLogic, "this->filterLogic");
}

public function testNoLogic()
{
$reqData = null;
parse_str('', $reqData);
// var_dump($reqData);
$context = ['filters' => $reqData];
foreach ($this->filters as $filter) {
$filter->apply($this->testEntityQb, $this->queryNameGen, TestEntity::class, 'get', $context);
}

$this->assertEquals(
str_replace('
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o"),
$this->testEntityQb->getDQL(),
'DQL');
}

public function testDdFilterAnd()
{
$reqData = null;
Expand Down Expand Up @@ -245,11 +262,11 @@ public function testRexExp()
'Parameter dd_p1');
}

public function testInnerJoinsLeft()
public function testInnerJoinsLeftDdFilterOr()
{
$this->assertTrue(Reflection::getProperty($this->filterLogic, 'innerJoinsLeft'));
$reqData = null;
parse_str('exists[toMany.bool]=false', $reqData);
parse_str('exists[toMany.bool]=false&or[dd][before]=2010-02-02', $reqData);
$context = ['filters' => $reqData];
foreach ($this->filters as $filter) {
$filter->apply($this->testEntityQb, $this->queryNameGen, TestEntity::class, 'get', $context);
Expand All @@ -259,14 +276,19 @@ public function testInnerJoinsLeft()
str_replace('
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o
LEFT JOIN o.toMany toMany_a1
WHERE toMany_a1.bool IS NULL"),
WHERE toMany_a1.bool IS NULL
OR (o.dd <= :dd_p1 AND o.dd IS NOT NULL)"),
$this->testEntityQb->getDQL(),
'DQL');
$this->assertEquals(
'2010-02-02',
$this->testEntityQb->getParameter('dd_p1')->getValue()->format('Y-m-d'),
'Parameter dd_p1');
}

public function testNoInnerJoinsLeft()
public function testInnerJoinsLeftNoLogic()
{
Reflection::setProperty($this->filterLogic, 'innerJoinsLeft', false);
$this->assertTrue(Reflection::getProperty($this->filterLogic, 'innerJoinsLeft'));
$reqData = null;
parse_str('exists[toMany.bool]=false', $reqData);
$context = ['filters' => $reqData];
Expand All @@ -283,6 +305,31 @@ public function testNoInnerJoinsLeft()
'DQL');
}

public function testNoInnerJoinsLeftDdFilterOr()
{
Reflection::setProperty($this->filterLogic, 'innerJoinsLeft', false);
$reqData = null;
parse_str('exists[toMany.bool]=false&or[dd][before]=2010-02-02', $reqData);
$context = ['filters' => $reqData];
foreach ($this->filters as $filter) {
$filter->apply($this->testEntityQb, $this->queryNameGen, TestEntity::class, 'get', $context);
}

$this->assertEquals(
str_replace('
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o
INNER JOIN o.toMany toMany_a1
WHERE toMany_a1.bool IS NULL
OR (o.dd <= :dd_p1 AND o.dd IS NOT NULL)"),
$this->testEntityQb->getDQL(),
'DQL');
$this->assertEquals(
'2010-02-02',
$this->testEntityQb->getParameter('dd_p1')->getValue()->format('Y-m-d'),
'Parameter dd_p1');

}

public function testWithFakeLeftJoin()
{
Reflection::setProperty($this->filterLogic, 'innerJoinsLeft', false);
Expand Down

0 comments on commit e316cc9

Please sign in to comment.