/vendor/symfony/form/Symfony/Component/Form/Tests/ResolvedFormTypeTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site · PHP · 282 lines · 180 code · 51 blank · 51 comment · 3 complexity · 77dd55135222ae29daf6e6c10c1a12ba MD5 · raw file

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