Passed
Pull Request — master (#7800)
by Maxime
09:37
created

QueryBuilderTest::testGetAllAliasesWithNoJoins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
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 testMultiSelectWithAlias() : void
92
    {
93
        $qb = $this->em->createQueryBuilder()
94
            ->from(CmsUser::class, 'u')
95
            ->select(['u' => ['id', 'username'], 'e' => ['id', 'name']]);
96
97
        self::assertValidQueryBuilder($qb, 'SELECT u.id, u.username, e.id, e.name 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

97
        self::/** @scrutinizer ignore-call */ 
98
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username, e.id, e.name FROM Doctrine\Tests\Models\CMS\CmsUser u');
Loading history...
98
    }
99
100
    public function testSimpleDelete() : void
101
    {
102
        $qb = $this->em->createQueryBuilder()
103
            ->delete(CmsUser::class, 'u');
104
105
        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

105
        self::/** @scrutinizer ignore-call */ 
106
              assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
Loading history...
106
    }
107
108
    public function testSimpleSelectWithFromIndexBy() : void
109
    {
110
        $qb = $this->em->createQueryBuilder()
111
            ->from(CmsUser::class, 'u', 'u.id')
112
            ->select('u.id', 'u.username');
113
114
        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

114
        self::/** @scrutinizer ignore-call */ 
115
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
Loading history...
115
    }
116
117
    public function testSimpleSelectWithIndexBy() : void
118
    {
119
        $qb = $this->em->createQueryBuilder()
120
            ->from(CmsUser::class, 'u')
121
            ->indexBy('u', 'u.id')
122
            ->select('u.id', 'u.username');
123
124
        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

124
        self::/** @scrutinizer ignore-call */ 
125
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
Loading history...
125
    }
126
127
    public function testSimpleUpdate() : void
128
    {
129
        $qb = $this->em->createQueryBuilder()
130
            ->update(CmsUser::class, 'u')
131
            ->set('u.username', ':username');
132
133
        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

133
        self::/** @scrutinizer ignore-call */ 
134
              assertValidQueryBuilder($qb, 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.username = :username');
Loading history...
134
    }
135
136
    public function testInnerJoin() : void
137
    {
138
        $qb = $this->em->createQueryBuilder()
139
            ->select('u', 'a')
140
            ->from(CmsUser::class, 'u')
141
            ->innerJoin('u.articles', 'a');
142
143
        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

143
        self::/** @scrutinizer ignore-call */ 
144
              assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a');
Loading history...
144
    }
145
146
    public function testComplexInnerJoin() : void
147
    {
148
        $qb = $this->em->createQueryBuilder()
149
            ->select('u', 'a')
150
            ->from(CmsUser::class, 'u')
151
            ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id');
152
153
        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

153
        self::/** @scrutinizer ignore-call */ 
154
              assertValidQueryBuilder(
Loading history...
154
            $qb,
155
            'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id'
156
        );
157
    }
158
159
    public function testComplexInnerJoinWithIndexBy() : void
160
    {
161
        $qb = $this->em->createQueryBuilder()
162
            ->select('u', 'a')
163
            ->from(CmsUser::class, 'u')
164
            ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id', 'a.name');
165
166
        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

166
        self::/** @scrutinizer ignore-call */ 
167
              assertValidQueryBuilder(
Loading history...
167
            $qb,
168
            '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'
169
        );
170
    }
171
172
    public function testLeftJoin() : void
173
    {
174
        $qb = $this->em->createQueryBuilder()
175
            ->select('u', 'a')
176
            ->from(CmsUser::class, 'u')
177
            ->leftJoin('u.articles', 'a');
178
179
        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

179
        self::/** @scrutinizer ignore-call */ 
180
              assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a');
Loading history...
180
    }
181
182
    public function testLeftJoinWithIndexBy() : void
183
    {
184
        $qb = $this->em->createQueryBuilder()
185
            ->select('u', 'a')
186
            ->from(CmsUser::class, 'u')
187
            ->leftJoin('u.articles', 'a', null, null, 'a.name');
188
189
        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

189
        self::/** @scrutinizer ignore-call */ 
190
              assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a INDEX BY a.name');
Loading history...
190
    }
191
192
    public function testMultipleFrom() : void
193
    {
194
        $qb = $this->em->createQueryBuilder()
195
            ->select('u', 'g')
196
            ->from(CmsUser::class, 'u')
197
            ->from(CmsGroup::class, 'g');
198
199
        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

199
        self::/** @scrutinizer ignore-call */ 
200
              assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g');
Loading history...
200
    }
201
202
    public function testMultipleFromWithIndexBy() : void
203
    {
204
        $qb = $this->em->createQueryBuilder()
205
            ->select('u', 'g')
206
            ->from(CmsUser::class, 'u')
207
            ->from(CmsGroup::class, 'g')
208
            ->indexBy('g', 'g.id');
209
210
        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

210
        self::/** @scrutinizer ignore-call */ 
211
              assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g INDEX BY g.id');
Loading history...
211
    }
212
213
    public function testMultipleFromWithJoin() : void
214
    {
215
        $qb = $this->em->createQueryBuilder()
216
            ->select('u', 'g')
217
            ->from(CmsUser::class, 'u')
218
            ->from(CmsGroup::class, 'g')
219
            ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id');
220
221
        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

221
        self::/** @scrutinizer ignore-call */ 
222
              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...
222
    }
223
224
    public function testMultipleFromWithMultipleJoin() : void
225
    {
226
        $qb = $this->em->createQueryBuilder()
227
            ->select('u', 'g')
228
            ->from(CmsUser::class, 'u')
229
            ->from(CmsArticle::class, 'a')
230
            ->innerJoin('u.groups', 'g')
231
            ->leftJoin('u.address', 'ad')
232
            ->innerJoin('a.comments', 'c');
233
234
        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

234
        self::/** @scrutinizer ignore-call */ 
235
              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...
235
    }
236
237
    public function testWhere() : void
238
    {
239
        $qb = $this->em->createQueryBuilder()
240
            ->select('u')
241
            ->from(CmsUser::class, 'u')
242
            ->where('u.id = :uid');
243
244
        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

244
        self::/** @scrutinizer ignore-call */ 
245
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid');
Loading history...
245
    }
246
247
    public function testComplexAndWhere() : void
248
    {
249
        $qb = $this->em->createQueryBuilder()
250
            ->select('u')
251
            ->from(CmsUser::class, 'u')
252
            ->where('u.id = :uid OR u.id = :uid2 OR u.id = :uid3')
253
            ->andWhere('u.name = :name');
254
255
        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

255
        self::/** @scrutinizer ignore-call */ 
256
              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...
256
    }
257
258
    public function testAndWhere() : void
259
    {
260
        $qb = $this->em->createQueryBuilder()
261
            ->select('u')
262
            ->from(CmsUser::class, 'u')
263
            ->where('u.id = :uid')
264
            ->andWhere('u.id = :uid2');
265
266
        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

266
        self::/** @scrutinizer ignore-call */ 
267
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
267
    }
268
269
    public function testOrWhere() : void
270
    {
271
        $qb = $this->em->createQueryBuilder()
272
            ->select('u')
273
            ->from(CmsUser::class, 'u')
274
            ->where('u.id = :uid')
275
            ->orWhere('u.id = :uid2');
276
277
        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

277
        self::/** @scrutinizer ignore-call */ 
278
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
Loading history...
278
    }
279
280
    public function testComplexAndWhereOrWhereNesting() : void
281
    {
282
        $qb = $this->em->createQueryBuilder();
283
        $qb->select('u')
284
           ->from(CmsUser::class, 'u')
285
           ->where('u.id = :uid')
286
           ->orWhere('u.id = :uid2')
287
           ->andWhere('u.id = :uid3')
288
           ->orWhere('u.name = :name1', 'u.name = :name2')
289
           ->andWhere('u.name <> :noname');
290
291
        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

291
        self::/** @scrutinizer ignore-call */ 
292
              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...
292
    }
293
294
    public function testAndWhereIn() : void
295
    {
296
        $qb = $this->em->createQueryBuilder();
297
        $qb->select('u')
298
           ->from(CmsUser::class, 'u')
299
           ->where('u.id = :uid')
300
           ->andWhere($qb->expr()->in('u.id', [1, 2, 3]));
301
302
        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

302
        self::/** @scrutinizer ignore-call */ 
303
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id IN(1, 2, 3)');
Loading history...
303
    }
304
305
    public function testOrWhereIn() : void
306
    {
307
        $qb = $this->em->createQueryBuilder();
308
        $qb->select('u')
309
           ->from(CmsUser::class, 'u')
310
           ->where('u.id = :uid')
311
           ->orWhere($qb->expr()->in('u.id', [1, 2, 3]));
312
313
        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

313
        self::/** @scrutinizer ignore-call */ 
314
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id IN(1, 2, 3)');
Loading history...
314
    }
315
316
    public function testAndWhereNotIn() : void
317
    {
318
        $qb = $this->em->createQueryBuilder();
319
        $qb->select('u')
320
           ->from(CmsUser::class, 'u')
321
           ->where('u.id = :uid')
322
           ->andWhere($qb->expr()->notIn('u.id', [1, 2, 3]));
323
324
        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

324
        self::/** @scrutinizer ignore-call */ 
325
              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...
325
    }
326
327
    public function testOrWhereNotIn() : void
328
    {
329
        $qb = $this->em->createQueryBuilder();
330
        $qb->select('u')
331
           ->from(CmsUser::class, 'u')
332
           ->where('u.id = :uid')
333
           ->orWhere($qb->expr()->notIn('u.id', [1, 2, 3]));
334
335
        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

335
        self::/** @scrutinizer ignore-call */ 
336
              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...
336
    }
337
338
    public function testGroupBy() : void
339
    {
340
        $qb = $this->em->createQueryBuilder()
341
            ->select('u')
342
            ->from(CmsUser::class, 'u')
343
            ->groupBy('u.id')
344
            ->addGroupBy('u.username');
345
346
        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

346
        self::/** @scrutinizer ignore-call */ 
347
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.username');
Loading history...
347
    }
348
349
    public function testMultiGroupByWithAlias() : void
350
    {
351
        $qb = $this->em->createQueryBuilder()
352
            ->select('u')
353
            ->from(CmsUser::class, 'u')
354
            ->groupBy(['u' => ['id', 'name'], 'e' => ['id']])
355
            ->addGroupBy('u.username');
356
357
        self::assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.name, e.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

357
        self::/** @scrutinizer ignore-call */ 
358
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.name, e.id, u.username');
Loading history...
358
    }
359
360
    public function testHaving() : void
361
    {
362
        $qb = $this->em->createQueryBuilder()
363
            ->select('u')
364
            ->from(CmsUser::class, 'u')
365
            ->groupBy('u.id')
366
            ->having('COUNT(u.id) > 1');
367
368
        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

368
        self::/** @scrutinizer ignore-call */ 
369
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1');
Loading history...
369
    }
370
371
    public function testAndHaving() : void
372
    {
373
        $qb = $this->em->createQueryBuilder()
374
            ->select('u')
375
            ->from(CmsUser::class, 'u')
376
            ->groupBy('u.id')
377
            ->having('COUNT(u.id) > 1')
378
            ->andHaving('COUNT(u.id) < 1');
379
380
        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

380
        self::/** @scrutinizer ignore-call */ 
381
              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...
381
    }
382
383
    public function testOrHaving() : void
384
    {
385
        $qb = $this->em->createQueryBuilder()
386
            ->select('u')
387
            ->from(CmsUser::class, 'u')
388
            ->groupBy('u.id')
389
            ->having('COUNT(u.id) > 1')
390
            ->andHaving('COUNT(u.id) < 1')
391
            ->orHaving('COUNT(u.id) > 1');
392
393
        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

393
        self::/** @scrutinizer ignore-call */ 
394
              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...
394
    }
395
396
    public function testOrderBy() : void
397
    {
398
        $qb = $this->em->createQueryBuilder()
399
            ->select('u')
400
            ->from(CmsUser::class, 'u')
401
            ->orderBy('u.username', 'ASC');
402
403
        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

403
        self::/** @scrutinizer ignore-call */ 
404
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
Loading history...
404
    }
405
406
    public function testOrderByWithExpression() : void
407
    {
408
        $qb = $this->em->createQueryBuilder();
409
        $qb->select('u')
410
            ->from(CmsUser::class, 'u')
411
            ->orderBy($qb->expr()->asc('u.username'));
412
413
        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

413
        self::/** @scrutinizer ignore-call */ 
414
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
Loading history...
414
    }
415
416
    public function testAddOrderBy() : void
417
    {
418
        $qb = $this->em->createQueryBuilder()
419
            ->select('u')
420
            ->from(CmsUser::class, 'u')
421
            ->orderBy('u.username', 'ASC')
422
            ->addOrderBy('u.username', 'DESC');
423
424
        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

424
        self::/** @scrutinizer ignore-call */ 
425
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
Loading history...
425
    }
426
427
    public function testAddOrderByWithExpression() : void
428
    {
429
        $qb = $this->em->createQueryBuilder();
430
        $qb->select('u')
431
            ->from(CmsUser::class, 'u')
432
            ->orderBy('u.username', 'ASC')
433
            ->addOrderBy($qb->expr()->desc('u.username'));
434
435
        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

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

692
        self::/** @scrutinizer ignore-call */ 
693
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
693
    }
694
695
    public function testMultipleAndWhere() : void
696
    {
697
        $qb = $this->em->createQueryBuilder()
698
            ->select('u')
699
            ->from(CmsUser::class, 'u')
700
            ->andWhere('u.id = :uid', 'u.id = :uid2');
701
702
        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

702
        self::/** @scrutinizer ignore-call */ 
703
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
703
    }
704
705
    public function testMultipleOrWhere() : void
706
    {
707
        $qb = $this->em->createQueryBuilder();
708
        $qb->select('u')
709
           ->from(CmsUser::class, 'u')
710
           ->orWhere('u.id = :uid', $qb->expr()->eq('u.id', ':uid2'));
711
712
        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

712
        self::/** @scrutinizer ignore-call */ 
713
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
Loading history...
713
    }
714
715
    public function testComplexWhere() : void
716
    {
717
        $qb     = $this->em->createQueryBuilder();
718
        $orExpr = $qb->expr()->orX();
719
        $orExpr->add($qb->expr()->eq('u.id', ':uid3'));
720
        $orExpr->add($qb->expr()->in('u.id', [1]));
721
722
        $qb->select('u')
723
           ->from(CmsUser::class, 'u')
724
           ->where($orExpr);
725
726
        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

726
        self::/** @scrutinizer ignore-call */ 
727
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR u.id IN(1)');
Loading history...
727
    }
728
729
    public function testWhereInWithStringLiterals() : void
730
    {
731
        $qb = $this->em->createQueryBuilder();
732
        $qb->select('u')
733
           ->from(CmsUser::class, 'u')
734
           ->where($qb->expr()->in('u.name', ['one', 'two', 'three']));
735
736
        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

736
        self::/** @scrutinizer ignore-call */ 
737
              assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
Loading history...
737
738
        $qb->where($qb->expr()->in('u.name', ["O'Reilly", "O'Neil", 'Smith']));
739
740
        self::assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith')");
741
    }
742
743
    public function testWhereInWithObjectLiterals() : void
744
    {
745
        $qb   = $this->em->createQueryBuilder();
746
        $expr = $this->em->getExpressionBuilder();
747
        $qb->select('u')
748
           ->from(CmsUser::class, 'u')
749
           ->where($expr->in('u.name', [$expr->literal('one'), $expr->literal('two'), $expr->literal('three')]));
750
751
        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

751
        self::/** @scrutinizer ignore-call */ 
752
              assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
Loading history...
752
753
        $qb->where($expr->in('u.name', [$expr->literal("O'Reilly"), $expr->literal("O'Neil"), $expr->literal('Smith')]));
754
755
        self::assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith')");
756
    }
757
758
    public function testNegation() : void
759
    {
760
        $expr   = $this->em->getExpressionBuilder();
761
        $orExpr = $expr->orX();
762
        $orExpr->add($expr->eq('u.id', ':uid3'));
763
        $orExpr->add($expr->not($expr->in('u.id', [1])));
764
765
        $qb = $this->em->createQueryBuilder();
766
        $qb->select('u')
767
           ->from(CmsUser::class, 'u')
768
           ->where($orExpr);
769
770
        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

770
        self::/** @scrutinizer ignore-call */ 
771
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR NOT(u.id IN(1))');
Loading history...
771
    }
772
773
    public function testSomeAllAny() : void
774
    {
775
        $qb   = $this->em->createQueryBuilder();
776
        $expr = $this->em->getExpressionBuilder();
777
778
        $qb->select('u')
779
           ->from(CmsUser::class, 'u')
780
           ->where($expr->gt('u.id', $expr->all('select a.id from Doctrine\Tests\Models\CMS\CmsArticle a')));
781
782
        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

782
        self::/** @scrutinizer ignore-call */ 
783
              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...
783
    }
784
785
    public function testMultipleIsolatedQueryConstruction() : void
786
    {
787
        $qb   = $this->em->createQueryBuilder();
788
        $expr = $this->em->getExpressionBuilder();
789
790
        $qb->select('u')->from(CmsUser::class, 'u');
791
        $qb->where($expr->eq('u.name', ':name'));
792
        $qb->setParameter('name', 'romanb');
793
794
        $q1 = $qb->getQuery();
795
796
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name', $q1->getDQL());
797
        self::assertCount(1, $q1->getParameters());
798
799
        // add another condition and construct a second query
800
        $qb->andWhere($expr->eq('u.id', ':id'));
801
        $qb->setParameter('id', 42);
802
803
        $q2 = $qb->getQuery();
804
805
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name AND u.id = :id', $q2->getDQL());
806
        self::assertNotSame($q1, $q2); // two different, independent queries
807
        self::assertCount(2, $q2->getParameters());
808
        self::assertCount(1, $q1->getParameters()); // $q1 unaffected
809
    }
810
811
    public function testGetEntityManager() : void
812
    {
813
        $qb = $this->em->createQueryBuilder();
814
        self::assertEquals($this->em->getWrappedEntityManager(), $qb->getEntityManager());
815
    }
816
817
    public function testInitialStateIsClean() : void
818
    {
819
        $qb = $this->em->createQueryBuilder();
820
        self::assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState());
821
    }
822
823
    public function testAlteringQueryChangesStateToDirty() : void
824
    {
825
        $qb = $this->em->createQueryBuilder()
826
            ->select('u')
827
            ->from(CmsUser::class, 'u');
828
829
        self::assertEquals(QueryBuilder::STATE_DIRTY, $qb->getState());
830
    }
831
832
    public function testSelectWithFuncExpression() : void
833
    {
834
        $qb   = $this->em->createQueryBuilder();
835
        $expr = $qb->expr();
836
        $qb->select($expr->count('e.id'));
837
838
        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

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

990
        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...
991
    }
992
993
    public function testGetRootAliases() : void
994
    {
995
        $qb = $this->em->createQueryBuilder()
996
            ->select('u')
997
            ->from(CmsUser::class, 'u');
998
999
        self::assertEquals(['u'], $qb->getRootAliases());
1000
    }
1001
1002
    public function testGetRootEntities() : void
1003
    {
1004
        $qb = $this->em->createQueryBuilder()
1005
            ->select('u')
1006
            ->from(CmsUser::class, 'u');
1007
1008
        self::assertEquals([CmsUser::class], $qb->getRootEntities());
1009
    }
1010
1011
    public function testGetSeveralRootAliases() : void
1012
    {
1013
        $qb = $this->em->createQueryBuilder()
1014
            ->select('u')
1015
            ->from(CmsUser::class, 'u')
1016
            ->from(CmsUser::class, 'u2');
1017
1018
        self::assertEquals(['u', 'u2'], $qb->getRootAliases());
1019
        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

1019
        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...
1020
    }
1021
1022
    public function testBCAddJoinWithoutRootAlias() : void
1023
    {
1024
        $qb = $this->em->createQueryBuilder()
1025
            ->select('u')
1026
            ->from(CmsUser::class, 'u')
1027
            ->add('join', ['INNER JOIN u.groups g'], true);
1028
1029
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g', $qb->getDQL());
1030
    }
1031
1032
    /**
1033
     * @group DDC-1211
1034
     */
1035
    public function testEmptyStringLiteral() : void
1036
    {
1037
        $expr = $this->em->getExpressionBuilder();
1038
        $qb   = $this->em->createQueryBuilder()
1039
            ->select('u')
1040
            ->from(CmsUser::class, 'u')
1041
            ->where($expr->eq('u.username', $expr->literal('')));
1042
1043
        self::assertEquals("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ''", $qb->getDQL());
1044
    }
1045
1046
    /**
1047
     * @group DDC-1211
1048
     */
1049
    public function testEmptyNumericLiteral() : void
1050
    {
1051
        $expr = $this->em->getExpressionBuilder();
1052
        $qb   = $this->em->createQueryBuilder()
1053
            ->select('u')
1054
            ->from(CmsUser::class, 'u')
1055
            ->where($expr->eq('u.username', $expr->literal(0)));
1056
1057
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 0', $qb->getDQL());
1058
    }
1059
1060
    /**
1061
     * @group DDC-1227
1062
     */
1063
    public function testAddFromString() : void
1064
    {
1065
        $qb = $this->em->createQueryBuilder()
1066
            ->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

1066
            ->add('select', /** @scrutinizer ignore-type */ 'u')
Loading history...
1067
            ->add('from', CmsUser::class . ' u');
1068
1069
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
1070
    }
1071
1072
    /**
1073
     * @group DDC-1619
1074
     */
1075
    public function testAddDistinct() : void
1076
    {
1077
        $qb = $this->em->createQueryBuilder()
1078
            ->select('u')
1079
            ->distinct()
1080
            ->from(CmsUser::class, 'u');
1081
1082
        self::assertEquals('SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
1083
    }
1084
1085
    /**
1086
     * @group DDC-2192
1087
     */
1088
    public function testWhereAppend() : void
1089
    {
1090
        $this->expectException(InvalidArgumentException::class);
1091
        $this->expectExceptionMessage("Using \$append = true does not have an effect with 'where' or 'having' parts. See QueryBuilder#andWhere() for an example for correct usage.");
1092
1093
        $qb = $this->em->createQueryBuilder()
0 ignored issues
show
Unused Code introduced by
The assignment to $qb is dead and can be removed.
Loading history...
1094
            ->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

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