@@ -190,6 +190,7 @@ public function testValidateUniqueness()
190
190
->atPath ('property.path.name ' )
191
191
->setParameter ('{{ value }} ' , '"Foo" ' )
192
192
->setInvalidValue ($ entity2 )
193
+ ->setCause (array ($ entity1 ))
193
194
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
194
195
->assertRaised ();
195
196
}
@@ -215,6 +216,7 @@ public function testValidateCustomErrorPath()
215
216
->atPath ('property.path.bar ' )
216
217
->setParameter ('{{ value }} ' , '"Foo" ' )
217
218
->setInvalidValue ($ entity2 )
219
+ ->setCause (array ($ entity1 ))
218
220
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
219
221
->assertRaised ();
220
222
}
@@ -268,6 +270,7 @@ public function testValidateUniquenessWithIgnoreNullDisabled()
268
270
->atPath ('property.path.name ' )
269
271
->setParameter ('{{ value }} ' , '"Foo" ' )
270
272
->setInvalidValue ('Foo ' )
273
+ ->setCause (array ($ entity1 ))
271
274
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
272
275
->assertRaised ();
273
276
}
@@ -346,6 +349,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
346
349
->atPath ('property.path.name2 ' )
347
350
->setParameter ('{{ value }} ' , '"Bar" ' )
348
351
->setInvalidValue ('Bar ' )
352
+ ->setCause (array ($ entity1 ))
349
353
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
350
354
->assertRaised ();
351
355
}
@@ -481,6 +485,7 @@ public function testAssociatedEntity()
481
485
->setParameter ('{{ value }} ' , 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity") identified by (id => 1) ' )
482
486
->setInvalidValue ($ entity1 )
483
487
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
488
+ ->setCause (array ($ associated , $ associated2 ))
484
489
->assertRaised ();
485
490
}
486
491
@@ -517,6 +522,7 @@ public function testValidateUniquenessNotToStringEntityWithAssociatedEntity()
517
522
->atPath ('property.path.single ' )
518
523
->setParameter ('{{ value }} ' , $ expectedValue )
519
524
->setInvalidValue ($ entity1 )
525
+ ->setCause (array ($ associated , $ associated2 ))
520
526
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
521
527
->assertRaised ();
522
528
}
@@ -575,6 +581,7 @@ public function testValidateUniquenessWithArrayValue()
575
581
->atPath ('property.path.phoneNumbers ' )
576
582
->setParameter ('{{ value }} ' , 'array ' )
577
583
->setInvalidValue (array (123 ))
584
+ ->setCause (array ($ entity1 ))
578
585
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
579
586
->assertRaised ();
580
587
}
@@ -652,6 +659,7 @@ public function testValidateInheritanceUniqueness()
652
659
->atPath ('property.path.name ' )
653
660
->setInvalidValue ('Foo ' )
654
661
->setCode ('23bd9dbf-6b9b-41cd-a99e-4844bcf3077f ' )
662
+ ->setCause (array ($ entity1 ))
655
663
->setParameters (array ('{{ value }} ' => '"Foo" ' ))
656
664
->assertRaised ();
657
665
}
@@ -703,6 +711,7 @@ public function testValidateUniquenessWithCompositeObjectNoToStringIdEntity()
703
711
->atPath ('property.path.objectOne ' )
704
712
->setParameter ('{{ value }} ' , $ expectedValue )
705
713
->setInvalidValue ($ objectOne )
714
+ ->setCause (array ($ entity ))
706
715
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
707
716
->assertRaised ();
708
717
}
@@ -730,6 +739,43 @@ public function testValidateUniquenessWithCustomDoctrineTypeValue()
730
739
->atPath ('property.path.name ' )
731
740
->setParameter ('{{ value }} ' , $ expectedValue )
732
741
->setInvalidValue ($ existingEntity ->name )
742
+ ->setCause (array ($ existingEntity ))
743
+ ->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
744
+ ->assertRaised ();
745
+ }
746
+
747
+ /**
748
+ * This is a functional test as there is a large integration necessary to get the validator working.
749
+ */
750
+ public function testValidateUniquenessCause ()
751
+ {
752
+ $ constraint = new UniqueEntity (array (
753
+ 'message ' => 'myMessage ' ,
754
+ 'fields ' => array ('name ' ),
755
+ 'em ' => self ::EM_NAME ,
756
+ ));
757
+
758
+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
759
+ $ entity2 = new SingleIntIdEntity (2 , 'Foo ' );
760
+
761
+ $ this ->validator ->validate ($ entity1 , $ constraint );
762
+
763
+ $ this ->assertNoViolation ();
764
+
765
+ $ this ->em ->persist ($ entity1 );
766
+ $ this ->em ->flush ();
767
+
768
+ $ this ->validator ->validate ($ entity1 , $ constraint );
769
+
770
+ $ this ->assertNoViolation ();
771
+
772
+ $ this ->validator ->validate ($ entity2 , $ constraint );
773
+
774
+ $ this ->buildViolation ('myMessage ' )
775
+ ->atPath ('property.path.name ' )
776
+ ->setParameter ('{{ value }} ' , '"Foo" ' )
777
+ ->setInvalidValue ($ entity2 )
778
+ ->setCause (array ($ entity1 ))
733
779
->setCode (UniqueEntity::NOT_UNIQUE_ERROR )
734
780
->assertRaised ();
735
781
}
0 commit comments