Failed Conditions
Push — develop ( 26c78d...044239 )
by Michael
63:26
created

testTooManyParametersShouldThrowException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\Mapping\FetchMode;
9
use Doctrine\ORM\NonUniqueResultException;
10
use Doctrine\ORM\Query;
11
use Doctrine\ORM\Query\Parameter;
12
use Doctrine\ORM\Query\QueryException;
13
use Doctrine\ORM\UnexpectedResultException;
14
use Doctrine\Tests\Models\CMS\CmsArticle;
15
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
16
use Doctrine\Tests\Models\CMS\CmsUser;
17
use Doctrine\Tests\OrmFunctionalTestCase;
18
use ProxyManager\Proxy\GhostObjectInterface;
19
20
/**
21
 * Functional Query tests.
22
 *
23
 * @author robo
24
 */
25
class QueryTest extends OrmFunctionalTestCase
26
{
27
    protected function setUp()
28
    {
29
        $this->useModelSet('cms');
30
31
        parent::setUp();
32
    }
33
34
    public function testSimpleQueries()
35
    {
36
        $user = new CmsUser;
37
        $user->name = 'Guilherme';
38
        $user->username = 'gblanco';
39
        $user->status = 'developer';
40
        $this->em->persist($user);
41
        $this->em->flush();
42
        $this->em->clear();
43
44
        $query = $this->em->createQuery("select u, upper(u.name) from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'");
45
46
        $result = $query->getResult();
47
48
        self::assertCount(1, $result);
49
        self::assertInstanceOf(CmsUser::class, $result[0][0]);
50
        self::assertEquals('Guilherme', $result[0][0]->name);
51
        self::assertEquals('gblanco', $result[0][0]->username);
52
        self::assertEquals('developer', $result[0][0]->status);
53
        self::assertEquals('GUILHERME', $result[0][1]);
54
55
        $resultArray = $query->getArrayResult();
56
        self::assertCount(1, $resultArray);
57
        self::assertInternalType('array', $resultArray[0][0]);
58
        self::assertEquals('Guilherme', $resultArray[0][0]['name']);
59
        self::assertEquals('gblanco', $resultArray[0][0]['username']);
60
        self::assertEquals('developer', $resultArray[0][0]['status']);
61
        self::assertEquals('GUILHERME', $resultArray[0][1]);
62
63
        $scalarResult = $query->getScalarResult();
64
        self::assertCount(1, $scalarResult);
65
        self::assertEquals('Guilherme', $scalarResult[0]['u_name']);
66
        self::assertEquals('gblanco', $scalarResult[0]['u_username']);
67
        self::assertEquals('developer', $scalarResult[0]['u_status']);
68
        self::assertEquals('GUILHERME', $scalarResult[0][1]);
69
70
        $query = $this->em->createQuery("select upper(u.name) from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'");
71
        self::assertEquals('GUILHERME', $query->getSingleScalarResult());
72
    }
73
74
    public function testJoinQueries()
75
    {
76
        $user = new CmsUser;
77
        $user->name = 'Guilherme';
78
        $user->username = 'gblanco';
79
        $user->status = 'developer';
80
81
        $article1 = new CmsArticle;
82
        $article1->topic = "Doctrine 2";
83
        $article1->text = "This is an introduction to Doctrine 2.";
84
        $user->addArticle($article1);
85
86
        $article2 = new CmsArticle;
87
        $article2->topic = "Symfony 2";
88
        $article2->text = "This is an introduction to Symfony 2.";
89
        $user->addArticle($article2);
90
91
        $this->em->persist($user);
92
        $this->em->persist($article1);
93
        $this->em->persist($article2);
94
95
        $this->em->flush();
96
        $this->em->clear();
97
98
        $query = $this->em->createQuery('select u, a from ' . CmsUser::class . ' u join u.articles a ORDER BY a.topic');
99
        $users = $query->getResult();
100
101
        self::assertCount(1, $users);
102
        self::assertInstanceOf(CmsUser::class, $users[0]);
103
        self::assertCount(2, $users[0]->articles);
104
        self::assertEquals('Doctrine 2', $users[0]->articles[0]->topic);
105
        self::assertEquals('Symfony 2', $users[0]->articles[1]->topic);
106
    }
107
108
    public function testUsingZeroBasedQueryParameterShouldWork()
109
    {
110
        $user = new CmsUser;
111
        $user->name = 'Jonathan';
112
        $user->username = 'jwage';
113
        $user->status = 'developer';
114
        $this->em->persist($user);
115
        $this->em->flush();
116
        $this->em->clear();
117
118
        $q = $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.username = ?0');
119
        $q->setParameter(0, 'jwage');
120
        $user = $q->getSingleResult();
121
122
        self::assertNotNull($user);
123
    }
124
125 View Code Duplication
    public function testUsingUnknownQueryParameterShouldThrowException()
126
    {
127
        $this->expectException(QueryException::class);
128
        $this->expectExceptionMessage('Invalid parameter: token 2 is not defined in the query.');
129
130
        $q = $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1');
131
        $q->setParameter(2, 'jwage');
132
        $user = $q->getSingleResult();
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
    }
134
135 View Code Duplication
    public function testTooManyParametersShouldThrowException()
136
    {
137
        $this->expectException(QueryException::class);
138
        $this->expectExceptionMessage('Too many parameters: the query defines 1 parameters and you bound 2');
139
140
        $q = $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1');
141
        $q->setParameter(1, 'jwage');
142
        $q->setParameter(2, 'jwage');
143
144
        $user = $q->getSingleResult();
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
    }
146
147
    public function testTooFewParametersShouldThrowException()
148
    {
149
        $this->expectException(QueryException::class);
150
        $this->expectExceptionMessage('Too few parameters: the query defines 1 parameters but you only bound 0');
151
152
        $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1')
153
                  ->getSingleResult();
154
    }
155
156
    public function testInvalidInputParameterThrowsException()
157
    {
158
        $this->expectException(QueryException::class);
159
160
        $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?')
161
                  ->setParameter(1, 'jwage')
162
                  ->getSingleResult();
163
    }
164
165
    public function testSetParameters()
166
    {
167
        $parameters = new ArrayCollection();
168
        $parameters->add(new Parameter(1, 'jwage'));
169
        $parameters->add(new Parameter(2, 'active'));
170
171
        $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1 AND u.status = ?2')
172
                  ->setParameters($parameters)
173
                  ->getResult();
174
175
        $extractValue = function (Parameter $parameter) {
176
            return $parameter->getValue();
177
        };
178
179
        self::assertSame(
180
            $parameters->map($extractValue)->toArray(),
181
            $this->sqlLoggerStack->queries[$this->sqlLoggerStack->currentQuery]['params']
182
        );
183
    }
184
185
    public function testSetParametersBackwardsCompatible()
186
    {
187
        $parameters = [1 => 'jwage', 2 => 'active'];
188
189
        $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1 AND u.status = ?2')
190
                  ->setParameters($parameters)
191
                  ->getResult();
192
193
        self::assertSame(
194
            array_values($parameters),
195
            $this->sqlLoggerStack->queries[$this->sqlLoggerStack->currentQuery]['params']
196
        );
197
    }
198
199
    /**
200
     * @group DDC-1070
201
     */
202
    public function testIterateResultAsArrayAndParams()
203
    {
204
        $article1 = new CmsArticle;
205
        $article1->topic = "Doctrine 2";
206
        $article1->text = "This is an introduction to Doctrine 2.";
207
208
        $article2 = new CmsArticle;
209
        $article2->topic = "Symfony 2";
210
        $article2->text = "This is an introduction to Symfony 2.";
211
212
        $this->em->persist($article1);
213
        $this->em->persist($article2);
214
215
        $this->em->flush();
216
        $this->em->clear();
217
        $articleId = $article1->id;
218
219
        $query = $this->em->createQuery('select a from ' . CmsArticle::class . ' a WHERE a.topic = ?1');
220
        $articles = $query->iterate(new ArrayCollection([new Parameter(1, 'Doctrine 2')]), Query::HYDRATE_ARRAY);
221
222
        $found = [];
223
224
        foreach ($articles AS $article) {
225
            $found[] = $article;
226
        }
227
228
        self::assertCount(1, $found);
229
        self::assertEquals(
230
            [
231
                [
232
                    [
233
                        'id'      => $articleId,
234
                        'topic'   => 'Doctrine 2',
235
                        'text'    => 'This is an introduction to Doctrine 2.',
236
                        'version' => 1
237
                    ]
238
                ]
239
            ],
240
            $found
241
        );
242
    }
243
244
    public function testIterateResult_IterativelyBuildUpUnitOfWork()
245
    {
246
        $article1 = new CmsArticle;
247
        $article1->topic = "Doctrine 2";
248
        $article1->text = "This is an introduction to Doctrine 2.";
249
250
        $article2 = new CmsArticle;
251
        $article2->topic = "Symfony 2";
252
        $article2->text = "This is an introduction to Symfony 2.";
253
254
        $this->em->persist($article1);
255
        $this->em->persist($article2);
256
257
        $this->em->flush();
258
        $this->em->clear();
259
260
        $query = $this->em->createQuery('select a from ' . CmsArticle::class . ' a');
261
        $articles = $query->iterate();
262
263
        $iteratedCount = 0;
264
        $topics = [];
265
266
        foreach($articles AS $row) {
267
            $article = $row[0];
268
            $topics[] = $article->topic;
269
270
            $identityMap = $this->em->getUnitOfWork()->getIdentityMap();
271
            $identityMapCount = count($identityMap[CmsArticle::class]);
272
            self::assertGreaterThan($iteratedCount, $identityMapCount);
273
274
            $iteratedCount++;
275
        }
276
277
        self::assertSame(["Doctrine 2", "Symfony 2"], $topics);
278
        self::assertSame(2, $iteratedCount);
279
280
        $this->em->flush();
281
        $this->em->clear();
282
    }
283
284
    public function testIterateResultClearEveryCycle()
285
    {
286
        $article1 = new CmsArticle;
287
        $article1->topic = "Doctrine 2";
288
        $article1->text = "This is an introduction to Doctrine 2.";
289
290
        $article2 = new CmsArticle;
291
        $article2->topic = "Symfony 2";
292
        $article2->text = "This is an introduction to Symfony 2.";
293
294
        $this->em->persist($article1);
295
        $this->em->persist($article2);
296
297
        $this->em->flush();
298
        $this->em->clear();
299
300
        $query    = $this->em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a");
301
        $articles = $query->iterate();
302
303
        $iteratedCount = 0;
304
        $topics = [];
305
        foreach($articles AS $row) {
306
            $article  = $row[0];
307
            $topics[] = $article->topic;
308
309
            $this->em->clear();
310
311
            $iteratedCount++;
312
        }
313
314
        self::assertSame(["Doctrine 2", "Symfony 2"], $topics);
315
        self::assertSame(2, $iteratedCount);
316
317
        $this->em->flush();
318
    }
319
320
    /**
321
     * @expectedException \Doctrine\ORM\Query\QueryException
322
     */
323
    public function testIterateResult_FetchJoinedCollection_ThrowsException()
324
    {
325
        $query = $this->em->createQuery("SELECT u, a FROM ' . CmsUser::class . ' u JOIN u.articles a");
326
        $articles = $query->iterate();
0 ignored issues
show
Unused Code introduced by
$articles is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
327
    }
328
329
    /**
330
     * @expectedException Doctrine\ORM\NoResultException
331
     */
332
    public function testGetSingleResultThrowsExceptionOnNoResult()
333
    {
334
        $this->em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a")
335
             ->getSingleResult();
336
    }
337
338
    /**
339
     * @expectedException Doctrine\ORM\NoResultException
340
     */
341
    public function testGetSingleScalarResultThrowsExceptionOnNoResult()
342
    {
343
        $this->em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a")
344
             ->getSingleScalarResult();
345
    }
346
347
    /**
348
     * @expectedException Doctrine\ORM\NonUniqueResultException
349
     */
350
    public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult()
351
    {
352
        $user = new CmsUser;
353
        $user->name = 'Guilherme';
354
        $user->username = 'gblanco';
355
        $user->status = 'developer';
356
357
        $article1 = new CmsArticle;
358
        $article1->topic = "Doctrine 2";
359
        $article1->text = "This is an introduction to Doctrine 2.";
360
        $user->addArticle($article1);
361
362
        $article2 = new CmsArticle;
363
        $article2->topic = "Symfony 2";
364
        $article2->text = "This is an introduction to Symfony 2.";
365
        $user->addArticle($article2);
366
367
        $this->em->persist($user);
368
        $this->em->persist($article1);
369
        $this->em->persist($article2);
370
371
        $this->em->flush();
372
        $this->em->clear();
373
374
        $this->em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a")
375
             ->getSingleScalarResult();
376
    }
377
378
    public function testModifiedLimitQuery()
379
    {
380 View Code Duplication
        for ($i = 0; $i < 5; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
381
            $user = new CmsUser;
382
            $user->name = 'Guilherme' . $i;
383
            $user->username = 'gblanco' . $i;
384
            $user->status = 'developer';
385
            $this->em->persist($user);
386
        }
387
388
        $this->em->flush();
389
        $this->em->clear();
390
391
        $data = $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u')
392
                  ->setFirstResult(1)
393
                  ->setMaxResults(2)
394
                  ->getResult();
395
396
        self::assertCount(2, $data);
397
        self::assertEquals('gblanco1', $data[0]->username);
398
        self::assertEquals('gblanco2', $data[1]->username);
399
400
        $data = $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u')
401
                  ->setFirstResult(3)
402
                  ->setMaxResults(2)
403
                  ->getResult();
404
405
        self::assertCount(2, $data);
406
        self::assertEquals('gblanco3', $data[0]->username);
407
        self::assertEquals('gblanco4', $data[1]->username);
408
409
        $data = $this->em->createQuery('SELECT u FROM ' . CmsUser::class . ' u')
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
410
                  ->setFirstResult(3)
411
                  ->setMaxResults(2)
412
                  ->getScalarResult();
413
    }
414
415
    public function testSupportsQueriesWithEntityNamespaces()
416
    {
417
        $this->em->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS');
418
419
        try {
420
            $query = $this->em->createQuery('UPDATE CMS:CmsUser u SET u.name = ?1');
421
422
            self::assertEquals('UPDATE "cms_users" SET "name" = ?', $query->getSQL());
423
424
            $query->free();
425
        } catch (\Exception $e) {
426
            $this->fail($e->getMessage());
427
        }
428
429
        $this->em->getConfiguration()->setEntityNamespaces([]);
430
    }
431
432
    /**
433
     * @group DDC-604
434
     */
435
    public function testEntityParameters()
436
    {
437
        $article = new CmsArticle;
438
        $article->topic = "dr. dolittle";
439
        $article->text = "Once upon a time ...";
440
        $author = new CmsUser;
441
        $author->name = "anonymous";
442
        $author->username = "anon";
443
        $author->status = "here";
444
        $article->user = $author;
445
        $this->em->persist($author);
446
        $this->em->persist($article);
447
        $this->em->flush();
448
        $this->em->clear();
449
450
        /* @var $result CmsArticle[] */
451
        $result = $this->em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a where a.topic = :topic and a.user = :user")
452
                ->setParameter("user", $this->em->getReference(CmsUser::class, $author->id))
453
                ->setParameter("topic", "dr. dolittle")
454
                ->getResult();
455
456
        self::assertCount(1, $result);
457
        self::assertInstanceOf(CmsArticle::class, $result[0]);
458
        self::assertEquals("dr. dolittle", $result[0]->topic);
459
460
        /* @var $user CmsUser|GhostObjectInterface */
461
        $user = $result[0]->user;
462
463
        self::assertInstanceOf(CmsUser::class, $user);
464
        self::assertInstanceOf(GhostObjectInterface::class, $user);
465
        self::assertFalse($user->isProxyInitialized());
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\Models\CMS\CmsUser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
466
    }
467
468
    /**
469
     * @group DDC-952
470
     */
471
    public function testEnableFetchEagerMode()
472
    {
473
        for ($i = 0; $i < 10; $i++) {
474
            $article = new CmsArticle;
475
476
            $article->topic = "dr. dolittle";
477
            $article->text = "Once upon a time ...";
478
479
            $author = new CmsUser;
480
481
            $author->name = "anonymous";
482
            $author->username = "anon".$i;
483
            $author->status = "here";
484
            $article->user = $author;
485
486
            $this->em->persist($author);
487
            $this->em->persist($article);
488
        }
489
490
        $this->em->flush();
491
        $this->em->clear();
492
493
        $articles = $this->em
494
            ->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a')
495
            ->setFetchMode(CmsArticle::class, 'user', FetchMode::EAGER)
496
            ->getResult();
497
498
        self::assertCount(10, $articles);
499
500
        foreach ($articles AS $article) {
501
            self::assertNotInstanceOf(GhostObjectInterface::class, $article);
502
        }
503
    }
504
505
    /**
506
     * @group DDC-991
507
     */
508
    public function testgetOneOrNullResult()
509
    {
510
        $user = new CmsUser;
511
        $user->name = 'Guilherme';
512
        $user->username = 'gblanco';
513
        $user->status = 'developer';
514
        $this->em->persist($user);
515
        $this->em->flush();
516
        $this->em->clear();
517
518
        $query = $this->em->createQuery("select u from " . CmsUser::class . " u where u.username = 'gblanco'");
519
        $fetchedUser = $query->getOneOrNullResult();
520
521
        self::assertInstanceOf(CmsUser::class, $fetchedUser);
522
        self::assertEquals('gblanco', $fetchedUser->username);
523
524
        $query = $this->em->createQuery("select u.username from " . CmsUser::class . " u where u.username = 'gblanco'");
525
        $fetchedUsername = $query->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR);
526
527
        self::assertEquals('gblanco', $fetchedUsername);
528
    }
529
530
    /**
531
     * @group DDC-991
532
     */
533
    public function testgetOneOrNullResultSeveralRows()
534
    {
535
        $user = new CmsUser;
536
        $user->name = 'Guilherme';
537
        $user->username = 'gblanco';
538
        $user->status = 'developer';
539
        $this->em->persist($user);
540
        $user = new CmsUser;
541
        $user->name = 'Roman';
542
        $user->username = 'romanb';
543
        $user->status = 'developer';
544
        $this->em->persist($user);
545
        $this->em->flush();
546
        $this->em->clear();
547
548
        $query = $this->em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
549
550
        $this->expectException(NonUniqueResultException::class);
551
552
        $fetchedUser = $query->getOneOrNullResult();
0 ignored issues
show
Unused Code introduced by
$fetchedUser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
553
    }
554
555
    /**
556
     * @group DDC-991
557
     */
558
    public function testgetOneOrNullResultNoRows()
559
    {
560
        $query = $this->em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
561
        self::assertNull($query->getOneOrNullResult());
562
563
        $query = $this->em->createQuery("select u.username from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'");
564
        self::assertNull($query->getOneOrNullResult(Query::HYDRATE_SCALAR));
565
    }
566
567
    /**
568
     * @group DBAL-171
569
     */
570
    public function testParameterOrder()
571
    {
572
        $user1 = new CmsUser;
573
        $user1->name = 'Benjamin';
574
        $user1->username = 'beberlei';
575
        $user1->status = 'developer';
576
        $this->em->persist($user1);
577
578
        $user2 = new CmsUser;
579
        $user2->name = 'Roman';
580
        $user2->username = 'romanb';
581
        $user2->status = 'developer';
582
        $this->em->persist($user2);
583
584
        $user3 = new CmsUser;
585
        $user3->name = 'Jonathan';
586
        $user3->username = 'jwage';
587
        $user3->status = 'developer';
588
        $this->em->persist($user3);
589
590
        $this->em->flush();
591
        $this->em->clear();
592
593
        $query = $this->em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.status = :a AND u.id IN (:b)");
594
        $query->setParameters(new ArrayCollection(
595
            [
596
            new Parameter('b', [$user1->id, $user2->id, $user3->id]),
597
            new Parameter('a', 'developer')
598
            ]
599
        ));
600
        $result = $query->getResult();
601
602
        self::assertCount(3, $result);
603
    }
604
605
    public function testDqlWithAutoInferOfParameters()
606
    {
607
        $user = new CmsUser;
608
        $user->name = 'Benjamin';
609
        $user->username = 'beberlei';
610
        $user->status = 'developer';
611
        $this->em->persist($user);
612
613
        $user = new CmsUser;
614
        $user->name = 'Roman';
615
        $user->username = 'romanb';
616
        $user->status = 'developer';
617
        $this->em->persist($user);
618
619
        $user = new CmsUser;
620
        $user->name = 'Jonathan';
621
        $user->username = 'jwage';
622
        $user->status = 'developer';
623
        $this->em->persist($user);
624
625
        $this->em->flush();
626
        $this->em->clear();
627
628
        $query = $this->em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username IN (?0)");
629
        $query->setParameter(0, ['beberlei', 'jwage']);
630
631
        $users = $query->execute();
632
633
        self::assertCount(2, $users);
634
    }
635
636
    public function testQueryBuilderWithStringWhereClauseContainingOrAndConditionalPrimary()
637
    {
638
        $qb = $this->em->createQueryBuilder();
639
        $qb->select('u')
640
           ->from(CmsUser::class, 'u')
641
           ->innerJoin('u.articles', 'a')
642
           ->where('(u.id = 0) OR (u.id IS NULL)');
643
644
        $query = $qb->getQuery();
645
        $users = $query->execute();
646
647
        self::assertCount(0, $users);
648
    }
649
650
    public function testQueryWithArrayOfEntitiesAsParameter()
651
    {
652
        $userA = new CmsUser;
653
        $userA->name = 'Benjamin';
654
        $userA->username = 'beberlei';
655
        $userA->status = 'developer';
656
        $this->em->persist($userA);
657
658
        $userB = new CmsUser;
659
        $userB->name = 'Roman';
660
        $userB->username = 'romanb';
661
        $userB->status = 'developer';
662
        $this->em->persist($userB);
663
664
        $userC = new CmsUser;
665
        $userC->name = 'Jonathan';
666
        $userC->username = 'jwage';
667
        $userC->status = 'developer';
668
        $this->em->persist($userC);
669
670
        $this->em->flush();
671
        $this->em->clear();
672
673
        $query = $this->em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u IN (?0) OR u.username = ?1");
674
        $query->setParameter(0, [$userA, $userC]);
675
        $query->setParameter(1, 'beberlei');
676
677
        $users = $query->execute();
678
679
        self::assertCount(2, $users);
680
    }
681
682
    public function testQueryWithHiddenAsSelectExpression()
683
    {
684
        $userA = new CmsUser;
685
        $userA->name = 'Benjamin';
686
        $userA->username = 'beberlei';
687
        $userA->status = 'developer';
688
        $this->em->persist($userA);
689
690
        $userB = new CmsUser;
691
        $userB->name = 'Roman';
692
        $userB->username = 'romanb';
693
        $userB->status = 'developer';
694
        $this->em->persist($userB);
695
696
        $userC = new CmsUser;
697
        $userC->name = 'Jonathan';
698
        $userC->username = 'jwage';
699
        $userC->status = 'developer';
700
        $this->em->persist($userC);
701
702
        $this->em->flush();
703
        $this->em->clear();
704
705
        $query = $this->em->createQuery("SELECT u, (SELECT COUNT(u2.id) FROM Doctrine\Tests\Models\CMS\CmsUser u2) AS HIDDEN total FROM Doctrine\Tests\Models\CMS\CmsUser u");
706
        $users = $query->execute();
707
708
        self::assertCount(3, $users);
709
        self::assertInstanceOf(CmsUser::class, $users[0]);
710
    }
711
712
    /**
713
     * @group DDC-1651
714
     */
715
    public function testSetParameterBindingSingleIdentifierObject()
716
    {
717
        $userC = new CmsUser;
718
        $userC->name = 'Jonathan';
719
        $userC->username = 'jwage';
720
        $userC->status = 'developer';
721
        $this->em->persist($userC);
722
723
        $this->em->flush();
724
        $this->em->clear();
725
726
        $q = $this->em->createQuery("SELECT DISTINCT u from Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1");
727
        $q->setParameter(1, $userC);
728
729
        self::assertEquals($userC, $q->getParameter(1)->getValue());
730
731
        // Parameter is not converted before, but it should be converted during execution. Test should not fail here
732
        $q->getResult();
733
    }
734
735
    /**
736
     * @group DDC-2319
737
     */
738
    public function testSetCollectionParameterBindingSingleIdentifierObject()
739
    {
740
        $u1 = new CmsUser;
741
        $u1->name = 'Name1';
742
        $u1->username = 'username1';
743
        $u1->status = 'developer';
744
        $this->em->persist($u1);
745
746
        $u2 = new CmsUser;
747
        $u2->name = 'Name2';
748
        $u2->username = 'username2';
749
        $u2->status = 'tester';
750
        $this->em->persist($u2);
751
752
        $u3 = new CmsUser;
753
        $u3->name = 'Name3';
754
        $u3->username = 'username3';
755
        $u3->status = 'tester';
756
        $this->em->persist($u3);
757
758
        $this->em->flush();
759
        $this->em->clear();
760
761
        $userCollection = new ArrayCollection();
762
763
        $userCollection->add($u1);
764
        $userCollection->add($u2);
765
        $userCollection->add($u3->getId());
766
767
        $q = $this->em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u IN (:users) ORDER BY u.id");
768
        $q->setParameter('users', $userCollection);
769
        $users = $q->execute();
770
771
        self::assertCount(3, $users);
772
        self::assertInstanceOf(CmsUser::class, $users[0]);
773
        self::assertInstanceOf(CmsUser::class, $users[1]);
774
        self::assertInstanceOf(CmsUser::class, $users[2]);
775
776
        $resultUser1 = $users[0];
777
        $resultUser2 = $users[1];
778
        $resultUser3 = $users[2];
779
780
        self::assertEquals($u1->username, $resultUser1->username);
781
        self::assertEquals($u2->username, $resultUser2->username);
782
        self::assertEquals($u3->username, $resultUser3->username);
783
    }
784
785
    /**
786
     * @group DDC-1822
787
     */
788
    public function testUnexpectedResultException()
789
    {
790
        $dql            = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u";
791
        $u1             = new CmsUser;
792
        $u2             = new CmsUser;
793
        $u1->name       = 'Fabio B. Silva';
794
        $u1->username   = 'FabioBatSilva';
795
        $u1->status     = 'developer';
796
        $u2->name       = 'Test';
797
        $u2->username   = 'test';
798
        $u2->status     = 'tester';
799
800
        try {
801
            $this->em->createQuery($dql)->getSingleResult();
802
            $this->fail('Expected exception "\Doctrine\ORM\NoResultException".');
803
        } catch (UnexpectedResultException $exc) {
804
            self::assertInstanceOf('\Doctrine\ORM\NoResultException', $exc);
805
        }
806
807
808
        $this->em->persist($u1);
809
        $this->em->persist($u2);
810
        $this->em->flush();
811
        $this->em->clear();
812
813
        try {
814
            $this->em->createQuery($dql)->getSingleResult();
815
            $this->fail('Expected exception "\Doctrine\ORM\NonUniqueResultException".');
816
        } catch (UnexpectedResultException $exc) {
817
            self::assertInstanceOf('\Doctrine\ORM\NonUniqueResultException', $exc);
818
        }
819
    }
820
821
    public function testMultipleJoinComponentsUsingInnerJoin()
822
    {
823
        $userA = new CmsUser;
824
        $userA->name = 'Benjamin';
825
        $userA->username = 'beberlei';
826
        $userA->status = 'developer';
827
828
        $phonenumberA = new CmsPhonenumber;
829
        $phonenumberA->phonenumber = '111111';
830
        $userA->addPhonenumber($phonenumberA);
831
832
        $userB = new CmsUser;
833
        $userB->name = 'Alexander';
834
        $userB->username = 'asm89';
835
        $userB->status = 'developer';
836
837
        $this->em->persist($userA);
838
        $this->em->persist($userB);
839
        $this->em->flush();
840
        $this->em->clear();
841
842
        $query = $this->em->createQuery("
843
            SELECT u, p
844
              FROM Doctrine\Tests\Models\CMS\CmsUser u
845
             INNER JOIN Doctrine\Tests\Models\CMS\CmsPhonenumber p WITH u = p.user
846
        ");
847
        $users = $query->execute();
848
849
        self::assertCount(2, $users);
850
        self::assertInstanceOf(CmsUser::class, $users[0]);
851
        self::assertInstanceOf(CmsPhonenumber::class, $users[1]);
852
    }
853
854
    public function testMultipleJoinComponentsUsingLeftJoin()
855
    {
856
        $userA = new CmsUser;
857
        $userA->name = 'Benjamin';
858
        $userA->username = 'beberlei';
859
        $userA->status = 'developer';
860
861
        $phonenumberA = new CmsPhonenumber;
862
        $phonenumberA->phonenumber = '111111';
863
        $userA->addPhonenumber($phonenumberA);
864
865
        $userB = new CmsUser;
866
        $userB->name = 'Alexander';
867
        $userB->username = 'asm89';
868
        $userB->status = 'developer';
869
870
        $this->em->persist($userA);
871
        $this->em->persist($userB);
872
        $this->em->flush();
873
        $this->em->clear();
874
875
        $query = $this->em->createQuery("
876
            SELECT u, p
877
              FROM Doctrine\Tests\Models\CMS\CmsUser u
878
              LEFT JOIN Doctrine\Tests\Models\CMS\CmsPhonenumber p WITH u = p.user
879
        ");
880
        $users = $query->execute();
881
882
        self::assertCount(4, $users);
883
        self::assertInstanceOf(CmsUser::class, $users[0]);
884
        self::assertInstanceOf(CmsPhonenumber::class, $users[1]);
885
        self::assertInstanceOf(CmsUser::class, $users[2]);
886
        self::assertNull($users[3]);
887
    }
888
}
889