36
36
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
37
37
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdNoToStringEntity ;
38
38
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdStringWrapperNameEntity ;
39
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdWithPrivateNameEntity ;
39
40
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleStringIdEntity ;
40
41
use Symfony \Bridge \Doctrine \Tests \Fixtures \Type \StringWrapper ;
41
42
use Symfony \Bridge \Doctrine \Tests \Fixtures \Type \StringWrapperType ;
@@ -90,12 +91,17 @@ protected function createRegistryMock($em = null)
90
91
return $ registry ;
91
92
}
92
93
93
- protected function createRepositoryMock ()
94
+ protected function createRepositoryMock (string $ className )
94
95
{
95
- return $ this ->getMockBuilder (MockableRepository::class)
96
+ $ repositoryMock = $ this ->getMockBuilder (MockableRepository::class)
96
97
->disableOriginalConstructor ()
97
98
->onlyMethods (['find ' , 'findAll ' , 'findOneBy ' , 'findBy ' , 'getClassName ' , 'findByCustom ' ])
98
99
->getMock ();
100
+
101
+ $ repositoryMock ->method ('getClassName ' )
102
+ ->willReturn ($ className );
103
+
104
+ return $ repositoryMock ;
99
105
}
100
106
101
107
protected function createEntityManagerMock ($ repositoryMock )
@@ -109,6 +115,10 @@ protected function createEntityManagerMock($repositoryMock)
109
115
$ classMetadata = $ this ->createMock (
110
116
class_exists (ClassMetadataInfo::class) ? ClassMetadataInfo::class : ClassMetadata::class
111
117
);
118
+ $ classMetadata
119
+ ->method ('getName ' )
120
+ ->willReturn ($ repositoryMock ->getClassName ())
121
+ ;
112
122
$ classMetadata
113
123
->expects ($ this ->any ())
114
124
->method ('hasField ' )
@@ -138,6 +148,7 @@ private function createSchema($em)
138
148
$ schemaTool = new SchemaTool ($ em );
139
149
$ schemaTool ->createSchema ([
140
150
$ em ->getClassMetadata (SingleIntIdEntity::class),
151
+ $ em ->getClassMetadata (SingleIntIdWithPrivateNameEntity::class),
141
152
$ em ->getClassMetadata (SingleIntIdNoToStringEntity::class),
142
153
$ em ->getClassMetadata (DoubleNameEntity::class),
143
154
$ em ->getClassMetadata (DoubleNullableNameEntity::class),
@@ -194,6 +205,25 @@ public static function provideUniquenessConstraints(): iterable
194
205
yield 'Named arguments ' => [new UniqueEntity (message: 'myMessage ' , fields: ['name ' ], em: 'foo ' )];
195
206
}
196
207
208
+ public function testValidateEntityWithPrivatePropertyAndProxyObject ()
209
+ {
210
+ $ entity = new SingleIntIdWithPrivateNameEntity (1 , 'Foo ' );
211
+ $ this ->em ->persist ($ entity );
212
+ $ this ->em ->flush ();
213
+
214
+ $ this ->em ->clear ();
215
+
216
+ // this will load a proxy object
217
+ $ entity = $ this ->em ->getReference (SingleIntIdWithPrivateNameEntity::class, 1 );
218
+
219
+ $ this ->validator ->validate ($ entity , new UniqueEntity ([
220
+ 'fields ' => ['name ' ],
221
+ 'em ' => self ::EM_NAME ,
222
+ ]));
223
+
224
+ $ this ->assertNoViolation ();
225
+ }
226
+
197
227
/**
198
228
* @dataProvider provideConstraintsWithCustomErrorPath
199
229
*/
@@ -387,7 +417,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
387
417
*/
388
418
public function testValidateUniquenessUsingCustomRepositoryMethod (UniqueEntity $ constraint )
389
419
{
390
- $ repository = $ this ->createRepositoryMock ();
420
+ $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class );
391
421
$ repository ->expects ($ this ->once ())
392
422
->method ('findByCustom ' )
393
423
->willReturn ([])
@@ -411,7 +441,7 @@ public function testValidateUniquenessWithUnrewoundArray(UniqueEntity $constrain
411
441
{
412
442
$ entity = new SingleIntIdEntity (1 , 'foo ' );
413
443
414
- $ repository = $ this ->createRepositoryMock ();
444
+ $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class );
415
445
$ repository ->expects ($ this ->once ())
416
446
->method ('findByCustom ' )
417
447
->willReturnCallback (
@@ -459,7 +489,7 @@ public function testValidateResultTypes($entity1, $result)
459
489
'repositoryMethod ' => 'findByCustom ' ,
460
490
]);
461
491
462
- $ repository = $ this ->createRepositoryMock ();
492
+ $ repository = $ this ->createRepositoryMock ($ entity1 ::class );
463
493
$ repository ->expects ($ this ->once ())
464
494
->method ('findByCustom ' )
465
495
->willReturn ($ result )
@@ -581,7 +611,7 @@ public function testAssociatedEntityWithNull()
581
611
582
612
public function testValidateUniquenessWithArrayValue ()
583
613
{
584
- $ repository = $ this ->createRepositoryMock ();
614
+ $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class );
585
615
$ this ->repositoryFactory ->setRepository ($ this ->em , SingleIntIdEntity::class, $ repository );
586
616
587
617
$ constraint = new UniqueEntity ([
@@ -662,7 +692,7 @@ public function testEntityManagerNullObject()
662
692
663
693
public function testValidateUniquenessOnNullResult ()
664
694
{
665
- $ repository = $ this ->createRepositoryMock ();
695
+ $ repository = $ this ->createRepositoryMock (SingleIntIdEntity::class );
666
696
$ repository
667
697
->method ('find ' )
668
698
->willReturn (null )
@@ -850,7 +880,7 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
850
880
'repositoryMethod ' => 'findByCustom ' ,
851
881
]);
852
882
853
- $ repository = $ this ->createRepositoryMock ();
883
+ $ repository = $ this ->createRepositoryMock ($ entity ::class );
854
884
$ repository ->expects ($ this ->once ())
855
885
->method ('findByCustom ' )
856
886
->willReturn ($ result )
0 commit comments