|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Unit\Artprima\QueryFilterBundle\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Artprima\QueryFilterBundle\Query\Condition; |
|
6
|
|
|
use Artprima\QueryFilterBundle\Query\ConditionManager; |
|
7
|
|
|
use Artprima\QueryFilterBundle\Query\Filter; |
|
8
|
|
|
use Artprima\QueryFilterBundle\Query\ProxyQueryBuilder; |
|
9
|
|
|
use Doctrine\ORM\EntityManager; |
|
10
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Doctrine\ORM\Query; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ProxyQueryBuilderTest |
|
16
|
|
|
* |
|
17
|
|
|
* @author Denis Voytyuk <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ProxyQueryBuilderTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
private $expressionBuilder; |
|
22
|
|
|
private $conditionManager; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->expressionBuilder = new Query\Expr; |
|
27
|
|
|
$expressions = [ |
|
28
|
|
|
'between' => new Condition\Between, |
|
29
|
|
|
'eq' => new Condition\Eq, |
|
30
|
|
|
'gt' => new Condition\Gt, |
|
31
|
|
|
'gte' => new Condition\Gte, |
|
32
|
|
|
'in' => new Condition\In, |
|
33
|
|
|
'is not null' => new Condition\IsNotNull, |
|
34
|
|
|
'is null' => new Condition\IsNull, |
|
35
|
|
|
'like' => new Condition\Like, |
|
36
|
|
|
'lt' => new Condition\Lt, |
|
37
|
|
|
'lte' => new Condition\Lte, |
|
38
|
|
|
'member of' => new Condition\MemberOf, |
|
39
|
|
|
'not between' => new Condition\NotBetween, |
|
40
|
|
|
'not eq' => new Condition\NotEq, |
|
41
|
|
|
'not in' => new Condition\NotIn, |
|
42
|
|
|
'not like' => new Condition\NotLike, |
|
43
|
|
|
]; |
|
44
|
|
|
$this->conditionManager = new ConditionManager(); |
|
45
|
|
|
foreach ($expressions as $key => $expression) { |
|
46
|
|
|
$this->conditionManager->add($expression, $key); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @dataProvider filterDataProvider |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testGetSortedAndFilteredQuery($filterBy, $sortBy, $expected) |
|
54
|
|
|
{ |
|
55
|
|
|
$em = $this->getMockBuilder(EntityManager::class) |
|
56
|
|
|
->disableOriginalConstructor() |
|
57
|
|
|
->getMock(); |
|
58
|
|
|
|
|
59
|
|
|
$em |
|
60
|
|
|
->expects(self::any()) |
|
61
|
|
|
->method('getExpressionBuilder') |
|
62
|
|
|
->willReturn($this->expressionBuilder); |
|
63
|
|
|
|
|
64
|
|
|
$qb = new QueryBuilder($em); |
|
65
|
|
|
|
|
66
|
|
|
$builder = new ProxyQueryBuilder($qb, $this->conditionManager); |
|
67
|
|
|
|
|
68
|
|
|
self::assertEquals($qb, $builder->getSortedAndFilteredQueryBuilder($filterBy, $sortBy)); |
|
69
|
|
|
self::assertEquals($expected, $qb->getDQL()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* NOTE: expected results are not valid DQL expressions (as they lack from section), |
|
74
|
|
|
* but for testing purposes we don't need that |
|
75
|
|
|
*/ |
|
76
|
|
|
public function filterDataProvider() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
// no filters, no sort |
|
80
|
|
|
[ |
|
81
|
|
|
[ |
|
82
|
|
|
], |
|
83
|
|
|
[ |
|
84
|
|
|
], |
|
85
|
|
|
'SELECT', |
|
86
|
|
|
], |
|
87
|
|
|
|
|
88
|
|
|
// no filters, only sort |
|
89
|
|
|
[ |
|
90
|
|
|
[ |
|
91
|
|
|
], |
|
92
|
|
|
[ |
|
93
|
|
|
't.id' => 'asc', |
|
94
|
|
|
], |
|
95
|
|
|
'SELECT ORDER BY t.id ASC', |
|
96
|
|
|
], |
|
97
|
|
|
|
|
98
|
|
|
// single |
|
99
|
|
|
[ |
|
100
|
|
|
[ |
|
101
|
|
|
(new Filter()) |
|
102
|
|
|
->setField('t.id') |
|
103
|
|
|
->setType('eq') |
|
104
|
|
|
->setX('10'), |
|
105
|
|
|
], |
|
106
|
|
|
[ |
|
107
|
|
|
't.id' => 'asc', |
|
108
|
|
|
], |
|
109
|
|
|
'SELECT WHERE t.id = ?1 ORDER BY t.id ASC', |
|
110
|
|
|
], |
|
111
|
|
|
|
|
112
|
|
|
// combined |
|
113
|
|
|
[ |
|
114
|
|
|
[ |
|
115
|
|
|
(new Filter()) |
|
116
|
|
|
->setField('t.id') |
|
117
|
|
|
->setType('eq') |
|
118
|
|
|
->setX('100'), |
|
119
|
|
|
(new Filter()) |
|
120
|
|
|
->setField('t.name') |
|
121
|
|
|
->setType('like') |
|
122
|
|
|
->setX('john doe'), |
|
123
|
|
|
], |
|
124
|
|
|
[ |
|
125
|
|
|
't.id' => 'asc', |
|
126
|
|
|
], |
|
127
|
|
|
'SELECT WHERE t.id = ?1 AND t.name LIKE ?2 ORDER BY t.id ASC', |
|
128
|
|
|
], |
|
129
|
|
|
|
|
130
|
|
|
// with OR connector |
|
131
|
|
|
[ |
|
132
|
|
|
[ |
|
133
|
|
|
(new Filter()) |
|
134
|
|
|
->setField('t.id') |
|
135
|
|
|
->setType('eq') |
|
136
|
|
|
->setX('100'), |
|
137
|
|
|
(new Filter()) |
|
138
|
|
|
->setField('t.name') |
|
139
|
|
|
->setType('like') |
|
140
|
|
|
->setX('john doe') |
|
141
|
|
|
->setConnector('or'), |
|
142
|
|
|
], |
|
143
|
|
|
[ |
|
144
|
|
|
't.id' => 'asc', |
|
145
|
|
|
], |
|
146
|
|
|
'SELECT WHERE t.id = ?1 OR t.name LIKE ?2 ORDER BY t.id ASC', |
|
147
|
|
|
], |
|
148
|
|
|
|
|
149
|
|
|
// with OR connector |
|
150
|
|
|
[ |
|
151
|
|
|
[ |
|
152
|
|
|
(new Filter()) |
|
153
|
|
|
->setField('t.id') |
|
154
|
|
|
->setType('eq') |
|
155
|
|
|
->setX('100'), |
|
156
|
|
|
(new Filter()) |
|
157
|
|
|
->setField('t.name') |
|
158
|
|
|
->setType('like') |
|
159
|
|
|
->setX('john doe') |
|
160
|
|
|
->setConnector('or'), |
|
161
|
|
|
], |
|
162
|
|
|
[ |
|
163
|
|
|
't.id' => 'asc', |
|
164
|
|
|
], |
|
165
|
|
|
'SELECT WHERE t.id = ?1 OR t.name LIKE ?2 ORDER BY t.id ASC', |
|
166
|
|
|
], |
|
167
|
|
|
|
|
168
|
|
|
// having |
|
169
|
|
|
[ |
|
170
|
|
|
[ |
|
171
|
|
|
(new Filter()) |
|
172
|
|
|
->setField('t.id') |
|
173
|
|
|
->setType('eq') |
|
174
|
|
|
->setX('100') |
|
175
|
|
|
->setHaving(true), |
|
176
|
|
|
], |
|
177
|
|
|
[ |
|
178
|
|
|
't.id' => 'asc', |
|
179
|
|
|
], |
|
180
|
|
|
'SELECT HAVING t.id = ?1 ORDER BY t.id ASC', |
|
181
|
|
|
], |
|
182
|
|
|
|
|
183
|
|
|
// where AND having |
|
184
|
|
|
[ |
|
185
|
|
|
[ |
|
186
|
|
|
(new Filter()) |
|
187
|
|
|
->setField('t.id') |
|
188
|
|
|
->setType('eq') |
|
189
|
|
|
->setX('100') |
|
190
|
|
|
->setHaving(true), |
|
191
|
|
|
(new Filter()) |
|
192
|
|
|
->setField('t.name') |
|
193
|
|
|
->setType('like') |
|
194
|
|
|
->setX('john doe') |
|
195
|
|
|
->setConnector('or'), |
|
196
|
|
|
], |
|
197
|
|
|
[ |
|
198
|
|
|
't.id' => 'asc', |
|
199
|
|
|
], |
|
200
|
|
|
'SELECT WHERE t.name LIKE ?2 HAVING t.id = ?1 ORDER BY t.id ASC', |
|
201
|
|
|
], |
|
202
|
|
|
]; |
|
203
|
|
|
} |
|
204
|
|
|
} |