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

https://bitbucket.org/laborautonomo/laborautonomo-site · PHP · 271 lines · 203 code · 50 blank · 18 comment · 1 complexity · 844de1e4945f720b0528aee7d51cec9b 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\FormBuilder;
  12. class FormBuilderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $dispatcher;
  15. private $factory;
  16. private $builder;
  17. protected function setUp()
  18. {
  19. if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
  20. $this->markTestSkipped('The "EventDispatcher" component is not available');
  21. }
  22. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  23. $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  24. $this->builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
  25. }
  26. protected function tearDown()
  27. {
  28. $this->dispatcher = null;
  29. $this->factory = null;
  30. $this->builder = null;
  31. }
  32. /**
  33. * Changing the name is not allowed, otherwise the name and property path
  34. * are not synchronized anymore
  35. *
  36. * @see FormType::buildForm
  37. */
  38. public function testNoSetName()
  39. {
  40. $this->assertFalse(method_exists($this->builder, 'setName'));
  41. }
  42. public function testAddNameNoString()
  43. {
  44. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  45. $this->builder->add(1234);
  46. }
  47. public function testAddTypeNoString()
  48. {
  49. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  50. $this->builder->add('foo', 1234);
  51. }
  52. public function testAddWithGuessFluent()
  53. {
  54. $this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
  55. $builder = $this->builder->add('foo');
  56. $this->assertSame($builder, $this->builder);
  57. }
  58. public function testAddIsFluent()
  59. {
  60. $builder = $this->builder->add('foo', 'text', array('bar' => 'baz'));
  61. $this->assertSame($builder, $this->builder);
  62. }
  63. public function testAdd()
  64. {
  65. $this->assertFalse($this->builder->has('foo'));
  66. $this->builder->add('foo', 'text');
  67. $this->assertTrue($this->builder->has('foo'));
  68. }
  69. public function testAll()
  70. {
  71. $this->factory->expects($this->once())
  72. ->method('createNamedBuilder')
  73. ->with('foo', 'text')
  74. ->will($this->returnValue(new FormBuilder('foo', null, $this->dispatcher, $this->factory)));
  75. $this->assertCount(0, $this->builder->all());
  76. $this->assertFalse($this->builder->has('foo'));
  77. $this->builder->add('foo', 'text');
  78. $children = $this->builder->all();
  79. $this->assertTrue($this->builder->has('foo'));
  80. $this->assertCount(1, $children);
  81. $this->assertArrayHasKey('foo', $children);
  82. }
  83. /*
  84. * https://github.com/symfony/symfony/issues/4693
  85. */
  86. public function testMaintainOrderOfLazyAndExplicitChildren()
  87. {
  88. $this->builder->add('foo', 'text');
  89. $this->builder->add($this->getFormBuilder('bar'));
  90. $this->builder->add('baz', 'text');
  91. $children = $this->builder->all();
  92. $this->assertSame(array('foo', 'bar', 'baz'), array_keys($children));
  93. }
  94. public function testAddFormType()
  95. {
  96. $this->assertFalse($this->builder->has('foo'));
  97. $this->builder->add('foo', $this->getMock('Symfony\Component\Form\FormTypeInterface'));
  98. $this->assertTrue($this->builder->has('foo'));
  99. }
  100. public function testRemove()
  101. {
  102. $this->builder->add('foo', 'text');
  103. $this->builder->remove('foo');
  104. $this->assertFalse($this->builder->has('foo'));
  105. }
  106. public function testRemoveUnknown()
  107. {
  108. $this->builder->remove('foo');
  109. $this->assertFalse($this->builder->has('foo'));
  110. }
  111. // https://github.com/symfony/symfony/pull/4826
  112. public function testRemoveAndGetForm()
  113. {
  114. $this->builder->add('foo', 'text');
  115. $this->builder->remove('foo');
  116. $form = $this->builder->getForm();
  117. $this->assertInstanceOf('Symfony\Component\Form\Form', $form);
  118. }
  119. public function testCreateNoTypeNo()
  120. {
  121. $this->factory->expects($this->once())
  122. ->method('createNamedBuilder')
  123. ->with('foo', 'text', null, array())
  124. ;
  125. $this->builder->create('foo');
  126. }
  127. public function testGetUnknown()
  128. {
  129. $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The child with the name "foo" does not exist.');
  130. $this->builder->get('foo');
  131. }
  132. public function testGetExplicitType()
  133. {
  134. $expectedType = 'text';
  135. $expectedName = 'foo';
  136. $expectedOptions = array('bar' => 'baz');
  137. $this->factory->expects($this->once())
  138. ->method('createNamedBuilder')
  139. ->with($expectedName, $expectedType, null, $expectedOptions)
  140. ->will($this->returnValue($this->getFormBuilder()));
  141. $this->builder->add($expectedName, $expectedType, $expectedOptions);
  142. $builder = $this->builder->get($expectedName);
  143. $this->assertNotSame($builder, $this->builder);
  144. }
  145. public function testGetGuessedType()
  146. {
  147. $expectedName = 'foo';
  148. $expectedOptions = array('bar' => 'baz');
  149. $this->factory->expects($this->once())
  150. ->method('createBuilderForProperty')
  151. ->with('stdClass', $expectedName, null, $expectedOptions)
  152. ->will($this->returnValue($this->getFormBuilder()));
  153. $this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
  154. $this->builder->add($expectedName, null, $expectedOptions);
  155. $builder = $this->builder->get($expectedName);
  156. $this->assertNotSame($builder, $this->builder);
  157. }
  158. public function testGetParent()
  159. {
  160. $this->assertNull($this->builder->getParent());
  161. }
  162. public function testGetParentForAddedBuilder()
  163. {
  164. $builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
  165. $this->builder->add($builder);
  166. $this->assertSame($this->builder, $builder->getParent());
  167. }
  168. public function testGetParentForRemovedBuilder()
  169. {
  170. $builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
  171. $this->builder->add($builder);
  172. $this->builder->remove('name');
  173. $this->assertNull($builder->getParent());
  174. }
  175. public function testGetParentForCreatedBuilder()
  176. {
  177. $this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
  178. $this->factory
  179. ->expects($this->once())
  180. ->method('createNamedBuilder')
  181. ->with('bar', 'text', null, array(), $this->builder)
  182. ;
  183. $this->factory
  184. ->expects($this->once())
  185. ->method('createBuilderForProperty')
  186. ->with('stdClass', 'foo', null, array(), $this->builder)
  187. ;
  188. $this->builder->create('foo');
  189. $this->builder->create('bar', 'text');
  190. }
  191. public function testGetFormConfigErasesReferences()
  192. {
  193. $builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
  194. $builder->setParent(new FormBuilder('parent', null, $this->dispatcher, $this->factory));
  195. $builder->add(new FormBuilder('child', null, $this->dispatcher, $this->factory));
  196. $config = $builder->getFormConfig();
  197. $reflClass = new \ReflectionClass($config);
  198. $factory = $reflClass->getProperty('factory');
  199. $parent = $reflClass->getProperty('parent');
  200. $children = $reflClass->getProperty('children');
  201. $unresolvedChildren = $reflClass->getProperty('unresolvedChildren');
  202. $factory->setAccessible(true);
  203. $parent->setAccessible(true);
  204. $children->setAccessible(true);
  205. $unresolvedChildren->setAccessible(true);
  206. $this->assertNull($factory->getValue($config));
  207. $this->assertNull($parent->getValue($config));
  208. $this->assertEmpty($children->getValue($config));
  209. $this->assertEmpty($unresolvedChildren->getValue($config));
  210. }
  211. private function getFormBuilder($name = 'name')
  212. {
  213. $mock = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  214. ->disableOriginalConstructor()
  215. ->getMock();
  216. $mock->expects($this->any())
  217. ->method('getName')
  218. ->will($this->returnValue($name));
  219. return $mock;
  220. }
  221. }