FormBuilderTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\ButtonBuilder;
  13. use Symfony\Component\Form\FormBuilder;
  14. use Symfony\Component\Form\SubmitButtonBuilder;
  15. class FormBuilderTest extends TestCase
  16. {
  17. private $dispatcher;
  18. private $factory;
  19. private $builder;
  20. protected function setUp()
  21. {
  22. $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  23. $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
  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 testAddNameNoStringAndNoInteger()
  43. {
  44. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\UnexpectedTypeException');
  45. $this->builder->add(true);
  46. }
  47. public function testAddTypeNoString()
  48. {
  49. $this->{method_exists($this, $_ = 'expectException') ? $_ : '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', 'Symfony\Component\Form\Extension\Core\Type\TextType', 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', 'Symfony\Component\Form\Extension\Core\Type\TextType');
  67. $this->assertTrue($this->builder->has('foo'));
  68. }
  69. public function testAddIntegerName()
  70. {
  71. $this->assertFalse($this->builder->has(0));
  72. $this->builder->add(0, 'Symfony\Component\Form\Extension\Core\Type\TextType');
  73. $this->assertTrue($this->builder->has(0));
  74. }
  75. public function testAll()
  76. {
  77. $this->factory->expects($this->once())
  78. ->method('createNamedBuilder')
  79. ->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType')
  80. ->will($this->returnValue(new FormBuilder('foo', null, $this->dispatcher, $this->factory)));
  81. $this->assertCount(0, $this->builder->all());
  82. $this->assertFalse($this->builder->has('foo'));
  83. $this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
  84. $children = $this->builder->all();
  85. $this->assertTrue($this->builder->has('foo'));
  86. $this->assertCount(1, $children);
  87. $this->assertArrayHasKey('foo', $children);
  88. }
  89. /*
  90. * https://github.com/symfony/symfony/issues/4693
  91. */
  92. public function testMaintainOrderOfLazyAndExplicitChildren()
  93. {
  94. $this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
  95. $this->builder->add($this->getFormBuilder('bar'));
  96. $this->builder->add('baz', 'Symfony\Component\Form\Extension\Core\Type\TextType');
  97. $children = $this->builder->all();
  98. $this->assertSame(array('foo', 'bar', 'baz'), array_keys($children));
  99. }
  100. public function testAddFormType()
  101. {
  102. $this->assertFalse($this->builder->has('foo'));
  103. $this->builder->add('foo', $this->getMockBuilder('Symfony\Component\Form\FormTypeInterface')->getMock());
  104. $this->assertTrue($this->builder->has('foo'));
  105. }
  106. public function testRemove()
  107. {
  108. $this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
  109. $this->builder->remove('foo');
  110. $this->assertFalse($this->builder->has('foo'));
  111. }
  112. public function testRemoveUnknown()
  113. {
  114. $this->builder->remove('foo');
  115. $this->assertFalse($this->builder->has('foo'));
  116. }
  117. // https://github.com/symfony/symfony/pull/4826
  118. public function testRemoveAndGetForm()
  119. {
  120. $this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
  121. $this->builder->remove('foo');
  122. $form = $this->builder->getForm();
  123. $this->assertInstanceOf('Symfony\Component\Form\Form', $form);
  124. }
  125. public function testCreateNoTypeNo()
  126. {
  127. $this->factory->expects($this->once())
  128. ->method('createNamedBuilder')
  129. ->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array())
  130. ;
  131. $this->builder->create('foo');
  132. }
  133. public function testAddButton()
  134. {
  135. $this->builder->add(new ButtonBuilder('reset'));
  136. $this->builder->add(new SubmitButtonBuilder('submit'));
  137. $this->assertCount(2, $this->builder->all());
  138. }
  139. public function testGetUnknown()
  140. {
  141. if (method_exists($this, 'expectException')) {
  142. $this->expectException('Symfony\Component\Form\Exception\InvalidArgumentException');
  143. $this->expectExceptionMessage('The child with the name "foo" does not exist.');
  144. } else {
  145. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidArgumentException', 'The child with the name "foo" does not exist.');
  146. }
  147. $this->builder->get('foo');
  148. }
  149. public function testGetExplicitType()
  150. {
  151. $expectedType = 'Symfony\Component\Form\Extension\Core\Type\TextType';
  152. $expectedName = 'foo';
  153. $expectedOptions = array('bar' => 'baz');
  154. $this->factory->expects($this->once())
  155. ->method('createNamedBuilder')
  156. ->with($expectedName, $expectedType, null, $expectedOptions)
  157. ->will($this->returnValue($this->getFormBuilder()));
  158. $this->builder->add($expectedName, $expectedType, $expectedOptions);
  159. $builder = $this->builder->get($expectedName);
  160. $this->assertNotSame($builder, $this->builder);
  161. }
  162. public function testGetGuessedType()
  163. {
  164. $expectedName = 'foo';
  165. $expectedOptions = array('bar' => 'baz');
  166. $this->factory->expects($this->once())
  167. ->method('createBuilderForProperty')
  168. ->with('stdClass', $expectedName, null, $expectedOptions)
  169. ->will($this->returnValue($this->getFormBuilder()));
  170. $this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
  171. $this->builder->add($expectedName, null, $expectedOptions);
  172. $builder = $this->builder->get($expectedName);
  173. $this->assertNotSame($builder, $this->builder);
  174. }
  175. public function testGetFormConfigErasesReferences()
  176. {
  177. $builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
  178. $builder->add(new FormBuilder('child', null, $this->dispatcher, $this->factory));
  179. $config = $builder->getFormConfig();
  180. $reflClass = new \ReflectionClass($config);
  181. $children = $reflClass->getProperty('children');
  182. $unresolvedChildren = $reflClass->getProperty('unresolvedChildren');
  183. $children->setAccessible(true);
  184. $unresolvedChildren->setAccessible(true);
  185. $this->assertEmpty($children->getValue($config));
  186. $this->assertEmpty($unresolvedChildren->getValue($config));
  187. }
  188. private function getFormBuilder($name = 'name')
  189. {
  190. $mock = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  191. ->disableOriginalConstructor()
  192. ->getMock();
  193. $mock->expects($this->any())
  194. ->method('getName')
  195. ->will($this->returnValue($name));
  196. return $mock;
  197. }
  198. }