PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Form/Tests/FormBuilderTest.php

https://github.com/Exercise/symfony
PHP | 228 lines | 171 code | 37 blank | 20 comment | 2 complexity | 6e5a41166f3c66dfde01cdbf25654978 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. use Symfony\Component\Form\Guess\Guess;
  13. class FormBuilderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $dispatcher;
  16. private $factory;
  17. private $builder;
  18. protected function setUp()
  19. {
  20. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  21. $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  22. $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher);
  23. }
  24. protected function tearDown()
  25. {
  26. $this->dispatcher = null;
  27. $this->factory = null;
  28. $this->builder = null;
  29. }
  30. public function getHtml4Ids()
  31. {
  32. // The full list is tested in FormTest, since both Form and FormBuilder
  33. // use the same implementation internally
  34. return array(
  35. array('#', false),
  36. array('a ', false),
  37. array("a\t", false),
  38. array("a\n", false),
  39. array('a.', false),
  40. );
  41. }
  42. /**
  43. * @dataProvider getHtml4Ids
  44. */
  45. public function testConstructAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
  46. {
  47. try {
  48. new FormBuilder($name, $this->factory, $this->dispatcher);
  49. if (!$accepted) {
  50. $this->fail(sprintf('The value "%s" should not be accepted', $name));
  51. }
  52. } catch (\InvalidArgumentException $e) {
  53. // if the value was not accepted, but should be, rethrow exception
  54. if ($accepted) {
  55. throw $e;
  56. }
  57. }
  58. }
  59. /**
  60. * Changing the name is not allowed, otherwise the name and property path
  61. * are not synchronized anymore
  62. *
  63. * @see FieldType::buildForm
  64. */
  65. public function testNoSetName()
  66. {
  67. $this->assertFalse(method_exists($this->builder, 'setName'));
  68. }
  69. public function testAddNameNoString()
  70. {
  71. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  72. $this->builder->add(1234);
  73. }
  74. public function testAddTypeNoString()
  75. {
  76. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  77. $this->builder->add('foo', 1234);
  78. }
  79. public function testAddWithGuessFluent()
  80. {
  81. $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
  82. $builder = $this->builder->add('foo');
  83. $this->assertSame($builder, $this->builder);
  84. }
  85. public function testAddIsFluent()
  86. {
  87. $builder = $this->builder->add('foo', 'text', array('bar' => 'baz'));
  88. $this->assertSame($builder, $this->builder);
  89. }
  90. public function testAdd()
  91. {
  92. $this->assertFalse($this->builder->has('foo'));
  93. $this->builder->add('foo', 'text');
  94. $this->assertTrue($this->builder->has('foo'));
  95. }
  96. public function testAddFormType()
  97. {
  98. $this->assertFalse($this->builder->has('foo'));
  99. $this->builder->add('foo', $this->getMock('Symfony\Component\Form\FormTypeInterface'));
  100. $this->assertTrue($this->builder->has('foo'));
  101. }
  102. public function testRemove()
  103. {
  104. $this->builder->add('foo', 'text');
  105. $this->builder->remove('foo');
  106. $this->assertFalse($this->builder->has('foo'));
  107. }
  108. public function testRemoveUnknown()
  109. {
  110. $this->builder->remove('foo');
  111. $this->assertFalse($this->builder->has('foo'));
  112. }
  113. public function testCreateNoTypeNoDataClass()
  114. {
  115. $this->factory->expects($this->once())
  116. ->method('createNamedBuilder')
  117. ->with('text', 'foo', null, array())
  118. ;
  119. $builder = $this->builder->create('foo');
  120. }
  121. public function testGetUnknown()
  122. {
  123. $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The field "foo" does not exist');
  124. $this->builder->get('foo');
  125. }
  126. public function testGetTyped()
  127. {
  128. $expectedType = 'text';
  129. $expectedName = 'foo';
  130. $expectedOptions = array('bar' => 'baz');
  131. $this->factory->expects($this->once())
  132. ->method('createNamedBuilder')
  133. ->with($expectedType, $expectedName, null, $expectedOptions)
  134. ->will($this->returnValue($this->getFormBuilder()));
  135. $this->builder->add($expectedName, $expectedType, $expectedOptions);
  136. $builder = $this->builder->get($expectedName);
  137. $this->assertNotSame($builder, $this->builder);
  138. }
  139. public function testGetGuessed()
  140. {
  141. $expectedName = 'foo';
  142. $expectedOptions = array('bar' => 'baz');
  143. $this->factory->expects($this->once())
  144. ->method('createBuilderForProperty')
  145. ->with('stdClass', $expectedName, null, $expectedOptions)
  146. ->will($this->returnValue($this->getFormBuilder()));
  147. $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
  148. $this->builder->add($expectedName, null, $expectedOptions);
  149. $builder = $this->builder->get($expectedName);
  150. $this->assertNotSame($builder, $this->builder);
  151. }
  152. public function testGetParent()
  153. {
  154. $this->assertNull($this->builder->getParent());
  155. }
  156. public function testGetParentForAddedBuilder()
  157. {
  158. $builder = new FormBuilder('name', $this->factory, $this->dispatcher);
  159. $this->builder->add($builder);
  160. $this->assertSame($this->builder, $builder->getParent());
  161. }
  162. public function testGetParentForRemovedBuilder()
  163. {
  164. $builder = new FormBuilder('name', $this->factory, $this->dispatcher);
  165. $this->builder->add($builder);
  166. $this->builder->remove('name');
  167. $this->assertNull($builder->getParent());
  168. }
  169. public function testGetParentForCreatedBuilder()
  170. {
  171. $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
  172. $this->factory
  173. ->expects($this->once())
  174. ->method('createNamedBuilder')
  175. ->with('text', 'bar', null, array(), $this->builder)
  176. ;
  177. $this->factory
  178. ->expects($this->once())
  179. ->method('createBuilderForProperty')
  180. ->with('stdClass', 'foo', null, array(), $this->builder)
  181. ;
  182. $this->builder->create('foo');
  183. $this->builder->create('bar', 'text');
  184. }
  185. private function getFormBuilder()
  186. {
  187. return $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  188. ->disableOriginalConstructor()
  189. ->getMock();
  190. }
  191. }