1616use Doctrine \Common \Persistence \ObjectManager ;
1717use Doctrine \Common \Persistence \ObjectRepository ;
1818use Symfony \Bridge \Doctrine \Test \DoctrineTestHelper ;
19+ use Symfony \Bridge \Doctrine \Test \TestRepositoryFactory ;
1920use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
2021use Symfony \Bridge \Doctrine \Tests \Fixtures \DoubleNameEntity ;
2122use Symfony \Bridge \Doctrine \Tests \Fixtures \AssociationEntity ;
@@ -48,9 +49,16 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
4849 */
4950 protected $ repository ;
5051
52+ protected $ repositoryFactory ;
53+
5154 protected function setUp ()
5255 {
53- $ this ->em = DoctrineTestHelper::createTestEntityManager ();
56+ $ this ->repositoryFactory = new TestRepositoryFactory ();
57+
58+ $ config = DoctrineTestHelper::createTestConfiguration ();
59+ $ config ->setRepositoryFactory ($ this ->repositoryFactory );
60+
61+ $ this ->em = DoctrineTestHelper::createTestEntityManager ($ config );
5462 $ this ->registry = $ this ->createRegistryMock ($ this ->em );
5563 $ this ->createSchema ($ this ->em );
5664
@@ -164,7 +172,7 @@ public function testValidateUniqueness()
164172
165173 $ this ->buildViolation ('myMessage ' )
166174 ->atPath ('property.path.name ' )
167- ->setParameter ('{{ value }} ' , 'Foo ' )
175+ ->setParameter ('{{ value }} ' , '" Foo" ' )
168176 ->setInvalidValue ('Foo ' )
169177 ->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
170178 ->assertRaised ();
@@ -189,7 +197,7 @@ public function testValidateCustomErrorPath()
189197
190198 $ this ->buildViolation ('myMessage ' )
191199 ->atPath ('property.path.bar ' )
192- ->setParameter ('{{ value }} ' , 'Foo ' )
200+ ->setParameter ('{{ value }} ' , '" Foo" ' )
193201 ->setInvalidValue ('Foo ' )
194202 ->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
195203 ->assertRaised ();
@@ -242,7 +250,7 @@ public function testValidateUniquenessWithIgnoreNull()
242250
243251 $ this ->buildViolation ('myMessage ' )
244252 ->atPath ('property.path.name ' )
245- ->setParameter ('{{ value }} ' , 'Foo ' )
253+ ->setParameter ('{{ value }} ' , '" Foo" ' )
246254 ->setInvalidValue ('Foo ' )
247255 ->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
248256 ->assertRaised ();
@@ -275,7 +283,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
275283
276284 $ this ->buildViolation ('myMessage ' )
277285 ->atPath ('property.path.name2 ' )
278- ->setParameter ('{{ value }} ' , 'Bar ' )
286+ ->setParameter ('{{ value }} ' , '" Bar" ' )
279287 ->setInvalidValue ('Bar ' )
280288 ->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
281289 ->assertRaised ();
@@ -446,7 +454,7 @@ public function testValidateUniquenessNotToStringEntityWithAssociatedEntity()
446454
447455 $ this ->buildViolation ('myMessage ' )
448456 ->atPath ('property.path.single ' )
449- ->setParameter ('{{ value }} ' , $ expectedValue )
457+ ->setParameter ('{{ value }} ' , ' " ' . $ expectedValue. ' " ' )
450458 ->setInvalidValue ($ expectedValue )
451459 ->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
452460 ->assertRaised ();
@@ -472,6 +480,44 @@ public function testAssociatedEntityWithNull()
472480 $ this ->assertNoViolation ();
473481 }
474482
483+ public function testValidateUniquenessWithArrayValue ()
484+ {
485+ $ repository = $ this ->createRepositoryMock ();
486+ $ this ->repositoryFactory ->setRepository ($ this ->em , 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' , $ repository );
487+
488+ $ constraint = new UniqueEntity (array (
489+ 'message ' => 'myMessage ' ,
490+ 'fields ' => array ('phoneNumbers ' ),
491+ 'em ' => self ::EM_NAME ,
492+ 'repositoryMethod ' => 'findByCustom ' ,
493+ ));
494+
495+ $ entity1 = new SingleIntIdEntity (1 , 'foo ' );
496+ $ entity1 ->phoneNumbers [] = 123 ;
497+
498+ $ repository ->expects ($ this ->once ())
499+ ->method ('findByCustom ' )
500+ ->will ($ this ->returnValue (array ($ entity1 )))
501+ ;
502+
503+ $ this ->em ->persist ($ entity1 );
504+ $ this ->em ->flush ();
505+
506+ $ entity2 = new SingleIntIdEntity (2 , 'bar ' );
507+ $ entity2 ->phoneNumbers [] = 123 ;
508+ $ this ->em ->persist ($ entity2 );
509+ $ this ->em ->flush ();
510+
511+ $ this ->validator ->validate ($ entity2 , $ constraint );
512+
513+ $ this ->buildViolation ('myMessage ' )
514+ ->atPath ('property.path.phoneNumbers ' )
515+ ->setParameter ('{{ value }} ' , 'array ' )
516+ ->setInvalidValue (array (123 ))
517+ ->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
518+ ->assertRaised ();
519+ }
520+
475521 /**
476522 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
477523 * @expectedExceptionMessage Object manager "foo" does not exist.
0 commit comments