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 );
@@ -445,13 +386,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
445
386
446
387
public function testValidateUniquenessUsingCustomRepositoryMethod ()
447
388
{
448
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
449
- $ repository ->expects ($ this ->once ())
450
- ->method ('findByCustom ' )
451
- ->willReturn ([])
452
- ;
453
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
454
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
389
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = [];
455
390
$ this ->validator = $ this ->createValidator ();
456
391
$ this ->validator ->initialize ($ this ->context );
457
392
@@ -466,22 +401,12 @@ public function testValidateUniquenessWithUnrewoundArray()
466
401
{
467
402
$ entity = new SingleIntIdEntity (1 , 'foo ' );
468
403
469
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
470
- $ repository ->expects ($ this ->once ())
471
- ->method ('findByCustom ' )
472
- ->willReturnCallback (
473
- function () use ($ entity ) {
474
- $ returnValue = [
475
- $ entity ,
476
- ];
477
- next ($ returnValue );
478
-
479
- return $ returnValue ;
480
- }
481
- )
482
- ;
483
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
484
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
404
+ $ returnValue = [
405
+ $ entity ,
406
+ ];
407
+ next ($ returnValue );
408
+
409
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ returnValue ;
485
410
$ this ->validator = $ this ->createValidator ();
486
411
$ this ->validator ->initialize ($ this ->context );
487
412
@@ -502,13 +427,7 @@ public function testValidateResultTypes($entity1, $result)
502
427
repositoryMethod: 'findByCustom ' ,
503
428
);
504
429
505
- $ repository = $ this ->createRepositoryMock ($ entity1 ::class);
506
- $ repository ->expects ($ this ->once ())
507
- ->method ('findByCustom ' )
508
- ->willReturn ($ result )
509
- ;
510
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
511
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
430
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ result ;
512
431
$ this ->validator = $ this ->createValidator ();
513
432
$ this ->validator ->initialize ($ this ->context );
514
433
@@ -658,9 +577,6 @@ public function testAssociatedEntityReferencedByPrimaryKey()
658
577
659
578
public function testValidateUniquenessWithArrayValue ()
660
579
{
661
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
662
- $ this ->repositoryFactory ->setRepository ($ this ->em , SingleIntIdEntity::class, $ repository );
663
-
664
580
$ constraint = new UniqueEntity (
665
581
message: 'myMessage ' ,
666
582
fields: ['phoneNumbers ' ],
@@ -671,10 +587,7 @@ public function testValidateUniquenessWithArrayValue()
671
587
$ entity1 = new SingleIntIdEntity (1 , 'foo ' );
672
588
$ entity1 ->phoneNumbers [] = 123 ;
673
589
674
- $ repository ->expects ($ this ->once ())
675
- ->method ('findByCustom ' )
676
- ->willReturn ([$ entity1 ])
677
- ;
590
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ entity1 ;
678
591
679
592
$ this ->em ->persist ($ entity1 );
680
593
$ this ->em ->flush ();
@@ -724,8 +637,6 @@ public function testEntityManagerNullObject()
724
637
// no "em" option set
725
638
);
726
639
727
- $ this ->em = null ;
728
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
729
640
$ this ->validator = $ this ->createValidator ();
730
641
$ this ->validator ->initialize ($ this ->context );
731
642
@@ -739,14 +650,6 @@ public function testEntityManagerNullObject()
739
650
740
651
public function testValidateUniquenessOnNullResult ()
741
652
{
742
- $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class);
743
- $ repository
744
- ->method ('find ' )
745
- ->willReturn (null )
746
- ;
747
-
748
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
749
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
750
653
$ this ->validator = $ this ->createValidator ();
751
654
$ this ->validator ->initialize ($ this ->context );
752
655
@@ -927,13 +830,7 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
927
830
repositoryMethod: 'findByCustom ' ,
928
831
);
929
832
930
- $ repository = $ this ->createRepositoryMock ($ entity ::class);
931
- $ repository ->expects ($ this ->once ())
932
- ->method ('findByCustom ' )
933
- ->willReturn ($ result )
934
- ;
935
- $ this ->em = $ this ->createEntityManagerMock ($ repository );
936
- $ this ->registry = $ this ->createRegistryMock ($ this ->em );
833
+ $ this ->em ->getRepository (SingleIntIdEntity::class)->result = $ result ;
937
834
$ this ->validator = $ this ->createValidator ();
938
835
$ this ->validator ->initialize ($ this ->context );
939
836
0 commit comments