QueryBuilderTest   F
last analyzed

Complexity

Total Complexity 92

Size/Duplication

Total Lines 1208
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 622
c 2
b 0
f 0
dl 0
loc 1208
rs 1.978
wmc 92

91 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testGetEntityManager() 0 4 1
A testInitialStateIsClean() 0 4 1
A testAddCriteriaWhereWithMultipleParametersWithDifferentFields() 0 14 1
A testAndWhereNotIn() 0 9 1
A testAndWhere() 0 9 1
A testSetParameterWithTypeJugglingWorks() 0 17 1
A testSetParameterWithNameZeroIsNotOverridden() 0 14 1
A testComplexInnerJoinWithIndexBy() 0 10 1
A testSimpleDelete() 0 6 1
A testInnerJoin() 0 8 1
A testGetAllAliasesWithNoJoins() 0 8 1
A testOrWhereIn() 0 9 1
A testHaving() 0 9 1
A testComplexInnerJoin() 0 10 1
A testOrderBy() 0 8 1
A testSimpleSelectWithIndexBy() 0 8 1
A testGetAllAliasesWithJoins() 0 10 1
A testLeftJoin() 0 8 1
A testGroupBy() 0 9 1
A testComplexAndWhereOrWhereNesting() 0 12 1
A assertValidQueryBuilder() 0 6 1
A testAddCriteriaOrderOnJoinAlias() 0 14 1
A testGetQuery() 0 8 1
A testMultipleFrom() 0 8 1
A testAddCriteriaOrder() 0 13 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 testRebuildsFromParts() 0 17 2
A testAddCriteriaLimit() 0 14 1
A testUpdateSetsType() 0 7 1
A testOrWhere() 0 9 1
A testAddCriteriaWhereWithMultipleParametersWithSubpathsAndDifferentProperties() 0 14 1
A testEmptySelectSetsType() 0 7 1
A testAddCriteriaWhereWithMultipleParametersWithSubpathsAndSameProperty() 0 14 1
A testSelectSetsType() 0 7 1
A testGetParameterTypeJuggling() 0 12 1
A testAddCriteriaUndefinedLimit() 0 14 1
A testAddOrderByWithExpression() 0 9 1
A testComplexAndWhere() 0 9 1
A testAndWhereIn() 0 9 1
A testSimpleUpdate() 0 7 1
A testSecondLevelCacheQueryBuilderOptions() 0 37 1
A testWhere() 0 8 1
A testSetParameterWithNameZeroDoesNotOverrideAnotherParameter() 0 14 1
A testAddCriteriaWhereWithMultipleParametersWithSameField() 0 14 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 testOrderByWithExpression() 0 8 1
A testAddMultipleSameCriteriaWhere() 0 16 1
A testOrWhereNotIn() 0 9 1
A testDeleteSetsType() 0 7 1
A testWhereAppend() 0 8 1
A testGetRootAliases() 0 7 1
A testEmptyStringLiteral() 0 9 1
A testAddFromString() 0 7 1
A testGetSeveralRootAliases() 0 9 1
A testEmptyNumericLiteral() 0 9 1
A testAddDistinct() 0 8 1
A testGetRootEntities() 0 7 1
A testBCAddJoinWithoutRootAlias() 0 8 1
A testGetRootAlias() 0 7 1
A testParametersAreCloned() 0 12 1
A testSetParameters() 0 14 1
A testAddCriteriaWhereWithJoinAlias() 0 15 1
A testMultipleWhere() 0 8 1
A testSelectWithFuncExpression() 0 7 1
A testSomeAllAny() 0 10 1
A testGetParameter() 0 13 1
A testWhereInWithObjectLiterals() 0 13 1
A testResetAllDQLParts() 0 12 1
A testAddCriteriaWhereOnJoinAliasWithDuplicateFields() 0 17 1
A testAddCriteriaWhereWithDefaultAndJoinAlias() 0 15 1
A testMultipleOrWhere() 0 8 1
A testMultipleIsolatedQueryConstruction() 0 24 1
A testDeepClone() 0 15 1
A testMultipleAndWhere() 0 8 1
A testResetDQLParts() 0 12 1
A testResetDQLPart() 0 14 1
A testComplexWhere() 0 12 1
A testGetParameters() 0 13 1
A testWhereInWithStringLiterals() 0 12 1
A testAlteringQueryChangesStateToDirty() 0 7 1
A testNegation() 0 13 1
A testSetParameter() 0 14 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\Mocks\EntityManagerMock;
15
use Doctrine\Tests\Models\Cache\State;
16
use Doctrine\Tests\Models\CMS\CmsArticle;
17
use Doctrine\Tests\Models\CMS\CmsGroup;
18
use Doctrine\Tests\Models\CMS\CmsUser;
19
use Doctrine\Tests\OrmTestCase;
20
use InvalidArgumentException;
21
use function array_filter;
22
use function get_class;
23
24
/**
25
 * Test case for the QueryBuilder class used to build DQL query string in a
26
 * object oriented way.
27
 */
28
class QueryBuilderTest extends OrmTestCase
29
{
30
    /** @var EntityManagerMock */
31
    private $em;
32
33
    protected function setUp() : void
34
    {
35
        $this->em = $this->getTestEntityManager();
36
    }
37
38
    protected function assertValidQueryBuilder(QueryBuilder $qb, $expectedDql)
39
    {
40
        $dql = $qb->getDQL();
41
        $q   = $qb->getQuery();
0 ignored issues
show
Unused Code introduced by
The assignment to $q is dead and can be removed.
Loading history...
42
43
        self::assertEquals($expectedDql, $dql);
44
    }
45
46
    public function testSelectSetsType() : void
47
    {
48
        $qb = $this->em->createQueryBuilder()
49
            ->delete(CmsUser::class, 'u')
50
            ->select('u.id', 'u.username');
51
52
        self::assertEquals($qb->getType(), QueryBuilder::SELECT);
53
    }
54
55
    public function testEmptySelectSetsType() : void
56
    {
57
        $qb = $this->em->createQueryBuilder()
58
            ->delete(CmsUser::class, 'u')
59
            ->select();
60
61
        self::assertEquals($qb->getType(), QueryBuilder::SELECT);
62
    }
63
64
    public function testDeleteSetsType() : void
65
    {
66
        $qb = $this->em->createQueryBuilder()
67
            ->from(CmsUser::class, 'u')
68
            ->delete();
69
70
        self::assertEquals($qb->getType(), QueryBuilder::DELETE);
71
    }
72
73
    public function testUpdateSetsType() : void
74
    {
75
        $qb = $this->em->createQueryBuilder()
76
            ->from(CmsUser::class, 'u')
77
            ->update();
78
79
        self::assertEquals($qb->getType(), QueryBuilder::UPDATE);
80
    }
81
82
    public function testSimpleSelect() : void
83
    {
84
        $qb = $this->em->createQueryBuilder()
85
            ->from(CmsUser::class, 'u')
86
            ->select('u.id', 'u.username');
87
88
        self::assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\ORM\Query...sertValidQueryBuilder() is not static, but was called statically. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

415
        self::/** @scrutinizer ignore-call */ 
416
              assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
Loading history...
416
    }
417
418
    public function testAddCriteriaWhere() : void
419
    {
420
        $qb = $this->em->createQueryBuilder();
421
        $qb->select('u')
422
            ->from(CmsUser::class, 'u');
423
424
        $criteria = new Criteria();
425
        $criteria->where($criteria->expr()->eq('field', 'value'));
426
427
        $qb->addCriteria($criteria);
428
429
        self::assertEquals('u.field = :field', (string) $qb->getDQLPart('where'));
430
        self::assertNotNull($qb->getParameter('field'));
431
    }
432
433
    public function testAddMultipleSameCriteriaWhere() : void
434
    {
435
        $qb = $this->em->createQueryBuilder();
436
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
437
438
        $criteria = new Criteria();
439
        $criteria->where($criteria->expr()->andX(
440
            $criteria->expr()->eq('field', 'value1'),
441
            $criteria->expr()->eq('field', 'value2')
442
        ));
443
444
        $qb->addCriteria($criteria);
445
446
        self::assertEquals('alias1.field = :field AND alias1.field = :field_1', (string) $qb->getDQLPart('where'));
447
        self::assertNotNull($qb->getParameter('field'));
448
        self::assertNotNull($qb->getParameter('field_1'));
449
    }
450
451
    /**
452
     * @group DDC-2844
453
     */
454
    public function testAddCriteriaWhereWithMultipleParametersWithSameField() : void
455
    {
456
        $qb = $this->em->createQueryBuilder();
457
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
458
459
        $criteria = new Criteria();
460
        $criteria->where($criteria->expr()->eq('field', 'value1'));
461
        $criteria->andWhere($criteria->expr()->gt('field', 'value2'));
462
463
        $qb->addCriteria($criteria);
464
465
        self::assertEquals('alias1.field = :field AND alias1.field > :field_1', (string) $qb->getDQLPart('where'));
466
        self::assertSame('value1', $qb->getParameter('field')->getValue());
467
        self::assertSame('value2', $qb->getParameter('field_1')->getValue());
468
    }
469
470
    /**
471
     * @group DDC-2844
472
     */
473
    public function testAddCriteriaWhereWithMultipleParametersWithDifferentFields() : void
474
    {
475
        $qb = $this->em->createQueryBuilder();
476
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
477
478
        $criteria = new Criteria();
479
        $criteria->where($criteria->expr()->eq('field1', 'value1'));
480
        $criteria->andWhere($criteria->expr()->gt('field2', 'value2'));
481
482
        $qb->addCriteria($criteria);
483
484
        self::assertEquals('alias1.field1 = :field1 AND alias1.field2 > :field2', (string) $qb->getDQLPart('where'));
485
        self::assertSame('value1', $qb->getParameter('field1')->getValue());
486
        self::assertSame('value2', $qb->getParameter('field2')->getValue());
487
    }
488
489
    /**
490
     * @group DDC-2844
491
     */
492
    public function testAddCriteriaWhereWithMultipleParametersWithSubpathsAndDifferentProperties() : void
493
    {
494
        $qb = $this->em->createQueryBuilder();
495
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
496
497
        $criteria = new Criteria();
498
        $criteria->where($criteria->expr()->eq('field1', 'value1'));
499
        $criteria->andWhere($criteria->expr()->gt('field2', 'value2'));
500
501
        $qb->addCriteria($criteria);
502
503
        self::assertEquals('alias1.field1 = :field1 AND alias1.field2 > :field2', (string) $qb->getDQLPart('where'));
504
        self::assertSame('value1', $qb->getParameter('field1')->getValue());
505
        self::assertSame('value2', $qb->getParameter('field2')->getValue());
506
    }
507
508
    /**
509
     * @group DDC-2844
510
     */
511
    public function testAddCriteriaWhereWithMultipleParametersWithSubpathsAndSameProperty() : void
512
    {
513
        $qb = $this->em->createQueryBuilder();
514
        $qb->select('alias1')->from(CmsUser::class, 'alias1');
515
516
        $criteria = new Criteria();
517
        $criteria->where($criteria->expr()->eq('field1', 'value1'));
518
        $criteria->andWhere($criteria->expr()->gt('field1', 'value2'));
519
520
        $qb->addCriteria($criteria);
521
522
        self::assertEquals('alias1.field1 = :field1 AND alias1.field1 > :field1_1', (string) $qb->getDQLPart('where'));
523
        self::assertSame('value1', $qb->getParameter('field1')->getValue());
524
        self::assertSame('value2', $qb->getParameter('field1_1')->getValue());
525
    }
526
527
    public function testAddCriteriaOrder() : void
528
    {
529
        $qb = $this->em->createQueryBuilder();
530
        $qb->select('u')
531
            ->from(CmsUser::class, 'u');
532
533
        $criteria = new Criteria();
534
        $criteria->orderBy(['field' => Criteria::DESC]);
535
536
        $qb->addCriteria($criteria);
537
538
        self::assertCount(1, $orderBy = $qb->getDQLPart('orderBy'));
539
        self::assertEquals('u.field DESC', (string) $orderBy[0]);
540
    }
541
542
    /**
543
     * @group DDC-3108
544
     */
545
    public function testAddCriteriaOrderOnJoinAlias() : void
546
    {
547
        $qb = $this->em->createQueryBuilder();
548
        $qb->select('u')
549
            ->from(CmsUser::class, 'u')
550
            ->join('u.article', 'a');
551
552
        $criteria = new Criteria();
553
        $criteria->orderBy(['a.field' => Criteria::DESC]);
554
555
        $qb->addCriteria($criteria);
556
557
        self::assertCount(1, $orderBy = $qb->getDQLPart('orderBy'));
558
        self::assertEquals('a.field DESC', (string) $orderBy[0]);
559
    }
560
561
    public function testAddCriteriaLimit() : void
562
    {
563
        $qb = $this->em->createQueryBuilder();
564
        $qb->select('u')
565
            ->from(CmsUser::class, 'u');
566
567
        $criteria = new Criteria();
568
        $criteria->setFirstResult(2);
569
        $criteria->setMaxResults(10);
570
571
        $qb->addCriteria($criteria);
572
573
        self::assertEquals(2, $qb->getFirstResult());
574
        self::assertEquals(10, $qb->getMaxResults());
575
    }
576
577
    public function testAddCriteriaUndefinedLimit() : void
578
    {
579
        $qb = $this->em->createQueryBuilder();
580
        $qb->select('u')
581
            ->from(CmsUser::class, 'u')
582
            ->setFirstResult(2)
583
            ->setMaxResults(10);
584
585
        $criteria = new Criteria();
586
587
        $qb->addCriteria($criteria);
588
589
        self::assertEquals(2, $qb->getFirstResult());
590
        self::assertEquals(10, $qb->getMaxResults());
591
    }
592
593
    public function testGetQuery() : void
594
    {
595
        $qb = $this->em->createQueryBuilder()
596
            ->select('u')
597
            ->from(CmsUser::class, 'u');
598
        $q  = $qb->getQuery();
599
600
        self::assertEquals(Query::class, get_class($q));
601
    }
602
603
    public function testSetParameter() : void
604
    {
605
        $qb = $this->em->createQueryBuilder()
606
            ->select('u')
607
            ->from(CmsUser::class, 'u')
608
            ->where('u.id = :id')
609
            ->setParameter('id', 1);
610
611
        $parameter = new Parameter('id', 1, ParameterTypeInferer::inferType(1));
612
        $inferred  = $qb->getParameter('id');
613
614
        self::assertSame($parameter->getValue(), $inferred->getValue());
615
        self::assertSame($parameter->getType(), $inferred->getType());
616
        self::assertFalse($inferred->typeWasSpecified());
617
    }
618
619
    public function testSetParameters() : void
620
    {
621
        $qb = $this->em->createQueryBuilder();
622
        $qb->select('u')
623
           ->from(CmsUser::class, 'u')
624
           ->where($qb->expr()->orX('u.username = :username', 'u.username = :username2'));
625
626
        $parameters = new ArrayCollection();
627
        $parameters->add(new Parameter('username', 'jwage'));
628
        $parameters->add(new Parameter('username2', 'jonwage'));
629
630
        $qb->setParameters($parameters);
631
632
        self::assertEquals($parameters, $qb->getQuery()->getParameters());
633
    }
634
635
    public function testGetParameters() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
792
    {
793
        $qb = $this->em->createQueryBuilder();
794
        self::assertEquals($this->em->getWrappedEntityManager(), $qb->getEntityManager());
795
    }
796
797
    public function testInitialStateIsClean() : void
798
    {
799
        $qb = $this->em->createQueryBuilder();
800
        self::assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState());
801
    }
802
803
    public function testAlteringQueryChangesStateToDirty() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : 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('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() : 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
        $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
     * @group DDC-1933
949
     */
950
    public function testParametersAreCloned() : void
951
    {
952
        $originalQb = new QueryBuilder($this->em);
953
954
        $originalQb->setParameter('parameter1', 'value1');
955
956
        $copy = clone $originalQb;
957
        $copy->setParameter('parameter2', 'value2');
958
959
        self::assertCount(1, $originalQb->getParameters());
960
        self::assertSame('value1', $copy->getParameter('parameter1')->getValue());
961
        self::assertSame('value2', $copy->getParameter('parameter2')->getValue());
962
    }
963
964
    public function testGetRootAlias() : void
965
    {
966
        $qb = $this->em->createQueryBuilder()
967
            ->select('u')
968
            ->from(CmsUser::class, 'u');
969
970
        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

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

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

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

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