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
;
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
;
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class ResolvedFormTypeTest
extends \PHPUnit_Framework_TestCase
26 * @var \PHPUnit_Framework_MockObject_MockObject
31 * @var \PHPUnit_Framework_MockObject_MockObject
36 * @var \PHPUnit_Framework_MockObject_MockObject
40 protected function setUp()
42 if (!class_exists('Symfony\Component\OptionsResolver\OptionsResolver')) {
43 $this->markTestSkipped('The "OptionsResolver" component is not available');
46 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
47 $this->markTestSkipped('The "EventDispatcher" component is not available');
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');
55 public function testCreateBuilder()
57 if (version_compare(\PHPUnit_Runner_Version
::id(), '3.7', '<')) {
58 $this->markTestSkipped('This test requires PHPUnit 3.7.');
61 $parentType = $this->getMockFormType();
62 $type = $this->getMockFormType();
63 $extension1 = $this->getMockFormTypeExtension();
64 $extension2 = $this->getMockFormTypeExtension();
66 $parentResolvedType = new ResolvedFormType($parentType);
67 $resolvedType = new ResolvedFormType($type, array($extension1, $extension2), $parentResolvedType);
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);
81 $assertIndexAndAddOption = function ($index, $option, $default) use ($assertIndex) {
82 $assertIndex = $assertIndex($index);
84 return function (OptionsResolverInterface
$resolver) use ($assertIndex, $index, $option, $default) {
87 $resolver->setDefaults(array($option => $default));
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')));
96 // The form type itself
97 $type->expects($this->once())
98 ->method('setDefaultOptions')
99 ->will($this->returnCallback($assertIndexAndAddOption(1, 'b', 'b_default')));
101 // And its extensions
102 $extension1->expects($this->once())
103 ->method('setDefaultOptions')
104 ->will($this->returnCallback($assertIndexAndAddOption(2, 'c', 'c_default')));
106 $extension2->expects($this->once())
107 ->method('setDefaultOptions')
108 ->will($this->returnCallback($assertIndexAndAddOption(3, 'd', 'd_default')));
110 $givenOptions = array('a' => 'a_custom', 'c' => 'c_custom');
111 $resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default');
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)));
119 // Then the type itself
120 $type->expects($this->once())
121 ->method('buildForm')
122 ->with($this->anything(), $resolvedOptions)
123 ->will($this->returnCallback($assertIndex(5)));
125 // Then its extensions
126 $extension1->expects($this->once())
127 ->method('buildForm')
128 ->with($this->anything(), $resolvedOptions)
129 ->will($this->returnCallback($assertIndex(6)));
131 $extension2->expects($this->once())
132 ->method('buildForm')
133 ->with($this->anything(), $resolvedOptions)
134 ->will($this->returnCallback($assertIndex(7)));
136 $factory = $this->getMockFormFactory();
137 $builder = $resolvedType->createBuilder($factory, 'name', $givenOptions);
139 $this->assertSame($resolvedType, $builder->getType());
142 public function testCreateView()
144 $parentType = $this->getMockFormType();
145 $type = $this->getMockFormType();
146 $field1Type = $this->getMockFormType();
147 $field2Type = $this->getMockFormType();
148 $extension1 = $this->getMockFormTypeExtension();
149 $extension2 = $this->getMockFormTypeExtension();
151 $parentResolvedType = new ResolvedFormType($parentType);
152 $resolvedType = new ResolvedFormType($type, array($extension1, $extension2), $parentResolvedType);
153 $field1ResolvedType = new ResolvedFormType($field1Type);
154 $field2ResolvedType = new ResolvedFormType($field2Type);
156 $options = array('a' => '1', 'b' => '2');
157 $form = $this->getBuilder('name', $options)
159 ->setDataMapper($this->dataMapper
)
160 ->setType($resolvedType)
161 ->add($this->getBuilder('foo')->setType($field1ResolvedType))
162 ->add($this->getBuilder('bar')->setType($field2ResolvedType))
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);
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)));
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)));
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)));
196 $extension2->expects($this->once())
197 ->method('buildView')
198 ->with($this->anything(), $form, $options)
199 ->will($this->returnCallback($assertIndexAndNbOfChildViews(3, 0)));
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)));
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)));
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)));
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)));
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)));
235 $extension2->expects($this->once())
236 ->method('finishView')
237 ->with($this->anything(), $form, $options)
238 ->will($this->returnCallback($assertIndexAndNbOfChildViews(11, 2)));
240 $parentView = new FormView();
241 $view = $resolvedType->createView($form, $parentView);
243 $this->assertSame($parentView, $view->parent
);
247 * @return \PHPUnit_Framework_MockObject_MockObject
249 private function getMockFormType()
251 return $this->getMock('Symfony\Component\Form\FormTypeInterface');
255 * @return \PHPUnit_Framework_MockObject_MockObject
257 private function getMockFormTypeExtension()
259 return $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
263 * @return \PHPUnit_Framework_MockObject_MockObject
265 private function getMockFormFactory()
267 return $this->getMock('Symfony\Component\Form\FormFactoryInterface');
271 * @param string $name
272 * @param array $options
274 * @return FormBuilder
276 protected function getBuilder($name = 'name', array $options = array())
278 return new FormBuilder($name, null, $this->dispatcher
, $this->factory
, $options);