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 ;
29
27
use Symfony \Bridge \Doctrine \Tests \Fixtures \DoubleNameEntity ;
30
28
use Symfony \Bridge \Doctrine \Tests \Fixtures \DoubleNullableNameEntity ;
31
29
use Symfony \Bridge \Doctrine \Tests \Fixtures \Employee ;
32
- use Symfony \Bridge \Doctrine \Tests \Fixtures \MockableRepository ;
33
30
use Symfony \Bridge \Doctrine \Tests \Fixtures \Person ;
31
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntityRepository ;
34
32
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
35
33
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdNoToStringEntity ;
36
34
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdStringWrapperNameEntity ;
@@ -91,54 +89,6 @@ protected function createRegistryMock($em = null)
91
89
return $ registry ;
92
90
}
93
91
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
-
142
92
protected function createValidator (): UniqueEntityValidator
143
93
{
144
94
return new UniqueEntityValidator ($ this ->registry );
@@ -398,13 +348,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
398
348
*/
399
349
public function testValidateUniquenessUsingCustomRepositoryMethod (UniqueEntity $ constraint )
400
350
{
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 = [];
408
352
$ this ->validator = $ this ->createValidator ();
409
353
$ this ->validator ->initialize ($ this ->context );
410
354
@@ -422,22 +366,12 @@ public function testValidateUniquenessWithUnrewoundArray(UniqueEntity $constrain
422
366
{
423
367
$ entity = new SingleIntIdEntity (1 , 'foo ' );
424
368
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 ;
441
375
$ this ->validator = $ this ->createValidator ();
442
376
$ this ->validator ->initialize ($ this ->context );
443
377
@@ -470,13 +404,7 @@ public function testValidateResultTypes($entity1, $result)
470
404
'repositoryMethod ' => 'findByCustom ' ,
471
405
]);
472
406
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 ;
480
408
$ this ->validator = $ this ->createValidator ();
481
409
$ this ->validator ->initialize ($ this ->context );
482
410
@@ -592,9 +520,6 @@ public function testAssociatedEntityWithNull()
592
520
593
521
public function testValidateUniquenessWithArrayValue ()
594
522
{
595
- $ repository = $ this ->createRepositoryMock ();
596
- $ this ->repositoryFactory ->setRepository ($ this ->em , SingleIntIdEntity::class, $ repository );
597
-
598
523
$ constraint = new UniqueEntity ([
599
524
'message ' => 'myMessage ' ,
600
525
'fields ' => ['phoneNumbers ' ],
@@ -605,10 +530,7 @@ public function testValidateUniquenessWithArrayValue()
605
530
$ entity1 = new SingleIntIdEntity (1 , 'foo ' );
606
531
$ entity1 ->phoneNumbers [] = 123 ;
607
532
608
- $ repository ->expects ($ this ->once ())
609
- ->method ('findByCustom ' )
610
- ->willReturn ([$ entity1 ])
611
- ;
533
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ entity1 ;
612
534
613
535
$ this ->em ->persist ($ entity1 );
614
536
$ this ->em ->flush ();
@@ -658,8 +580,6 @@ public function testEntityManagerNullObject()
658
580
// no "em" option set
659
581
]);
660
582
661
- $ this ->em = null ;
662
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
663
583
$ this ->validator = $ this ->createValidator ();
664
584
$ this ->validator ->initialize ($ this ->context );
665
585
@@ -673,14 +593,6 @@ public function testEntityManagerNullObject()
673
593
674
594
public function testValidateUniquenessOnNullResult ()
675
595
{
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 );
684
596
$ this ->validator = $ this ->createValidator ();
685
597
$ this ->validator ->initialize ($ this ->context );
686
598
@@ -861,13 +773,7 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
861
773
'repositoryMethod ' => 'findByCustom ' ,
862
774
]);
863
775
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 ;
871
777
$ this ->validator = $ this ->createValidator ();
872
778
$ this ->validator ->initialize ($ this ->context );
873
779
0 commit comments