-
Notifications
You must be signed in to change notification settings - Fork 9
/
DateFilterTest.php
172 lines (147 loc) · 5.91 KB
/
DateFilterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Metaclass\FilterBundle\Tests\Filter;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface;
use Metaclass\FilterBundle\Filter\DateFilter as AdaptedDateFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGenerator;
use Doctrine\Persistence\ManagerRegistry;
use Metaclass\FilterBundle\Entity\TestEntity;
use Metaclass\FilterBundle\Filter\FilterLogic;
use Metaclass\FilterBundle\Tests\Utils\Reflection;
class DateFilterTest extends KernelTestCase
{
/** @var ManagerRegistry doctrine */
private $doctrine;
/** @var */
private $repo;
/** @var QueryNameGenerator */
private $queryNameGen;
/** @var \Doctrine\ORM\QueryBuilder */
private $qb;
/** @var FilterInterface */
private $dateFilter;
private $adaptedDateFilter;
/** @var FilterLogic */
private $filterLogic;
public function setUp(): void
{
$kernel = static::bootKernel();
$container = $kernel->getContainer();
$this->doctrine = $container->get('doctrine');
$this->repo = $this->doctrine->getRepository(TestEntity::class);
$this->qb = $this->repo->createQueryBuilder('o');
$this->queryNameGen = new QueryNameGenerator();
$filterLocator = $container->get('test.api_platform.filter_locator');
$requestStack = null;
$logger = null;
$nameConverter = null;
$this->filterLogic = new FilterLogic($filterLocator, $this->doctrine, $logger, []);
$this->dateFilter = new DateFilter($this->doctrine, $logger, ['dd' => DateFilterInterface::EXCLUDE_NULL]);
$this->adaptedDateFilter = new AdaptedDateFilter($this->doctrine, $logger, ['dd' => DateFilterInterface::EXCLUDE_NULL]);
}
public function testExcludeNull()
{
$reqData = [];
parse_str('dd[before]=2021-01-01&dd[after]=2021-03-03', $reqData);
// var_dump($reqData);
$context = ['filters' => $reqData];
$qb2 = $this->repo->createQueryBuilder('o');
$qng2 = new QueryNameGenerator();
$this->dateFilter->apply($this->qb, $this->queryNameGen, TestEntity::class, null, $context);
$this->adaptedDateFilter->apply($qb2, $qng2, TestEntity::class, null, $context);
$this->assertEquals(
str_replace('
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o WHERE
(o.dd IS NOT NULL AND o.dd <= :dd_p1)
AND
(o.dd IS NOT NULL AND o.dd >= :dd_p2)
"),
$qb2->getDQL(),
'DQL adaptedDateFilter produces 2 semantically complete expressions');
$this->assertNotEquals(
$this->qb->getDQL(),
$qb2->getDQL(),
'dql adapted against original'
);
/*
$this->assertEquals(
str_replace('
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o WHERE
o.dd IS NOT NULL
AND
o.dd <= :dd_p1
AND
o.dd >= :dd_p2
"),
$this->qb->getDQL(),
'Datefilter produces 3 expessions that depend on one another and therefore are not Semantically not complete');
*/
}
public function testNoNullManagement()
{
$reqData = null;
parse_str('dd[strictly_before]=2021-01-01&dd[after]=2021-03-03', $reqData);
// var_dump($reqData);
$context = ['filters' => $reqData];
$dateFilter = new DateFilter($this->doctrine, null, ['dd' => null]);
$dateFilter->apply($this->qb, $this->queryNameGen, TestEntity::class, null, $context);
$adaptedDateFilter = new AdaptedDateFilter($this->doctrine, null, ['dd' => null]);
$qb2 = $this->repo->createQueryBuilder('o');
$qng2 = new QueryNameGenerator();
$adaptedDateFilter->apply($qb2, $qng2, TestEntity::class, null, $context);
$this->assertEquals(
$this->qb->getDQL(),
$qb2->getDQL(),
'dql'
);
}
public function testIncludeNullAfter()
{
$reqData = null;
parse_str('dd[before]=2021-01-01&dd[strictly_after]=2021-03-03', $reqData);
// var_dump($reqData);
$context = ['filters' => $reqData];
$dateFilter = new DateFilter($this->doctrine, null, ['dd' => DateFilterInterface::INCLUDE_NULL_AFTER]);
$dateFilter->apply($this->qb, $this->queryNameGen, TestEntity::class, null, $context);
$adaptedDateFilter = new AdaptedDateFilter($this->doctrine, null, ['dd' => DateFilterInterface::INCLUDE_NULL_AFTER]);
$qb2 = $this->repo->createQueryBuilder('o');
$qng2 = new QueryNameGenerator();
$adaptedDateFilter->apply($qb2, $qng2, TestEntity::class, null, $context);
$this->assertEquals(
$this->qb->getDQL(),
$qb2->getDQL(),
'dql'
);
}
public function testAdaptedWithFilterLogic()
{
Reflection::setProperty($this->filterLogic, 'filters', [$this->adaptedDateFilter]);
$operator = 'or';
$reqData = null;
parse_str('or[dd][before]=2021-01-01&or[dd][after]=2021-03-03', $reqData);
// var_dump($reqData);
$context = ['filters' => $reqData];
$args = [$this->qb, $this->queryNameGen, TestEntity::class, null, $context];
$result = Reflection::callMethod($this->filterLogic, 'doGenerate', $args);
$this->assertEquals(
1,
count($result),
"number of expressions");
$this->assertEquals(
str_replace('
', '', "(o.dd IS NOT NULL AND o.dd <= :dd_p1)
OR (o.dd IS NOT NULL AND o.dd >= :dd_p2)"),
(string) $result[0],
'DQL');
$this->assertEquals(
'2021-01-01',
$this->qb->getParameter('dd_p1')->getValue()->format('Y-m-d'),
'Parameter dd_p1');
$this->assertEquals(
'2021-03-03',
$this->qb->getParameter('dd_p2')->getValue()->format('Y-m-d'),
'Parameter dd_p2');
}
}