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 ;
2927use Symfony \Bridge \Doctrine \Tests \Fixtures \DoubleNameEntity ;
3028use Symfony \Bridge \Doctrine \Tests \Fixtures \DoubleNullableNameEntity ;
3129use Symfony \Bridge \Doctrine \Tests \Fixtures \Employee ;
32- use Symfony \Bridge \Doctrine \Tests \Fixtures \MockableRepository ;
3330use Symfony \Bridge \Doctrine \Tests \Fixtures \Person ;
31+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntityRepository ;
3432use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
3533use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdNoToStringEntity ;
3634use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdStringWrapperNameEntity ;
@@ -91,54 +89,6 @@ protected function createRegistryMock($em = null)
9189 return $ registry ;
9290 }
9391
94- protected function createRepositoryMock ()
95- {
96- return $ this ->getMockBuilder (MockableRepository::class)
97- ->disableOriginalConstructor ()
98- ->onlyMethods (['find ' , 'findAll ' , 'findOneBy ' , 'findBy ' , 'getClassName ' , 'findByCustom ' ])
99- ->getMock ();
100- }
101-
102- protected function createEntityManagerMock ($ repositoryMock )
103- {
104- $ em = $ this ->createMock (ObjectManager::class);
105- $ em ->expects ($ this ->any ())
106- ->method ('getRepository ' )
107- ->willReturn ($ repositoryMock )
108- ;
109-
110- $ classMetadata = $ this ->createMock (
111- class_exists (ClassMetadataInfo::class) ? ClassMetadataInfo::class : ClassMetadata::class
112- );
113- $ classMetadata
114- ->expects ($ this ->any ())
115- ->method ('hasField ' )
116- ->willReturn (true )
117- ;
118- $ refl = $ this ->createMock (\ReflectionProperty::class);
119- $ refl
120- ->method ('getName ' )
121- ->willReturn ('name ' )
122- ;
123- $ refl
124- ->method ('getValue ' )
125- ->willReturn (true )
126- ;
127-
128- if (property_exists (ClassMetadata::class, 'propertyAccessors ' )) {
129- $ classMetadata ->propertyAccessors ['name ' ] = RawValuePropertyAccessor::fromReflectionProperty ($ refl );
130- } else {
131- $ classMetadata ->reflFields = ['name ' => $ refl ];
132- }
133-
134- $ em ->expects ($ this ->any ())
135- ->method ('getClassMetadata ' )
136- ->willReturn ($ classMetadata )
137- ;
138-
139- return $ em ;
140- }
141-
14292 protected function createValidator (): UniqueEntityValidator
14393 {
14494 return new UniqueEntityValidator ($ this ->registry );
@@ -398,13 +348,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
398348 */
399349 public function testValidateUniquenessUsingCustomRepositoryMethod (UniqueEntity $ constraint )
400350 {
401- $ repository = $ this ->createRepositoryMock ();
402- $ repository ->expects ($ this ->once ())
403- ->method ('findByCustom ' )
404- ->willReturn ([])
405- ;
406- $ this ->em = $ this ->createEntityManagerMock ($ repository );
407- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
351+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = [];
408352 $ this ->validator = $ this ->createValidator ();
409353 $ this ->validator ->initialize ($ this ->context );
410354
@@ -422,22 +366,12 @@ public function testValidateUniquenessWithUnrewoundArray(UniqueEntity $constrain
422366 {
423367 $ entity = new SingleIntIdEntity (1 , 'foo ' );
424368
425- $ repository = $ this ->createRepositoryMock ();
426- $ repository ->expects ($ this ->once ())
427- ->method ('findByCustom ' )
428- ->willReturnCallback (
429- function () use ($ entity ) {
430- $ returnValue = [
431- $ entity ,
432- ];
433- next ($ returnValue );
434-
435- return $ returnValue ;
436- }
437- )
438- ;
439- $ this ->em = $ this ->createEntityManagerMock ($ repository );
440- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
369+ $ returnValue = [
370+ $ entity ,
371+ ];
372+ next ($ returnValue );
373+
374+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ returnValue ;
441375 $ this ->validator = $ this ->createValidator ();
442376 $ this ->validator ->initialize ($ this ->context );
443377
@@ -470,13 +404,7 @@ public function testValidateResultTypes($entity1, $result)
470404 'repositoryMethod ' => 'findByCustom ' ,
471405 ]);
472406
473- $ repository = $ this ->createRepositoryMock ();
474- $ repository ->expects ($ this ->once ())
475- ->method ('findByCustom ' )
476- ->willReturn ($ result )
477- ;
478- $ this ->em = $ this ->createEntityManagerMock ($ repository );
479- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
407+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ result ;
480408 $ this ->validator = $ this ->createValidator ();
481409 $ this ->validator ->initialize ($ this ->context );
482410
@@ -592,9 +520,6 @@ public function testAssociatedEntityWithNull()
592520
593521 public function testValidateUniquenessWithArrayValue ()
594522 {
595- $ repository = $ this ->createRepositoryMock ();
596- $ this ->repositoryFactory ->setRepository ($ this ->em , SingleIntIdEntity::class, $ repository );
597-
598523 $ constraint = new UniqueEntity ([
599524 'message ' => 'myMessage ' ,
600525 'fields ' => ['phoneNumbers ' ],
@@ -605,10 +530,7 @@ public function testValidateUniquenessWithArrayValue()
605530 $ entity1 = new SingleIntIdEntity (1 , 'foo ' );
606531 $ entity1 ->phoneNumbers [] = 123 ;
607532
608- $ repository ->expects ($ this ->once ())
609- ->method ('findByCustom ' )
610- ->willReturn ([$ entity1 ])
611- ;
533+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ entity1 ;
612534
613535 $ this ->em ->persist ($ entity1 );
614536 $ this ->em ->flush ();
@@ -658,8 +580,6 @@ public function testEntityManagerNullObject()
658580 // no "em" option set
659581 ]);
660582
661- $ this ->em = null ;
662- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
663583 $ this ->validator = $ this ->createValidator ();
664584 $ this ->validator ->initialize ($ this ->context );
665585
@@ -673,14 +593,6 @@ public function testEntityManagerNullObject()
673593
674594 public function testValidateUniquenessOnNullResult ()
675595 {
676- $ repository = $ this ->createRepositoryMock ();
677- $ repository
678- ->method ('find ' )
679- ->willReturn (null )
680- ;
681-
682- $ this ->em = $ this ->createEntityManagerMock ($ repository );
683- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
684596 $ this ->validator = $ this ->createValidator ();
685597 $ this ->validator ->initialize ($ this ->context );
686598
@@ -861,13 +773,7 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
861773 'repositoryMethod ' => 'findByCustom ' ,
862774 ]);
863775
864- $ repository = $ this ->createRepositoryMock ();
865- $ repository ->expects ($ this ->once ())
866- ->method ('findByCustom ' )
867- ->willReturn ($ result )
868- ;
869- $ this ->em = $ this ->createEntityManagerMock ($ repository );
870- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
776+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ result ;
871777 $ this ->validator = $ this ->createValidator ();
872778 $ this ->validator ->initialize ($ this ->context );
873779
0 commit comments