Failed Conditions
Pull Request — master (#6643)
by
unknown
07:56
created

QueryBuilderTest::testOrderByWithExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Criteria;
9
use Doctrine\ORM\Cache;
10
use Doctrine\ORM\Query;
11
use Doctrine\ORM\Query\Parameter;
12
use Doctrine\ORM\Query\ParameterTypeInferer;
13
use Doctrine\ORM\QueryBuilder;
14
use Doctrine\Tests\Mocks\EntityManagerMock;
15
use Doctrine\Tests\Models\Cache\State;
16
use Doctrine\Tests\Models\CMS\CmsArticle;
17
use Doctrine\Tests\Models\CMS\CmsGroup;
18
use Doctrine\Tests\Models\CMS\CmsUser;
19
use Doctrine\Tests\OrmTestCase;
20
use InvalidArgumentException;
21
use function array_filter;
22
use function get_class;
23
24
/**
25
 * Test case for the QueryBuilder class used to build DQL query string in a
26
 * object oriented way.
27
 */
28
class QueryBuilderTest extends OrmTestCase
29
{
30
    /** @var EntityManagerMock */
31
    private $em;
32
33
    protected function setUp() : void
34
    {
35
        $this->em = $this->getTestEntityManager();
36
    }
37
38
    protected function assertValidQueryBuilder(QueryBuilder $qb, $expectedDql)
39
    {
40
        $dql = $qb->getDQL();
41
        $q   = $qb->getQuery();
0 ignored issues
show
Unused Code introduced by
The assignment to $q is dead and can be removed.
Loading history...
42
43
        self::assertEquals($expectedDql, $dql);
44
    }
45
46
    public function testSelectSetsType() : void
47
    {
48
        $qb = $this->em->createQueryBuilder()
49
            ->delete(CmsUser::class, 'u')
50
            ->select('u.id', 'u.username');
51
52
        self::assertEquals($qb->getType(), QueryBuilder::SELECT);
53
    }
54
55
    public function testEmptySelectSetsType() : void
56
    {
57
        $qb = $this->em->createQueryBuilder()
58
            ->delete(CmsUser::class, 'u')
59
            ->select();
60
61
        self::assertEquals($qb->getType(), QueryBuilder::SELECT);
62
    }
63
64
    public function testDeleteSetsType() : void
65
    {
66
        $qb = $this->em->createQueryBuilder()
67
            ->from(CmsUser::class, 'u')
68
            ->delete();
69
70
        self::assertEquals($qb->getType(), QueryBuilder::DELETE);
71
    }
72
73
    public function testUpdateSetsType() : void
74
    {
75
        $qb = $this->em->createQueryBuilder()
76
            ->from(CmsUser::class, 'u')
77
            ->update();
78
79
        self::assertEquals($qb->getType(), QueryBuilder::UPDATE);
80
    }
81
82
    public function testSimpleSelect() : void
83
    {
84
        $qb = $this->em->createQueryBuilder()
85
            ->from(CmsUser::class, 'u')
86
            ->select('u.id', 'u.username');
87
88
        self::assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        self::/** @scrutinizer ignore-call */ 
89
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u');
Loading history...
89
    }
90
91
    public function testSimpleDelete() : void
92
    {
93
        $qb = $this->em->createQueryBuilder()
94
            ->delete(CmsUser::class, 'u');
95
96
        self::assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        self::/** @scrutinizer ignore-call */ 
97
              assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
Loading history...
97
    }
98
99
    public function testSimpleSelectWithFromIndexBy() : void
100
    {
101
        $qb = $this->em->createQueryBuilder()
102
            ->from(CmsUser::class, 'u', 'u.id')
103
            ->select('u.id', 'u.username');
104
105
        self::assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        self::/** @scrutinizer ignore-call */ 
106
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
Loading history...
106
    }
107
108
    public function testSimpleSelectWithIndexBy() : void
109
    {
110
        $qb = $this->em->createQueryBuilder()
111
            ->from(CmsUser::class, 'u')
112
            ->indexBy('u', 'u.id')
113
            ->select('u.id', 'u.username');
114
115
        self::assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        self::/** @scrutinizer ignore-call */ 
116
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
Loading history...
116
    }
117
118
    public function testSimpleUpdate() : void
119
    {
120
        $qb = $this->em->createQueryBuilder()
121
            ->update(CmsUser::class, 'u')
122
            ->set('u.username', ':username');
123
124
        self::assertValidQueryBuilder($qb, 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.username = :username');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        self::/** @scrutinizer ignore-call */ 
125
              assertValidQueryBuilder($qb, 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.username = :username');
Loading history...
125
    }
126
127
    public function testInnerJoin() : void
128
    {
129
        $qb = $this->em->createQueryBuilder()
130
            ->select('u', 'a')
131
            ->from(CmsUser::class, 'u')
132
            ->innerJoin('u.articles', 'a');
133
134
        self::assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
        self::/** @scrutinizer ignore-call */ 
135
              assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a');
Loading history...
135
    }
136
137
    public function testComplexInnerJoin() : void
138
    {
139
        $qb = $this->em->createQueryBuilder()
140
            ->select('u', 'a')
141
            ->from(CmsUser::class, 'u')
142
            ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id');
143
144
        self::assertValidQueryBuilder(
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        self::/** @scrutinizer ignore-call */ 
145
              assertValidQueryBuilder(
Loading history...
145
            $qb,
146
            'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id'
147
        );
148
    }
149
150
    public function testComplexInnerJoinWithIndexBy() : void
151
    {
152
        $qb = $this->em->createQueryBuilder()
153
            ->select('u', 'a')
154
            ->from(CmsUser::class, 'u')
155
            ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id', 'a.name');
156
157
        self::assertValidQueryBuilder(
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        self::/** @scrutinizer ignore-call */ 
158
              assertValidQueryBuilder(
Loading history...
158
            $qb,
159
            'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a INDEX BY a.name ON u.id = a.author_id'
160
        );
161
    }
162
163
    public function testLeftJoin() : void
164
    {
165
        $qb = $this->em->createQueryBuilder()
166
            ->select('u', 'a')
167
            ->from(CmsUser::class, 'u')
168
            ->leftJoin('u.articles', 'a');
169
170
        self::assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
        self::/** @scrutinizer ignore-call */ 
171
              assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a');
Loading history...
171
    }
172
173
    public function testLeftJoinWithIndexBy() : void
174
    {
175
        $qb = $this->em->createQueryBuilder()
176
            ->select('u', 'a')
177
            ->from(CmsUser::class, 'u')
178
            ->leftJoin('u.articles', 'a', null, null, 'a.name');
179
180
        self::assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a INDEX BY a.name');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

180
        self::/** @scrutinizer ignore-call */ 
181
              assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a INDEX BY a.name');
Loading history...
181
    }
182
183
    public function testMultipleFrom() : void
184
    {
185
        $qb = $this->em->createQueryBuilder()
186
            ->select('u', 'g')
187
            ->from(CmsUser::class, 'u')
188
            ->from(CmsGroup::class, 'g');
189
190
        self::assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
        self::/** @scrutinizer ignore-call */ 
191
              assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g');
Loading history...
191
    }
192
193
    public function testMultipleFromWithIndexBy() : void
194
    {
195
        $qb = $this->em->createQueryBuilder()
196
            ->select('u', 'g')
197
            ->from(CmsUser::class, 'u')
198
            ->from(CmsGroup::class, 'g')
199
            ->indexBy('g', 'g.id');
200
201
        self::assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g INDEX BY g.id');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
        self::/** @scrutinizer ignore-call */ 
202
              assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g INDEX BY g.id');
Loading history...
202
    }
203
204
    public function testMultipleFromWithJoin() : void
205
    {
206
        $qb = $this->em->createQueryBuilder()
207
            ->select('u', 'g')
208
            ->from(CmsUser::class, 'u')
209
            ->from(CmsGroup::class, 'g')
210
            ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id');
211
212
        self::assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id, Doctrine\Tests\Models\CMS\CmsGroup g');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
        self::/** @scrutinizer ignore-call */ 
213
              assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id, Doctrine\Tests\Models\CMS\CmsGroup g');
Loading history...
213
    }
214
215
    public function testMultipleFromWithMultipleJoin() : void
216
    {
217
        $qb = $this->em->createQueryBuilder()
218
            ->select('u', 'g')
219
            ->from(CmsUser::class, 'u')
220
            ->from(CmsArticle::class, 'a')
221
            ->innerJoin('u.groups', 'g')
222
            ->leftJoin('u.address', 'ad')
223
            ->innerJoin('a.comments', 'c');
224
225
        self::assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g LEFT JOIN u.address ad, Doctrine\Tests\Models\CMS\CmsArticle a INNER JOIN a.comments c');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
        self::/** @scrutinizer ignore-call */ 
226
              assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g LEFT JOIN u.address ad, Doctrine\Tests\Models\CMS\CmsArticle a INNER JOIN a.comments c');
Loading history...
226
    }
227
228
    public function testWhere() : void
229
    {
230
        $qb = $this->em->createQueryBuilder()
231
            ->select('u')
232
            ->from(CmsUser::class, 'u')
233
            ->where('u.id = :uid');
234
235
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
        self::/** @scrutinizer ignore-call */ 
236
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid');
Loading history...
236
    }
237
238
    public function testComplexAndWhere() : void
239
    {
240
        $qb = $this->em->createQueryBuilder()
241
            ->select('u')
242
            ->from(CmsUser::class, 'u')
243
            ->where('u.id = :uid OR u.id = :uid2 OR u.id = :uid3')
244
            ->andWhere('u.name = :name');
245
246
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid OR u.id = :uid2 OR u.id = :uid3) AND u.name = :name');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
        self::/** @scrutinizer ignore-call */ 
247
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid OR u.id = :uid2 OR u.id = :uid3) AND u.name = :name');
Loading history...
247
    }
248
249
    public function testAndWhere() : void
250
    {
251
        $qb = $this->em->createQueryBuilder()
252
            ->select('u')
253
            ->from(CmsUser::class, 'u')
254
            ->where('u.id = :uid')
255
            ->andWhere('u.id = :uid2');
256
257
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

257
        self::/** @scrutinizer ignore-call */ 
258
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
258
    }
259
260
    public function testOrWhere() : void
261
    {
262
        $qb = $this->em->createQueryBuilder()
263
            ->select('u')
264
            ->from(CmsUser::class, 'u')
265
            ->where('u.id = :uid')
266
            ->orWhere('u.id = :uid2');
267
268
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

268
        self::/** @scrutinizer ignore-call */ 
269
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
Loading history...
269
    }
270
271
    public function testComplexAndWhereOrWhereNesting() : void
272
    {
273
        $qb = $this->em->createQueryBuilder();
274
        $qb->select('u')
275
           ->from(CmsUser::class, 'u')
276
           ->where('u.id = :uid')
277
           ->orWhere('u.id = :uid2')
278
           ->andWhere('u.id = :uid3')
279
           ->orWhere('u.name = :name1', 'u.name = :name2')
280
           ->andWhere('u.name <> :noname');
281
282
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (((u.id = :uid OR u.id = :uid2) AND u.id = :uid3) OR u.name = :name1 OR u.name = :name2) AND u.name <> :noname');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

282
        self::/** @scrutinizer ignore-call */ 
283
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (((u.id = :uid OR u.id = :uid2) AND u.id = :uid3) OR u.name = :name1 OR u.name = :name2) AND u.name <> :noname');
Loading history...
283
    }
284
285
    public function testAndWhereIn() : void
286
    {
287
        $qb = $this->em->createQueryBuilder();
288
        $qb->select('u')
289
           ->from(CmsUser::class, 'u')
290
           ->where('u.id = :uid')
291
           ->andWhere($qb->expr()->in('u.id', [1, 2, 3]));
292
293
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id IN(1, 2, 3)');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

293
        self::/** @scrutinizer ignore-call */ 
294
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id IN(1, 2, 3)');
Loading history...
294
    }
295
296
    public function testBetween() : void
297
    {
298
        $qb = $this->_em->createQueryBuilder();
0 ignored issues
show
Bug Best Practice introduced by
The property _em does not exist on Doctrine\Tests\ORM\QueryBuilderTest. Did you maybe forget to declare it?
Loading history...
299
        $qb->select('g')
300
            ->from(CmsGroup::class, 'g')
301
            ->where($qb->expr()->between('g.id', 1, 10));
302
        $this->assertValidQueryBuilder($qb, 'SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id BETWEEN 1 AND 10');
303
        $this->assertEquals($qb->getQuery()->getSQL(), 'SELECT c0_.id AS id_0, c0_.name AS name_1 FROM cms_groups c0_ WHERE c0_.id BETWEEN 1 AND 10');
304
305
        $qb = $this->_em->createQueryBuilder();
306
        $qb->select('g')
307
            ->from(CmsGroup::class, 'g')
308
            ->where($qb->expr()->between('CURRENT_TIMESTAMP()', 1, 10));
309
        $this->assertValidQueryBuilder($qb, 'SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE CURRENT_TIMESTAMP() BETWEEN 1 AND 10');
310
        $this->assertEquals($qb->getQuery()->getSQL(), 'SELECT c0_.id AS id_0, c0_.name AS name_1 FROM cms_groups c0_ WHERE CURRENT_TIMESTAMP BETWEEN 1 AND 10');
311
312
        $qb = $this->_em->createQueryBuilder();
313
        $qb->select('g')
314
            ->from(CmsGroup::class, 'g')
315
            ->where($qb->expr()->between(5, 1, 10));
316
        $this->assertValidQueryBuilder($qb, 'SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE 5 BETWEEN 1 AND 10');
317
        $this->assertEquals($qb->getQuery()->getSQL(), 'SELECT c0_.id AS id_0, c0_.name AS name_1 FROM cms_groups c0_ WHERE 5 BETWEEN 1 AND 10');
318
319
        $qb = $this->_em->createQueryBuilder();
320
        $qb->select('g')
321
            ->from(CmsGroup::class, 'g')
322
            ->where($qb->expr()->between('WeekDay(:weekdayParameter) + 1', 1, 10));
323
        $this->assertValidQueryBuilder($qb, 'SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE WeekDay(:weekdayParameter) + 1 BETWEEN 1 AND 10');
324
        //$this->assertEquals($qb->getQuery()->getSQL(), 'SELECT c0_.id AS id_0, c0_.name AS name_1 FROM cms_groups c0_ WHERE WeekDay(CURRENT_TIMESTAMP()) BETWEEN 1 AND 10');
325
326
        $qb = $this->_em->createQueryBuilder();
327
        $qb->select('g')
328
            ->from(CmsGroup::class, 'g')
329
            ->where($qb->expr()->between('WeekDay(CURRENT_TIMESTAMP()) + 1', 1, 10));
330
        $this->assertValidQueryBuilder($qb, 'SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE WeekDay(CURRENT_TIMESTAMP()) + 1 BETWEEN 1 AND 10');
331
        //$this->assertEquals($qb->getQuery()->getSQL(), 'SELECT c0_.id AS id_0, c0_.name AS name_1 FROM cms_groups c0_ WHERE WeekDay(CURRENT_TIMESTAMP()) BETWEEN 1 AND 10');
332
    }
333
334
    public function testOrWhereIn() : void
335
    {
336
        $qb = $this->em->createQueryBuilder();
337
        $qb->select('u')
338
           ->from(CmsUser::class, 'u')
339
           ->where('u.id = :uid')
340
           ->orWhere($qb->expr()->in('u.id', [1, 2, 3]));
341
342
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id IN(1, 2, 3)');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

342
        self::/** @scrutinizer ignore-call */ 
343
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id IN(1, 2, 3)');
Loading history...
343
    }
344
345
    public function testAndWhereNotIn() : void
346
    {
347
        $qb = $this->em->createQueryBuilder();
348
        $qb->select('u')
349
           ->from(CmsUser::class, 'u')
350
           ->where('u.id = :uid')
351
           ->andWhere($qb->expr()->notIn('u.id', [1, 2, 3]));
352
353
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id NOT IN(1, 2, 3)');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

353
        self::/** @scrutinizer ignore-call */ 
354
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id NOT IN(1, 2, 3)');
Loading history...
354
    }
355
356
    public function testOrWhereNotIn() : void
357
    {
358
        $qb = $this->em->createQueryBuilder();
359
        $qb->select('u')
360
           ->from(CmsUser::class, 'u')
361
           ->where('u.id = :uid')
362
           ->orWhere($qb->expr()->notIn('u.id', [1, 2, 3]));
363
364
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id NOT IN(1, 2, 3)');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

364
        self::/** @scrutinizer ignore-call */ 
365
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id NOT IN(1, 2, 3)');
Loading history...
365
    }
366
367
    public function testGroupBy() : void
368
    {
369
        $qb = $this->em->createQueryBuilder()
370
            ->select('u')
371
            ->from(CmsUser::class, 'u')
372
            ->groupBy('u.id')
373
            ->addGroupBy('u.username');
374
375
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.username');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

375
        self::/** @scrutinizer ignore-call */ 
376
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.username');
Loading history...
376
    }
377
378
    public function testHaving() : void
379
    {
380
        $qb = $this->em->createQueryBuilder()
381
            ->select('u')
382
            ->from(CmsUser::class, 'u')
383
            ->groupBy('u.id')
384
            ->having('COUNT(u.id) > 1');
385
386
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

386
        self::/** @scrutinizer ignore-call */ 
387
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1');
Loading history...
387
    }
388
389
    public function testAndHaving() : void
390
    {
391
        $qb = $this->em->createQueryBuilder()
392
            ->select('u')
393
            ->from(CmsUser::class, 'u')
394
            ->groupBy('u.id')
395
            ->having('COUNT(u.id) > 1')
396
            ->andHaving('COUNT(u.id) < 1');
397
398
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1 AND COUNT(u.id) < 1');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

398
        self::/** @scrutinizer ignore-call */ 
399
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1 AND COUNT(u.id) < 1');
Loading history...
399
    }
400
401
    public function testOrHaving() : void
402
    {
403
        $qb = $this->em->createQueryBuilder()
404
            ->select('u')
405
            ->from(CmsUser::class, 'u')
406
            ->groupBy('u.id')
407
            ->having('COUNT(u.id) > 1')
408
            ->andHaving('COUNT(u.id) < 1')
409
            ->orHaving('COUNT(u.id) > 1');
410
411
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING (COUNT(u.id) > 1 AND COUNT(u.id) < 1) OR COUNT(u.id) > 1');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

411
        self::/** @scrutinizer ignore-call */ 
412
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING (COUNT(u.id) > 1 AND COUNT(u.id) < 1) OR COUNT(u.id) > 1');
Loading history...
412
    }
413
414
    public function testOrderBy() : void
415
    {
416
        $qb = $this->em->createQueryBuilder()
417
            ->select('u')
418
            ->from(CmsUser::class, 'u')
419
            ->orderBy('u.username', 'ASC');
420
421
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

421
        self::/** @scrutinizer ignore-call */ 
422
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
Loading history...
422
    }
423
424
    public function testOrderByWithExpression() : void
425
    {
426
        $qb = $this->em->createQueryBuilder();
427
        $qb->select('u')
428
            ->from(CmsUser::class, 'u')
429
            ->orderBy($qb->expr()->asc('u.username'));
430
431
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

431
        self::/** @scrutinizer ignore-call */ 
432
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
Loading history...
432
    }
433
434
    public function testAddOrderBy() : void
435
    {
436
        $qb = $this->em->createQueryBuilder()
437
            ->select('u')
438
            ->from(CmsUser::class, 'u')
439
            ->orderBy('u.username', 'ASC')
440
            ->addOrderBy('u.username', 'DESC');
441
442
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

442
        self::/** @scrutinizer ignore-call */ 
443
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
Loading history...
443
    }
444
445
    public function testAddOrderByWithExpression() : void
446
    {
447
        $qb = $this->em->createQueryBuilder();
448
        $qb->select('u')
449
            ->from(CmsUser::class, 'u')
450
            ->orderBy('u.username', 'ASC')
451
            ->addOrderBy($qb->expr()->desc('u.username'));
452
453
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

453
        self::/** @scrutinizer ignore-call */ 
454
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
Loading history...
454
    }
455
456
    public function testAddCriteriaWhere() : void
457
    {
458
        $qb = $this->em->createQueryBuilder();
459
        $qb->select('u')
460
            ->from(CmsUser::class, 'u');
461
462
        $criteria = new Criteria();
463
        $criteria->where($criteria->expr()->eq('field', 'value'));
464
465
        $qb->addCriteria($criteria);
466
467
        self::assertEquals('u.field = :field', (string) $qb->getDQLPart('where'));
468
        self::assertNotNull($qb->getParameter('field'));
469
    }
470
471
    public function testAddMultipleSameCriteriaWhere() : void
472
    {
473
        $qb = $this->em->createQueryBuilder();
474
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
475
476
        $criteria = new Criteria();
477
        $criteria->where($criteria->expr()->andX(
478
            $criteria->expr()->eq('field', 'value1'),
479
            $criteria->expr()->eq('field', 'value2')
480
        ));
481
482
        $qb->addCriteria($criteria);
483
484
        self::assertEquals('alias1.field = :field AND alias1.field = :field_1', (string) $qb->getDQLPart('where'));
485
        self::assertNotNull($qb->getParameter('field'));
486
        self::assertNotNull($qb->getParameter('field_1'));
487
    }
488
489
    /**
490
     * @group DDC-2844
491
     */
492
    public function testAddCriteriaWhereWithMultipleParametersWithSameField() : void
493
    {
494
        $qb = $this->em->createQueryBuilder();
495
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
496
497
        $criteria = new Criteria();
498
        $criteria->where($criteria->expr()->eq('field', 'value1'));
499
        $criteria->andWhere($criteria->expr()->gt('field', 'value2'));
500
501
        $qb->addCriteria($criteria);
502
503
        self::assertEquals('alias1.field = :field AND alias1.field > :field_1', (string) $qb->getDQLPart('where'));
504
        self::assertSame('value1', $qb->getParameter('field')->getValue());
505
        self::assertSame('value2', $qb->getParameter('field_1')->getValue());
506
    }
507
508
    /**
509
     * @group DDC-2844
510
     */
511
    public function testAddCriteriaWhereWithMultipleParametersWithDifferentFields() : void
512
    {
513
        $qb = $this->em->createQueryBuilder();
514
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
515
516
        $criteria = new Criteria();
517
        $criteria->where($criteria->expr()->eq('field1', 'value1'));
518
        $criteria->andWhere($criteria->expr()->gt('field2', 'value2'));
519
520
        $qb->addCriteria($criteria);
521
522
        self::assertEquals('alias1.field1 = :field1 AND alias1.field2 > :field2', (string) $qb->getDQLPart('where'));
523
        self::assertSame('value1', $qb->getParameter('field1')->getValue());
524
        self::assertSame('value2', $qb->getParameter('field2')->getValue());
525
    }
526
527
    /**
528
     * @group DDC-2844
529
     */
530
    public function testAddCriteriaWhereWithMultipleParametersWithSubpathsAndDifferentProperties() : void
531
    {
532
        $qb = $this->em->createQueryBuilder();
533
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
534
535
        $criteria = new Criteria();
536
        $criteria->where($criteria->expr()->eq('field1', 'value1'));
537
        $criteria->andWhere($criteria->expr()->gt('field2', 'value2'));
538
539
        $qb->addCriteria($criteria);
540
541
        self::assertEquals('alias1.field1 = :field1 AND alias1.field2 > :field2', (string) $qb->getDQLPart('where'));
542
        self::assertSame('value1', $qb->getParameter('field1')->getValue());
543
        self::assertSame('value2', $qb->getParameter('field2')->getValue());
544
    }
545
546
    /**
547
     * @group DDC-2844
548
     */
549
    public function testAddCriteriaWhereWithMultipleParametersWithSubpathsAndSameProperty() : void
550
    {
551
        $qb = $this->em->createQueryBuilder();
552
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
553
554
        $criteria = new Criteria();
555
        $criteria->where($criteria->expr()->eq('field1', 'value1'));
556
        $criteria->andWhere($criteria->expr()->gt('field1', 'value2'));
557
558
        $qb->addCriteria($criteria);
559
560
        self::assertEquals('alias1.field1 = :field1 AND alias1.field1 > :field1_1', (string) $qb->getDQLPart('where'));
561
        self::assertSame('value1', $qb->getParameter('field1')->getValue());
562
        self::assertSame('value2', $qb->getParameter('field1_1')->getValue());
563
    }
564
565
    public function testAddCriteriaOrder() : void
566
    {
567
        $qb = $this->em->createQueryBuilder();
568
        $qb->select('u')
569
            ->from(CmsUser::class, 'u');
570
571
        $criteria = new Criteria();
572
        $criteria->orderBy(['field' => Criteria::DESC]);
573
574
        $qb->addCriteria($criteria);
575
576
        self::assertCount(1, $orderBy = $qb->getDQLPart('orderBy'));
577
        self::assertEquals('u.field DESC', (string) $orderBy[0]);
578
    }
579
580
    /**
581
     * @group DDC-3108
582
     */
583
    public function testAddCriteriaOrderOnJoinAlias() : void
584
    {
585
        $qb = $this->em->createQueryBuilder();
586
        $qb->select('u')
587
            ->from(CmsUser::class, 'u')
588
            ->join('u.article', 'a');
589
590
        $criteria = new Criteria();
591
        $criteria->orderBy(['a.field' => Criteria::DESC]);
592
593
        $qb->addCriteria($criteria);
594
595
        self::assertCount(1, $orderBy = $qb->getDQLPart('orderBy'));
596
        self::assertEquals('a.field DESC', (string) $orderBy[0]);
597
    }
598
599
    public function testAddCriteriaLimit() : void
600
    {
601
        $qb = $this->em->createQueryBuilder();
602
        $qb->select('u')
603
            ->from(CmsUser::class, 'u');
604
605
        $criteria = new Criteria();
606
        $criteria->setFirstResult(2);
607
        $criteria->setMaxResults(10);
608
609
        $qb->addCriteria($criteria);
610
611
        self::assertEquals(2, $qb->getFirstResult());
612
        self::assertEquals(10, $qb->getMaxResults());
613
    }
614
615
    public function testAddCriteriaUndefinedLimit() : void
616
    {
617
        $qb = $this->em->createQueryBuilder();
618
        $qb->select('u')
619
            ->from(CmsUser::class, 'u')
620
            ->setFirstResult(2)
621
            ->setMaxResults(10);
622
623
        $criteria = new Criteria();
624
625
        $qb->addCriteria($criteria);
626
627
        self::assertEquals(2, $qb->getFirstResult());
628
        self::assertEquals(10, $qb->getMaxResults());
629
    }
630
631
    public function testGetQuery() : void
632
    {
633
        $qb = $this->em->createQueryBuilder()
634
            ->select('u')
635
            ->from(CmsUser::class, 'u');
636
        $q  = $qb->getQuery();
637
638
        self::assertEquals(Query::class, get_class($q));
639
    }
640
641
    public function testSetParameter() : void
642
    {
643
        $qb = $this->em->createQueryBuilder()
644
            ->select('u')
645
            ->from(CmsUser::class, 'u')
646
            ->where('u.id = :id')
647
            ->setParameter('id', 1);
648
649
        $parameter = new Parameter('id', 1, ParameterTypeInferer::inferType(1));
650
        $inferred  = $qb->getParameter('id');
651
652
        self::assertSame($parameter->getValue(), $inferred->getValue());
653
        self::assertSame($parameter->getType(), $inferred->getType());
654
        self::assertFalse($inferred->typeWasSpecified());
655
    }
656
657
    public function testSetParameters() : void
658
    {
659
        $qb = $this->em->createQueryBuilder();
660
        $qb->select('u')
661
           ->from(CmsUser::class, 'u')
662
           ->where($qb->expr()->orX('u.username = :username', 'u.username = :username2'));
663
664
        $parameters = new ArrayCollection();
665
        $parameters->add(new Parameter('username', 'jwage'));
666
        $parameters->add(new Parameter('username2', 'jonwage'));
667
668
        $qb->setParameters($parameters);
669
670
        self::assertEquals($parameters, $qb->getQuery()->getParameters());
671
    }
672
673
    public function testGetParameters() : void
674
    {
675
        $qb = $this->em->createQueryBuilder();
676
        $qb->select('u')
677
           ->from(CmsUser::class, 'u')
678
           ->where('u.id = :id');
679
680
        $parameters = new ArrayCollection();
681
        $parameters->add(new Parameter('id', 1));
682
683
        $qb->setParameters($parameters);
684
685
        self::assertEquals($parameters, $qb->getParameters());
686
    }
687
688
    public function testGetParameter() : void
689
    {
690
        $qb = $this->em->createQueryBuilder()
691
            ->select('u')
692
            ->from(CmsUser::class, 'u')
693
            ->where('u.id = :id');
694
695
        $parameters = new ArrayCollection();
696
        $parameters->add(new Parameter('id', 1));
697
698
        $qb->setParameters($parameters);
699
700
        self::assertEquals($parameters->first(), $qb->getParameter('id'));
701
    }
702
703
    public function testMultipleWhere() : void
704
    {
705
        $qb = $this->em->createQueryBuilder()
706
            ->select('u')
707
            ->from(CmsUser::class, 'u')
708
            ->where('u.id = :uid', 'u.id = :uid2');
709
710
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

710
        self::/** @scrutinizer ignore-call */ 
711
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
711
    }
712
713
    public function testMultipleAndWhere() : void
714
    {
715
        $qb = $this->em->createQueryBuilder()
716
            ->select('u')
717
            ->from(CmsUser::class, 'u')
718
            ->andWhere('u.id = :uid', 'u.id = :uid2');
719
720
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

720
        self::/** @scrutinizer ignore-call */ 
721
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
721
    }
722
723
    public function testMultipleOrWhere() : void
724
    {
725
        $qb = $this->em->createQueryBuilder();
726
        $qb->select('u')
727
           ->from(CmsUser::class, 'u')
728
           ->orWhere('u.id = :uid', $qb->expr()->eq('u.id', ':uid2'));
729
730
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

730
        self::/** @scrutinizer ignore-call */ 
731
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
Loading history...
731
    }
732
733
    public function testComplexWhere() : void
734
    {
735
        $qb     = $this->em->createQueryBuilder();
736
        $orExpr = $qb->expr()->orX();
737
        $orExpr->add($qb->expr()->eq('u.id', ':uid3'));
738
        $orExpr->add($qb->expr()->in('u.id', [1]));
739
740
        $qb->select('u')
741
           ->from(CmsUser::class, 'u')
742
           ->where($orExpr);
743
744
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR u.id IN(1)');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

744
        self::/** @scrutinizer ignore-call */ 
745
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR u.id IN(1)');
Loading history...
745
    }
746
747
    public function testWhereInWithStringLiterals() : void
748
    {
749
        $qb = $this->em->createQueryBuilder();
750
        $qb->select('u')
751
           ->from(CmsUser::class, 'u')
752
           ->where($qb->expr()->in('u.name', ['one', 'two', 'three']));
753
754
        self::assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

754
        self::/** @scrutinizer ignore-call */ 
755
              assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
Loading history...
755
756
        $qb->where($qb->expr()->in('u.name', ["O'Reilly", "O'Neil", 'Smith']));
757
758
        self::assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith')");
759
    }
760
761
    public function testWhereInWithObjectLiterals() : void
762
    {
763
        $qb   = $this->em->createQueryBuilder();
764
        $expr = $this->em->getExpressionBuilder();
765
        $qb->select('u')
766
           ->from(CmsUser::class, 'u')
767
           ->where($expr->in('u.name', [$expr->literal('one'), $expr->literal('two'), $expr->literal('three')]));
768
769
        self::assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

769
        self::/** @scrutinizer ignore-call */ 
770
              assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
Loading history...
770
771
        $qb->where($expr->in('u.name', [$expr->literal("O'Reilly"), $expr->literal("O'Neil"), $expr->literal('Smith')]));
772
773
        self::assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith')");
774
    }
775
776
    public function testNegation() : void
777
    {
778
        $expr   = $this->em->getExpressionBuilder();
779
        $orExpr = $expr->orX();
780
        $orExpr->add($expr->eq('u.id', ':uid3'));
781
        $orExpr->add($expr->not($expr->in('u.id', [1])));
782
783
        $qb = $this->em->createQueryBuilder();
784
        $qb->select('u')
785
           ->from(CmsUser::class, 'u')
786
           ->where($orExpr);
787
788
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR NOT(u.id IN(1))');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

788
        self::/** @scrutinizer ignore-call */ 
789
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR NOT(u.id IN(1))');
Loading history...
789
    }
790
791
    public function testSomeAllAny() : void
792
    {
793
        $qb   = $this->em->createQueryBuilder();
794
        $expr = $this->em->getExpressionBuilder();
795
796
        $qb->select('u')
797
           ->from(CmsUser::class, 'u')
798
           ->where($expr->gt('u.id', $expr->all('select a.id from Doctrine\Tests\Models\CMS\CmsArticle a')));
799
800
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ALL(select a.id from Doctrine\Tests\Models\CMS\CmsArticle a)');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

800
        self::/** @scrutinizer ignore-call */ 
801
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ALL(select a.id from Doctrine\Tests\Models\CMS\CmsArticle a)');
Loading history...
801
    }
802
803
    public function testMultipleIsolatedQueryConstruction() : void
804
    {
805
        $qb   = $this->em->createQueryBuilder();
806
        $expr = $this->em->getExpressionBuilder();
807
808
        $qb->select('u')->from(CmsUser::class, 'u');
809
        $qb->where($expr->eq('u.name', ':name'));
810
        $qb->setParameter('name', 'romanb');
811
812
        $q1 = $qb->getQuery();
813
814
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name', $q1->getDQL());
815
        self::assertCount(1, $q1->getParameters());
816
817
        // add another condition and construct a second query
818
        $qb->andWhere($expr->eq('u.id', ':id'));
819
        $qb->setParameter('id', 42);
820
821
        $q2 = $qb->getQuery();
822
823
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name AND u.id = :id', $q2->getDQL());
824
        self::assertNotSame($q1, $q2); // two different, independent queries
825
        self::assertCount(2, $q2->getParameters());
826
        self::assertCount(1, $q1->getParameters()); // $q1 unaffected
827
    }
828
829
    public function testGetEntityManager() : void
830
    {
831
        $qb = $this->em->createQueryBuilder();
832
        self::assertEquals($this->em->getWrappedEntityManager(), $qb->getEntityManager());
833
    }
834
835
    public function testInitialStateIsClean() : void
836
    {
837
        $qb = $this->em->createQueryBuilder();
838
        self::assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState());
839
    }
840
841
    public function testAlteringQueryChangesStateToDirty() : void
842
    {
843
        $qb = $this->em->createQueryBuilder()
844
            ->select('u')
845
            ->from(CmsUser::class, 'u');
846
847
        self::assertEquals(QueryBuilder::STATE_DIRTY, $qb->getState());
848
    }
849
850
    public function testSelectWithFuncExpression() : void
851
    {
852
        $qb   = $this->em->createQueryBuilder();
853
        $expr = $qb->expr();
854
        $qb->select($expr->count('e.id'));
855
856
        self::assertValidQueryBuilder($qb, 'SELECT COUNT(e.id)');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

856
        self::/** @scrutinizer ignore-call */ 
857
              assertValidQueryBuilder($qb, 'SELECT COUNT(e.id)');
Loading history...
857
    }
858
859
    public function testResetDQLPart() : void
860
    {
861
        $qb = $this->em->createQueryBuilder()
862
            ->select('u')
863
            ->from(CmsUser::class, 'u')
864
            ->where('u.username = ?1')->orderBy('u.username');
865
866
        self::assertEquals('u.username = ?1', (string) $qb->getDQLPart('where'));
867
        self::assertCount(1, $qb->getDQLPart('orderBy'));
868
869
        $qb->resetDQLPart('where')->resetDQLPart('orderBy');
870
871
        self::assertNull($qb->getDQLPart('where'));
872
        self::assertCount(0, $qb->getDQLPart('orderBy'));
873
    }
874
875
    public function testResetDQLParts() : void
876
    {
877
        $qb = $this->em->createQueryBuilder()
878
            ->select('u')
879
            ->from(CmsUser::class, 'u')
880
            ->where('u.username = ?1')->orderBy('u.username');
881
882
        $qb->resetDQLParts(['where', 'orderBy']);
883
884
        self::assertCount(1, $qb->getDQLPart('select'));
885
        self::assertNull($qb->getDQLPart('where'));
886
        self::assertCount(0, $qb->getDQLPart('orderBy'));
887
    }
888
889
    public function testResetAllDQLParts() : void
890
    {
891
        $qb = $this->em->createQueryBuilder()
892
            ->select('u')
893
            ->from(CmsUser::class, 'u')
894
            ->where('u.username = ?1')->orderBy('u.username');
895
896
        $qb->resetDQLParts();
897
898
        self::assertCount(0, $qb->getDQLPart('select'));
899
        self::assertNull($qb->getDQLPart('where'));
900
        self::assertCount(0, $qb->getDQLPart('orderBy'));
901
    }
902
903
    /**
904
     * @group DDC-867
905
     */
906
    public function testDeepClone() : void
907
    {
908
        $qb = $this->em->createQueryBuilder()
909
            ->select('u')
910
            ->from(CmsUser::class, 'u')
911
            ->andWhere('u.username = ?1')
912
            ->andWhere('u.status = ?2');
913
914
        $expr = $qb->getDQLPart('where');
915
        self::assertEquals(2, $expr->count(), 'Modifying the second query should affect the first one.');
916
917
        $qb2 = clone $qb;
918
        $qb2->andWhere('u.name = ?3');
919
920
        self::assertEquals(2, $expr->count(), 'Modifying the second query should affect the first one.');
921
    }
922
923
    /**
924
     * @group DDC-3108
925
     */
926
    public function testAddCriteriaWhereWithJoinAlias() : void
927
    {
928
        $qb = $this->em->createQueryBuilder();
929
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
930
        $qb->join('alias1.articles', 'alias2');
931
932
        $criteria = new Criteria();
933
        $criteria->where($criteria->expr()->eq('field', 'value1'));
934
        $criteria->andWhere($criteria->expr()->gt('alias2.field', 'value2'));
935
936
        $qb->addCriteria($criteria);
937
938
        self::assertEquals('alias1.field = :field AND alias2.field > :alias2_field', (string) $qb->getDQLPart('where'));
939
        self::assertSame('value1', $qb->getParameter('field')->getValue());
940
        self::assertSame('value2', $qb->getParameter('alias2_field')->getValue());
941
    }
942
943
    /**
944
     * @group DDC-3108
945
     */
946
    public function testAddCriteriaWhereWithDefaultAndJoinAlias() : void
947
    {
948
        $qb = $this->em->createQueryBuilder();
949
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
950
        $qb->join('alias1.articles', 'alias2');
951
952
        $criteria = new Criteria();
953
        $criteria->where($criteria->expr()->eq('alias1.field', 'value1'));
954
        $criteria->andWhere($criteria->expr()->gt('alias2.field', 'value2'));
955
956
        $qb->addCriteria($criteria);
957
958
        self::assertEquals('alias1.field = :alias1_field AND alias2.field > :alias2_field', (string) $qb->getDQLPart('where'));
959
        self::assertSame('value1', $qb->getParameter('alias1_field')->getValue());
960
        self::assertSame('value2', $qb->getParameter('alias2_field')->getValue());
961
    }
962
963
    /**
964
     * @group DDC-3108
965
     */
966
    public function testAddCriteriaWhereOnJoinAliasWithDuplicateFields() : void
967
    {
968
        $qb = $this->em->createQueryBuilder();
969
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
970
        $qb->join('alias1.articles', 'alias2');
971
972
        $criteria = new Criteria();
973
        $criteria->where($criteria->expr()->eq('alias1.field', 'value1'));
974
        $criteria->andWhere($criteria->expr()->gt('alias2.field', 'value2'));
975
        $criteria->andWhere($criteria->expr()->lt('alias2.field', 'value3'));
976
977
        $qb->addCriteria($criteria);
978
979
        self::assertEquals('(alias1.field = :alias1_field AND alias2.field > :alias2_field) AND alias2.field < :alias2_field_2', (string) $qb->getDQLPart('where'));
980
        self::assertSame('value1', $qb->getParameter('alias1_field')->getValue());
981
        self::assertSame('value2', $qb->getParameter('alias2_field')->getValue());
982
        self::assertSame('value3', $qb->getParameter('alias2_field_2')->getValue());
983
    }
984
985
    /**
986
     * @group DDC-1933
987
     */
988
    public function testParametersAreCloned() : void
989
    {
990
        $originalQb = new QueryBuilder($this->em);
991
992
        $originalQb->setParameter('parameter1', 'value1');
993
994
        $copy = clone $originalQb;
995
        $copy->setParameter('parameter2', 'value2');
996
997
        self::assertCount(1, $originalQb->getParameters());
998
        self::assertSame('value1', $copy->getParameter('parameter1')->getValue());
999
        self::assertSame('value2', $copy->getParameter('parameter2')->getValue());
1000
    }
1001
1002
    public function testGetRootAlias() : void
1003
    {
1004
        $qb = $this->em->createQueryBuilder()
1005
            ->select('u')
1006
            ->from(CmsUser::class, 'u');
1007
1008
        self::assertEquals('u', $qb->getRootAlias());
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\QueryBuilder::getRootAlias() has been deprecated: Please use $qb->getRootAliases() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1008
        self::assertEquals('u', /** @scrutinizer ignore-deprecated */ $qb->getRootAlias());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1009
    }
1010
1011
    public function testGetRootAliases() : void
1012
    {
1013
        $qb = $this->em->createQueryBuilder()
1014
            ->select('u')
1015
            ->from(CmsUser::class, 'u');
1016
1017
        self::assertEquals(['u'], $qb->getRootAliases());
1018
    }
1019
1020
    public function testGetRootEntities() : void
1021
    {
1022
        $qb = $this->em->createQueryBuilder()
1023
            ->select('u')
1024
            ->from(CmsUser::class, 'u');
1025
1026
        self::assertEquals([CmsUser::class], $qb->getRootEntities());
1027
    }
1028
1029
    public function testGetSeveralRootAliases() : void
1030
    {
1031
        $qb = $this->em->createQueryBuilder()
1032
            ->select('u')
1033
            ->from(CmsUser::class, 'u')
1034
            ->from(CmsUser::class, 'u2');
1035
1036
        self::assertEquals(['u', 'u2'], $qb->getRootAliases());
1037
        self::assertEquals('u', $qb->getRootAlias());
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\QueryBuilder::getRootAlias() has been deprecated: Please use $qb->getRootAliases() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1037
        self::assertEquals('u', /** @scrutinizer ignore-deprecated */ $qb->getRootAlias());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1038
    }
1039
1040
    public function testBCAddJoinWithoutRootAlias() : void
1041
    {
1042
        $qb = $this->em->createQueryBuilder()
1043
            ->select('u')
1044
            ->from(CmsUser::class, 'u')
1045
            ->add('join', ['INNER JOIN u.groups g'], true);
1046
1047
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g', $qb->getDQL());
1048
    }
1049
1050
    /**
1051
     * @group DDC-1211
1052
     */
1053
    public function testEmptyStringLiteral() : void
1054
    {
1055
        $expr = $this->em->getExpressionBuilder();
1056
        $qb   = $this->em->createQueryBuilder()
1057
            ->select('u')
1058
            ->from(CmsUser::class, 'u')
1059
            ->where($expr->eq('u.username', $expr->literal('')));
1060
1061
        self::assertEquals("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ''", $qb->getDQL());
1062
    }
1063
1064
    /**
1065
     * @group DDC-1211
1066
     */
1067
    public function testEmptyNumericLiteral() : void
1068
    {
1069
        $expr = $this->em->getExpressionBuilder();
1070
        $qb   = $this->em->createQueryBuilder()
1071
            ->select('u')
1072
            ->from(CmsUser::class, 'u')
1073
            ->where($expr->eq('u.username', $expr->literal(0)));
1074
1075
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 0', $qb->getDQL());
1076
    }
1077
1078
    /**
1079
     * @group DDC-1227
1080
     */
1081
    public function testAddFromString() : void
1082
    {
1083
        $qb = $this->em->createQueryBuilder()
1084
            ->add('select', 'u')
0 ignored issues
show
Bug introduced by
'u' of type string is incompatible with the type array<mixed,mixed>|object expected by parameter $dqlPart of Doctrine\ORM\QueryBuilder::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1084
            ->add('select', /** @scrutinizer ignore-type */ 'u')
Loading history...
1085
            ->add('from', CmsUser::class . ' u');
1086
1087
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
1088
    }
1089
1090
    /**
1091
     * @group DDC-1619
1092
     */
1093
    public function testAddDistinct() : void
1094
    {
1095
        $qb = $this->em->createQueryBuilder()
1096
            ->select('u')
1097
            ->distinct()
1098
            ->from(CmsUser::class, 'u');
1099
1100
        self::assertEquals('SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
1101
    }
1102
1103
    /**
1104
     * @group DDC-2192
1105
     */
1106
    public function testWhereAppend() : void
1107
    {
1108
        $this->expectException(InvalidArgumentException::class);
1109
        $this->expectExceptionMessage("Using \$append = true does not have an effect with 'where' or 'having' parts. See QueryBuilder#andWhere() for an example for correct usage.");
1110
1111
        $qb = $this->em->createQueryBuilder()
0 ignored issues
show
Unused Code introduced by
The assignment to $qb is dead and can be removed.
Loading history...
1112
            ->add('where', 'u.foo = ?1')
0 ignored issues
show
Bug introduced by
'u.foo = ?1' of type string is incompatible with the type array<mixed,mixed>|object expected by parameter $dqlPart of Doctrine\ORM\QueryBuilder::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1112
            ->add('where', /** @scrutinizer ignore-type */ 'u.foo = ?1')
Loading history...
1113
            ->add('where', 'u.bar = ?2', true);
1114
    }
1115
1116
    public function testSecondLevelCacheQueryBuilderOptions() : void
1117
    {
1118
        $defaultQueryBuilder = $this->em->createQueryBuilder()
1119
            ->select('s')
1120
            ->from(State::class, 's');
1121
1122
        self::assertFalse($defaultQueryBuilder->isCacheable());
1123
        self::assertEquals(0, $defaultQueryBuilder->getLifetime());
1124
        self::assertNull($defaultQueryBuilder->getCacheRegion());
1125
        self::assertNull($defaultQueryBuilder->getCacheMode());
1126
1127
        $defaultQuery = $defaultQueryBuilder->getQuery();
1128
1129
        self::assertFalse($defaultQuery->isCacheable());
1130
        self::assertEquals(0, $defaultQuery->getLifetime());
1131
        self::assertNull($defaultQuery->getCacheRegion());
1132
        self::assertNull($defaultQuery->getCacheMode());
1133
1134
        $builder = $this->em->createQueryBuilder()
1135
            ->select('s')
1136
            ->setLifetime(123)
1137
            ->setCacheable(true)
1138
            ->setCacheRegion('foo_reg')
1139
            ->setCacheMode(Cache::MODE_REFRESH)
1140
            ->from(State::class, 's');
1141
1142
        self::assertTrue($builder->isCacheable());
1143
        self::assertEquals(123, $builder->getLifetime());
1144
        self::assertEquals('foo_reg', $builder->getCacheRegion());
1145
        self::assertEquals(Cache::MODE_REFRESH, $builder->getCacheMode());
1146
1147
        $query = $builder->getQuery();
1148
1149
        self::assertTrue($query->isCacheable());
1150
        self::assertEquals(123, $query->getLifetime());
1151
        self::assertEquals('foo_reg', $query->getCacheRegion());
1152
        self::assertEquals(Cache::MODE_REFRESH, $query->getCacheMode());
1153
    }
1154
1155
    /**
1156
     * @group DDC-2253
1157
     */
1158
    public function testRebuildsFromParts() : void
1159
    {
1160
        $qb = $this->em->createQueryBuilder()
1161
          ->select('u')
1162
          ->from(CmsUser::class, 'u')
1163
          ->join('u.article', 'a');
1164
1165
        $dqlParts = $qb->getDQLParts();
1166
        $dql      = $qb->getDQL();
1167
1168
        $qb2 = $this->em->createQueryBuilder();
1169
        foreach (array_filter($dqlParts) as $name => $part) {
1170
            $qb2->add($name, $part);
1171
        }
1172
        $dql2 = $qb2->getDQL();
1173
1174
        self::assertEquals($dql, $dql2);
1175
    }
1176
1177
    public function testGetAllAliasesWithNoJoins() : void
1178
    {
1179
        $qb = $this->em->createQueryBuilder();
1180
        $qb->select('u')->from(CmsUser::class, 'u');
1181
1182
        $aliases = $qb->getAllAliases();
1183
1184
        self::assertEquals(['u'], $aliases);
1185
    }
1186
1187
    public function testGetAllAliasesWithJoins() : void
1188
    {
1189
        $qb = $this->em->createQueryBuilder()
1190
            ->select('u')
1191
            ->from(CmsUser::class, 'u')
1192
            ->join('u.groups', 'g');
1193
1194
        $aliases = $qb->getAllAliases();
1195
1196
        self::assertEquals(['u', 'g'], $aliases);
1197
    }
1198
1199
    /**
1200
     * @group 6699
1201
     */
1202
    public function testGetParameterTypeJuggling() : void
1203
    {
1204
        $builder = $this->em->createQueryBuilder()
1205
                            ->select('u')
1206
                            ->from(CmsUser::class, 'u')
1207
                            ->where('u.id = ?0');
1208
1209
        $builder->setParameter(0, 0);
1210
1211
        self::assertCount(1, $builder->getParameters());
1212
        self::assertSame(0, $builder->getParameter(0)->getValue());
1213
        self::assertSame(0, $builder->getParameter('0')->getValue());
1214
    }
1215
1216
    /**
1217
     * @group 6699
1218
     */
1219
    public function testSetParameterWithNameZeroIsNotOverridden() : void
1220
    {
1221
        $builder = $this->em->createQueryBuilder()
1222
                            ->select('u')
1223
                            ->from(CmsUser::class, 'u')
1224
                            ->where('u.id != ?0')
1225
                            ->andWhere('u.username = :name');
1226
1227
        $builder->setParameter(0, 0);
1228
        $builder->setParameter('name', 'Doctrine');
1229
1230
        self::assertCount(2, $builder->getParameters());
1231
        self::assertSame(0, $builder->getParameter('0')->getValue());
1232
        self::assertSame('Doctrine', $builder->getParameter('name')->getValue());
1233
    }
1234
1235
    /**
1236
     * @group 6699
1237
     */
1238
    public function testSetParameterWithNameZeroDoesNotOverrideAnotherParameter() : void
1239
    {
1240
        $builder = $this->em->createQueryBuilder()
1241
                            ->select('u')
1242
                            ->from(CmsUser::class, 'u')
1243
                            ->where('u.id != ?0')
1244
                            ->andWhere('u.username = :name');
1245
1246
        $builder->setParameter('name', 'Doctrine');
1247
        $builder->setParameter(0, 0);
1248
1249
        self::assertCount(2, $builder->getParameters());
1250
        self::assertSame(0, $builder->getParameter(0)->getValue());
1251
        self::assertSame('Doctrine', $builder->getParameter('name')->getValue());
1252
    }
1253
1254
    /**
1255
     * @group 6699
1256
     */
1257
    public function testSetParameterWithTypeJugglingWorks() : void
1258
    {
1259
        $builder = $this->em->createQueryBuilder()
1260
                            ->select('u')
1261
                            ->from(CmsUser::class, 'u')
1262
                            ->where('u.id != ?0')
1263
                            ->andWhere('u.username = :name');
1264
1265
        $builder->setParameter('0', 1);
1266
        $builder->setParameter('name', 'Doctrine');
1267
        $builder->setParameter(0, 2);
1268
        $builder->setParameter('0', 3);
1269
1270
        self::assertCount(2, $builder->getParameters());
1271
        self::assertSame(3, $builder->getParameter(0)->getValue());
1272
        self::assertSame(3, $builder->getParameter('0')->getValue());
1273
        self::assertSame('Doctrine', $builder->getParameter('name')->getValue());
1274
    }
1275
}
1276