11
11
12
12
namespace Symfony \Tests \Component \Form \Extension \Validator \Validator ;
13
13
14
+ use Symfony \Component \Form \FormInterface ;
14
15
use Symfony \Component \Form \FormBuilder ;
15
16
use Symfony \Component \Form \FormError ;
16
17
use Symfony \Component \Form \Util \PropertyPath ;
@@ -91,6 +92,16 @@ protected function getMockForm()
91
92
return $ this ->getMock ('Symfony\Tests\Component\Form\FormInterface ' );
92
93
}
93
94
95
+ /**
96
+ * Access has to be public, as this method is called via callback array
97
+ * in {@link testValidateFormDataCanHandleCallbackValidationGroups()}
98
+ * and {@link testValidateFormDataUsesInheritedCallbackValidationGroup()}
99
+ */
100
+ public function getValidationGroups (FormInterface $ form )
101
+ {
102
+ return array ('group1 ' , 'group2 ' );
103
+ }
104
+
94
105
public function testUseValidateValueWhenValidationConstraintExist ()
95
106
{
96
107
$ constraint = $ this ->getMockForAbstractClass ('Symfony\Component\Validator\Constraint ' );
@@ -598,6 +609,52 @@ public function testValidateFormData()
598
609
DelegatingValidator::validateFormData ($ form , $ context );
599
610
}
600
611
612
+ public function testValidateFormDataCanHandleCallbackValidationGroups ()
613
+ {
614
+ $ graphWalker = $ this ->getMockGraphWalker ();
615
+ $ metadataFactory = $ this ->getMockMetadataFactory ();
616
+ $ context = new ExecutionContext ('Root ' , $ graphWalker , $ metadataFactory );
617
+ $ object = $ this ->getMock ('\stdClass ' );
618
+ $ form = $ this ->getBuilder ()
619
+ ->setAttribute ('validation_groups ' , array ($ this , 'getValidationGroups ' ))
620
+ ->getForm ();
621
+
622
+ $ graphWalker ->expects ($ this ->at (0 ))
623
+ ->method ('walkReference ' )
624
+ ->with ($ object , 'group1 ' , 'data ' , true );
625
+ $ graphWalker ->expects ($ this ->at (1 ))
626
+ ->method ('walkReference ' )
627
+ ->with ($ object , 'group2 ' , 'data ' , true );
628
+
629
+ $ form ->setData ($ object );
630
+
631
+ DelegatingValidator::validateFormData ($ form , $ context );
632
+ }
633
+
634
+ public function testValidateFormDataCanHandleClosureValidationGroups ()
635
+ {
636
+ $ graphWalker = $ this ->getMockGraphWalker ();
637
+ $ metadataFactory = $ this ->getMockMetadataFactory ();
638
+ $ context = new ExecutionContext ('Root ' , $ graphWalker , $ metadataFactory );
639
+ $ object = $ this ->getMock ('\stdClass ' );
640
+ $ form = $ this ->getBuilder ()
641
+ ->setAttribute ('validation_groups ' , function (FormInterface $ form ){
642
+ return array ('group1 ' , 'group2 ' );
643
+ })
644
+ ->getForm ();
645
+
646
+ $ graphWalker ->expects ($ this ->at (0 ))
647
+ ->method ('walkReference ' )
648
+ ->with ($ object , 'group1 ' , 'data ' , true );
649
+ $ graphWalker ->expects ($ this ->at (1 ))
650
+ ->method ('walkReference ' )
651
+ ->with ($ object , 'group2 ' , 'data ' , true );
652
+
653
+ $ form ->setData ($ object );
654
+
655
+ DelegatingValidator::validateFormData ($ form , $ context );
656
+ }
657
+
601
658
public function testValidateFormDataUsesInheritedValidationGroup ()
602
659
{
603
660
$ graphWalker = $ this ->getMockGraphWalker ();
@@ -623,6 +680,64 @@ public function testValidateFormDataUsesInheritedValidationGroup()
623
680
DelegatingValidator::validateFormData ($ child , $ context );
624
681
}
625
682
683
+ public function testValidateFormDataUsesInheritedCallbackValidationGroup ()
684
+ {
685
+ $ graphWalker = $ this ->getMockGraphWalker ();
686
+ $ metadataFactory = $ this ->getMockMetadataFactory ();
687
+ $ context = new ExecutionContext ('Root ' , $ graphWalker , $ metadataFactory );
688
+ $ context ->setPropertyPath ('path ' );
689
+ $ object = $ this ->getMock ('\stdClass ' );
690
+
691
+ $ parent = $ this ->getBuilder ()
692
+ ->setAttribute ('validation_groups ' , array ($ this , 'getValidationGroups ' ))
693
+ ->getForm ();
694
+ $ child = $ this ->getBuilder ()
695
+ ->setAttribute ('validation_groups ' , null )
696
+ ->getForm ();
697
+ $ parent ->add ($ child );
698
+
699
+ $ child ->setData ($ object );
700
+
701
+ $ graphWalker ->expects ($ this ->at (0 ))
702
+ ->method ('walkReference ' )
703
+ ->with ($ object , 'group1 ' , 'path.data ' , true );
704
+ $ graphWalker ->expects ($ this ->at (1 ))
705
+ ->method ('walkReference ' )
706
+ ->with ($ object , 'group2 ' , 'path.data ' , true );
707
+
708
+ DelegatingValidator::validateFormData ($ child , $ context );
709
+ }
710
+
711
+ public function testValidateFormDataUsesInheritedClosureValidationGroup ()
712
+ {
713
+ $ graphWalker = $ this ->getMockGraphWalker ();
714
+ $ metadataFactory = $ this ->getMockMetadataFactory ();
715
+ $ context = new ExecutionContext ('Root ' , $ graphWalker , $ metadataFactory );
716
+ $ context ->setPropertyPath ('path ' );
717
9E81
+ $ object = $ this ->getMock ('\stdClass ' );
718
+
719
+ $ parent = $ this ->getBuilder ()
720
+ ->setAttribute ('validation_groups ' , function (FormInterface $ form ){
721
+ return array ('group1 ' , 'group2 ' );
722
+ })
723
+ ->getForm ();
724
+ $ child = $ this ->getBuilder ()
725
+ ->setAttribute ('validation_groups ' , null )
726
+ ->getForm ();
727
+ $ parent ->add ($ child );
728
+
729
+ $ child ->setData ($ object );
730
+
731
+ $ graphWalker ->expects ($ this ->at (0 ))
732
+ ->method ('walkReference ' )
733
+ ->with ($ object , 'group1 ' , 'path.data ' , true );
734
+ $ graphWalker ->expects ($ this ->at (1 ))
735
+ ->method ('walkReference ' )
736
+ ->with ($ object , 'group2 ' , 'path.data ' , true );
737
+
738
+ DelegatingValidator::validateFormData ($ child , $ context );
739
+ }
740
+
626
741
public function testValidateFormDataAppendsPropertyPath ()
627
742
{
628
743
$ graphWalker = $ this ->getMockGraphWalker ();
0 commit comments