FormTest.php 20 KB

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