FormTest.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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\FormView;
  15. use Symfony\Component\Form\FormBuilder;
  16. use Symfony\Component\Form\FormError;
  17. use Symfony\Component\Form\Exception\TransformationFailedException;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\File\UploadedFile;
  20. use Symfony\Component\EventDispatcher\EventDispatcher;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Tests\Component\Form\Fixtures\FixedDataTransformer;
  23. use Symfony\Tests\Component\Form\Fixtures\FixedFilterListener;
  24. class FormTest extends \PHPUnit_Framework_TestCase
  25. {
  26. private $dispatcher;
  27. private $factory;
  28. private $builder;
  29. private $form;
  30. protected function setUp()
  31. {
  32. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  33. $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  34. $this->form = $this->getBuilder()->getForm();
  35. }
  36. /**
  37. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  38. */
  39. public function testConstructExpectsValidValidators()
  40. {
  41. $validators = array(new \stdClass());
  42. new Form('name', $this->dispatcher, array(), array(), array(), null, $validators);
  43. }
  44. public function testDataIsInitializedEmpty()
  45. {
  46. $norm = new FixedDataTransformer(array(
  47. '' => 'foo',
  48. ));
  49. $client = new FixedDataTransformer(array(
  50. 'foo' => 'bar',
  51. ));
  52. $form = new Form('name', $this->dispatcher, array(), array($client), array($norm));
  53. $this->assertNull($form->getData());
  54. $this->assertSame('foo', $form->getNormData());
  55. $this->assertSame('bar', $form->getClientData());
  56. }
  57. public function testErrorsBubbleUpIfEnabled()
  58. {
  59. $error = new FormError('Error!');
  60. $parent = $this->form;
  61. $form = $this->getBuilder()->setErrorBubbling(true)->getForm();
  62. $form->setParent($parent);
  63. $form->addError($error);
  64. $this->assertEquals(array(), $form->getErrors());
  65. $this->assertEquals(array($error), $parent->getErrors());
  66. }
  67. public function testErrorsDontBubbleUpIfDisabled()
  68. {
  69. $error = new FormError('Error!');
  70. $parent = $this->form;
  71. $form = $this->getBuilder()->setErrorBubbling(false)->getForm();
  72. $form->setParent($parent);
  73. $form->addError($error);
  74. $this->assertEquals(array($error), $form->getErrors());
  75. $this->assertEquals(array(), $parent->getErrors());
  76. }
  77. public function testValidIfAllChildrenAreValid()
  78. {
  79. $this->form->add($this->getValidForm('firstName'));
  80. $this->form->add($this->getValidForm('lastName'));
  81. $this->form->bind(array(
  82. 'firstName' => 'Bernhard',
  83. 'lastName' => 'Schussek',
  84. ));
  85. $this->assertTrue($this->form->isValid());
  86. }
  87. public function testInvalidIfChildrenIsInvalid()
  88. {
  89. $this->form->add($this->getValidForm('firstName'));
  90. $this->form->add($this->getInvalidForm('lastName'));
  91. $this->form->bind(array(
  92. 'firstName' => 'Bernhard',
  93. 'lastName' => 'Schussek',
  94. ));
  95. $this->assertFalse($this->form->isValid());
  96. }
  97. public function testBind()
  98. {
  99. $child = $this->getMockForm('firstName');
  100. $this->form->add($child);
  101. $child->expects($this->once())
  102. ->method('bind')
  103. ->with($this->equalTo('Bernhard'));
  104. $this->form->bind(array('firstName' => 'Bernhard'));
  105. $this->assertEquals(array('firstName' => 'Bernhard'), $this->form->getData());
  106. }
  107. public function testBindForwardsNullIfValueIsMissing()
  108. {
  109. $child = $this->getMockForm('firstName');
  110. $this->form->add($child);
  111. $child->expects($this->once())
  112. ->method('bind')
  113. ->with($this->equalTo(null));
  114. $this->form->bind(array());
  115. }
  116. public function testBindIsIgnoredIfReadOnly()
  117. {
  118. $form = $this->getBuilder()
  119. ->setReadOnly(true)
  120. ->setData('initial')
  121. ->getForm();
  122. $form->bind('new');
  123. $this->assertEquals('initial', $form->getData());
  124. $this->assertTrue($form->isBound());
  125. }
  126. public function testNeverRequiredIfParentNotRequired()
  127. {
  128. $parent = $this->getBuilder()->setRequired(false)->getForm();
  129. $child = $this->getBuilder()->setRequired(true)->getForm();
  130. $child->setParent($parent);
  131. $this->assertFalse($child->isRequired());
  132. }
  133. public function testRequired()
  134. {
  135. $parent = $this->getBuilder()->setRequired(true)->getForm();
  136. $child = $this->getBuilder()->setRequired(true)->getForm();
  137. $child->setParent($parent);
  138. $this->assertTrue($child->isRequired());
  139. }
  140. public function testNotRequired()
  141. {
  142. $parent = $this->getBuilder()->setRequired(true)->getForm();
  143. $child = $this->getBuilder()->setRequired(false)->getForm();
  144. $child->setParent($parent);
  145. $this->assertFalse($child->isRequired());
  146. }
  147. public function testAlwaysReadOnlyIfParentReadOnly()
  148. {
  149. $parent = $this->getBuilder()->setReadOnly(true)->getForm();
  150. $child = $this->getBuilder()->setReadOnly(false)->getForm();
  151. $child->setParent($parent);
  152. $this->assertTrue($child->isReadOnly());
  153. }
  154. public function testReadOnly()
  155. {
  156. $parent = $this->getBuilder()->setReadOnly(false)->getForm();
  157. $child = $this->getBuilder()->setReadOnly(true)->getForm();
  158. $child->setParent($parent);
  159. $this->assertTrue($child->isReadOnly());
  160. }
  161. public function testNotReadOnly()
  162. {
  163. $parent = $this->getBuilder()->setReadOnly(false)->getForm();
  164. $child = $this->getBuilder()->setReadOnly(false)->getForm();
  165. $child->setParent($parent);
  166. $this->assertFalse($child->isReadOnly());
  167. }
  168. public function testCloneChildren()
  169. {
  170. $child = $this->getBuilder('child')->getForm();
  171. $this->form->add($child);
  172. $clone = clone $this->form;
  173. $this->assertNotSame($this->form, $clone);
  174. $this->assertNotSame($child, $clone['child']);
  175. }
  176. public function testGetRootReturnsRootOfParent()
  177. {
  178. $parent = $this->getMockForm();
  179. $parent->expects($this->once())
  180. ->method('getRoot')
  181. ->will($this->returnValue('ROOT'));
  182. $this->form->setParent($parent);
  183. $this->assertEquals('ROOT', $this->form->getRoot());
  184. }
  185. public function testGetRootReturnsSelfIfNoParent()
  186. {
  187. $this->assertSame($this->form, $this->form->getRoot());
  188. }
  189. public function testEmptyIfEmptyArray()
  190. {
  191. $this->form->setData(array());
  192. $this->assertTrue($this->form->isEmpty());
  193. }
  194. public function testEmptyIfNull()
  195. {
  196. $this->form->setData(null);
  197. $this->assertTrue($this->form->isEmpty());
  198. }
  199. public function testEmptyIfEmptyString()
  200. {
  201. $this->form->setData('');
  202. $this->assertTrue($this->form->isEmpty());
  203. }
  204. public function testNotEmptyIfText()
  205. {
  206. $this->form->setData('foobar');
  207. $this->assertFalse($this->form->isEmpty());
  208. }
  209. public function testNotEmptyIfChildNotEmpty()
  210. {
  211. $child = $this->getMockForm();
  212. $child->expects($this->once())
  213. ->method('isEmpty')
  214. ->will($this->returnValue(false));
  215. $this->form->setData(null);
  216. $this->form->add($child);
  217. $this->assertFalse($this->form->isEmpty());
  218. }
  219. public function testValidIfBound()
  220. {
  221. $this->form->bind('foobar');
  222. $this->assertTrue($this->form->isValid());
  223. }
  224. public function testValidIfBoundAndReadOnly()
  225. {
  226. $form = $this->getBuilder()->setReadOnly(true)->getForm();
  227. $form->bind('foobar');
  228. $this->assertTrue($form->isValid());
  229. }
  230. public function testValidIfBoundAndReadOnlyWithChildren()
  231. {
  232. $this->factory->expects($this->once())
  233. ->method('createNamedBuilder')
  234. ->with('text', 'name', null, array())
  235. ->will($this->returnValue($this->getBuilder('name')));
  236. $form = $this->getBuilder('person')
  237. ->setReadOnly(true)
  238. ->add('name', 'text')
  239. ->getForm();
  240. $form->bind(array('name' => 'Jacques Doe'));
  241. $this->assertTrue($form->isValid());
  242. }
  243. public function testNotValidIfNotBound()
  244. {
  245. $this->assertFalse($this->form->isValid());
  246. }
  247. public function testNotValidIfErrors()
  248. {
  249. $this->form->bind('foobar');
  250. $this->form->addError(new FormError('Error!'));
  251. $this->assertFalse($this->form->isValid());
  252. }
  253. public function testNotValidIfChildNotValid()
  254. {
  255. $child = $this->getMockForm();
  256. $child->expects($this->once())
  257. ->method('isValid')
  258. ->will($this->returnValue(false));
  259. $this->form->bind('foobar');
  260. $this->form->add($child);
  261. $this->assertFalse($this->form->isValid());
  262. }
  263. public function testHasErrors()
  264. {
  265. $this->form->addError(new FormError('Error!'));
  266. $this->assertTrue($this->form->hasErrors());
  267. }
  268. public function testHasNoErrors()
  269. {
  270. $this->assertFalse($this->form->hasErrors());
  271. }
  272. public function testHasChildren()
  273. {
  274. $this->form->add($this->getBuilder()->getForm());
  275. $this->assertTrue($this->form->hasChildren());
  276. }
  277. public function testHasNoChildren()
  278. {
  279. $this->assertFalse($this->form->hasChildren());
  280. }
  281. public function testAdd()
  282. {
  283. $child = $this->getBuilder('foo')->getForm();
  284. $this->form->add($child);
  285. $this->assertSame($this->form, $child->getParent());
  286. $this->assertSame(array('foo' => $child), $this->form->getChildren());
  287. }
  288. public function testRemove()
  289. {
  290. $child = $this->getBuilder('foo')->getForm();
  291. $this->form->add($child);
  292. $this->form->remove('foo');
  293. $this->assertNull($child->getParent());
  294. $this->assertFalse($this->form->hasChildren());
  295. }
  296. public function testRemoveIgnoresUnknownName()
  297. {
  298. $this->form->remove('notexisting');
  299. }
  300. public function testArrayAccess()
  301. {
  302. $child = $this->getBuilder('foo')->getForm();
  303. $this->form[] = $child;
  304. $this->assertTrue(isset($this->form['foo']));
  305. $this->assertSame($child, $this->form['foo']);
  306. unset($this->form['foo']);
  307. $this->assertFalse(isset($this->form['foo']));
  308. }
  309. public function testCountable()
  310. {
  311. $this->form->add($this->getBuilder('foo')->getForm());
  312. $this->form->add($this->getBuilder('bar')->getForm());
  313. $this->assertEquals(2, count($this->form));
  314. }
  315. public function testIterator()
  316. {
  317. $this->form->add($this->getBuilder('foo')->getForm());
  318. $this->form->add($this->getBuilder('bar')->getForm());
  319. $this->assertSame($this->form->getChildren(), iterator_to_array($this->form));
  320. }
  321. public function testBound()
  322. {
  323. $this->form->bind('foobar');
  324. $this->assertTrue($this->form->isBound());
  325. }
  326. public function testNotBound()
  327. {
  328. $this->assertFalse($this->form->isBound());
  329. }
  330. public function testSetDataExecutesTransformationChain()
  331. {
  332. // use real event dispatcher now
  333. $form = $this->getBuilder('name', new EventDispatcher())
  334. ->addEventSubscriber(new FixedFilterListener(array(
  335. 'onSetData' => array(
  336. 'app' => 'filtered',
  337. ),
  338. )))
  339. ->appendNormTransformer(new FixedDataTransformer(array(
  340. '' => '',
  341. 'filtered' => 'norm',
  342. )))
  343. ->appendClientTransformer(new FixedDataTransformer(array(
  344. '' => '',
  345. 'norm' => 'client',
  346. )))
  347. ->getForm();
  348. $form->setData('app');
  349. $this->assertEquals('filtered', $form->getData());
  350. $this->assertEquals('norm', $form->getNormData());
  351. $this->assertEquals('client', $form->getClientData());
  352. }
  353. public function testSetDataExecutesClientTransformersInOrder()
  354. {
  355. $form = $this->getBuilder()
  356. ->appendClientTransformer(new FixedDataTransformer(array(
  357. '' => '',
  358. 'first' => 'second',
  359. )))
  360. ->appendClientTransformer(new FixedDataTransformer(array(
  361. '' => '',
  362. 'second' => 'third',
  363. )))
  364. ->getForm();
  365. $form->setData('first');
  366. $this->assertEquals('third', $form->getClientData());
  367. }
  368. public function testSetDataExecutesNormTransformersInOrder()
  369. {
  370. $form = $this->getBuilder()
  371. ->appendNormTransformer(new FixedDataTransformer(array(
  372. '' => '',
  373. 'first' => 'second',
  374. )))
  375. ->appendNormTransformer(new FixedDataTransformer(array(
  376. '' => '',
  377. 'second' => 'third',
  378. )))
  379. ->getForm();
  380. $form->setData('first');
  381. $this->assertEquals('third', $form->getNormData());
  382. }
  383. /*
  384. * When there is no data transformer, the data must have the same format
  385. * in all three representations
  386. */
  387. public function testSetDataConvertsScalarToStringIfNoTransformer()
  388. {
  389. $form = $this->getBuilder()->getForm();
  390. $form->setData(1);
  391. $this->assertSame('1', $form->getData());
  392. $this->assertSame('1', $form->getNormData());
  393. $this->assertSame('1', $form->getClientData());
  394. }
  395. /*
  396. * Data in client format should, if possible, always be a string to
  397. * facilitate differentiation between '0' and ''
  398. */
  399. public function testSetDataConvertsScalarToStringIfOnlyNormTransformer()
  400. {
  401. $form = $this->getBuilder()
  402. ->appendNormTransformer(new FixedDataTransformer(array(
  403. '' => '',
  404. 1 => 23,
  405. )))
  406. ->getForm();
  407. $form->setData(1);
  408. $this->assertSame(1, $form->getData());
  409. $this->assertSame(23, $form->getNormData());
  410. $this->assertSame('23', $form->getClientData());
  411. }
  412. /*
  413. * NULL remains NULL in app and norm format to remove the need to treat
  414. * empty values and NULL explicitely in the application
  415. */
  416. public function testSetDataConvertsNullToStringIfNoTransformer()
  417. {
  418. $form = $this->getBuilder()->getForm();
  419. $form->setData(null);
  420. $this->assertNull($form->getData());
  421. $this->assertNull($form->getNormData());
  422. $this->assertSame('', $form->getClientData());
  423. }
  424. public function testBindConvertsEmptyToNullIfNoTransformer()
  425. {
  426. $form = $this->getBuilder()->getForm();
  427. $form->bind('');
  428. $this->assertNull($form->getData());
  429. $this->assertNull($form->getNormData());
  430. $this->assertSame('', $form->getClientData());
  431. }
  432. public function testBindExecutesTransformationChain()
  433. {
  434. // use real event dispatcher now
  435. $form = $this->getBuilder('name', new EventDispatcher())
  436. ->addEventSubscriber(new FixedFilterListener(array(
  437. 'onBindClientData' => array(
  438. 'client' => 'filteredclient',
  439. ),
  440. 'onBindNormData' => array(
  441. 'norm' => 'filterednorm',
  442. ),
  443. )))
  444. ->appendClientTransformer(new FixedDataTransformer(array(
  445. '' => '',
  446. // direction is reversed!
  447. 'norm' => 'filteredclient',
  448. 'filterednorm' => 'cleanedclient'
  449. )))
  450. ->appendNormTransformer(new FixedDataTransformer(array(
  451. '' => '',
  452. // direction is reversed!
  453. 'app' => 'filterednorm',
  454. )))
  455. ->getForm();
  456. $form->setData('app');
  457. $this->assertEquals('app', $form->getData());
  458. $this->assertEquals('filterednorm', $form->getNormData());
  459. $this->assertEquals('cleanedclient', $form->getClientData());
  460. }
  461. public function testBindExecutesClientTransformersInReverseOrder()
  462. {
  463. $form = $this->getBuilder()
  464. ->appendClientTransformer(new FixedDataTransformer(array(
  465. '' => '',
  466. 'third' => 'second',
  467. )))
  468. ->appendClientTransformer(new FixedDataTransformer(array(
  469. '' => '',
  470. 'second' => 'first',
  471. )))
  472. ->getForm();
  473. $form->bind('first');
  474. $this->assertEquals('third', $form->getNormData());
  475. }
  476. public function testBindExecutesNormTransformersInReverseOrder()
  477. {
  478. $form = $this->getBuilder()
  479. ->appendNormTransformer(new FixedDataTransformer(array(
  480. '' => '',
  481. 'third' => 'second',
  482. )))
  483. ->appendNormTransformer(new FixedDataTransformer(array(
  484. '' => '',
  485. 'second' => 'first',
  486. )))
  487. ->getForm();
  488. $form->bind('first');
  489. $this->assertEquals('third', $form->getData());
  490. }
  491. public function testSynchronizedByDefault()
  492. {
  493. $this->assertTrue($this->form->isSynchronized());
  494. }
  495. public function testSynchronizedAfterBinding()
  496. {
  497. $this->form->bind('foobar');
  498. $this->assertTrue($this->form->isSynchronized());
  499. }
  500. public function testNotSynchronizedIfTransformationFailed()
  501. {
  502. $transformer = $this->getDataTransformer();
  503. $transformer->expects($this->once())
  504. ->method('reverseTransform')
  505. ->will($this->throwException(new TransformationFailedException()));
  506. $form = $this->getBuilder()
  507. ->appendClientTransformer($transformer)
  508. ->getForm();
  509. $form->bind('foobar');
  510. $this->assertFalse($form->isSynchronized());
  511. }
  512. public function testEmptyDataCreatedBeforeTransforming()
  513. {
  514. $form = $this->getBuilder()
  515. ->setEmptyData('foo')
  516. ->appendClientTransformer(new FixedDataTransformer(array(
  517. '' => '',
  518. // direction is reversed!
  519. 'bar' => 'foo',
  520. )))
  521. ->getForm();
  522. $form->bind('');
  523. $this->assertEquals('bar', $form->getData());
  524. }
  525. public function testEmptyDataFromClosure()
  526. {
  527. $test = $this;
  528. $form = $this->getBuilder()
  529. ->setEmptyData(function ($form) use ($test) {
  530. // the form instance is passed to the closure to allow use
  531. // of form data when creating the empty value
  532. $test->assertInstanceOf('Symfony\Component\Form\FormInterface', $form);
  533. return 'foo';
  534. })
  535. ->appendClientTransformer(new FixedDataTransformer(array(
  536. '' => '',
  537. // direction is reversed!
  538. 'bar' => 'foo',
  539. )))
  540. ->getForm();
  541. $form->bind('');
  542. $this->assertEquals('bar', $form->getData());
  543. }
  544. public function testAddMapsClientDataToForm()
  545. {
  546. $mapper = $this->getDataMapper();
  547. $form = $this->getBuilder()
  548. ->setDataMapper($mapper)
  549. ->appendClientTransformer(new FixedDataTransformer(array(
  550. '' => '',
  551. 'foo' => 'bar',
  552. )))
  553. ->setData('foo')
  554. ->getForm();
  555. $child = $this->getBuilder()->getForm();
  556. $mapper->expects($this->once())
  557. ->method('mapDataToForm')
  558. ->with('bar', $child);
  559. $form->add($child);
  560. }
  561. public function testSetDataMapsClientDataToChildren()
  562. {
  563. $mapper = $this->getDataMapper();
  564. $form = $this->getBuilder()
  565. ->setDataMapper($mapper)
  566. ->appendClientTransformer(new FixedDataTransformer(array(
  567. '' => '',
  568. 'foo' => 'bar',
  569. )))
  570. ->getForm();
  571. $form->add($child1 = $this->getBuilder('firstName')->getForm());
  572. $form->add($child2 = $this->getBuilder('lastName')->getForm());
  573. $mapper->expects($this->once())
  574. ->method('mapDataToForms')
  575. ->with('bar', array('firstName' => $child1, 'lastName' => $child2));
  576. $form->setData('foo');
  577. }
  578. public function testBindMapsBoundChildrenOntoExistingClientData()
  579. {
  580. $test = $this;
  581. $mapper = $this->getDataMapper();
  582. $form = $this->getBuilder()
  583. ->setDataMapper($mapper)
  584. ->appendClientTransformer(new FixedDataTransformer(array(
  585. '' => '',
  586. 'foo' => 'bar',
  587. )))
  588. ->setData('foo')
  589. ->getForm();
  590. $form->add($child1 = $this->getBuilder('firstName')->getForm());
  591. $form->add($child2 = $this->getBuilder('lastName')->getForm());
  592. $mapper->expects($this->once())
  593. ->method('mapFormsToData')
  594. ->with(array('firstName' => $child1, 'lastName' => $child2), 'bar')
  595. ->will($this->returnCallback(function ($children, $bar) use ($test) {
  596. $test->assertEquals('Bernhard', $children['firstName']->getData());
  597. $test->assertEquals('Schussek', $children['lastName']->getData());
  598. }));
  599. $form->bind(array(
  600. 'firstName' => 'Bernhard',
  601. 'lastName' => 'Schussek',
  602. ));
  603. }
  604. public function testBindMapsBoundChildrenOntoEmptyData()
  605. {
  606. $test = $this;
  607. $mapper = $this->getDataMapper();
  608. $object = new \stdClass();
  609. $form = $this->getBuilder()
  610. ->setDataMapper($mapper)
  611. ->setEmptyData($object)
  612. ->setData(null)
  613. ->getForm();
  614. $form->add($child = $this->getBuilder('name')->getForm());
  615. $mapper->expects($this->once())
  616. ->method('mapFormsToData')
  617. ->with(array('name' => $child), $object);
  618. $form->bind(array(
  619. 'name' => 'Bernhard',
  620. ));
  621. }
  622. public function testBindValidatesAfterTransformation()
  623. {
  624. $test = $this;
  625. $validator = $this->getFormValidator();
  626. $form = $this->getBuilder()
  627. ->addValidator($validator)
  628. ->getForm();
  629. $validator->expects($this->once())
  630. ->method('validate')
  631. ->with($form)
  632. ->will($this->returnCallback(function ($form) use ($test) {
  633. $test->assertEquals('foobar', $form->getData());
  634. }));
  635. $form->bind('foobar');
  636. }
  637. public function requestMethodProvider()
  638. {
  639. return array(
  640. array('POST'),
  641. array('PUT'),
  642. );
  643. }
  644. /**
  645. * @dataProvider requestMethodProvider
  646. */
  647. public function testBindPostOrPutRequest($method)
  648. {
  649. $path = tempnam(sys_get_temp_dir(), 'sf2');
  650. touch($path);
  651. $values = array(
  652. 'author' => array(
  653. 'name' => 'Bernhard',
  654. 'image' => array('filename' => 'foobar.png'),
  655. ),
  656. );
  657. $files = array(
  658. 'author' => array(
  659. 'error' => array('image' => UPLOAD_ERR_OK),
  660. 'name' => array('image' => 'upload.png'),
  661. 'size' => array('image' => 123),
  662. 'tmp_name' => array('image' => $path),
  663. 'type' => array('image' => 'image/png'),
  664. ),
  665. );
  666. $request = new Request(array(), $values, array(), array(), $files, array(
  667. 'REQUEST_METHOD' => $method,
  668. ));
  669. $form = $this->getBuilder('author')->getForm();
  670. $form->add($this->getBuilder('name')->getForm());
  671. $form->add($this->getBuilder('image')->getForm());
  672. $form->bindRequest($request);
  673. $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  674. $this->assertEquals('Bernhard', $form['name']->getData());
  675. $this->assertEquals($file, $form['image']->getData());
  676. unlink($path);
  677. }
  678. public function testBindGetRequest()
  679. {
  680. $values = array(
  681. 'author' => array(
  682. 'firstName' => 'Bernhard',
  683. 'lastName' => 'Schussek',
  684. ),
  685. );
  686. $request = new Request($values, array(), array(), array(), array(), array(
  687. 'REQUEST_METHOD' => 'GET',
  688. ));
  689. $form = $this->getBuilder('author')->getForm();
  690. $form->add($this->getBuilder('firstName')->getForm());
  691. $form->add($this->getBuilder('lastName')->getForm());
  692. $form->bindRequest($request);
  693. $this->assertEquals('Bernhard', $form['firstName']->getData());
  694. $this->assertEquals('Schussek', $form['lastName']->getData());
  695. }
  696. public function testBindResetsErrors()
  697. {
  698. $form = $this->getBuilder()->getForm();
  699. $form->addError(new FormError('Error!'));
  700. $form->bind('foobar');
  701. $this->assertSame(array(), $form->getErrors());
  702. }
  703. public function testCreateView()
  704. {
  705. $test = $this;
  706. $type1 = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  707. $type1Extension = $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
  708. $type1->expects($this->any())
  709. ->method('getExtensions')
  710. ->will($this->returnValue(array($type1Extension)));
  711. $type2 = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  712. $type2Extension = $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
  713. $type2->expects($this->any())
  714. ->method('getExtensions')
  715. ->will($this->returnValue(array($type2Extension)));
  716. $calls = array();
  717. $type1->expects($this->once())
  718. ->method('buildView')
  719. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  720. $calls[] = 'type1::buildView';
  721. $test->assertTrue($view->hasParent());
  722. $test->assertFalse($view->hasChildren());
  723. }));
  724. $type1Extension->expects($this->once())
  725. ->method('buildView')
  726. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  727. $calls[] = 'type1ext::buildView';
  728. $test->assertTrue($view->hasParent());
  729. $test->assertFalse($view->hasChildren());
  730. }));
  731. $type2->expects($this->once())
  732. ->method('buildView')
  733. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  734. $calls[] = 'type2::buildView';
  735. $test->assertTrue($view->hasParent());
  736. $test->assertFalse($view->hasChildren());
  737. }));
  738. $type2Extension->expects($this->once())
  739. ->method('buildView')
  740. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  741. $calls[] = 'type2ext::buildView';
  742. $test->assertTrue($view->hasParent());
  743. $test->assertFalse($view->hasChildren());
  744. }));
  745. $type1->expects($this->once())
  746. ->method('buildViewBottomUp')
  747. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  748. $calls[] = 'type1::buildViewBottomUp';
  749. $test->assertTrue($view->hasChildren());
  750. }));
  751. $type1Extension->expects($this->once())
  752. ->method('buildViewBottomUp')
  753. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  754. $calls[] = 'type1ext::buildViewBottomUp';
  755. $test->assertTrue($view->hasChildren());
  756. }));
  757. $type2->expects($this->once())
  758. ->method('buildViewBottomUp')
  759. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  760. $calls[] = 'type2::buildViewBottomUp';
  761. $test->assertTrue($view->hasChildren());
  762. }));
  763. $type2Extension->expects($this->once())
  764. ->method('buildViewBottomUp')
  765. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  766. $calls[] = 'type2ext::buildViewBottomUp';
  767. $test->assertTrue($view->hasChildren());
  768. }));
  769. $form = $this->getBuilder()->setTypes(array($type1, $type2))->getForm();
  770. $form->setParent($this->getBuilder()->getForm());
  771. $form->add($this->getBuilder()->getForm());
  772. $form->createView();
  773. $this->assertEquals(array(
  774. 0 => 'type1::buildView',
  775. 1 => 'type1ext::buildView',
  776. 2 => 'type2::buildView',
  777. 3 => 'type2ext::buildView',
  778. 4 => 'type1::buildViewBottomUp',
  779. 5 => 'type1ext::buildViewBottomUp',
  780. 6 => 'type2::buildViewBottomUp',
  781. 7 => 'type2ext::buildViewBottomUp',
  782. ), $calls);
  783. }
  784. public function testCreateViewAcceptsParent()
  785. {
  786. $parent = new FormView();
  787. $form = $this->getBuilder()->getForm();
  788. $view = $form->createView($parent);
  789. $this->assertSame($parent, $view->getParent());
  790. }
  791. protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
  792. {
  793. return new FormBuilder($name, $this->factory, $dispatcher ?: $this->dispatcher);
  794. }
  795. protected function getMockForm($name = 'name')
  796. {
  797. $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
  798. $form->expects($this->any())
  799. ->method('getName')
  800. ->will($this->returnValue($name));
  801. return $form;
  802. }
  803. protected function getValidForm($name)
  804. {
  805. $form = $this->getMockForm($name);
  806. $form->expects($this->any())
  807. ->method('isValid')
  808. ->will($this->returnValue(true));
  809. return $form;
  810. }
  811. protected function getInvalidForm($name)
  812. {
  813. $form = $this->getMockForm($name);
  814. $form->expects($this->any())
  815. ->method('isValid')
  816. ->will($this->returnValue(false));
  817. return $form;
  818. }
  819. protected function getDataMapper()
  820. {
  821. return $this->getMock('Symfony\Component\Form\DataMapperInterface');
  822. }
  823. protected function getDataTransformer()
  824. {
  825. return $this->getMock('Symfony\Component\Form\DataTransformerInterface');
  826. }
  827. protected function getFormValidator()
  828. {
  829. return $this->getMock('Symfony\Component\Form\FormValidatorInterface');
  830. }
  831. }