4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Validator\EventListener
;
14 use Symfony\Component\Form\FormBuilder
;
15 use Symfony\Component\Form\FormEvent
;
16 use Symfony\Component\Form\Extension\Validator\Constraints\Form
;
17 use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener
;
18 use Symfony\Component\PropertyAccess\PropertyPath
;
19 use Symfony\Component\Validator\ConstraintViolation
;
21 class ValidationListenerTest
extends \PHPUnit_Framework_TestCase
24 * @var \PHPUnit_Framework_MockObject_MockObject
29 * @var \PHPUnit_Framework_MockObject_MockObject
34 * @var \PHPUnit_Framework_MockObject_MockObject
39 * @var \PHPUnit_Framework_MockObject_MockObject
41 private $violationMapper;
44 * @var ValidationListener
50 private $messageTemplate;
54 protected function setUp()
56 if (!class_exists('Symfony\Component\EventDispatcher\Event')) {
57 $this->markTestSkipped('The "EventDispatcher" component is not available');
60 $this->dispatcher
= $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
61 $this->factory
= $this->getMock('Symfony\Component\Form\FormFactoryInterface');
62 $this->validator
= $this->getMock('Symfony\Component\Validator\ValidatorInterface');
63 $this->violationMapper
= $this->getMock('Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface');
64 $this->listener
= new ValidationListener($this->validator
, $this->violationMapper
);
65 $this->message
= 'Message';
66 $this->messageTemplate
= 'Message template';
67 $this->params
= array('foo' => 'bar');
70 private function getConstraintViolation($code = null)
72 return new ConstraintViolation($this->message
, $this->messageTemplate
, $this->params
, null, 'prop.path', null, null, $code);
75 private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
77 $builder = new FormBuilder($name, $dataClass, $this->dispatcher
, $this->factory
);
78 $builder->setPropertyPath(new PropertyPath($propertyPath ?: $name));
79 $builder->setAttribute('error_mapping', array());
80 $builder->setErrorBubbling(false);
81 $builder->setMapped(true);
86 private function getForm($name = 'name', $propertyPath = null, $dataClass = null)
88 return $this->getBuilder($name, $propertyPath, $dataClass)->getForm();
91 private function getMockForm()
93 return $this->getMock('Symfony\Component\Form\Test\FormInterface');
96 // More specific mapping tests can be found in ViolationMapperTest
97 public function testMapViolation()
99 $violation = $this->getConstraintViolation();
100 $form = $this->getForm('street');
102 $this->validator
->expects($this->once())
104 ->will($this->returnValue(array($violation)));
106 $this->violationMapper
->expects($this->once())
107 ->method('mapViolation')
108 ->with($violation, $form, false);
110 $this->listener
->validateForm(new FormEvent($form, null));
113 public function testMapViolationAllowsNonSyncIfInvalid()
115 $violation = $this->getConstraintViolation(Form
::ERR_INVALID
);
116 $form = $this->getForm('street');
118 $this->validator
->expects($this->once())
120 ->will($this->returnValue(array($violation)));
122 $this->violationMapper
->expects($this->once())
123 ->method('mapViolation')
125 ->with($violation, $form, true);
127 $this->listener
->validateForm(new FormEvent($form, null));
130 public function testValidateIgnoresNonRoot()
132 $form = $this->getMockForm();
133 $form->expects($this->once())
135 ->will($this->returnValue(false));
137 $this->validator
->expects($this->never())
138 ->method('validate');
140 $this->violationMapper
->expects($this->never())
141 ->method('mapViolation');
143 $this->listener
->validateForm(new FormEvent($form, null));