15
15
use Doctrine \Common \Persistence \ObjectManager ;
16
16
use Doctrine \Common \Persistence \ObjectRepository ;
17
17
use Symfony \Bridge \Doctrine \Test \DoctrineTestHelper ;
18
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \ChildEntity ;
18
19
use Symfony \Bridge \Doctrine \Tests \Fixtures \CompositeIntIdEntity ;
19
20
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
20
21
use Symfony \Bridge \Doctrine \Tests \Fixtures \DoubleNameEntity ;
@@ -81,10 +82,13 @@ protected function createEntityManagerMock($repositoryMock)
81
82
$ em = $ this ->getMockBuilder ('Doctrine\Common\Persistence\ObjectManager ' )
82
83
->getMock ()
83
84
;
84
- $ em ->expects ($ this ->any ())
85
- ->method ('getRepository ' )
86
- ->will ($ this ->returnValue ($ repositoryMock ))
87
- ;
85
+
86
+ if ($ repositoryMock ) {
87
+ $ em ->expects ($ this ->any ())
88
+ ->method ('getRepository ' )
89
+ ->will ($ this ->returnValue ($ repositoryMock ))
90
+ ;
91
+ }
88
92
89
93
$ classMetadata = $ this ->getMock ('Doctrine\Common\Persistence\Mapping\ClassMetadata ' );
90
94
$ classMetadata
@@ -140,6 +144,7 @@ public function testValidateUniqueness()
140
144
'message ' => 'myMessage ' ,
141
145
'fields ' => array ('name ' ),
142
146
'em ' => self ::EM_NAME ,
147
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ,
143
148
));
144
149
145
150
$ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
@@ -171,6 +176,7 @@ public function testValidateCustomErrorPath()
171
176
'fields ' => array ('name ' ),
172
177
'em ' => self ::EM_NAME ,
173
178
'errorPath ' => 'bar ' ,
179
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ,
174
180
));
175
181
176
182
$ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
@@ -193,6 +199,7 @@ public function testValidateUniquenessWithNull()
193
199
'message ' => 'myMessage ' ,
194
200
'fields ' => array ('name ' ),
195
201
'em ' => self ::EM_NAME ,
202
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ,
196
203
));
197
204
198
205
$ entity1 = new SingleIntIdEntity (1 , null );
@@ -214,6 +221,7 @@ public function testValidateUniquenessWithIgnoreNull()
214
221
'fields ' => array ('name ' , 'name2 ' ),
215
222
'em ' => self ::EM_NAME ,
216
223
'ignoreNull ' => false ,
224
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity ' ,
217
225
));
218
226
219
227
$ entity1 = new DoubleNameEntity (1 , 'Foo ' , null );
@@ -245,6 +253,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
245
253
'fields ' => array ('name ' , 'name2 ' ),
246
254
'em ' => self ::EM_NAME ,
247
255
'errorPath ' => 'name2 ' ,
256
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity ' ,
248
257
));
249
258
250
259
$ entity1 = new DoubleNameEntity (1 , 'Foo ' , 'Bar ' );
@@ -276,6 +285,7 @@ public function testValidateUniquenessUsingCustomRepositoryMethod()
276
285
'fields ' => array ('name ' ),
277
286
'em ' => self ::EM_NAME ,
278
287
'repositoryMethod ' => 'findByCustom ' ,
288
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ,
279
289
));
280
290
281
291
$ repository = $ this ->createRepositoryMock ();
@@ -302,6 +312,7 @@ public function testValidateUniquenessWithUnrewoundArray()
302
312
'fields ' => array ('name ' ),
303
313
'em ' => self ::EM_NAME ,
304
314
'repositoryMethod ' => 'findByCustom ' ,
315
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ,
305
316
));
306
317
307
318
$ entity = new SingleIntIdEntity (1 , 'foo ' );
@@ -336,6 +347,7 @@ public function testAssociatedEntity()
336
347
'message ' => 'myMessage ' ,
337
348
'fields ' => array ('single ' ),
338
349
'em ' => self ::EM_NAME ,
350
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity ' ,
339
351
));
340
352
341
353
$ entity1 = new SingleIntIdEntity (1 , 'foo ' );
@@ -370,6 +382,7 @@ public function testAssociatedEntityWithNull()
370
382
'fields ' => array ('single ' ),
371
383
'em ' => self ::EM_NAME ,
372
384
'ignoreNull ' => false ,
385
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity ' ,
373
386
));
374
387
375
388
$ associated = new AssociationEntity ();
@@ -393,6 +406,7 @@ public function testAssociatedCompositeEntity()
393
406
'message ' => 'myMessage ' ,
394
407
'fields ' => array ('composite ' ),
395
408
'em ' => self ::EM_NAME ,
409
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity ' ,
396
410
));
397
411
398
412
$ composite = new CompositeIntIdEntity (1 , 1 , 'test ' );
@@ -416,6 +430,7 @@ public function testDedicatedEntityManagerNullObject()
416
430
'message ' => 'myMessage ' ,
417
431
'fields ' => array ('name ' ),
418
432
'em ' => self ::EM_NAME ,
433
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ,
419
434
));
420
435
421
436
$ this ->em = null ;
@@ -428,24 +443,28 @@ public function testDedicatedEntityManagerNullObject()
428
443
$ this ->validator ->validate ($ entity , $ constraint );
429
444
}
430
445
431
- /**
432
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
433
- * @expectedExceptionMessage Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"
434
- */
435
- public function testEntityManagerNullObject ()
446
+ public function testTargetsRepoIsUsedForChildren ()
436
447
{
437
448
$ constraint = new UniqueEntity (array (
438
449
'message ' => 'myMessage ' ,
439
450
'fields ' => array ('name ' ),
440
- // no "em" option set
451
+ 'em ' => self ::EM_NAME ,
452
+ 'target ' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ,
441
453
));
442
454
443
- $ this ->em = null ;
455
+ $ repository = $ this ->createRepositoryMock ();
456
+ $ this ->em = $ this ->createEntityManagerMock (null );
457
+ $ this ->em ->expects ($ this ->atLeastOnce ())
458
+ ->method ('getRepository ' )
459
+ ->with ('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' )
460
+ ->will ($ this ->returnValue ($ repository ))
461
+ ;
444
462
$ this ->registry = $ this ->createRegistryMock ($ this ->em );
463
+
445
464
$ this ->validator = $ this ->createValidator ();
446
465
$ this ->validator ->initialize ($ this ->context );
447
466
448
- $ entity = new SingleIntIdEntity (1 , null );
467
+ $ entity = new ChildEntity (1 , ' Foo ' );
449
468
450
469
$ this ->validator ->validate ($ entity , $ constraint );
451
470
}
0 commit comments