Failed Conditions
Push — master ( ddb3cd...4476ec )
by Marco
11:47
created

QueryBuilderTest   F

Complexity

Total Complexity 92

Size/Duplication

Total Lines 1210
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 92
c 1
b 0
f 0
dl 0
loc 1210
rs 0.6314

91 Methods

Rating   Name   Duplication   Size   Complexity  
A testAndWhere() 0 9 1
A testMultipleWhere() 0 8 1
A testComplexInnerJoinWithIndexBy() 0 10 1
A testSimpleDelete() 0 6 1
A testInnerJoin() 0 8 1
A testWhereInWithObjectLiterals() 0 13 1
A testHaving() 0 9 1
A testComplexInnerJoin() 0 10 1
A testOrderBy() 0 8 1
A testSimpleSelectWithIndexBy() 0 8 1
A testLeftJoin() 0 8 1
A testGroupBy() 0 9 1
A testMultipleAndWhere() 0 8 1
A testComplexAndWhereOrWhereNesting() 0 12 1
A assertValidQueryBuilder() 0 6 1
A testSetParameter() 0 11 1
A testGetQuery() 0 8 1
A testMultipleFrom() 0 8 1
A testSimpleSelectWithFromIndexBy() 0 7 1
A testAddOrderBy() 0 9 1
A testMultipleFromWithJoin() 0 9 1
A testSimpleSelect() 0 7 1
A testLeftJoinWithIndexBy() 0 8 1
A testAddCriteriaLimit() 0 14 1
A testWhereInWithStringLiterals() 0 12 1
A testOrWhere() 0 9 1
A testSelectSetsType() 0 7 1
A testAddCriteriaUndefinedLimit() 0 14 1
A testSetParameters() 0 14 1
A testComplexAndWhere() 0 9 1
A testSimpleUpdate() 0 7 1
A testWhere() 0 8 1
A setUp() 0 3 1
A testAddCriteriaWhere() 0 13 1
A testMultipleFromWithIndexBy() 0 9 1
A testOrHaving() 0 11 1
A testAndHaving() 0 10 1
A testMultipleFromWithMultipleJoin() 0 11 1
A testAddMultipleSameCriteriaWhere() 0 16 1
A testGetParameters() 0 13 1
A testAddCriteriaWhereWithMultipleParametersWithDifferentFields() 0 14 1
A testAndWhereNotIn() 0 9 1
A testSomeAllAny() 0 10 1
A testGetParameter() 0 13 1
A testOrWhereIn() 0 9 1
A testMultipleOrWhere() 0 8 1
A testAddCriteriaOrderOnJoinAlias() 0 14 1
A testAddCriteriaOrder() 0 13 1
A testComplexWhere() 0 12 1
A testUpdateSetsType() 0 7 1
A testAddCriteriaWhereWithMultipleParametersWithSubpathsAndDifferentProperties() 0 14 1
A testEmptySelectSetsType() 0 7 1
A testAddCriteriaWhereWithMultipleParametersWithSubpathsAndSameProperty() 0 14 1
A testAddOrderByWithExpression() 0 9 1
A testAndWhereIn() 0 9 1
A testAddCriteriaWhereWithMultipleParametersWithSameField() 0 14 1
A testNegation() 0 13 1
A testOrderByWithExpression() 0 8 1
A testOrWhereNotIn() 0 9 1
A testDeleteSetsType() 0 7 1
A testAddCriteriaWhereWithJoinAlias() 0 15 1
A testSelectWithFuncExpression() 0 7 1
A testSetParameterWithTypeJugglingWorks() 0 17 1
A testSetParameterWithNameZeroIsNotOverridden() 0 14 1
A testGetAllAliasesWithNoJoins() 0 8 1
A testResetAllDQLParts() 0 12 1
A testAddCriteriaWhereOnJoinAliasWithDuplicateFields() 0 17 1
A testAddCriteriaWhereWithDefaultAndJoinAlias() 0 15 1
B testMultipleIsolatedQueryConstruction() 0 24 1
A testWhereAppend() 0 8 1
A testGetEntityManager() 0 4 1
A testDeepClone() 0 15 1
A testGetAllAliasesWithJoins() 0 10 1
A testResetDQLParts() 0 12 1
A testGetRootAliases() 0 7 1
A testEmptyStringLiteral() 0 9 1
A testResetDQLPart() 0 14 1
A testAddFromString() 0 7 1
A testRebuildsFromParts() 0 17 2
A testGetSeveralRootAliases() 0 9 1
A testGetParameterTypeJuggling() 0 12 1
A testInitialStateIsClean() 0 4 1
A testEmptyNumericLiteral() 0 9 1
A testAddDistinct() 0 8 1
A testGetRootEntities() 0 7 1
B testSecondLevelCacheQueryBuilderOptions() 0 37 1
A testBCAddJoinWithoutRootAlias() 0 8 1
A testAlteringQueryChangesStateToDirty() 0 7 1
A testSetParameterWithNameZeroDoesNotOverrideAnotherParameter() 0 14 1
A testGetRootAlias() 0 7 1
A testParametersAreCloned() 0 12 1

How to fix   Complexity   

Complex Class

Complex classes like QueryBuilderTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use QueryBuilderTest, and based on these observations, apply Extract Interface, too.

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\Models\Cache\State;
15
use Doctrine\Tests\Models\CMS\CmsArticle;
16
use Doctrine\Tests\Models\CMS\CmsGroup;
17
use Doctrine\Tests\Models\CMS\CmsUser;
18
use Doctrine\Tests\OrmTestCase;
19
20
/**
21
 * Test case for the QueryBuilder class used to build DQL query string in a
22
 * object oriented way.
23
 *
24
 * @author      Jonathan H. Wage <[email protected]>
25
 * @author      Roman Borschel <[email protected]
26
 * @since       2.0
27
 */
28
class QueryBuilderTest extends OrmTestCase
29
{
30
    /**
31
     * @var \Doctrine\ORM\EntityManagerInterface
32
     */
33
    private $em;
34
35
    protected function setUp()
36
    {
37
        $this->em = $this->getTestEntityManager();
38
    }
39
40
    protected function assertValidQueryBuilder(QueryBuilder $qb, $expectedDql)
41
    {
42
        $dql = $qb->getDQL();
43
        $q = $qb->getQuery();
0 ignored issues
show
Unused Code introduced by
The assignment to $q is dead and can be removed.
Loading history...
44
45
        self::assertEquals($expectedDql, $dql);
46
    }
47
48
    public function testSelectSetsType()
49
    {
50
        $qb = $this->em->createQueryBuilder()
51
            ->delete(CmsUser::class, 'u')
52
            ->select('u.id', 'u.username');
53
54
        self::assertEquals($qb->getType(), QueryBuilder::SELECT);
55
    }
56
57
    public function testEmptySelectSetsType()
58
    {
59
        $qb = $this->em->createQueryBuilder()
60
            ->delete(CmsUser::class, 'u')
61
            ->select();
62
63
        self::assertEquals($qb->getType(), QueryBuilder::SELECT);
64
    }
65
66
    public function testDeleteSetsType()
67
    {
68
        $qb = $this->em->createQueryBuilder()
69
            ->from(CmsUser::class, 'u')
70
            ->delete();
71
72
        self::assertEquals($qb->getType(), QueryBuilder::DELETE);
73
    }
74
75
    public function testUpdateSetsType()
76
    {
77
        $qb = $this->em->createQueryBuilder()
78
            ->from(CmsUser::class, 'u')
79
            ->update();
80
81
        self::assertEquals($qb->getType(), QueryBuilder::UPDATE);
82
    }
83
84
    public function testSimpleSelect()
85
    {
86
        $qb = $this->em->createQueryBuilder()
87
            ->from(CmsUser::class, 'u')
88
            ->select('u.id', 'u.username');
89
90
        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

90
        self::/** @scrutinizer ignore-call */ 
91
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u');
Loading history...
91
    }
92
93
    public function testSimpleDelete()
94
    {
95
        $qb = $this->em->createQueryBuilder()
96
            ->delete(CmsUser::class, 'u');
97
98
        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

98
        self::/** @scrutinizer ignore-call */ 
99
              assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
Loading history...
99
    }
100
101
    public function testSimpleSelectWithFromIndexBy()
102
    {
103
        $qb = $this->em->createQueryBuilder()
104
            ->from(CmsUser::class, 'u', 'u.id')
105
            ->select('u.id', 'u.username');
106
107
        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

107
        self::/** @scrutinizer ignore-call */ 
108
              assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
Loading history...
108
    }
109
110
    public function testSimpleSelectWithIndexBy()
111
    {
112
        $qb = $this->em->createQueryBuilder()
113
            ->from(CmsUser::class, 'u')
114
            ->indexBy('u', 'u.id')
115
            ->select('u.id', 'u.username');
116
117
        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

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

126
        self::/** @scrutinizer ignore-call */ 
127
              assertValidQueryBuilder($qb, 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.username = :username');
Loading history...
127
    }
128
129
    public function testInnerJoin()
130
    {
131
        $qb = $this->em->createQueryBuilder()
132
            ->select('u', 'a')
133
            ->from(CmsUser::class, 'u')
134
            ->innerJoin('u.articles', 'a');
135
136
        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

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

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

159
        self::/** @scrutinizer ignore-call */ 
160
              assertValidQueryBuilder(
Loading history...
160
            $qb,
161
            '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'
162
        );
163
    }
164
165
    public function testLeftJoin()
166
    {
167
        $qb = $this->em->createQueryBuilder()
168
            ->select('u', 'a')
169
            ->from(CmsUser::class, 'u')
170
            ->leftJoin('u.articles', 'a');
171
172
        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

172
        self::/** @scrutinizer ignore-call */ 
173
              assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a');
Loading history...
173
    }
174
175
    public function testLeftJoinWithIndexBy()
176
    {
177
        $qb = $this->em->createQueryBuilder()
178
            ->select('u', 'a')
179
            ->from(CmsUser::class, 'u')
180
            ->leftJoin('u.articles', 'a', null, null, 'a.name');
181
182
        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

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

192
        self::/** @scrutinizer ignore-call */ 
193
              assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g');
Loading history...
193
    }
194
195
    public function testMultipleFromWithIndexBy()
196
    {
197
        $qb = $this->em->createQueryBuilder()
198
            ->select('u', 'g')
199
            ->from(CmsUser::class, 'u')
200
            ->from(CmsGroup::class, 'g')
201
            ->indexBy('g', 'g.id');
202
203
        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

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

214
        self::/** @scrutinizer ignore-call */ 
215
              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...
215
    }
216
217
    public function testMultipleFromWithMultipleJoin()
218
    {
219
        $qb = $this->em->createQueryBuilder()
220
            ->select('u', 'g')
221
            ->from(CmsUser::class, 'u')
222
            ->from(CmsArticle::class, 'a')
223
            ->innerJoin('u.groups', 'g')
224
            ->leftJoin('u.address', 'ad')
225
            ->innerJoin('a.comments', 'c');
226
227
        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

227
        self::/** @scrutinizer ignore-call */ 
228
              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...
228
    }
229
230
    public function testWhere()
231
    {
232
        $qb = $this->em->createQueryBuilder()
233
            ->select('u')
234
            ->from(CmsUser::class, 'u')
235
            ->where('u.id = :uid');
236
237
        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

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

248
        self::/** @scrutinizer ignore-call */ 
249
              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...
249
    }
250
251
    public function testAndWhere()
252
    {
253
        $qb = $this->em->createQueryBuilder()
254
            ->select('u')
255
            ->from(CmsUser::class, 'u')
256
            ->where('u.id = :uid')
257
            ->andWhere('u.id = :uid2');
258
259
        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

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

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

284
        self::/** @scrutinizer ignore-call */ 
285
              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...
285
    }
286
287
    public function testAndWhereIn()
288
    {
289
        $qb = $this->em->createQueryBuilder();
290
        $qb->select('u')
291
           ->from(CmsUser::class, 'u')
292
           ->where('u.id = :uid')
293
           ->andWhere($qb->expr()->in('u.id', [1, 2, 3]));
294
295
        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

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

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

317
        self::/** @scrutinizer ignore-call */ 
318
              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...
318
    }
319
320
    public function testOrWhereNotIn()
321
    {
322
        $qb = $this->em->createQueryBuilder();
323
        $qb->select('u')
324
           ->from(CmsUser::class, 'u')
325
           ->where('u.id = :uid')
326
           ->orWhere($qb->expr()->notIn('u.id', [1, 2, 3]));
327
328
        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

328
        self::/** @scrutinizer ignore-call */ 
329
              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...
329
    }
330
331
    public function testGroupBy()
332
    {
333
        $qb = $this->em->createQueryBuilder()
334
            ->select('u')
335
            ->from(CmsUser::class, 'u')
336
            ->groupBy('u.id')
337
            ->addGroupBy('u.username');
338
339
        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

339
        self::/** @scrutinizer ignore-call */ 
340
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.username');
Loading history...
340
    }
341
342
    public function testHaving()
343
    {
344
        $qb = $this->em->createQueryBuilder()
345
            ->select('u')
346
            ->from(CmsUser::class, 'u')
347
            ->groupBy('u.id')
348
            ->having('COUNT(u.id) > 1');
349
350
        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

350
        self::/** @scrutinizer ignore-call */ 
351
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1');
Loading history...
351
    }
352
353
    public function testAndHaving()
354
    {
355
        $qb = $this->em->createQueryBuilder()
356
            ->select('u')
357
            ->from(CmsUser::class, 'u')
358
            ->groupBy('u.id')
359
            ->having('COUNT(u.id) > 1')
360
            ->andHaving('COUNT(u.id) < 1');
361
362
        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

362
        self::/** @scrutinizer ignore-call */ 
363
              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...
363
    }
364
365
    public function testOrHaving()
366
    {
367
        $qb = $this->em->createQueryBuilder()
368
            ->select('u')
369
            ->from(CmsUser::class, 'u')
370
            ->groupBy('u.id')
371
            ->having('COUNT(u.id) > 1')
372
            ->andHaving('COUNT(u.id) < 1')
373
            ->orHaving('COUNT(u.id) > 1');
374
375
        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

375
        self::/** @scrutinizer ignore-call */ 
376
              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...
376
    }
377
378
    public function testOrderBy()
379
    {
380
        $qb = $this->em->createQueryBuilder()
381
            ->select('u')
382
            ->from(CmsUser::class, 'u')
383
            ->orderBy('u.username', 'ASC');
384
385
        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

385
        self::/** @scrutinizer ignore-call */ 
386
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
Loading history...
386
    }
387
388
    public function testOrderByWithExpression()
389
    {
390
        $qb = $this->em->createQueryBuilder();
391
        $qb->select('u')
392
            ->from(CmsUser::class, 'u')
393
            ->orderBy($qb->expr()->asc('u.username'));
394
395
        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

395
        self::/** @scrutinizer ignore-call */ 
396
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
Loading history...
396
    }
397
398
    public function testAddOrderBy()
399
    {
400
        $qb = $this->em->createQueryBuilder()
401
            ->select('u')
402
            ->from(CmsUser::class, 'u')
403
            ->orderBy('u.username', 'ASC')
404
            ->addOrderBy('u.username', 'DESC');
405
406
        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

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

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

672
        self::/** @scrutinizer ignore-call */ 
673
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
673
    }
674
675
    public function testMultipleAndWhere()
676
    {
677
        $qb = $this->em->createQueryBuilder()
678
            ->select('u')
679
            ->from(CmsUser::class, 'u')
680
            ->andWhere('u.id = :uid', 'u.id = :uid2');
681
682
        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

682
        self::/** @scrutinizer ignore-call */ 
683
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
Loading history...
683
    }
684
685
    public function testMultipleOrWhere()
686
    {
687
        $qb = $this->em->createQueryBuilder();
688
        $qb->select('u')
689
           ->from(CmsUser::class, 'u')
690
           ->orWhere('u.id = :uid', $qb->expr()->eq('u.id', ':uid2'));
691
692
        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

692
        self::/** @scrutinizer ignore-call */ 
693
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
Loading history...
693
    }
694
695
    public function testComplexWhere()
696
    {
697
        $qb = $this->em->createQueryBuilder();
698
        $orExpr = $qb->expr()->orX();
699
        $orExpr->add($qb->expr()->eq('u.id', ':uid3'));
700
        $orExpr->add($qb->expr()->in('u.id', [1]));
701
702
        $qb->select('u')
703
           ->from(CmsUser::class, 'u')
704
           ->where($orExpr);
705
706
        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

706
        self::/** @scrutinizer ignore-call */ 
707
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR u.id IN(1)');
Loading history...
707
    }
708
709
    public function testWhereInWithStringLiterals()
710
    {
711
        $qb = $this->em->createQueryBuilder();
712
        $qb->select('u')
713
           ->from(CmsUser::class, 'u')
714
           ->where($qb->expr()->in('u.name', ['one', 'two', 'three']));
715
716
        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

716
        self::/** @scrutinizer ignore-call */ 
717
              assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
Loading history...
717
718
        $qb->where($qb->expr()->in('u.name', ["O'Reilly", "O'Neil", 'Smith']));
719
720
        self::assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith')");
721
    }
722
723
    public function testWhereInWithObjectLiterals()
724
    {
725
        $qb = $this->em->createQueryBuilder();
726
        $expr = $this->em->getExpressionBuilder();
727
        $qb->select('u')
728
           ->from(CmsUser::class, 'u')
729
           ->where($expr->in('u.name', [$expr->literal('one'), $expr->literal('two'), $expr->literal('three')]));
730
731
        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

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

750
        self::/** @scrutinizer ignore-call */ 
751
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR NOT(u.id IN(1))');
Loading history...
751
    }
752
753
    public function testSomeAllAny()
754
    {
755
        $qb = $this->em->createQueryBuilder();
756
        $expr = $this->em->getExpressionBuilder();
757
758
        $qb->select('u')
759
           ->from(CmsUser::class, 'u')
760
           ->where($expr->gt('u.id', $expr->all('select a.id from Doctrine\Tests\Models\CMS\CmsArticle a')));
761
762
        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

762
        self::/** @scrutinizer ignore-call */ 
763
              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...
763
    }
764
765
    public function testMultipleIsolatedQueryConstruction()
766
    {
767
        $qb = $this->em->createQueryBuilder();
768
        $expr = $this->em->getExpressionBuilder();
769
770
        $qb->select('u')->from(CmsUser::class, 'u');
771
        $qb->where($expr->eq('u.name', ':name'));
772
        $qb->setParameter('name', 'romanb');
773
774
        $q1 = $qb->getQuery();
775
776
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name', $q1->getDQL());
777
        self::assertCount(1, $q1->getParameters());
778
779
        // add another condition and construct a second query
780
        $qb->andWhere($expr->eq('u.id', ':id'));
781
        $qb->setParameter('id', 42);
782
783
        $q2 = $qb->getQuery();
784
785
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name AND u.id = :id', $q2->getDQL());
786
        self::assertNotSame($q1, $q2); // two different, independent queries
787
        self::assertCount(2, $q2->getParameters());
788
        self::assertCount(1, $q1->getParameters()); // $q1 unaffected
789
    }
790
791
    public function testGetEntityManager()
792
    {
793
        $qb = $this->em->createQueryBuilder();
794
        self::assertEquals($this->em, $qb->getEntityManager());
795
    }
796
797
    public function testInitialStateIsClean()
798
    {
799
        $qb = $this->em->createQueryBuilder();
800
        self::assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState());
801
    }
802
803
    public function testAlteringQueryChangesStateToDirty()
804
    {
805
        $qb = $this->em->createQueryBuilder()
806
            ->select('u')
807
            ->from(CmsUser::class, 'u');
808
809
        self::assertEquals(QueryBuilder::STATE_DIRTY, $qb->getState());
810
    }
811
812
    public function testSelectWithFuncExpression()
813
    {
814
        $qb = $this->em->createQueryBuilder();
815
        $expr = $qb->expr();
816
        $qb->select($expr->count('e.id'));
817
818
        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

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

971
        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...
972
    }
973
974
    public function testGetRootAliases()
975
    {
976
        $qb = $this->em->createQueryBuilder()
977
            ->select('u')
978
            ->from(CmsUser::class, 'u');
979
980
        self::assertEquals(['u'], $qb->getRootAliases());
981
    }
982
983
    public function testGetRootEntities()
984
    {
985
        $qb = $this->em->createQueryBuilder()
986
            ->select('u')
987
            ->from(CmsUser::class, 'u');
988
989
        self::assertEquals([CmsUser::class], $qb->getRootEntities());
990
    }
991
992
    public function testGetSeveralRootAliases()
993
    {
994
        $qb = $this->em->createQueryBuilder()
995
            ->select('u')
996
            ->from(CmsUser::class, 'u')
997
            ->from(CmsUser::class, 'u2');
998
999
        self::assertEquals(['u', 'u2'], $qb->getRootAliases());
1000
        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

1000
        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...
1001
    }
1002
1003
    public function testBCAddJoinWithoutRootAlias()
1004
    {
1005
        $qb = $this->em->createQueryBuilder()
1006
            ->select('u')
1007
            ->from(CmsUser::class, 'u')
1008
            ->add('join', ['INNER JOIN u.groups g'], true);
1009
1010
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g', $qb->getDQL());
1011
    }
1012
1013
    /**
1014
     * @group DDC-1211
1015
     */
1016
    public function testEmptyStringLiteral()
1017
    {
1018
        $expr = $this->em->getExpressionBuilder();
1019
        $qb = $this->em->createQueryBuilder()
1020
            ->select('u')
1021
            ->from(CmsUser::class, 'u')
1022
            ->where($expr->eq('u.username', $expr->literal("")));
1023
1024
        self::assertEquals("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ''", $qb->getDQL());
1025
    }
1026
1027
    /**
1028
     * @group DDC-1211
1029
     */
1030
    public function testEmptyNumericLiteral()
1031
    {
1032
        $expr = $this->em->getExpressionBuilder();
1033
        $qb = $this->em->createQueryBuilder()
1034
            ->select('u')
1035
            ->from(CmsUser::class, 'u')
1036
            ->where($expr->eq('u.username', $expr->literal(0)));
1037
1038
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 0', $qb->getDQL());
1039
    }
1040
1041
    /**
1042
     * @group DDC-1227
1043
     */
1044
    public function testAddFromString()
1045
    {
1046
        $qb = $this->em->createQueryBuilder()
1047
            ->add('select', 'u')
0 ignored issues
show
Bug introduced by
'u' of type string is incompatible with the type object|array<mixed,mixed> 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

1047
            ->add('select', /** @scrutinizer ignore-type */ 'u')
Loading history...
1048
            ->add('from', CmsUser::class . ' u');
1049
1050
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
1051
    }
1052
1053
    /**
1054
     * @group DDC-1619
1055
     */
1056
    public function testAddDistinct()
1057
    {
1058
        $qb = $this->em->createQueryBuilder()
1059
            ->select('u')
1060
            ->distinct()
1061
            ->from(CmsUser::class, 'u');
1062
1063
        self::assertEquals('SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
1064
    }
1065
1066
    /**
1067
     * @group DDC-2192
1068
     */
1069
    public function testWhereAppend()
1070
    {
1071
        $this->expectException(\InvalidArgumentException::class);
1072
        $this->expectExceptionMessage("Using \$append = true does not have an effect with 'where' or 'having' parts. See QueryBuilder#andWhere() for an example for correct usage.");
1073
1074
        $qb = $this->em->createQueryBuilder()
0 ignored issues
show
Unused Code introduced by
The assignment to $qb is dead and can be removed.
Loading history...
1075
            ->add('where', 'u.foo = ?1')
0 ignored issues
show
Bug introduced by
'u.foo = ?1' of type string is incompatible with the type object|array<mixed,mixed> 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

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