8000 Add DelegatingValidator tests · symfony/symfony@d08ec5e · GitHub
[go: up one dir, main page]

Skip to content

Commit d08ec5e

Browse files
committed
Add DelegatingValidator tests
1 parent e1822e7 commit d08ec5e

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

tests/Symfony/Tests/Component/Form/Extension/Validator/Validator/DelegatingValidatorTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Tests\Component\Form\Extension\Validator\Validator;
1313

14+
use Symfony\Component\Form\FormInterface;
1415
use Symfony\Component\Form\FormBuilder;
1516
use Symfony\Component\Form\FormError;
1617
use Symfony\Component\Form\Util\PropertyPath;
@@ -91,6 +92,16 @@ protected function getMockForm()
9192
return $this->getMock('Symfony\Tests\Component\Form\FormInterface');
9293
}
9394

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+
94105
public function testUseValidateValueWhenValidationConstraintExist()
95106
{
96107
$constraint = $this->getMockForAbstractClass('Symfony\Component\Validator\Constraint');
@@ -598,6 +609,52 @@ public function testValidateFormData()
598609
DelegatingValidator::validateFormData($form, $context);
599610
}
600611

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+
601658
public function testValidateFormDataUsesInheritedValidationGroup()
602659
{
603660
$graphWalker = $this->getMockGraphWalker();
@@ -623,6 +680,64 @@ public function testValidateFormDataUsesInheritedValidationGroup()
623680
DelegatingValidator::validateFormData($child, $context);
624681
}
625682

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+
626741
public function testValidateFormDataAppendsPropertyPath()
627742
{
628743
$graphWalker = $this->getMockGraphWalker();

0 commit comments

Comments
 (0)
0