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