|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Tools\Pagination; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\Type; |
|
6
|
|
|
use Doctrine\ORM\Query; |
|
7
|
|
|
use Doctrine\ORM\Tools\Pagination\WhereInWalker; |
|
8
|
|
|
use Doctrine\Tests\DbalTypes\Rot13Type; |
|
9
|
|
|
use Doctrine\Tests\Models\ValueConversionType\AuxiliaryEntity; |
|
10
|
|
|
use Doctrine\Tests\Models\ValueConversionType\OwningManyToOneIdForeignKeyEntity; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @group DDC-1613 |
|
14
|
|
|
*/ |
|
15
|
|
|
class WhereInWalkerTest extends PaginationTestCase |
|
16
|
|
|
{ |
|
17
|
|
|
protected function setUp() : void |
|
18
|
|
|
{ |
|
19
|
|
|
parent::setUp(); |
|
20
|
|
|
|
|
21
|
|
|
if (! Type::hasType('rot13')) { |
|
22
|
|
|
Type::addType('rot13', Rot13Type::class); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testWhereInQuery_NoWhere() |
|
27
|
|
|
{ |
|
28
|
|
|
$query = $this->entityManager->createQuery( |
|
29
|
|
|
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g' |
|
30
|
|
|
); |
|
31
|
|
|
$whereInQuery = clone $query; |
|
32
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
33
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
34
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertEquals( |
|
37
|
|
|
"SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE u0_.id IN (?)", $whereInQuery->getSQL() |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
41
|
|
|
$whereInQuery, |
|
42
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testCountQuery_MixedResultsWithName() |
|
47
|
|
|
{ |
|
48
|
|
|
$query = $this->entityManager->createQuery( |
|
49
|
|
|
'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a' |
|
50
|
|
|
); |
|
51
|
|
|
$whereInQuery = clone $query; |
|
52
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
53
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
54
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals( |
|
57
|
|
|
"SELECT a0_.id AS id_0, a0_.name AS name_1, sum(a0_.name) AS sclr_2 FROM Author a0_ WHERE a0_.id IN (?)", $whereInQuery->getSQL() |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
61
|
|
|
$whereInQuery, |
|
62
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testWhereInQuery_SingleWhere() |
|
67
|
|
|
{ |
|
68
|
|
|
$query = $this->entityManager->createQuery( |
|
69
|
|
|
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g WHERE 1 = 1' |
|
70
|
|
|
); |
|
71
|
|
|
$whereInQuery = clone $query; |
|
72
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
73
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
74
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals( |
|
77
|
|
|
"SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE 1 = 1 AND u0_.id IN (?)", $whereInQuery->getSQL() |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
81
|
|
|
$whereInQuery, |
|
82
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testWhereInQuery_MultipleWhereWithAnd() |
|
87
|
|
|
{ |
|
88
|
|
|
$query = $this->entityManager->createQuery( |
|
89
|
|
|
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g WHERE 1 = 1 AND 2 = 2' |
|
90
|
|
|
); |
|
91
|
|
|
$whereInQuery = clone $query; |
|
92
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
93
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
94
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals( |
|
97
|
|
|
"SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE 1 = 1 AND 2 = 2 AND u0_.id IN (?)", $whereInQuery->getSQL() |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
101
|
|
|
$whereInQuery, |
|
102
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testWhereInQuery_MultipleWhereWithOr() |
|
107
|
|
|
{ |
|
108
|
|
|
$query = $this->entityManager->createQuery( |
|
109
|
|
|
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g WHERE 1 = 1 OR 2 = 2' |
|
110
|
|
|
); |
|
111
|
|
|
$whereInQuery = clone $query; |
|
112
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
113
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
114
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertEquals( |
|
117
|
|
|
"SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE (1 = 1 OR 2 = 2) AND u0_.id IN (?)", $whereInQuery->getSQL() |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
121
|
|
|
$whereInQuery, |
|
122
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testWhereInQuery_MultipleWhereWithMixed_1() |
|
127
|
|
|
{ |
|
128
|
|
|
$query = $this->entityManager->createQuery( |
|
129
|
|
|
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g WHERE (1 = 1 OR 2 = 2) AND 3 = 3' |
|
130
|
|
|
); |
|
131
|
|
|
$whereInQuery = clone $query; |
|
132
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
133
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
134
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertEquals( |
|
137
|
|
|
"SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE (1 = 1 OR 2 = 2) AND 3 = 3 AND u0_.id IN (?)", $whereInQuery->getSQL() |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
141
|
|
|
$whereInQuery, |
|
142
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testWhereInQuery_MultipleWhereWithMixed_2() |
|
147
|
|
|
{ |
|
148
|
|
|
$query = $this->entityManager->createQuery( |
|
149
|
|
|
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g WHERE 1 = 1 AND 2 = 2 OR 3 = 3' |
|
150
|
|
|
); |
|
151
|
|
|
$whereInQuery = clone $query; |
|
152
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
153
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
154
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
155
|
|
|
|
|
156
|
|
|
$this->assertEquals( |
|
157
|
|
|
"SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE (1 = 1 AND 2 = 2 OR 3 = 3) AND u0_.id IN (?)", $whereInQuery->getSQL() |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
161
|
|
|
$whereInQuery, |
|
162
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testWhereInQuery_WhereNot() |
|
167
|
|
|
{ |
|
168
|
|
|
$query = $this->entityManager->createQuery( |
|
169
|
|
|
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g WHERE NOT 1 = 2' |
|
170
|
|
|
); |
|
171
|
|
|
$whereInQuery = clone $query; |
|
172
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
173
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
174
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertEquals( |
|
177
|
|
|
"SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE (NOT 1 = 2) AND u0_.id IN (?)", $whereInQuery->getSQL() |
|
178
|
|
|
); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
181
|
|
|
$whereInQuery, |
|
182
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
183
|
|
|
); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Arbitrary Join |
|
188
|
|
|
*/ |
|
189
|
|
|
public function testWhereInQueryWithArbitraryJoin_NoWhere() |
|
190
|
|
|
{ |
|
191
|
|
|
$whereInQuery = $this->entityManager->createQuery( |
|
192
|
|
|
'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH p.category = c' |
|
193
|
|
|
); |
|
194
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
195
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
196
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
197
|
|
|
|
|
198
|
|
|
$this->assertEquals( |
|
199
|
|
|
"SELECT b0_.id AS id_0, b0_.author_id AS author_id_1, b0_.category_id AS category_id_2 FROM BlogPost b0_ INNER JOIN Category c1_ ON (b0_.category_id = c1_.id) WHERE b0_.id IN (?)", $whereInQuery->getSQL() |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
203
|
|
|
$whereInQuery, |
|
204
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function testWhereInQueryWithArbitraryJoin_SingleWhere() |
|
209
|
|
|
{ |
|
210
|
|
|
$whereInQuery = $this->entityManager->createQuery( |
|
211
|
|
|
'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH p.category = c WHERE 1 = 1' |
|
212
|
|
|
); |
|
213
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
214
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); |
|
215
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertEquals( |
|
218
|
|
|
"SELECT b0_.id AS id_0, b0_.author_id AS author_id_1, b0_.category_id AS category_id_2 FROM BlogPost b0_ INNER JOIN Category c1_ ON (b0_.category_id = c1_.id) WHERE 1 = 1 AND b0_.id IN (?)", $whereInQuery->getSQL() |
|
219
|
|
|
); |
|
220
|
|
|
|
|
221
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
222
|
|
|
$whereInQuery, |
|
223
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testWillReplaceBoundQueryIdentifiersWithConvertedTypesAsPerIdentifierMapping() : void |
|
228
|
|
|
{ |
|
229
|
|
|
$whereInQuery = $this->entityManager->createQuery( |
|
230
|
|
|
'SELECT e.id4 FROM ' . AuxiliaryEntity::class . ' e' |
|
231
|
|
|
); |
|
232
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
233
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 3); |
|
234
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, ['foo', 'bar', 'baz']); |
|
235
|
|
|
|
|
236
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
237
|
|
|
$whereInQuery, |
|
238
|
|
|
['sbb', 'one', 'onm'] |
|
239
|
|
|
); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function testWillReplaceBoundQueryIdentifiersWithConvertedTypesAsPerAssociatedEntityIdentifierMapping() : void |
|
243
|
|
|
{ |
|
244
|
|
|
$whereInQuery = $this->entityManager->createQuery( |
|
245
|
|
|
'SELECT e FROM ' . OwningManyToOneIdForeignKeyEntity::class . ' e' |
|
246
|
|
|
); |
|
247
|
|
|
$whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]); |
|
248
|
|
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 3); |
|
249
|
|
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, ['foo', 'bar', 'baz']); |
|
250
|
|
|
|
|
251
|
|
|
$this->assertPaginatorWhereInParameterToBe( |
|
252
|
|
|
$whereInQuery, |
|
253
|
|
|
['sbb', 'one', 'onm'] |
|
254
|
|
|
); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** @param mixed $parameter */ |
|
258
|
|
|
private function assertPaginatorWhereInParameterToBe(Query $query, $parameter) : void |
|
259
|
|
|
{ |
|
260
|
|
|
$query->getSQL(); // forces walker to process the query |
|
261
|
|
|
|
|
262
|
|
|
$boundParameter = $query->getParameter(WhereInWalker::PAGINATOR_ID_ALIAS); |
|
263
|
|
|
|
|
264
|
|
|
self::assertNotNull($boundParameter); |
|
265
|
|
|
self::assertSame($parameter, $boundParameter->getValue()); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
|