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

https://gitlab.com/Marwamimo/Crowdrise_Web · PHP · 360 lines · 238 code · 71 blank · 51 comment · 4 complexity · 5261f73e95c249da67fe334c0927bf1f 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. private $parentType;
  34. private $type;
  35. private $extension1;
  36. private $extension2;
  37. private $parentResolvedType;
  38. private $resolvedType;
  39. protected function setUp()
  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. $this->parentType = $this->getMockFormType();
  45. $this->type = $this->getMockFormType();
  46. $this->extension1 = $this->getMockFormTypeExtension();
  47. $this->extension2 = $this->getMockFormTypeExtension();
  48. $this->parentResolvedType = new ResolvedFormType($this->parentType);
  49. $this->resolvedType = new ResolvedFormType($this->type, array($this->extension1, $this->extension2), $this->parentResolvedType);
  50. }
  51. public function testGetOptionsResolver()
  52. {
  53. if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
  54. $this->markTestSkipped('This test requires PHPUnit 3.7.');
  55. }
  56. $test = $this;
  57. $i = 0;
  58. $assertIndexAndAddOption = function ($index, $option, $default) use (&$i, $test) {
  59. return function (OptionsResolverInterface $resolver) use (&$i, $test, $index, $option, $default) {
  60. /* @var \PHPUnit_Framework_TestCase $test */
  61. $test->assertEquals($index, $i, 'Executed at index '.$index);
  62. ++$i;
  63. $resolver->setDefaults(array($option => $default));
  64. };
  65. };
  66. // First the default options are generated for the super type
  67. $this->parentType->expects($this->once())
  68. ->method('setDefaultOptions')
  69. ->will($this->returnCallback($assertIndexAndAddOption(0, 'a', 'a_default')));
  70. // The form type itself
  71. $this->type->expects($this->once())
  72. ->method('setDefaultOptions')
  73. ->will($this->returnCallback($assertIndexAndAddOption(1, 'b', 'b_default')));
  74. // And its extensions
  75. $this->extension1->expects($this->once())
  76. ->method('setDefaultOptions')
  77. ->will($this->returnCallback($assertIndexAndAddOption(2, 'c', 'c_default')));
  78. $this->extension2->expects($this->once())
  79. ->method('setDefaultOptions')
  80. ->will($this->returnCallback($assertIndexAndAddOption(3, 'd', 'd_default')));
  81. $givenOptions = array('a' => 'a_custom', 'c' => 'c_custom');
  82. $resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default');
  83. $resolver = $this->resolvedType->getOptionsResolver();
  84. $this->assertEquals($resolvedOptions, $resolver->resolve($givenOptions));
  85. }
  86. public function testCreateBuilder()
  87. {
  88. if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
  89. $this->markTestSkipped('This test requires PHPUnit 3.7.');
  90. }
  91. $givenOptions = array('a' => 'a_custom', 'c' => 'c_custom');
  92. $resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default');
  93. $optionsResolver = $this->getMock('Symfony\Component\OptionsResolver\OptionsResolverInterface');
  94. $this->resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType')
  95. ->setConstructorArgs(array($this->type, array($this->extension1, $this->extension2), $this->parentResolvedType))
  96. ->setMethods(array('getOptionsResolver'))
  97. ->getMock();
  98. $this->resolvedType->expects($this->once())
  99. ->method('getOptionsResolver')
  100. ->will($this->returnValue($optionsResolver));
  101. $optionsResolver->expects($this->once())
  102. ->method('resolve')
  103. ->with($givenOptions)
  104. ->will($this->returnValue($resolvedOptions));
  105. $factory = $this->getMockFormFactory();
  106. $builder = $this->resolvedType->createBuilder($factory, 'name', $givenOptions);
  107. $this->assertSame($this->resolvedType, $builder->getType());
  108. $this->assertSame($resolvedOptions, $builder->getOptions());
  109. $this->assertNull($builder->getDataClass());
  110. }
  111. public function testCreateBuilderWithDataClassOption()
  112. {
  113. if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
  114. $this->markTestSkipped('This test requires PHPUnit 3.7.');
  115. }
  116. $givenOptions = array('data_class' => 'Foo');
  117. $resolvedOptions = array('data_class' => '\stdClass');
  118. $optionsResolver = $this->getMock('Symfony\Component\OptionsResolver\OptionsResolverInterface');
  119. $this->resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType')
  120. ->setConstructorArgs(array($this->type, array($this->extension1, $this->extension2), $this->parentResolvedType))
  121. ->setMethods(array('getOptionsResolver'))
  122. ->getMock();
  123. $this->resolvedType->expects($this->once())
  124. ->method('getOptionsResolver')
  125. ->will($this->returnValue($optionsResolver));
  126. $optionsResolver->expects($this->once())
  127. ->method('resolve')
  128. ->with($givenOptions)
  129. ->will($this->returnValue($resolvedOptions));
  130. $factory = $this->getMockFormFactory();
  131. $builder = $this->resolvedType->createBuilder($factory, 'name', $givenOptions);
  132. $this->assertSame($this->resolvedType, $builder->getType());
  133. $this->assertSame($resolvedOptions, $builder->getOptions());
  134. $this->assertSame('\stdClass', $builder->getDataClass());
  135. }
  136. public function testBuildForm()
  137. {
  138. if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
  139. $this->markTestSkipped('This test requires PHPUnit 3.7.');
  140. }
  141. $test = $this;
  142. $i = 0;
  143. $assertIndex = function ($index) use (&$i, $test) {
  144. return function () use (&$i, $test, $index) {
  145. /* @var \PHPUnit_Framework_TestCase $test */
  146. $test->assertEquals($index, $i, 'Executed at index '.$index);
  147. ++$i;
  148. };
  149. };
  150. $options = array('a' => 'Foo', 'b' => 'Bar');
  151. $builder = $this->getMock('Symfony\Component\Form\Test\FormBuilderInterface');
  152. // First the form is built for the super type
  153. $this->parentType->expects($this->once())
  154. ->method('buildForm')
  155. ->with($builder, $options)
  156. ->will($this->returnCallback($assertIndex(0)));
  157. // Then the type itself
  158. $this->type->expects($this->once())
  159. ->method('buildForm')
  160. ->with($builder, $options)
  161. ->will($this->returnCallback($assertIndex(1)));
  162. // Then its extensions
  163. $this->extension1->expects($this->once())
  164. ->method('buildForm')
  165. ->with($builder, $options)
  166. ->will($this->returnCallback($assertIndex(2)));
  167. $this->extension2->expects($this->once())
  168. ->method('buildForm')
  169. ->with($builder, $options)
  170. ->will($this->returnCallback($assertIndex(3)));
  171. $this->resolvedType->buildForm($builder, $options);
  172. }
  173. public function testCreateView()
  174. {
  175. $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
  176. $view = $this->resolvedType->createView($form);
  177. $this->assertInstanceOf('Symfony\Component\Form\FormView', $view);
  178. $this->assertNull($view->parent);
  179. }
  180. public function testCreateViewWithParent()
  181. {
  182. $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
  183. $parentView = $this->getMock('Symfony\Component\Form\FormView');
  184. $view = $this->resolvedType->createView($form, $parentView);
  185. $this->assertInstanceOf('Symfony\Component\Form\FormView', $view);
  186. $this->assertSame($parentView, $view->parent);
  187. }
  188. public function testBuildView()
  189. {
  190. $options = array('a' => '1', 'b' => '2');
  191. $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
  192. $view = $this->getMock('Symfony\Component\Form\FormView');
  193. $test = $this;
  194. $i = 0;
  195. $assertIndex = function ($index) use (&$i, $test) {
  196. return function () use (&$i, $test, $index) {
  197. /* @var \PHPUnit_Framework_TestCase $test */
  198. $test->assertEquals($index, $i, 'Executed at index '.$index);
  199. ++$i;
  200. };
  201. };
  202. // First the super type
  203. $this->parentType->expects($this->once())
  204. ->method('buildView')
  205. ->with($view, $form, $options)
  206. ->will($this->returnCallback($assertIndex(0)));
  207. // Then the type itself
  208. $this->type->expects($this->once())
  209. ->method('buildView')
  210. ->with($view, $form, $options)
  211. ->will($this->returnCallback($assertIndex(1)));
  212. // Then its extensions
  213. $this->extension1->expects($this->once())
  214. ->method('buildView')
  215. ->with($view, $form, $options)
  216. ->will($this->returnCallback($assertIndex(2)));
  217. $this->extension2->expects($this->once())
  218. ->method('buildView')
  219. ->with($view, $form, $options)
  220. ->will($this->returnCallback($assertIndex(3)));
  221. $this->resolvedType->buildView($view, $form, $options);
  222. }
  223. public function testFinishView()
  224. {
  225. $options = array('a' => '1', 'b' => '2');
  226. $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
  227. $view = $this->getMock('Symfony\Component\Form\FormView');
  228. $test = $this;
  229. $i = 0;
  230. $assertIndex = function ($index) use (&$i, $test) {
  231. return function () use (&$i, $test, $index) {
  232. /* @var \PHPUnit_Framework_TestCase $test */
  233. $test->assertEquals($index, $i, 'Executed at index '.$index);
  234. ++$i;
  235. };
  236. };
  237. // First the super type
  238. $this->parentType->expects($this->once())
  239. ->method('finishView')
  240. ->with($view, $form, $options)
  241. ->will($this->returnCallback($assertIndex(0)));
  242. // Then the type itself
  243. $this->type->expects($this->once())
  244. ->method('finishView')
  245. ->with($view, $form, $options)
  246. ->will($this->returnCallback($assertIndex(1)));
  247. // Then its extensions
  248. $this->extension1->expects($this->once())
  249. ->method('finishView')
  250. ->with($view, $form, $options)
  251. ->will($this->returnCallback($assertIndex(2)));
  252. $this->extension2->expects($this->once())
  253. ->method('finishView')
  254. ->with($view, $form, $options)
  255. ->will($this->returnCallback($assertIndex(3)));
  256. $this->resolvedType->finishView($view, $form, $options);
  257. }
  258. /**
  259. * @return \PHPUnit_Framework_MockObject_MockObject
  260. */
  261. private function getMockFormType()
  262. {
  263. return $this->getMock('Symfony\Component\Form\FormTypeInterface');
  264. }
  265. /**
  266. * @return \PHPUnit_Framework_MockObject_MockObject
  267. */
  268. private function getMockFormTypeExtension()
  269. {
  270. return $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
  271. }
  272. /**
  273. * @return \PHPUnit_Framework_MockObject_MockObject
  274. */
  275. private function getMockFormFactory()
  276. {
  277. return $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  278. }
  279. /**
  280. * @param string $name
  281. * @param array $options
  282. *
  283. * @return FormBuilder
  284. */
  285. protected function getBuilder($name = 'name', array $options = array())
  286. {
  287. return new FormBuilder($name, null, $this->dispatcher, $this->factory, $options);
  288. }
  289. }