FormTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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\Tests\Component\Form;
  11. require_once __DIR__.'/Fixtures/FixedDataTransformer.php';
  12. use Symfony\Component\Form\Form;
  13. use Symfony\Component\Form\FormBuilder;
  14. use Symfony\Component\Form\FormError;
  15. use Symfony\Tests\Component\Form\Fixtures\FixedDataTransformer;
  16. class FormTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $dispatcher;
  19. private $builder;
  20. private $form;
  21. protected function setUp()
  22. {
  23. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  24. $this->form = $this->getBuilder()->getForm();
  25. }
  26. /**
  27. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  28. */
  29. public function testConstructExpectsValidValidators()
  30. {
  31. $validators = array(new \stdClass());
  32. new Form('name', $this->dispatcher, array(), null, null, null, $validators);
  33. }
  34. public function testDataIsInitializedEmpty()
  35. {
  36. $norm = new FixedDataTransformer(array(
  37. '' => 'foo',
  38. ));
  39. $client = new FixedDataTransformer(array(
  40. 'foo' => 'bar',
  41. ));
  42. $form = new Form('name', $this->dispatcher, array(), $client, $norm);
  43. $this->assertNull($form->getData());
  44. $this->assertSame('foo', $form->getNormData());
  45. $this->assertSame('bar', $form->getClientData());
  46. }
  47. public function testErrorsBubbleUpIfEnabled()
  48. {
  49. $error = new FormError('Error!');
  50. $parent = $this->form;
  51. $form = $this->getBuilder()->setErrorBubbling(true)->getForm();
  52. $form->setParent($parent);
  53. $form->addError($error);
  54. $this->assertEquals(array(), $form->getErrors());
  55. $this->assertEquals(array($error), $parent->getErrors());
  56. }
  57. public function testErrorsDontBubbleUpIfDisabled()
  58. {
  59. $error = new FormError('Error!');
  60. $parent = $this->form;
  61. $form = $this->getBuilder()->setErrorBubbling(false)->getForm();
  62. $form->setParent($parent);
  63. $form->addError($error);
  64. $this->assertEquals(array($error), $form->getErrors());
  65. $this->assertEquals(array(), $parent->getErrors());
  66. }
  67. public function testValidIfAllChildrenAreValid()
  68. {
  69. $this->form->add($this->getValidForm('firstName'));
  70. $this->form->add($this->getValidForm('lastName'));
  71. $this->form->bind(array(
  72. 'firstName' => 'Bernhard',
  73. 'lastName' => 'Schussek',
  74. ));
  75. $this->assertTrue($this->form->isValid());
  76. }
  77. public function testInvalidIfChildrenIsInvalid()
  78. {
  79. $this->form->add($this->getValidForm('firstName'));
  80. $this->form->add($this->getInvalidForm('lastName'));
  81. $this->form->bind(array(
  82. 'firstName' => 'Bernhard',
  83. 'lastName' => 'Schussek',
  84. ));
  85. $this->assertFalse($this->form->isValid());
  86. }
  87. public function testBind()
  88. {
  89. $child = $this->getMockForm('firstName');
  90. $this->form->add($child);
  91. $child->expects($this->once())
  92. ->method('bind')
  93. ->with($this->equalTo('Bernhard'));
  94. $this->form->bind(array('firstName' => 'Bernhard'));
  95. $this->assertEquals(array('firstName' => 'Bernhard'), $this->form->getData());
  96. }
  97. public function testBindForwardsNullIfValueIsMissing()
  98. {
  99. $child = $this->getMockForm('firstName');
  100. $this->form->add($child);
  101. $child->expects($this->once())
  102. ->method('bind')
  103. ->with($this->equalTo(null));
  104. $this->form->bind(array());
  105. }
  106. public function testNeverRequiredIfParentNotRequired()
  107. {
  108. $parent = $this->getBuilder()->setRequired(false)->getForm();
  109. $child = $this->getBuilder()->setRequired(true)->getForm();
  110. $child->setParent($parent);
  111. $this->assertFalse($child->isRequired());
  112. }
  113. public function testRequired()
  114. {
  115. $parent = $this->getBuilder()->setRequired(true)->getForm();
  116. $child = $this->getBuilder()->setRequired(true)->getForm();
  117. $child->setParent($parent);
  118. $this->assertTrue($child->isRequired());
  119. }
  120. public function testNotRequired()
  121. {
  122. $parent = $this->getBuilder()->setRequired(true)->getForm();
  123. $child = $this->getBuilder()->setRequired(false)->getForm();
  124. $child->setParent($parent);
  125. $this->assertFalse($child->isRequired());
  126. }
  127. public function testAlwaysReadOnlyIfParentReadOnly()
  128. {
  129. $parent = $this->getBuilder()->setReadOnly(true)->getForm();
  130. $child = $this->getBuilder()->setReadOnly(false)->getForm();
  131. $child->setParent($parent);
  132. $this->assertTrue($child->isReadOnly());
  133. }
  134. public function testReadOnly()
  135. {
  136. $parent = $this->getBuilder()->setReadOnly(false)->getForm();
  137. $child = $this->getBuilder()->setReadOnly(true)->getForm();
  138. $child->setParent($parent);
  139. $this->assertTrue($child->isReadOnly());
  140. }
  141. public function testNotReadOnly()
  142. {
  143. $parent = $this->getBuilder()->setReadOnly(false)->getForm();
  144. $child = $this->getBuilder()->setReadOnly(false)->getForm();
  145. $child->setParent($parent);
  146. $this->assertFalse($child->isReadOnly());
  147. }
  148. public function testCloneChildren()
  149. {
  150. $child = $this->getBuilder('child')->getForm();
  151. $this->form->add($child);
  152. $clone = clone $this->form;
  153. $this->assertNotSame($this->form, $clone);
  154. $this->assertNotSame($child, $clone['child']);
  155. }
  156. public function testGetRootReturnsRootOfParent()
  157. {
  158. $parent = $this->getMockForm();
  159. $parent->expects($this->once())
  160. ->method('getRoot')
  161. ->will($this->returnValue('ROOT'));
  162. $this->form->setParent($parent);
  163. $this->assertEquals('ROOT', $this->form->getRoot());
  164. }
  165. public function testGetRootReturnsSelfIfNoParent()
  166. {
  167. $this->assertSame($this->form, $this->form->getRoot());
  168. }
  169. public function testIsEmptyIfEmptyArray()
  170. {
  171. $this->form->setData(array());
  172. $this->assertTrue($this->form->isEmpty());
  173. }
  174. public function testIsEmptyIfNull()
  175. {
  176. $this->form->setData(null);
  177. $this->assertTrue($this->form->isEmpty());
  178. }
  179. public function testIsEmptyIfEmptyString()
  180. {
  181. $this->form->setData('');
  182. $this->assertTrue($this->form->isEmpty());
  183. }
  184. public function testIsNotEmptyIfText()
  185. {
  186. $this->form->setData('foobar');
  187. $this->assertFalse($this->form->isEmpty());
  188. }
  189. public function testIsNotEmptyIfChildNotEmpty()
  190. {
  191. $child = $this->getMockForm();
  192. $child->expects($this->once())
  193. ->method('isEmpty')
  194. ->will($this->returnValue(false));
  195. $this->form->setData(null);
  196. $this->form->add($child);
  197. $this->assertFalse($this->form->isEmpty());
  198. }
  199. public function testValidIfBound()
  200. {
  201. $this->form->bind('foobar');
  202. $this->assertTrue($this->form->isValid());
  203. }
  204. public function testNotValidIfNotBound()
  205. {
  206. $this->assertFalse($this->form->isValid());
  207. }
  208. public function testNotValidIfErrors()
  209. {
  210. $this->form->bind('foobar');
  211. $this->form->addError(new FormError('Error!'));
  212. $this->assertFalse($this->form->isValid());
  213. }
  214. public function testNotValidIfChildNotValid()
  215. {
  216. $child = $this->getMockForm();
  217. $child->expects($this->once())
  218. ->method('isValid')
  219. ->will($this->returnValue(false));
  220. $this->form->bind('foobar');
  221. $this->form->add($child);
  222. $this->assertFalse($this->form->isValid());
  223. }
  224. public function testHasErrors()
  225. {
  226. $this->form->addError(new FormError('Error!'));
  227. $this->assertTrue($this->form->hasErrors());
  228. }
  229. public function testHasNoErrors()
  230. {
  231. $this->assertFalse($this->form->hasErrors());
  232. }
  233. public function testHasChildren()
  234. {
  235. $this->form->add($this->getBuilder()->getForm());
  236. $this->assertTrue($this->form->hasChildren());
  237. }
  238. public function testHasNoChildren()
  239. {
  240. $this->assertFalse($this->form->hasChildren());
  241. }
  242. public function testAdd()
  243. {
  244. $child = $this->getBuilder('foo')->getForm();
  245. $this->form->add($child);
  246. $this->assertSame($this->form, $child->getParent());
  247. $this->assertSame(array('foo' => $child), $this->form->getChildren());
  248. }
  249. public function testRemove()
  250. {
  251. $child = $this->getBuilder('foo')->getForm();
  252. $this->form->add($child);
  253. $this->form->remove('foo');
  254. $this->assertNull($child->getParent());
  255. $this->assertFalse($this->form->hasChildren());
  256. }
  257. public function testRemoveIgnoresUnknownName()
  258. {
  259. $this->form->remove('notexisting');
  260. }
  261. public function testArrayAccess()
  262. {
  263. $child = $this->getBuilder('foo')->getForm();
  264. $this->form[] = $child;
  265. $this->assertTrue(isset($this->form['foo']));
  266. $this->assertSame($child, $this->form['foo']);
  267. unset($this->form['foo']);
  268. $this->assertFalse(isset($this->form['foo']));
  269. }
  270. public function testCountable()
  271. {
  272. $this->form->add($this->getBuilder('foo')->getForm());
  273. $this->form->add($this->getBuilder('bar')->getForm());
  274. $this->assertEquals(2, count($this->form));
  275. }
  276. public function testIterator()
  277. {
  278. $this->form->add($this->getBuilder('foo')->getForm());
  279. $this->form->add($this->getBuilder('bar')->getForm());
  280. $this->assertSame($this->form->getChildren(), iterator_to_array($this->form));
  281. }
  282. public function testIsBound()
  283. {
  284. $this->form->bind('foobar');
  285. $this->assertTrue($this->form->isBound());
  286. }
  287. public function testIsNotBound()
  288. {
  289. $this->assertFalse($this->form->isBound());
  290. }
  291. protected function getBuilder($name = 'name')
  292. {
  293. return new FormBuilder($name, $this->dispatcher);
  294. }
  295. protected function getMockForm($name = 'name')
  296. {
  297. $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
  298. $form->expects($this->any())
  299. ->method('getName')
  300. ->will($this->returnValue($name));
  301. return $form;
  302. }
  303. protected function getValidForm($name)
  304. {
  305. $form = $this->getMockForm($name);
  306. $form->expects($this->any())
  307. ->method('isValid')
  308. ->will($this->returnValue(true));
  309. return $form;
  310. }
  311. protected function getInvalidForm($name)
  312. {
  313. $form = $this->getMockForm($name);
  314. $form->expects($this->any())
  315. ->method('isValid')
  316. ->will($this->returnValue(false));
  317. return $form;
  318. }
  319. }