QueryBuilderTest   F
last analyzed

Complexity

Total Complexity 92

Size/Duplication

Total Lines 1208
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 619
dl 0
loc 1208
rs 1.981
c 0
b 0
f 0
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 testAddCriteriaWhereWithJoinAlias() 0 15 1
A testAndWhere() 0 9 1
A testMultipleWhere() 0 8 1
A testSelectWithFuncExpression() 0 7 1
A testSetParameterWithTypeJugglingWorks() 0 17 1
A testSetParameterWithNameZeroIsNotOverridden() 0 14 1
A testComplexInnerJoinWithIndexBy() 0 10 1
A testSomeAllAny() 0 10 1
A testSimpleDelete() 0 6 1
A testInnerJoin() 0 8 1
A testGetParameter() 0 13 1
A testGetAllAliasesWithNoJoins() 0 8 1
A testOrWhereIn() 0 9 1
A testWhereInWithObjectLiterals() 0 13 1
A testHaving() 0 9 1
A testResetAllDQLParts() 0 12 1
A testAddCriteriaWhereOnJoinAliasWithDuplicateFields() 0 17 1
A testComplexInnerJoin() 0 10 1
A testAddCriteriaWhereWithDefaultAndJoinAlias() 0 15 1
A testMultipleOrWhere() 0 8 1
A testOrderBy() 0 8 1
A testMultipleIsolatedQueryConstruction() 0 24 1
A testWhereAppend() 0 8 1
A testSimpleSelectWithIndexBy() 0 8 1
A testDeepClone() 0 15 1
A testGetAllAliasesWithJoins() 0 10 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 testResetDQLParts() 0 12 1
A testSetParameter() 0 11 1
A testGetRootAliases() 0 7 1
A testAddCriteriaOrderOnJoinAlias() 0 14 1
A testGetQuery() 0 8 1
A testEmptyStringLiteral() 0 9 1
A testResetDQLPart() 0 14 1
A testMultipleFrom() 0 8 1
A testAddFromString() 0 7 1
A testAddCriteriaOrder() 0 13 1
A testSimpleSelectWithFromIndexBy() 0 7 1
A testAddOrderBy() 0 9 1
A testComplexWhere() 0 12 1
A testMultipleFromWithJoin() 0 9 1
A testSimpleSelect() 0 7 1
A testLeftJoinWithIndexBy() 0 8 1
A testGetParameters() 0 13 1
A testRebuildsFromParts() 0 17 2
A testAddCriteriaLimit() 0 14 1
A testGetSeveralRootAliases() 0 9 1
A testUpdateSetsType() 0 7 1
A testWhereInWithStringLiterals() 0 12 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 testSetParameters() 0 14 1
A testComplexAndWhere() 0 9 1
A testEmptyNumericLiteral() 0 9 1
A testAddDistinct() 0 8 1
A testGetRootEntities() 0 7 1
A testAndWhereIn() 0 9 1
A testSimpleUpdate() 0 7 1
A testSecondLevelCacheQueryBuilderOptions() 0 37 1
A testBCAddJoinWithoutRootAlias() 0 8 1
A testWhere() 0 8 1
A testAlteringQueryChangesStateToDirty() 0 7 1
A testSetParameterWithNameZeroDoesNotOverrideAnotherParameter() 0 14 1
A testAddCriteriaWhereWithMultipleParametersWithSameField() 0 14 1
A testAddCriteriaWhere() 0 13 1
A testMultipleFromWithIndexBy() 0 9 1
A testGetRootAlias() 0 7 1
A testOrHaving() 0 11 1
A testAndHaving() 0 10 1
A testNegation() 0 13 1
A testMultipleFromWithMultipleJoin() 0 11 1
A testOrderByWithExpression() 0 8 1
A testAddMultipleSameCriteriaWhere() 0 16 1
A testParametersAreCloned() 0 12 1
A testOrWhereNotIn() 0 9 1
A testDeleteSetsType() 0 7 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\EntityManagerInterface;
11
use Doctrine\ORM\Query;
12
use Doctrine\ORM\Query\Parameter;
13
use Doctrine\ORM\Query\ParameterTypeInferer;
14
use Doctrine\ORM\QueryBuilder;
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 function array_filter;
21
use function get_class;
22
23
/**
24
 * Test case for the QueryBuilder class used to build DQL query string in a
25
 * object oriented way.
26
 *
27
 */
28
class QueryBuilderTest extends OrmTestCase
29
{
30
    /** @var EntityManagerInterface */
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
613
        self::assertEquals($parameter, $qb->getParameter('id'));
614
    }
615
616
    public function testSetParameters() : void
617
    {
618
        $qb = $this->em->createQueryBuilder();
619
        $qb->select('u')
620
           ->from(CmsUser::class, 'u')
621
           ->where($qb->expr()->orX('u.username = :username', 'u.username = :username2'));
622
623
        $parameters = new ArrayCollection();
624
        $parameters->add(new Parameter('username', 'jwage'));
625
        $parameters->add(new Parameter('username2', 'jonwage'));
626
627
        $qb->setParameters($parameters);
628
629
        self::assertEquals($parameters, $qb->getQuery()->getParameters());
630
    }
631
632
633
    public function testGetParameters() : void
634
    {
635
        $qb = $this->em->createQueryBuilder();
636
        $qb->select('u')
637
           ->from(CmsUser::class, 'u')
638
           ->where('u.id = :id');
639
640
        $parameters = new ArrayCollection();
641
        $parameters->add(new Parameter('id', 1));
642
643
        $qb->setParameters($parameters);
644
645
        self::assertEquals($parameters, $qb->getParameters());
646
    }
647
648
    public function testGetParameter() : void
649
    {
650
        $qb = $this->em->createQueryBuilder()
651
            ->select('u')
652
            ->from(CmsUser::class, 'u')
653
            ->where('u.id = :id');
654
655
        $parameters = new ArrayCollection();
656
        $parameters->add(new Parameter('id', 1));
657
658
        $qb->setParameters($parameters);
659
660
        self::assertEquals($parameters->first(), $qb->getParameter('id'));
661
    }
662
663
    public function testMultipleWhere() : void
664
    {
665
        $qb = $this->em->createQueryBuilder()
666
            ->select('u')
667
            ->from(CmsUser::class, 'u')
668
            ->where('u.id = :uid', 'u.id = :uid2');
669
670
        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

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

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

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

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

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

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

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

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

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

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

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

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

1073
            ->add('where', /** @scrutinizer ignore-type */ 'u.foo = ?1')
Loading history...
1074
            ->add('where', 'u.bar = ?2', true)
1075
        ;
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