Failed Conditions
Push — master ( ee4e26...e98654 )
by Marco
13:06
created

testMatchingCriteriaEqComparison()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 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\Common\Collections\Criteria;
9
use Doctrine\DBAL\Connection;
10
use Doctrine\DBAL\LockMode;
11
use Doctrine\ORM\EntityRepository;
12
use Doctrine\ORM\OptimisticLockException;
13
use Doctrine\ORM\ORMException;
14
use Doctrine\ORM\Query;
15
use Doctrine\ORM\TransactionRequiredException;
16
use Doctrine\Tests\Models\CMS\CmsAddress;
17
use Doctrine\Tests\Models\CMS\CmsEmail;
18
use Doctrine\Tests\Models\CMS\CmsUser;
19
use Doctrine\Tests\Models\DDC753\DDC753CustomRepository;
20
use Doctrine\Tests\Models\DDC753\DDC753DefaultRepository;
21
use Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository;
22
use Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository;
23
use Doctrine\Tests\Models\DDC753\DDC753InvalidRepository;
24
use Doctrine\Tests\OrmFunctionalTestCase;
25
26
/**
27
 * @author robo
28
 */
29
class EntityRepositoryTest extends OrmFunctionalTestCase
30
{
31
    protected function setUp()
32
    {
33
        $this->useModelSet('cms');
34
        parent::setUp();
35
    }
36
37
    public function loadFixture()
38
    {
39
        $user = new CmsUser;
40
        $user->name = 'Roman';
41
        $user->username = 'romanb';
42
        $user->status = 'freak';
43
        $this->em->persist($user);
44
45
        $user2 = new CmsUser;
46
        $user2->name = 'Guilherme';
47
        $user2->username = 'gblanco';
48
        $user2->status = 'dev';
49
        $this->em->persist($user2);
50
51
        $user3 = new CmsUser;
52
        $user3->name = 'Benjamin';
53
        $user3->username = 'beberlei';
54
        $user3->status = null;
55
        $this->em->persist($user3);
56
57
        $user4 = new CmsUser;
58
        $user4->name = 'Alexander';
59
        $user4->username = 'asm89';
60
        $user4->status = 'dev';
61
        $this->em->persist($user4);
62
63
        $this->em->flush();
64
65
        $user1Id = $user->getId();
66
67
        unset($user, $user2, $user3, $user4);
68
69
        $this->em->clear();
70
71
        return $user1Id;
72
    }
73
74
    public function loadAssociatedFixture()
75
    {
76
        $address = new CmsAddress();
77
        $address->city = "Berlin";
78
        $address->country = "Germany";
79
        $address->street = "Foostreet";
80
        $address->zip = "12345";
81
82
        $user = new CmsUser();
83
        $user->name = 'Roman';
84
        $user->username = 'romanb';
85
        $user->status = 'freak';
86
        $user->setAddress($address);
87
88
        $this->em->persist($user);
89
        $this->em->persist($address);
90
        $this->em->flush();
91
        $this->em->clear();
92
93
        return [$user->id, $address->id];
94
    }
95
96
    public function loadFixtureUserEmail()
97
    {
98
        $user1 = new CmsUser();
99
        $user2 = new CmsUser();
100
        $user3 = new CmsUser();
101
102
        $email1 = new CmsEmail();
103
        $email2 = new CmsEmail();
104
        $email3 = new CmsEmail();
105
106
        $user1->name     = 'Test 1';
107
        $user1->username = 'test1';
108
        $user1->status   = 'active';
109
110
        $user2->name     = 'Test 2';
111
        $user2->username = 'test2';
112
        $user2->status   = 'active';
113
114
        $user3->name     = 'Test 3';
115
        $user3->username = 'test3';
116
        $user3->status   = 'active';
117
118
        $email1->email   = '[email protected]';
119
        $email2->email   = '[email protected]';
120
        $email3->email   = '[email protected]';
121
122
        $user1->setEmail($email1);
123
        $user2->setEmail($email2);
124
        $user3->setEmail($email3);
125
126
        $this->em->persist($user1);
127
        $this->em->persist($user2);
128
        $this->em->persist($user3);
129
130
        $this->em->persist($email1);
131
        $this->em->persist($email2);
132
        $this->em->persist($email3);
133
134
        $this->em->flush();
135
        $this->em->clear();
136
137
        return [$user1, $user2, $user3];
138
    }
139
140
    public function buildUser($name, $username, $status, $address)
141
    {
142
        $user = new CmsUser();
143
        $user->name     = $name;
144
        $user->username = $username;
145
        $user->status   = $status;
146
        $user->setAddress($address);
147
148
        $this->em->persist($user);
149
        $this->em->flush();
150
151
        return $user;
152
    }
153
154
    public function buildAddress($country, $city, $street, $zip)
155
    {
156
        $address = new CmsAddress();
157
        $address->country = $country;
158
        $address->city    = $city;
159
        $address->street  = $street;
160
        $address->zip     = $zip;
161
162
        $this->em->persist($address);
163
        $this->em->flush();
164
165
        return $address;
166
    }
167
168
    public function testBasicFind()
169
    {
170
        $user1Id = $this->loadFixture();
171
        $repos = $this->em->getRepository(CmsUser::class);
172
173
        $user = $repos->find($user1Id);
174
        self::assertInstanceOf(CmsUser::class, $user);
175
        self::assertEquals('Roman', $user->name);
176
        self::assertEquals('freak', $user->status);
177
    }
178
179
    public function testFindByField()
180
    {
181
        $user1Id = $this->loadFixture();
182
        $repos = $this->em->getRepository(CmsUser::class);
183
184
        $users = $repos->findBy(['status' => 'dev']);
185
        self::assertCount(2, $users);
186
        self::assertInstanceOf(CmsUser::class, $users[0]);
187
        self::assertEquals('Guilherme', $users[0]->name);
188
        self::assertEquals('dev', $users[0]->status);
189
    }
190
191
    public function testFindByAssociationWithIntegerAsParameter()
192
    {
193
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
194
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
195
196
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
197
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
198
199
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
200
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
201
202
        unset($address1, $address2, $address3);
203
204
        $this->em->clear();
205
206
        $repository = $this->em->getRepository(CmsAddress::class);
207
        $addresses  = $repository->findBy(['user' => [$user1->getId(), $user2->getId()]]);
208
209
        self::assertCount(2, $addresses);
210
        self::assertInstanceOf(CmsAddress::class, $addresses[0]);
211
    }
212
213
    public function testFindByAssociationWithObjectAsParameter()
214
    {
215
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
216
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
217
218
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
219
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
220
221
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
222
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
223
224
        unset($address1, $address2, $address3);
225
226
        $this->em->clear();
227
228
        $repository = $this->em->getRepository(CmsAddress::class);
229
        $addresses  = $repository->findBy(['user' => [$user1, $user2]]);
230
231
        self::assertCount(2, $addresses);
232
        self::assertInstanceOf(CmsAddress::class, $addresses[0]);
233
    }
234
235
    public function testFindFieldByMagicCall()
236
    {
237
        $user1Id = $this->loadFixture();
238
        $repos = $this->em->getRepository(CmsUser::class);
239
240
        $users = $repos->findByStatus('dev');
241
        self::assertCount(2, $users);
242
        self::assertInstanceOf(CmsUser::class, $users[0]);
243
        self::assertEquals('Guilherme', $users[0]->name);
244
        self::assertEquals('dev', $users[0]->status);
245
    }
246
247
    public function testFindAll()
248
    {
249
        $user1Id = $this->loadFixture();
250
        $repos = $this->em->getRepository(CmsUser::class);
251
252
        $users = $repos->findAll();
253
        self::assertCount(4, $users);
254
    }
255
256
    public function testCount()
257
    {
258
        $this->loadFixture();
259
        $repos = $this->em->getRepository(CmsUser::class);
260
261
        $userCount = $repos->count([]);
262
        self::assertSame(4, $userCount);
263
264
        $userCount = $repos->count(['status' => 'dev']);
265
        self::assertSame(2, $userCount);
266
267
        $userCount = $repos->count(['status' => 'nonexistent']);
268
        self::assertSame(0, $userCount);
269
    }
270
271
    public function testCountBy()
272
    {
273
        $this->loadFixture();
274
        $repos = $this->em->getRepository(CmsUser::class);
275
276
        $userCount = $repos->countByStatus('dev');
277
        self::assertSame(2, $userCount);
278
    }
279
280
    /**
281
     * @expectedException \Doctrine\ORM\ORMException
282
     */
283
    public function testExceptionIsThrownWhenCallingFindByWithoutParameter()
284
    {
285
        $this->em->getRepository(CmsUser::class)
286
                  ->findByStatus();
287
    }
288
289
    /**
290
     * @expectedException \Doctrine\ORM\ORMException
291
     */
292
    public function testExceptionIsThrownWhenUsingInvalidFieldName()
293
    {
294
        $this->em->getRepository(CmsUser::class)
295
                  ->findByThisFieldDoesNotExist('testvalue');
296
    }
297
298
    /**
299
     * @group locking
300
     * @group DDC-178
301
     */
302
    public function testPessimisticReadLockWithoutTransaction_ThrowsException()
303
    {
304
        $this->expectException(TransactionRequiredException::class);
305
306
        $this->em->getRepository(CmsUser::class)
307
                  ->find(1, LockMode::PESSIMISTIC_READ);
308
    }
309
310
    /**
311
     * @group locking
312
     * @group DDC-178
313
     */
314
    public function testPessimisticWriteLockWithoutTransaction_ThrowsException()
315
    {
316
        $this->expectException(TransactionRequiredException::class);
317
318
        $this->em->getRepository(CmsUser::class)
319
                  ->find(1, LockMode::PESSIMISTIC_WRITE);
320
    }
321
322
    /**
323
     * @group locking
324
     * @group DDC-178
325
     */
326
    public function testOptimisticLockUnversionedEntity_ThrowsException()
327
    {
328
        $this->expectException(OptimisticLockException::class);
329
330
        $this->em->getRepository(CmsUser::class)
331
                  ->find(1, LockMode::OPTIMISTIC);
332
    }
333
334
    /**
335
     * @group locking
336
     * @group DDC-178
337
     */
338
    public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException()
339
    {
340
        $user = new CmsUser;
341
        $user->name = 'Roman';
342
        $user->username = 'romanb';
343
        $user->status = 'freak';
344
        $this->em->persist($user);
345
        $this->em->flush();
346
347
        $userId = $user->id;
348
349
        $this->em->find(CmsUser::class, $userId);
350
351
        $this->expectException(OptimisticLockException::class);
352
353
        $this->em->find(CmsUser::class, $userId, LockMode::OPTIMISTIC);
354
    }
355
356
    /**
357
     * @group DDC-819
358
     */
359
    public function testFindMagicCallByNullValue()
360
    {
361
        $this->loadFixture();
362
363
        $repos = $this->em->getRepository(CmsUser::class);
364
365
        $users = $repos->findByStatus(null);
366
        self::assertCount(1, $users);
367
    }
368
369
    /**
370
     * @group DDC-819
371
     */
372
    public function testInvalidMagicCall()
373
    {
374
        $this->expectException(\BadMethodCallException::class);
375
376
        $repos = $this->em->getRepository(CmsUser::class);
377
        $repos->foo();
378
    }
379
380
    /**
381
     * @group DDC-817
382
     */
383
    public function testFindByAssociationKey_ExceptionOnInverseSide()
384
    {
385
        list($userId, $addressId) = $this->loadAssociatedFixture();
386
        $repos = $this->em->getRepository(CmsUser::class);
387
388
        $this->expectException(ORMException::class);
389
        $this->expectExceptionMessage("You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association. Find methods only work on owning side associations.");
390
391
        $user = $repos->findBy(['address' => $addressId]);
392
    }
393
394
    /**
395
     * @group DDC-817
396
     */
397
    public function testFindOneByAssociationKey()
398
    {
399
        list($userId, $addressId) = $this->loadAssociatedFixture();
400
        $repos = $this->em->getRepository(CmsAddress::class);
401
        $address = $repos->findOneBy(['user' => $userId]);
402
403
        self::assertInstanceOf(CmsAddress::class, $address);
404
        self::assertEquals($addressId, $address->id);
405
    }
406
407
    /**
408
     * @group DDC-1241
409
     */
410
    public function testFindOneByOrderBy()
411
    {
412
        $this->loadFixture();
413
414
        $repos = $this->em->getRepository(CmsUser::class);
415
        $userAsc = $repos->findOneBy([], ["username" => "ASC"]);
416
        $userDesc = $repos->findOneBy([], ["username" => "DESC"]);
417
418
        self::assertNotSame($userAsc, $userDesc);
419
    }
420
421
    /**
422
     * @group DDC-817
423
     */
424
    public function testFindByAssociationKey()
425
    {
426
        list($userId, $addressId) = $this->loadAssociatedFixture();
427
        $repos = $this->em->getRepository(CmsAddress::class);
428
        $addresses = $repos->findBy(['user' => $userId]);
429
430
        self::assertContainsOnly(CmsAddress::class, $addresses);
431
        self::assertCount(1, $addresses);
432
        self::assertEquals($addressId, $addresses[0]->id);
433
    }
434
435
    /**
436
     * @group DDC-817
437
     */
438
    public function testFindAssociationByMagicCall()
439
    {
440
        list($userId, $addressId) = $this->loadAssociatedFixture();
441
        $repos = $this->em->getRepository(CmsAddress::class);
442
        $addresses = $repos->findByUser($userId);
443
444
        self::assertContainsOnly(CmsAddress::class, $addresses);
445
        self::assertCount(1, $addresses);
446
        self::assertEquals($addressId, $addresses[0]->id);
447
    }
448
449
    /**
450
     * @group DDC-817
451
     */
452
    public function testFindOneAssociationByMagicCall()
453
    {
454
        list($userId, $addressId) = $this->loadAssociatedFixture();
455
        $repos = $this->em->getRepository(CmsAddress::class);
456
        $address = $repos->findOneByUser($userId);
457
458
        self::assertInstanceOf(CmsAddress::class, $address);
459
        self::assertEquals($addressId, $address->id);
460
    }
461
462
    /**
463
     * @group DDC-1087
464
     */
465
    public function testIsNullCriteriaDoesNotGenerateAParameter()
466
    {
467
        $repos = $this->em->getRepository(CmsUser::class);
468
        $users = $repos->findBy(['status' => null, 'username' => 'romanb']);
469
470
        $params = $this->sqlLoggerStack->queries[$this->sqlLoggerStack->currentQuery]['params'];
471
        self::assertCount(1, $params, "Should only execute with one parameter.");
472
        self::assertEquals(['romanb'], $params);
473
    }
474
475
    public function testIsNullCriteria()
476
    {
477
        $this->loadFixture();
478
479
        $repos = $this->em->getRepository(CmsUser::class);
480
481
        $users = $repos->findBy(['status' => null]);
482
        self::assertCount(1, $users);
483
    }
484
485
    /**
486
     * @group DDC-1094
487
     */
488
    public function testFindByLimitOffset()
489
    {
490
        $this->loadFixture();
491
492
        $repos = $this->em->getRepository(CmsUser::class);
493
494
        $users1 = $repos->findBy([], null, 1, 0);
495
        $users2 = $repos->findBy([], null, 1, 1);
496
497
        self::assertCount(4, $repos->findBy([]));
498
        self::assertCount(1, $users1);
499
        self::assertCount(1, $users2);
500
        self::assertNotSame($users1[0], $users2[0]);
501
    }
502
503
    /**
504
     * @group DDC-1094
505
     */
506
    public function testFindByOrderBy()
507
    {
508
        $this->loadFixture();
509
510
        $repos = $this->em->getRepository(CmsUser::class);
511
        $usersAsc = $repos->findBy([], ["username" => "ASC"]);
512
        $usersDesc = $repos->findBy([], ["username" => "DESC"]);
513
514
        self::assertCount(4, $usersAsc, "Pre-condition: only four users in fixture");
515
        self::assertCount(4, $usersDesc, "Pre-condition: only four users in fixture");
516
        self::assertSame($usersAsc[0], $usersDesc[3]);
517
        self::assertSame($usersAsc[3], $usersDesc[0]);
518
    }
519
520
    /**
521
     * @group DDC-1376
522
     */
523
    public function testFindByOrderByAssociation()
524
    {
525
        $this->loadFixtureUserEmail();
526
527
        $repository = $this->em->getRepository(CmsUser::class);
528
        $resultAsc  = $repository->findBy([], ['email' => 'ASC']);
529
        $resultDesc = $repository->findBy([], ['email' => 'DESC']);
530
531
        self::assertCount(3, $resultAsc);
532
        self::assertCount(3, $resultDesc);
533
534
        self::assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId());
535
        self::assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId());
536
    }
537
538
    /**
539
     * @group DDC-1426
540
     */
541
    public function testFindFieldByMagicCallOrderBy()
542
    {
543
        $this->loadFixture();
544
        $repos = $this->em->getRepository(CmsUser::class);
545
546
        $usersAsc = $repos->findByStatus('dev', ['username' => "ASC"]);
547
        $usersDesc = $repos->findByStatus('dev', ['username' => "DESC"]);
548
549
        self::assertCount(2, $usersAsc);
550
        self::assertCount(2, $usersDesc);
551
552
        self::assertInstanceOf(CmsUser::class, $usersAsc[0]);
553
        self::assertEquals('Alexander', $usersAsc[0]->name);
554
        self::assertEquals('dev', $usersAsc[0]->status);
555
556
        self::assertSame($usersAsc[0], $usersDesc[1]);
557
        self::assertSame($usersAsc[1], $usersDesc[0]);
558
    }
559
560
    /**
561
     * @group DDC-1426
562
     */
563
    public function testFindFieldByMagicCallLimitOffset()
564
    {
565
        $this->loadFixture();
566
        $repos = $this->em->getRepository(CmsUser::class);
567
568
        $users1 = $repos->findByStatus('dev', [], 1, 0);
569
        $users2 = $repos->findByStatus('dev', [], 1, 1);
570
571
        self::assertCount(1, $users1);
572
        self::assertCount(1, $users2);
573
        self::assertNotSame($users1[0], $users2[0]);
574
    }
575
576
    /**
577
     * @group DDC-753
578
     */
579
    public function testDefaultRepositoryClassName()
580
    {
581
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
582
        $this->em->getConfiguration()->setDefaultRepositoryClassName(DDC753DefaultRepository::class);
583
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class);
584
585
        $repos = $this->em->getRepository(DDC753EntityWithDefaultCustomRepository::class);
586
        self::assertInstanceOf(DDC753DefaultRepository::class, $repos);
587
        self::assertTrue($repos->isDefaultRepository());
588
589
590
        $repos = $this->em->getRepository(DDC753EntityWithCustomRepository::class);
591
        self::assertInstanceOf(DDC753CustomRepository::class, $repos);
592
        self::assertTrue($repos->isCustomRepository());
593
594
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class);
595
        $this->em->getConfiguration()->setDefaultRepositoryClassName(EntityRepository::class);
596
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
597
    }
598
599
    /**
600
     * @group DDC-753
601
     * @expectedException Doctrine\ORM\ORMException
602
     * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. It must be a Doctrine\Common\Persistence\ObjectRepository.
603
     */
604
    public function testSetDefaultRepositoryInvalidClassError()
605
    {
606
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
607
        $this->em->getConfiguration()->setDefaultRepositoryClassName(DDC753InvalidRepository::class);
608
    }
609
610
    /**
611
     * @group DDC-3257
612
     */
613
    public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash()
614
    {
615
        self::assertSame(
616
            $this->em->getRepository('\\' . CmsUser::class),
617
            $this->em->getRepository(CmsUser::class)
618
        );
619
    }
620
621
    /**
622
     * @group DDC-1376
623
     *
624
     * @expectedException Doctrine\ORM\ORMException
625
     * @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association.
626
     */
627
    public function testInvalidOrderByAssociation()
628
    {
629
        $this->em->getRepository(CmsUser::class)
630
            ->findBy(['status' => 'test'], ['address' => 'ASC']);
631
    }
632
633
    /**
634
     * @group DDC-1500
635
     */
636
    public function testInvalidOrientation()
637
    {
638
        $this->expectException(ORMException::class);
639
        $this->expectExceptionMessage('Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username');
640
641
        $repo = $this->em->getRepository(CmsUser::class);
642
        $repo->findBy(['status' => 'test'], ['username' => 'INVALID']);
643
    }
644
645
    /**
646
     * @group DDC-1713
647
     */
648
    public function testFindByAssociationArray()
649
    {
650
        $repo = $this->em->getRepository(CmsAddress::class);
651
        $data = $repo->findBy(['user' => [1, 2, 3]]);
652
653
        $query = array_pop($this->sqlLoggerStack->queries);
654
        self::assertEquals([1,2,3], $query['params'][0]);
655
        self::assertEquals(Connection::PARAM_INT_ARRAY, $query['types'][0]);
656
    }
657
658
    /**
659
     * @group DDC-1637
660
     */
661
    public function testMatchingEmptyCriteria()
662
    {
663
        $this->loadFixture();
664
665
        $repository = $this->em->getRepository(CmsUser::class);
666
        $users = $repository->matching(new Criteria());
667
668
        self::assertCount(4, $users);
669
    }
670
671
    /**
672
     * @group DDC-1637
673
     */
674
    public function testMatchingCriteriaEqComparison()
675
    {
676
        $this->loadFixture();
677
678
        $repository = $this->em->getRepository(CmsUser::class);
679
        $users = $repository->matching(new Criteria(
680
            Criteria::expr()->eq('username', 'beberlei')
681
        ));
682
683
        self::assertCount(1, $users);
684
    }
685
686
    /**
687
     * @group DDC-1637
688
     */
689
    public function testMatchingCriteriaNeqComparison()
690
    {
691
        $this->loadFixture();
692
693
        $repository = $this->em->getRepository(CmsUser::class);
694
        $users = $repository->matching(new Criteria(
695
            Criteria::expr()->neq('username', 'beberlei')
696
        ));
697
698
        self::assertCount(3, $users);
699
    }
700
701
    /**
702
     * @group DDC-1637
703
     */
704
    public function testMatchingCriteriaInComparison()
705
    {
706
        $this->loadFixture();
707
708
        $repository = $this->em->getRepository(CmsUser::class);
709
        $users = $repository->matching(new Criteria(
710
            Criteria::expr()->in('username', ['beberlei', 'gblanco'])
711
        ));
712
713
        self::assertCount(2, $users);
714
    }
715
716
    /**
717
     * @group DDC-1637
718
     */
719
    public function testMatchingCriteriaNotInComparison()
720
    {
721
        $this->loadFixture();
722
723
        $repository = $this->em->getRepository(CmsUser::class);
724
        $users = $repository->matching(new Criteria(
725
            Criteria::expr()->notIn('username', ['beberlei', 'gblanco', 'asm89'])
726
        ));
727
728
        self::assertCount(1, $users);
729
    }
730
731
    /**
732
     * @group DDC-1637
733
     */
734
    public function testMatchingCriteriaLtComparison()
735
    {
736
        $firstUserId = $this->loadFixture();
737
738
        $repository = $this->em->getRepository(CmsUser::class);
739
        $users = $repository->matching(new Criteria(
740
            Criteria::expr()->lt('id', $firstUserId + 1)
741
        ));
742
743
        self::assertCount(1, $users);
744
    }
745
746
    /**
747
     * @group DDC-1637
748
     */
749
    public function testMatchingCriteriaLeComparison()
750
    {
751
        $firstUserId = $this->loadFixture();
752
753
        $repository = $this->em->getRepository(CmsUser::class);
754
        $users = $repository->matching(new Criteria(
755
            Criteria::expr()->lte('id', $firstUserId + 1)
756
        ));
757
758
        self::assertCount(2, $users);
759
    }
760
761
    /**
762
     * @group DDC-1637
763
     */
764
    public function testMatchingCriteriaGtComparison()
765
    {
766
        $firstUserId = $this->loadFixture();
767
768
        $repository = $this->em->getRepository(CmsUser::class);
769
        $users = $repository->matching(new Criteria(
770
            Criteria::expr()->gt('id', $firstUserId)
771
        ));
772
773
        self::assertCount(3, $users);
774
    }
775
776
    /**
777
     * @group DDC-1637
778
     */
779
    public function testMatchingCriteriaGteComparison()
780
    {
781
        $firstUserId = $this->loadFixture();
782
783
        $repository = $this->em->getRepository(CmsUser::class);
784
        $users = $repository->matching(new Criteria(
785
            Criteria::expr()->gte('id', $firstUserId)
786
        ));
787
788
        self::assertCount(4, $users);
789
    }
790
791
    /**
792
     * @group DDC-2430
793
     */
794
    public function testMatchingCriteriaAssocationByObjectInMemory()
795
    {
796
        list($userId, $addressId) = $this->loadAssociatedFixture();
797
798
        $user = $this->em->find(CmsUser::class, $userId);
799
800
        $criteria = new Criteria(
801
            Criteria::expr()->eq('user', $user)
802
        );
803
804
        $repository = $this->em->getRepository(CmsAddress::class);
805
        $addresses = $repository->matching($criteria);
806
807
        self::assertCount(1, $addresses);
808
809
        $addresses = new ArrayCollection($repository->findAll());
810
811
        self::assertCount(1, $addresses->matching($criteria));
812
    }
813
814
    /**
815
     * @group DDC-2430
816
     */
817
    public function testMatchingCriteriaAssocationInWithArray()
818
    {
819
        list($userId, $addressId) = $this->loadAssociatedFixture();
820
821
        $user = $this->em->find(CmsUser::class, $userId);
822
823
        $criteria = new Criteria(
824
            Criteria::expr()->in('user', [$user])
825
        );
826
827
        $repository = $this->em->getRepository(CmsAddress::class);
828
        $addresses = $repository->matching($criteria);
829
830
        self::assertCount(1, $addresses);
831
832
        $addresses = new ArrayCollection($repository->findAll());
833
834
        self::assertCount(1, $addresses->matching($criteria));
835
    }
836
837
    public function testMatchingCriteriaContainsComparison()
838
    {
839
        $this->loadFixture();
840
841
        $repository = $this->em->getRepository(CmsUser::class);
842
843
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar')));
844
        self::assertCount(0, $users);
845
846
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom')));
847
        self::assertCount(1, $users);
848
849
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev')));
850
        self::assertCount(2, $users);
851
    }
852
853
    public function testMatchingCriteriaStartsWithComparison()
854
    {
855
        $this->loadFixture();
856
857
        $repository = $this->em->getRepository(CmsUser::class);
858
859
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'Foo')));
860
        self::assertCount(0, $users);
861
862
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'R')));
863
        self::assertCount(1, $users);
864
865
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('status', 'de')));
866
        self::assertCount(2, $users);
867
    }
868
869
    public function testMatchingCriteriaEndsWithComparison()
870
    {
871
        $this->loadFixture();
872
873
        $repository = $this->em->getRepository(CmsUser::class);
874
875
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'foo')));
876
        self::assertCount(0, $users);
877
878
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'oman')));
879
        self::assertCount(1, $users);
880
881
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('status', 'ev')));
882
        self::assertCount(2, $users);
883
    }
884
885
    /**
886
     * @group DDC-2478
887
     */
888
    public function testMatchingCriteriaNullAssocComparison()
889
    {
890
        $fixtures       = $this->loadFixtureUserEmail();
891
        $user           = $this->em->find(get_class($fixtures[0]), $fixtures[0]->id);
892
        $repository     = $this->em->getRepository(CmsUser::class);
893
        $criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email'));
894
        $criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null));
895
896
        $user->setEmail(null);
897
        $this->em->flush();
898
        $this->em->clear();
899
900
        $usersIsNull = $repository->matching($criteriaIsNull);
901
        $usersEqNull = $repository->matching($criteriaEqNull);
902
903
        self::assertCount(1, $usersIsNull);
904
        self::assertCount(1, $usersEqNull);
905
906
        self::assertInstanceOf(CmsUser::class, $usersIsNull[0]);
907
        self::assertInstanceOf(CmsUser::class, $usersEqNull[0]);
908
909
        self::assertNull($usersIsNull[0]->getEmail());
910
        self::assertNull($usersEqNull[0]->getEmail());
911
    }
912
913
    /**
914
     * @group DDC-2055
915
     */
916
    public function testCreateResultSetMappingBuilder()
917
    {
918
        $repository = $this->em->getRepository(CmsUser::class);
919
        $rsm = $repository->createResultSetMappingBuilder('u');
920
921
        self::assertInstanceOf(Query\ResultSetMappingBuilder::class, $rsm);
922
        self::assertEquals(['u' => CmsUser::class], $rsm->aliasMap);
923
    }
924
925
    /**
926
     * @group DDC-3045
927
     */
928
    public function testFindByFieldInjectionPrevented()
929
    {
930
        $this->expectException(ORMException::class);
931
        $this->expectExceptionMessage('Unrecognized field: ');
932
933
        $repository = $this->em->getRepository(CmsUser::class);
934
        $repository->findBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']);
935
    }
936
937
    /**
938
     * @group DDC-3045
939
     */
940
    public function testFindOneByFieldInjectionPrevented()
941
    {
942
        $this->expectException(ORMException::class);
943
        $this->expectExceptionMessage('Unrecognized field: ');
944
945
        $repository = $this->em->getRepository(CmsUser::class);
946
        $repository->findOneBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']);
947
    }
948
949
    /**
950
     * @group DDC-3045
951
     */
952
    public function testMatchingInjectionPrevented()
953
    {
954
        $this->expectException(ORMException::class);
955
        $this->expectExceptionMessage('Unrecognized field: ');
956
957
        $repository = $this->em->getRepository(CmsUser::class);
958
        $result     = $repository->matching(new Criteria(
959
            Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei')
960
        ));
961
962
        // Because repository returns a lazy collection, we call toArray to force initialization
963
        $result->toArray();
964
    }
965
966
    /**
967
     * @group DDC-3045
968
     */
969
    public function testFindInjectionPrevented()
970
    {
971
        $this->expectException(ORMException::class);
972
        $this->expectExceptionMessage('Unrecognized identifier fields: ');
973
974
        $repository = $this->em->getRepository(CmsUser::class);
975
        $repository->find(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test', 'id' => 1]);
976
    }
977
978
    /**
979
     * @group DDC-3056
980
     */
981
    public function testFindByNullValueInInCondition()
982
    {
983
        $user1 = new CmsUser();
984
        $user2 = new CmsUser();
985
986
        $user1->username = 'ocramius';
987
        $user1->name = 'Marco';
988
        $user2->status = null;
989
        $user2->username = 'deeky666';
990
        $user2->name = 'Steve';
991
        $user2->status = 'dbal maintainer';
992
993
        $this->em->persist($user1);
994
        $this->em->persist($user2);
995
        $this->em->flush();
996
997
        $users = $this->em->getRepository(CmsUser::class)->findBy(['status' => [null]]);
998
999
        self::assertCount(1, $users);
1000
        self::assertSame($user1, reset($users));
1001
    }
1002
1003
    /**
1004
     * @group DDC-3056
1005
     */
1006
    public function testFindByNullValueInMultipleInCriteriaValues()
1007
    {
1008
        $user1 = new CmsUser();
1009
        $user2 = new CmsUser();
1010
1011
        $user1->username = 'ocramius';
1012
        $user1->name = 'Marco';
1013
        $user2->status = null;
1014
        $user2->username = 'deeky666';
1015
        $user2->name = 'Steve';
1016
        $user2->status = 'dbal maintainer';
1017
1018
        $this->em->persist($user1);
1019
        $this->em->persist($user2);
1020
        $this->em->flush();
1021
1022
        $users = $this
1023
            ->em
1024
            ->getRepository(CmsUser::class)
1025
            ->findBy(['status' => ['foo', null]]);
1026
1027
        self::assertCount(1, $users);
1028
        self::assertSame($user1, reset($users));
1029
    }
1030
1031
    /**
1032
     * @group DDC-3056
1033
     */
1034
    public function testFindMultipleByNullValueInMultipleInCriteriaValues()
1035
    {
1036
        $user1 = new CmsUser();
1037
        $user2 = new CmsUser();
1038
1039
        $user1->username = 'ocramius';
1040
        $user1->name = 'Marco';
1041
        $user2->status = null;
1042
        $user2->username = 'deeky666';
1043
        $user2->name = 'Steve';
1044
        $user2->status = 'dbal maintainer';
1045
1046
        $this->em->persist($user1);
1047
        $this->em->persist($user2);
1048
        $this->em->flush();
1049
1050
        $users = $this
1051
            ->em
1052
            ->getRepository(CmsUser::class)
1053
            ->findBy(['status' => ['dbal maintainer', null]]);
1054
1055
        self::assertCount(2, $users);
1056
1057
        foreach ($users as $user) {
1058
            self::assertContains($user, [$user1, $user2]);
1059
        }
1060
    }
1061
}
1062