FormTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. require_once __DIR__.'/Fixtures/FixedFilterListener.php';
  13. use Symfony\Component\Form\Form;
  14. use Symfony\Component\Form\FormBuilder;
  15. use Symfony\Component\Form\FormError;
  16. use Symfony\Component\Form\DataTransformer\TransformationFailedException;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\EventDispatcher\EventDispatcher;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Tests\Component\Form\Fixtures\FixedDataTransformer;
  22. use Symfony\Tests\Component\Form\Fixtures\FixedFilterListener;
  23. class FormTest extends \PHPUnit_Framework_TestCase
  24. {
  25. private $dispatcher;
  26. private $builder;
  27. private $form;
  28. protected function setUp()
  29. {
  30. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  31. $this->form = $this->getBuilder()->getForm();
  32. }
  33. /**
  34. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  35. */
  36. public function testConstructExpectsValidValidators()
  37. {
  38. $validators = array(new \stdClass());
  39. new Form('name', $this->dispatcher, array(), null, null, null, $validators);
  40. }
  41. public function testDataIsInitializedEmpty()
  42. {
  43. $norm = new FixedDataTransformer(array(
  44. '' => 'foo',
  45. ));
  46. $client = new FixedDataTransformer(array(
  47. 'foo' => 'bar',
  48. ));
  49. $form = new Form('name', $this->dispatcher, array(), $client, $norm);
  50. $this->assertNull($form->getData());
  51. $this->assertSame('foo', $form->getNormData());
  52. $this->assertSame('bar', $form->getClientData());
  53. }
  54. public function testErrorsBubbleUpIfEnabled()
  55. {
  56. $error = new FormError('Error!');
  57. $parent = $this->form;
  58. $form = $this->getBuilder()->setErrorBubbling(true)->getForm();
  59. $form->setParent($parent);
  60. $form->addError($error);
  61. $this->assertEquals(array(), $form->getErrors());
  62. $this->assertEquals(array($error), $parent->getErrors());
  63. }
  64. public function testErrorsDontBubbleUpIfDisabled()
  65. {
  66. $error = new FormError('Error!');
  67. $parent = $this->form;
  68. $form = $this->getBuilder()->setErrorBubbling(false)->getForm();
  69. $form->setParent($parent);
  70. $form->addError($error);
  71. $this->assertEquals(array($error), $form->getErrors());
  72. $this->assertEquals(array(), $parent->getErrors());
  73. }
  74. public function testValidIfAllChildrenAreValid()
  75. {
  76. $this->form->add($this->getValidForm('firstName'));
  77. $this->form->add($this->getValidForm('lastName'));
  78. $this->form->bind(array(
  79. 'firstName' => 'Bernhard',
  80. 'lastName' => 'Schussek',
  81. ));
  82. $this->assertTrue($this->form->isValid());
  83. }
  84. public function testInvalidIfChildrenIsInvalid()
  85. {
  86. $this->form->add($this->getValidForm('firstName'));
  87. $this->form->add($this->getInvalidForm('lastName'));
  88. $this->form->bind(array(
  89. 'firstName' => 'Bernhard',
  90. 'lastName' => 'Schussek',
  91. ));
  92. $this->assertFalse($this->form->isValid());
  93. }
  94. public function testBind()
  95. {
  96. $child = $this->getMockForm('firstName');
  97. $this->form->add($child);
  98. $child->expects($this->once())
  99. ->method('bind')
  100. ->with($this->equalTo('Bernhard'));
  101. $this->form->bind(array('firstName' => 'Bernhard'));
  102. $this->assertEquals(array('firstName' => 'Bernhard'), $this->form->getData());
  103. }
  104. public function testBindForwardsNullIfValueIsMissing()
  105. {
  106. $child = $this->getMockForm('firstName');
  107. $this->form->add($child);
  108. $child->expects($this->once())
  109. ->method('bind')
  110. ->with($this->equalTo(null));
  111. $this->form->bind(array());
  112. }
  113. public function testBindIsIgnoredIfReadOnly()
  114. {
  115. $form = $this->getBuilder()
  116. ->setReadOnly(true)
  117. ->setData('initial')
  118. ->getForm();
  119. $form->bind('new');
  120. $this->assertEquals('initial', $form->getData());
  121. }
  122. public function testNeverRequiredIfParentNotRequired()
  123. {
  124. $parent = $this->getBuilder()->setRequired(false)->getForm();
  125. $child = $this->getBuilder()->setRequired(true)->getForm();
  126. $child->setParent($parent);
  127. $this->assertFalse($child->isRequired());
  128. }
  129. public function testRequired()
  130. {
  131. $parent = $this->getBuilder()->setRequired(true)->getForm();
  132. $child = $this->getBuilder()->setRequired(true)->getForm();
  133. $child->setParent($parent);
  134. $this->assertTrue($child->isRequired());
  135. }
  136. public function testNotRequired()
  137. {
  138. $parent = $this->getBuilder()->setRequired(true)->getForm();
  139. $child = $this->getBuilder()->setRequired(false)->getForm();
  140. $child->setParent($parent);
  141. $this->assertFalse($child->isRequired());
  142. }
  143. public function testAlwaysReadOnlyIfParentReadOnly()
  144. {
  145. $parent = $this->getBuilder()->setReadOnly(true)->getForm();
  146. $child = $this->getBuilder()->setReadOnly(false)->getForm();
  147. $child->setParent($parent);
  148. $this->assertTrue($child->isReadOnly());
  149. }
  150. public function testReadOnly()
  151. {
  152. $parent = $this->getBuilder()->setReadOnly(false)->getForm();
  153. $child = $this->getBuilder()->setReadOnly(true)->getForm();
  154. $child->setParent($parent);
  155. $this->assertTrue($child->isReadOnly());
  156. }
  157. public function testNotReadOnly()
  158. {
  159. $parent = $this->getBuilder()->setReadOnly(false)->getForm();
  160. $child = $this->getBuilder()->setReadOnly(false)->getForm();
  161. $child->setParent($parent);
  162. $this->assertFalse($child->isReadOnly());
  163. }
  164. public function testCloneChildren()
  165. {
  166. $child = $this->getBuilder('child')->getForm();
  167. $this->form->add($child);
  168. $clone = clone $this->form;
  169. $this->assertNotSame($this->form, $clone);
  170. $this->assertNotSame($child, $clone['child']);
  171. }
  172. public function testGetRootReturnsRootOfParent()
  173. {
  174. $parent = $this->getMockForm();
  175. $parent->expects($this->once())
  176. ->method('getRoot')
  177. ->will($this->returnValue('ROOT'));
  178. $this->form->setParent($parent);
  179. $this->assertEquals('ROOT', $this->form->getRoot());
  180. }
  181. public function testGetRootReturnsSelfIfNoParent()
  182. {
  183. $this->assertSame($this->form, $this->form->getRoot());
  184. }
  185. public function testIsEmptyIfEmptyArray()
  186. {
  187. $this->form->setData(array());
  188. $this->assertTrue($this->form->isEmpty());
  189. }
  190. public function testIsEmptyIfNull()
  191. {
  192. $this->form->setData(null);
  193. $this->assertTrue($this->form->isEmpty());
  194. }
  195. public function testIsEmptyIfEmptyString()
  196. {
  197. $this->form->setData('');
  198. $this->assertTrue($this->form->isEmpty());
  199. }
  200. public function testIsNotEmptyIfText()
  201. {
  202. $this->form->setData('foobar');
  203. $this->assertFalse($this->form->isEmpty());
  204. }
  205. public function testIsNotEmptyIfChildNotEmpty()
  206. {
  207. $child = $this->getMockForm();
  208. $child->expects($this->once())
  209. ->method('isEmpty')
  210. ->will($this->returnValue(false));
  211. $this->form->setData(null);
  212. $this->form->add($child);
  213. $this->assertFalse($this->form->isEmpty());
  214. }
  215. public function testValidIfBound()
  216. {
  217. $this->form->bind('foobar');
  218. $this->assertTrue($this->form->isValid());
  219. }
  220. public function testNotValidIfNotBound()
  221. {
  222. $this->assertFalse($this->form->isValid());
  223. }
  224. public function testNotValidIfErrors()
  225. {
  226. $this->form->bind('foobar');
  227. $this->form->addError(new FormError('Error!'));
  228. $this->assertFalse($this->form->isValid());
  229. }
  230. public function testNotValidIfChildNotValid()
  231. {
  232. $child = $this->getMockForm();
  233. $child->expects($this->once())
  234. ->method('isValid')
  235. ->will($this->returnValue(false));
  236. $this->form->bind('foobar');
  237. $this->form->add($child);
  238. $this->assertFalse($this->form->isValid());
  239. }
  240. public function testHasErrors()
  241. {
  242. $this->form->addError(new FormError('Error!'));
  243. $this->assertTrue($this->form->hasErrors());
  244. }
  245. public function testHasNoErrors()
  246. {
  247. $this->assertFalse($this->form->hasErrors());
  248. }
  249. public function testHasChildren()
  250. {
  251. $this->form->add($this->getBuilder()->getForm());
  252. $this->assertTrue($this->form->hasChildren());
  253. }
  254. public function testHasNoChildren()
  255. {
  256. $this->assertFalse($this->form->hasChildren());
  257. }
  258. public function testAdd()
  259. {
  260. $child = $this->getBuilder('foo')->getForm();
  261. $this->form->add($child);
  262. $this->assertSame($this->form, $child->getParent());
  263. $this->assertSame(array('foo' => $child), $this->form->getChildren());
  264. }
  265. public function testRemove()
  266. {
  267. $child = $this->getBuilder('foo')->getForm();
  268. $this->form->add($child);
  269. $this->form->remove('foo');
  270. $this->assertNull($child->getParent());
  271. $this->assertFalse($this->form->hasChildren());
  272. }
  273. public function testRemoveIgnoresUnknownName()
  274. {
  275. $this->form->remove('notexisting');
  276. }
  277. public function testArrayAccess()
  278. {
  279. $child = $this->getBuilder('foo')->getForm();
  280. $this->form[] = $child;
  281. $this->assertTrue(isset($this->form['foo']));
  282. $this->assertSame($child, $this->form['foo']);
  283. unset($this->form['foo']);
  284. $this->assertFalse(isset($this->form['foo']));
  285. }
  286. public function testCountable()
  287. {
  288. $this->form->add($this->getBuilder('foo')->getForm());
  289. $this->form->add($this->getBuilder('bar')->getForm());
  290. $this->assertEquals(2, count($this->form));
  291. }
  292. public function testIterator()
  293. {
  294. $this->form->add($this->getBuilder('foo')->getForm());
  295. $this->form->add($this->getBuilder('bar')->getForm());
  296. $this->assertSame($this->form->getChildren(), iterator_to_array($this->form));
  297. }
  298. public function testIsBound()
  299. {
  300. $this->form->bind('foobar');
  301. $this->assertTrue($this->form->isBound());
  302. }
  303. public function testIsNotBound()
  304. {
  305. $this->assertFalse($this->form->isBound());
  306. }
  307. public function testAddMapsClientDataToForm()
  308. {
  309. $mapper = $this->getDataMapper();
  310. $form = $this->getBuilder()
  311. ->setDataMapper($mapper)
  312. ->setClientTransformer(new FixedDataTransformer(array(
  313. '' => '',
  314. 'foo' => 'bar',
  315. )))
  316. ->setData('foo')
  317. ->getForm();
  318. $child = $this->getBuilder()->getForm();
  319. $mapper->expects($this->once())
  320. ->method('mapDataToForm')
  321. ->with('bar', $child);
  322. $form->add($child);
  323. }
  324. public function testSetDataMapsClientDataToChildren()
  325. {
  326. $mapper = $this->getDataMapper();
  327. $child1 = $this->getBuilder('firstName')->getForm();
  328. $child2 = $this->getBuilder('lastName')->getForm();
  329. $form = $this->getBuilder()
  330. ->setDataMapper($mapper)
  331. ->setClientTransformer(new FixedDataTransformer(array(
  332. '' => '',
  333. 'foo' => 'bar',
  334. )))
  335. ->getForm();
  336. $form->add($child1);
  337. $form->add($child2);
  338. $mapper->expects($this->once())
  339. ->method('mapDataToForms')
  340. ->with('bar', array('firstName' => $child1, 'lastName' => $child2));
  341. $form->setData('foo');
  342. }
  343. public function testBindMapsBoundChildrenOntoExistingClientData()
  344. {
  345. $test = $this;
  346. $mapper = $this->getDataMapper();
  347. $child1 = $this->getBuilder('firstName')->getForm();
  348. $child2 = $this->getBuilder('lastName')->getForm();
  349. $form = $this->getBuilder()
  350. ->setDataMapper($mapper)
  351. ->setClientTransformer(new FixedDataTransformer(array(
  352. '' => '',
  353. 'foo' => 'bar',
  354. )))
  355. ->setData('foo')
  356. ->getForm();
  357. $form->add($child1);
  358. $form->add($child2);
  359. $mapper->expects($this->once())
  360. ->method('mapFormsToData')
  361. ->with(array('firstName' => $child1, 'lastName' => $child2), 'bar')
  362. ->will($this->returnCallback(function ($children, $bar) use ($test) {
  363. $test->assertEquals('Bernhard', $children['firstName']->getData());
  364. $test->assertEquals('Schussek', $children['lastName']->getData());
  365. }));
  366. $form->bind(array(
  367. 'firstName' => 'Bernhard',
  368. 'lastName' => 'Schussek',
  369. ));
  370. }
  371. public function testSetDataExecutesTransformationChain()
  372. {
  373. // use real event dispatcher now
  374. $form = $this->getBuilder('name', new EventDispatcher())
  375. ->addEventSubscriber(new FixedFilterListener(array(
  376. 'filterSetData' => array(
  377. 'app' => 'filtered',
  378. ),
  379. )))
  380. ->setNormTransformer(new FixedDataTransformer(array(
  381. '' => '',
  382. 'filtered' => 'norm',
  383. )))
  384. ->setClientTransformer(new FixedDataTransformer(array(
  385. '' => '',
  386. 'norm' => 'client',
  387. )))
  388. ->getForm();
  389. $form->setData('app');
  390. $this->assertEquals('filtered', $form->getData());
  391. $this->assertEquals('norm', $form->getNormData());
  392. $this->assertEquals('client', $form->getClientData());
  393. }
  394. public function testBindExecutesTransformationChain()
  395. {
  396. // use real event dispatcher now
  397. $form = $this->getBuilder('name', new EventDispatcher())
  398. ->addEventSubscriber(new FixedFilterListener(array(
  399. 'filterBoundClientData' => array(
  400. 'client' => 'filteredclient',
  401. ),
  402. 'filterBoundNormData' => array(
  403. 'norm' => 'filterednorm',
  404. ),
  405. )))
  406. ->setClientTransformer(new FixedDataTransformer(array(
  407. '' => '',
  408. // direction is reversed!
  409. 'norm' => 'filteredclient',
  410. 'filterednorm' => 'cleanedclient'
  411. )))
  412. ->setNormTransformer(new FixedDataTransformer(array(
  413. '' => '',
  414. // direction is reversed!
  415. 'app' => 'filterednorm',
  416. )))
  417. ->getForm();
  418. $form->setData('app');
  419. $this->assertEquals('app', $form->getData());
  420. $this->assertEquals('filterednorm', $form->getNormData());
  421. $this->assertEquals('cleanedclient', $form->getClientData());
  422. }
  423. public function testIsSynchronizedByDefault()
  424. {
  425. $this->assertTrue($this->form->isSynchronized());
  426. }
  427. public function testIsSynchronizedAfterBinding()
  428. {
  429. $this->form->bind('foobar');
  430. $this->assertTrue($this->form->isSynchronized());
  431. }
  432. public function testIsNotSynchronizedIfTransformationFailed()
  433. {
  434. $transformer = $this->getDataTransformer();
  435. $transformer->expects($this->once())
  436. ->method('reverseTransform')
  437. ->will($this->throwException(new TransformationFailedException()));
  438. $form = $this->getBuilder()
  439. ->setClientTransformer($transformer)
  440. ->getForm();
  441. $form->bind('foobar');
  442. $this->assertFalse($form->isSynchronized());
  443. }
  444. public function testBindValidatesAfterTransformation()
  445. {
  446. $test = $this;
  447. $validator = $this->getFormValidator();
  448. $form = $this->getBuilder()
  449. ->addValidator($validator)
  450. ->getForm();
  451. $validator->expects($this->once())
  452. ->method('validate')
  453. ->with($form)
  454. ->will($this->returnCallback(function ($form) use ($test) {
  455. $test->assertEquals('foobar', $form->getData());
  456. }));
  457. $form->bind('foobar');
  458. }
  459. public function requestMethodProvider()
  460. {
  461. return array(
  462. array('POST'),
  463. array('PUT'),
  464. );
  465. }
  466. /**
  467. * @dataProvider requestMethodProvider
  468. */
  469. public function testBindPostOrPutRequest($method)
  470. {
  471. $path = tempnam(sys_get_temp_dir(), 'sf2');
  472. touch($path);
  473. $values = array(
  474. 'author' => array(
  475. 'name' => 'Bernhard',
  476. 'image' => array('filename' => 'foobar.png'),
  477. ),
  478. );
  479. $files = array(
  480. 'author' => array(
  481. 'error' => array('image' => UPLOAD_ERR_OK),
  482. 'name' => array('image' => 'upload.png'),
  483. 'size' => array('image' => 123),
  484. 'tmp_name' => array('image' => $path),
  485. 'type' => array('image' => 'image/png'),
  486. ),
  487. );
  488. $request = new Request(array(), $values, array(), array(), $files, array(
  489. 'REQUEST_METHOD' => $method,
  490. ));
  491. $form = $this->getBuilder('author')->getForm();
  492. $form->add($this->getBuilder('name')->getForm());
  493. $form->add($this->getBuilder('image')->getForm());
  494. $form->bindRequest($request);
  495. $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  496. $this->assertEquals('Bernhard', $form['name']->getData());
  497. $this->assertEquals($file, $form['image']->getData());
  498. unlink($path);
  499. }
  500. public function testBindGetRequest()
  501. {
  502. $values = array(
  503. 'author' => array(
  504. 'firstName' => 'Bernhard',
  505. 'lastName' => 'Schussek',
  506. ),
  507. );
  508. $request = new Request($values, array(), array(), array(), array(), array(
  509. 'REQUEST_METHOD' => 'GET',
  510. ));
  511. $form = $this->getBuilder('author')->getForm();
  512. $form->add($this->getBuilder('firstName')->getForm());
  513. $form->add($this->getBuilder('lastName')->getForm());
  514. $form->bindRequest($request);
  515. $this->assertEquals('Bernhard', $form['firstName']->getData());
  516. $this->assertEquals('Schussek', $form['lastName']->getData());
  517. }
  518. protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
  519. {
  520. return new FormBuilder($name, $dispatcher ?: $this->dispatcher);
  521. }
  522. protected function getMockForm($name = 'name')
  523. {
  524. $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
  525. $form->expects($this->any())
  526. ->method('getName')
  527. ->will($this->returnValue($name));
  528. return $form;
  529. }
  530. protected function getValidForm($name)
  531. {
  532. $form = $this->getMockForm($name);
  533. $form->expects($this->any())
  534. ->method('isValid')
  535. ->will($this->returnValue(true));
  536. return $form;
  537. }
  538. protected function getInvalidForm($name)
  539. {
  540. $form = $this->getMockForm($name);
  541. $form->expects($this->any())
  542. ->method('isValid')
  543. ->will($this->returnValue(false));
  544. return $form;
  545. }
  546. protected function getDataMapper()
  547. {
  548. return $this->getMock('Symfony\Component\Form\DataMapper\DataMapperInterface');
  549. }
  550. protected function getDataTransformer()
  551. {
  552. return $this->getMock('Symfony\Component\Form\DataTransformer\DataTransformerInterface');
  553. }
  554. protected function getFormValidator()
  555. {
  556. return $this->getMock('Symfony\Component\Form\Validator\FormValidatorInterface');
  557. }
  558. }