1414use Doctrine \Common \Collections \ArrayCollection ;
1515use Doctrine \DBAL \Types \Type ;
1616use Doctrine \ORM \EntityRepository ;
17- use Doctrine \ORM \Mapping \ClassMetadata ;
1817use Doctrine \ORM \Mapping \ClassMetadataInfo ;
19- use Doctrine \ORM \Mapping \PropertyAccessors \RawValuePropertyAccessor ;
2018use Doctrine \ORM \Tools \SchemaTool ;
2119use Doctrine \Persistence \ManagerRegistry ;
2220use Doctrine \Persistence \ObjectManager ;
3331use Symfony \Bridge \Doctrine \Tests \Fixtures \Dto ;
3432use Symfony \Bridge \Doctrine \Tests \Fixtures \Employee ;
3533use Symfony \Bridge \Doctrine \Tests \Fixtures \HireAnEmployee ;
36- use Symfony \Bridge \Doctrine \Tests \Fixtures \MockableRepository ;
3734use Symfony \Bridge \Doctrine \Tests \Fixtures \Person ;
35+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntityRepository ;
3836use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
3937use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdNoToStringEntity ;
4038use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdStringWrapperNameEntity ;
@@ -99,63 +97,6 @@ protected function createRegistryMock($em = null)
9997 return $ registry ;
10098 }
10199
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-
159100 protected function createValidator (): UniqueEntityValidator
160101 {
161102 return new UniqueEntityValidator ($ this ->registry );
@@ -435,13 +376,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
435376 */
436377 public function testValidateUniquenessUsingCustomRepositoryMethod (UniqueEntity $ constraint )
437378 {
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 = [];
445380 $ this ->validator = $ this ->createValidator ();
446381 $ this ->validator ->initialize ($ this ->context );
447382
@@ -459,22 +394,12 @@ public function testValidateUniquenessWithUnrewoundArray(UniqueEntity $constrain
459394 {
460395 $ entity = new SingleIntIdEntity (1 , 'foo ' );
461396
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 ;
478403 $ this ->validator = $ this ->createValidator ();
479404 $ this ->validator ->initialize ($ this ->context );
480405
@@ -507,13 +432,7 @@ public function testValidateResultTypes($entity1, $result)
507432 'repositoryMethod ' => 'findByCustom ' ,
508433 ]);
509434
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 ;
517436 $ this ->validator = $ this ->createValidator ();
518437 $ this ->validator ->initialize ($ this ->context );
519438
@@ -663,9 +582,6 @@ public function testAssociatedEntityReferencedByPrimaryKey()
663582
664583 public function testValidateUniquenessWithArrayValue ()
665584 {
666- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
667- $ this ->repositoryFactory ->setRepository ($ this ->em , SingleIntIdEntity::class, $ repository );
668-
669585 $ constraint = new UniqueEntity ([
670586 'message ' => 'myMessage ' ,
671587 'fields ' => ['phoneNumbers ' ],
@@ -676,10 +592,7 @@ public function testValidateUniquenessWithArrayValue()
676592 $ entity1 = new SingleIntIdEntity (1 , 'foo ' );
677593 $ entity1 ->phoneNumbers [] = 123 ;
678594
679- $ repository ->expects ($ this ->once ())
680- ->method ('findByCustom ' )
681- ->willReturn ([$ entity1 ])
682- ;
595+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ entity1 ;
683596
684597 $ this ->em ->persist ($ entity1 );
685598 $ this ->em ->flush ();
@@ -729,8 +642,6 @@ public function testEntityManagerNullObject()
729642 // no "em" option set
730643 ]);
731644
732- $ this ->em = null ;
733- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
734645 $ this ->validator = $ this ->createValidator ();
735646 $ this ->validator ->initialize ($ this ->context );
736647
@@ -744,14 +655,6 @@ public function testEntityManagerNullObject()
744655
745656 public function testValidateUniquenessOnNullResult ()
746657 {
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 );
755658 $ this ->validator = $ this ->createValidator ();
756659 $ this ->validator ->initialize ($ this ->context );
757660
@@ -932,13 +835,7 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
932835 'repositoryMethod ' => 'findByCustom ' ,
933836 ]);
934837
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 ;
942839 $ this ->validator = $ this ->createValidator ();
943840 $ this ->validator ->initialize ($ this ->context );
944841
0 commit comments