FormTest.php 31 KB

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