14
14
use Doctrine \Common \Collections \ArrayCollection ;
15
15
use Doctrine \DBAL \Types \Type ;
16
16
use Doctrine \ORM \EntityRepository ;
17
- use Doctrine \ORM \Mapping \ClassMetadata ;
18
17
use Doctrine \ORM \Mapping \ClassMetadataInfo ;
19
- use Doctrine \ORM \Mapping \PropertyAccessors \RawValuePropertyAccessor ;
20
18
use Doctrine \ORM \Tools \SchemaTool ;
21
19
use Doctrine \Persistence \ManagerRegistry ;
22
20
use Doctrine \Persistence \ObjectManager ;
33
31
use Symfony \Bridge \Doctrine \Tests \Fixtures \Dto ;
34
32
use Symfony \Bridge \Doctrine \Tests \Fixtures \Employee ;
35
33
use Symfony \Bridge \Doctrine \Tests \Fixtures \HireAnEmployee ;
36
- use Symfony \Bridge \Doctrine \Tests \Fixtures \MockableRepository ;
37
34
use Symfony \Bridge \Doctrine \Tests \Fixtures \Person ;
35
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntityRepository ;
38
36
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
39
37
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdNoToStringEntity ;
40
38
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdStringWrapperNameEntity ;
@@ -99,63 +97,6 @@ protected function createRegistryMock($em = null)
99
97
return $ registry ;
100
98
}
101
99
102
- protected function createRepositoryMock (string $ className )
103
- {
104
- $ repositoryMock = $ this ->getMockBuilder (MockableRepository::class)
105
- ->disableOriginalConstructor ()
106
- ->onlyMethods (['find ' , 'findAll ' , 'findOneBy ' , 'findBy ' , 'getClassName ' , 'findByCustom ' ])
107
- ->getMock ();
108
-
109
- $ repositoryMock ->method ('getClassName ' )
110
- ->willReturn ($ className );
111
-
112
- return $ repositoryMock ;
113
- }
114
-
115
- protected function createEntityManagerMock ($ repositoryMock )
116
- {
117
- $ em = $ this ->createMock (ObjectManager::class);
118
- $ em ->expects ($ this ->any ())
119
- ->method ('getRepository ' )
120
- ->willReturn ($ repositoryMock )
121
- ;
122
-
123
- $ classMetadata = $ this ->createMock (
124
- class_exists (ClassMetadataInfo::class) ? ClassMetadataInfo::class : ClassMetadata::class
125
- );
126
- $ classMetadata
127
- ->method ('getName ' )
128
- ->willReturn ($ repositoryMock ->getClassName ())
129
- ;
130
- $ classMetadata
131
- ->expects ($ this ->any ())
132
- ->method ('hasField ' )
133
- ->willReturn (true )
134
- ;
135
- $ refl = $ this ->createMock (\ReflectionProperty::class);
136
- $ refl
137
- ->method ('getName ' )
138
- ->willReturn ('name ' )
139
- ;
140
- $ refl
141
- ->method ('getValue ' )
142
- ->willReturn (true )
143
- ;
144
-
145
- if (property_exists (ClassMetadata::class, 'propertyAccessors ' )) {
146
- $ classMetadata ->propertyAccessors ['name ' ] = RawValuePropertyAccessor::fromReflectionProperty ($ refl );
147
- } else {
148
- $ classMetadata ->reflFields = ['name ' => $ refl ];
149
- }
150
-
151
- $ em ->expects ($ this ->any ())
152
- ->method ('getClassMetadata ' )
153
- ->willReturn ($ classMetadata )
154
- ;
155
-
156
- return $ em ;
157
- }
158
-
159
100
protected function createValidator (): UniqueEntityValidator
160
101
{
161
102
return new UniqueEntityValidator ($ this ->registry );
@@ -435,13 +376,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
435
376
*/
436
377
public function testValidateUniquenessUsingCustomRepositoryMethod (UniqueEntity $ constraint )
437
378
{
438
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
439
- $ repository ->expects ($ this ->once ())
440
- ->method ('findByCustom ' )
441
- ->willReturn ([])
442
- ;
443
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
444
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
379
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = [];
445
380
$ this ->validator = $ this ->createValidator ();
446
381
$ this ->validator ->initialize ($ this ->context );
447
382
@@ -459,22 +394,12 @@ public function testValidateUniquenessWithUnrewoundArray(UniqueEntity $constrain
459
394
{
460
395
$ entity = new SingleIntIdEntity (1 , 'foo ' );
461
396
462
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
463
- $ repository ->expects ($ this ->once ())
464
- ->method ('findByCustom ' )
465
- ->willReturnCallback (
466
- function () use ($ entity ) {
467
- $ returnValue = [
468
- $ entity ,
469
- ];
470
- next ($ returnValue );
471
-
472
- return $ returnValue ;
473
- }
474
- )
475
- ;
476
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
477
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
397
+ $ returnValue = [
398
+ $ entity ,
399
+ ];
400
+ next ($ returnValue );
401
+
402
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ returnValue ;
478
403
$ this ->validator = $ this ->createValidator ();
479
404
$ this ->validator ->initialize ($ this ->context );
480
405
@@ -507,13 +432,7 @@ public function testValidateResultTypes($entity1, $result)
507
432
'repositoryMethod ' => 'findByCustom ' ,
508
433
]);
509
434
510
- $ repository = $ this ->createRepositoryMock ($ entity1 ::class);
511
- $ repository ->expects ($ this ->once ())
512
- ->method ('findByCustom ' )
513
- ->willReturn ($ result )
514
- ;
515
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
516
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
435
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ result ;
517
436
$ this ->validator = $ this ->createValidator ();
518
437
$ this ->validator ->initialize ($ this ->context );
519
438
@@ -663,9 +582,6 @@ public function testAssociatedEntityReferencedByPrimaryKey()
663
582
664
583
public function testValidateUniquenessWithArrayValue ()
665
584
{
666
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
667
- $ this ->repositoryFactory ->setRepository ($ this ->em , SingleIntIdEntity::class, $ repository );
668
-
669
585
$ constraint = new UniqueEntity ([
670
586
'message ' => 'myMessage ' ,
671
587
'fields ' => ['phoneNumbers ' ],
@@ -676,10 +592,7 @@ public function testValidateUniquenessWithArrayValue()
676
592
$ entity1 = new SingleIntIdEntity (1 , 'foo ' );
677
593
$ entity1 ->phoneNumbers [] = 123 ;
678
594
679
- $ repository ->expects ($ this ->once ())
680
- ->method ('findByCustom ' )
681
- ->willReturn ([$ entity1 ])
682
- ;
595
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ entity1 ;
683
596
684
597
$ this ->em ->persist ($ entity1 );
685
598
$ this ->em ->flush ();
@@ -729,8 +642,6 @@ public function testEntityManagerNullObject()
729
642
// no "em" option set
730
643
]);
731
644
732
- $ this ->em = null ;
733
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
734
645
$ this ->validator = $ this ->createValidator ();
735
646
$ this ->validator ->initialize ($ this ->context );
736
647
@@ -744,14 +655,6 @@ public function testEntityManagerNullObject()
744
655
745
656
public function testValidateUniquenessOnNullResult ()
746
657
{
747
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
748
- $ repository
749
- ->method ('find ' )
750
- ->willReturn (null )
751
- ;
752
-
753
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
754
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
755
658
$ this ->validator = $ this ->createValidator ();
756
659
$ this ->validator ->initialize ($ this ->context );
757
660
@@ -932,13 +835,7 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
932
835
'repositoryMethod ' => 'findByCustom ' ,
933
836
]);
934
837
935
- $ repository = $ this ->createRepositoryMock ($ entity ::class);
936
- $ repository ->expects ($ this ->once ())
937
- ->method ('findByCustom ' )
938
- ->willReturn ($ result )
939
- ;
940
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
941
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
838
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ result ;
942
839
$ this ->validator = $ this ->createValidator ();
943
840
$ this ->validator ->initialize ($ this ->context );
944
841
0 commit comments