]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Validator / EventListener / ValidationListenerTest.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Tests\Extension\Validator\EventListener;
13
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;
20
21 class ValidationListenerTest extends \PHPUnit_Framework_TestCase
22 {
23 /**
24 * @var \PHPUnit_Framework_MockObject_MockObject
25 */
26 private $dispatcher;
27
28 /**
29 * @var \PHPUnit_Framework_MockObject_MockObject
30 */
31 private $factory;
32
33 /**
34 * @var \PHPUnit_Framework_MockObject_MockObject
35 */
36 private $validator;
37
38 /**
39 * @var \PHPUnit_Framework_MockObject_MockObject
40 */
41 private $violationMapper;
42
43 /**
44 * @var ValidationListener
45 */
46 private $listener;
47
48 private $message;
49
50 private $messageTemplate;
51
52 private $params;
53
54 protected function setUp()
55 {
56 if (!class_exists('Symfony\Component\EventDispatcher\Event')) {
57 $this->markTestSkipped('The "EventDispatcher" component is not available');
58 }
59
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');
68 }
69
70 private function getConstraintViolation($code = null)
71 {
72 return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code);
73 }
74
75 private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
76 {
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);
82
83 return $builder;
84 }
85
86 private function getForm($name = 'name', $propertyPath = null, $dataClass = null)
87 {
88 return $this->getBuilder($name, $propertyPath, $dataClass)->getForm();
89 }
90
91 private function getMockForm()
92 {
93 return $this->getMock('Symfony\Component\Form\Test\FormInterface');
94 }
95
96 // More specific mapping tests can be found in ViolationMapperTest
97 public function testMapViolation()
98 {
99 $violation = $this->getConstraintViolation();
100 $form = $this->getForm('street');
101
102 $this->validator->expects($this->once())
103 ->method('validate')
104 ->will($this->returnValue(array($violation)));
105
106 $this->violationMapper->expects($this->once())
107 ->method('mapViolation')
108 ->with($violation, $form, false);
109
110 $this->listener->validateForm(new FormEvent($form, null));
111 }
112
113 public function testMapViolationAllowsNonSyncIfInvalid()
114 {
115 $violation = $this->getConstraintViolation(Form::ERR_INVALID);
116 $form = $this->getForm('street');
117
118 $this->validator->expects($this->once())
119 ->method('validate')
120 ->will($this->returnValue(array($violation)));
121
122 $this->violationMapper->expects($this->once())
123 ->method('mapViolation')
124 // pass true now
125 ->with($violation, $form, true);
126
127 $this->listener->validateForm(new FormEvent($form, null));
128 }
129
130 public function testValidateIgnoresNonRoot()
131 {
132 $form = $this->getMockForm();
133 $form->expects($this->once())
134 ->method('isRoot')
135 ->will($this->returnValue(false));
136
137 $this->validator->expects($this->never())
138 ->method('validate');
139
140 $this->violationMapper->expects($this->never())
141 ->method('mapViolation');
142
143 $this->listener->validateForm(new FormEvent($form, null));
144 }
145 }