]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/ResolvedFormTypeTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / ResolvedFormTypeTest.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;
13
14 use Symfony\Component\Form\ResolvedFormType;
15 use Symfony\Component\Form\FormView;
16 use Symfony\Component\Form\FormBuilder;
17 use Symfony\Component\Form\Form;
18 use Symfony\Component\OptionsResolver\OptionsResolverInterface;
19
20 /**
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 */
23 class ResolvedFormTypeTest extends \PHPUnit_Framework_TestCase
24 {
25 /**
26 * @var \PHPUnit_Framework_MockObject_MockObject
27 */
28 private $dispatcher;
29
30 /**
31 * @var \PHPUnit_Framework_MockObject_MockObject
32 */
33 private $factory;
34
35 /**
36 * @var \PHPUnit_Framework_MockObject_MockObject
37 */
38 private $dataMapper;
39
40 protected function setUp()
41 {
42 if (!class_exists('Symfony\Component\OptionsResolver\OptionsResolver')) {
43 $this->markTestSkipped('The "OptionsResolver" component is not available');
44 }
45
46 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
47 $this->markTestSkipped('The "EventDispatcher" component is not available');
48 }
49
50 $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
51 $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
52 $this->dataMapper = $this->getMock('Symfony\Component\Form\DataMapperInterface');
53 }
54
55 public function testCreateBuilder()
56 {
57 if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
58 $this->markTestSkipped('This test requires PHPUnit 3.7.');
59 }
60
61 $parentType = $this->getMockFormType();
62 $type = $this->getMockFormType();
63 $extension1 = $this->getMockFormTypeExtension();
64 $extension2 = $this->getMockFormTypeExtension();
65
66 $parentResolvedType = new ResolvedFormType($parentType);
67 $resolvedType = new ResolvedFormType($type, array($extension1, $extension2), $parentResolvedType);
68
69 $test = $this;
70 $i = 0;
71
72 $assertIndex = function ($index) use (&$i, $test) {
73 return function () use (&$i, $test, $index) {
74 /* @var \PHPUnit_Framework_TestCase $test */
75 $test->assertEquals($index, $i, 'Executed at index '.$index);
76
77 ++$i;
78 };
79 };
80
81 $assertIndexAndAddOption = function ($index, $option, $default) use ($assertIndex) {
82 $assertIndex = $assertIndex($index);
83
84 return function (OptionsResolverInterface $resolver) use ($assertIndex, $index, $option, $default) {
85 $assertIndex();
86
87 $resolver->setDefaults(array($option => $default));
88 };
89 };
90
91 // First the default options are generated for the super type
92 $parentType->expects($this->once())
93 ->method('setDefaultOptions')
94 ->will($this->returnCallback($assertIndexAndAddOption(0, 'a', 'a_default')));
95
96 // The form type itself
97 $type->expects($this->once())
98 ->method('setDefaultOptions')
99 ->will($this->returnCallback($assertIndexAndAddOption(1, 'b', 'b_default')));
100
101 // And its extensions
102 $extension1->expects($this->once())
103 ->method('setDefaultOptions')
104 ->will($this->returnCallback($assertIndexAndAddOption(2, 'c', 'c_default')));
105
106 $extension2->expects($this->once())
107 ->method('setDefaultOptions')
108 ->will($this->returnCallback($assertIndexAndAddOption(3, 'd', 'd_default')));
109
110 $givenOptions = array('a' => 'a_custom', 'c' => 'c_custom');
111 $resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default');
112
113 // Then the form is built for the super type
114 $parentType->expects($this->once())
115 ->method('buildForm')
116 ->with($this->anything(), $resolvedOptions)
117 ->will($this->returnCallback($assertIndex(4)));
118
119 // Then the type itself
120 $type->expects($this->once())
121 ->method('buildForm')
122 ->with($this->anything(), $resolvedOptions)
123 ->will($this->returnCallback($assertIndex(5)));
124
125 // Then its extensions
126 $extension1->expects($this->once())
127 ->method('buildForm')
128 ->with($this->anything(), $resolvedOptions)
129 ->will($this->returnCallback($assertIndex(6)));
130
131 $extension2->expects($this->once())
132 ->method('buildForm')
133 ->with($this->anything(), $resolvedOptions)
134 ->will($this->returnCallback($assertIndex(7)));
135
136 $factory = $this->getMockFormFactory();
137 $builder = $resolvedType->createBuilder($factory, 'name', $givenOptions);
138
139 $this->assertSame($resolvedType, $builder->getType());
140 }
141
142 public function testCreateView()
143 {
144 $parentType = $this->getMockFormType();
145 $type = $this->getMockFormType();
146 $field1Type = $this->getMockFormType();
147 $field2Type = $this->getMockFormType();
148 $extension1 = $this->getMockFormTypeExtension();
149 $extension2 = $this->getMockFormTypeExtension();
150
151 $parentResolvedType = new ResolvedFormType($parentType);
152 $resolvedType = new ResolvedFormType($type, array($extension1, $extension2), $parentResolvedType);
153 $field1ResolvedType = new ResolvedFormType($field1Type);
154 $field2ResolvedType = new ResolvedFormType($field2Type);
155
156 $options = array('a' => '1', 'b' => '2');
157 $form = $this->getBuilder('name', $options)
158 ->setCompound(true)
159 ->setDataMapper($this->dataMapper)
160 ->setType($resolvedType)
161 ->add($this->getBuilder('foo')->setType($field1ResolvedType))
162 ->add($this->getBuilder('bar')->setType($field2ResolvedType))
163 ->getForm();
164
165 $test = $this;
166 $i = 0;
167
168 $assertIndexAndNbOfChildViews = function ($index, $nbOfChildViews) use (&$i, $test) {
169 return function (FormView $view) use (&$i, $test, $index, $nbOfChildViews) {
170 /* @var \PHPUnit_Framework_TestCase $test */
171 $test->assertEquals($index, $i, 'Executed at index '.$index);
172 $test->assertCount($nbOfChildViews, $view);
173
174 ++$i;
175 };
176 };
177
178 // First the super type
179 $parentType->expects($this->once())
180 ->method('buildView')
181 ->with($this->anything(), $form, $options)
182 ->will($this->returnCallback($assertIndexAndNbOfChildViews(0, 0)));
183
184 // Then the type itself
185 $type->expects($this->once())
186 ->method('buildView')
187 ->with($this->anything(), $form, $options)
188 ->will($this->returnCallback($assertIndexAndNbOfChildViews(1, 0)));
189
190 // Then its extensions
191 $extension1->expects($this->once())
192 ->method('buildView')
193 ->with($this->anything(), $form, $options)
194 ->will($this->returnCallback($assertIndexAndNbOfChildViews(2, 0)));
195
196 $extension2->expects($this->once())
197 ->method('buildView')
198 ->with($this->anything(), $form, $options)
199 ->will($this->returnCallback($assertIndexAndNbOfChildViews(3, 0)));
200
201 // Now the first child form
202 $field1Type->expects($this->once())
203 ->method('buildView')
204 ->will($this->returnCallback($assertIndexAndNbOfChildViews(4, 0)));
205 $field1Type->expects($this->once())
206 ->method('finishView')
207 ->will($this->returnCallback($assertIndexAndNbOfChildViews(5, 0)));
208
209 // And the second child form
210 $field2Type->expects($this->once())
211 ->method('buildView')
212 ->will($this->returnCallback($assertIndexAndNbOfChildViews(6, 0)));
213 $field2Type->expects($this->once())
214 ->method('finishView')
215 ->will($this->returnCallback($assertIndexAndNbOfChildViews(7, 0)));
216
217 // Again first the parent
218 $parentType->expects($this->once())
219 ->method('finishView')
220 ->with($this->anything(), $form, $options)
221 ->will($this->returnCallback($assertIndexAndNbOfChildViews(8, 2)));
222
223 // Then the type itself
224 $type->expects($this->once())
225 ->method('finishView')
226 ->with($this->anything(), $form, $options)
227 ->will($this->returnCallback($assertIndexAndNbOfChildViews(9, 2)));
228
229 // Then its extensions
230 $extension1->expects($this->once())
231 ->method('finishView')
232 ->with($this->anything(), $form, $options)
233 ->will($this->returnCallback($assertIndexAndNbOfChildViews(10, 2)));
234
235 $extension2->expects($this->once())
236 ->method('finishView')
237 ->with($this->anything(), $form, $options)
238 ->will($this->returnCallback($assertIndexAndNbOfChildViews(11, 2)));
239
240 $parentView = new FormView();
241 $view = $resolvedType->createView($form, $parentView);
242
243 $this->assertSame($parentView, $view->parent);
244 }
245
246 /**
247 * @return \PHPUnit_Framework_MockObject_MockObject
248 */
249 private function getMockFormType()
250 {
251 return $this->getMock('Symfony\Component\Form\FormTypeInterface');
252 }
253
254 /**
255 * @return \PHPUnit_Framework_MockObject_MockObject
256 */
257 private function getMockFormTypeExtension()
258 {
259 return $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
260 }
261
262 /**
263 * @return \PHPUnit_Framework_MockObject_MockObject
264 */
265 private function getMockFormFactory()
266 {
267 return $this->getMock('Symfony\Component\Form\FormFactoryInterface');
268 }
269
270 /**
271 * @param string $name
272 * @param array $options
273 *
274 * @return FormBuilder
275 */
276 protected function getBuilder($name = 'name', array $options = array())
277 {
278 return new FormBuilder($name, null, $this->dispatcher, $this->factory, $options);
279 }
280 }