FormTest.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. /**
  250. * @expectedException \LogicException
  251. */
  252. public function testNotValidIfNotBound()
  253. {
  254. $this->form->isValid();
  255. }
  256. public function testNotValidIfErrors()
  257. {
  258. $this->form->bind('foobar');
  259. $this->form->addError(new FormError('Error!'));
  260. $this->assertFalse($this->form->isValid());
  261. }
  262. public function testNotValidIfChildNotValid()
  263. {
  264. $child = $this->getMockForm();
  265. $child->expects($this->once())
  266. ->method('isValid')
  267. ->will($this->returnValue(false));
  268. $this->form->bind('foobar');
  269. $this->form->add($child);
  270. $this->assertFalse($this->form->isValid());
  271. }
  272. public function testHasErrors()
  273. {
  274. $this->form->addError(new FormError('Error!'));
  275. $this->assertTrue($this->form->hasErrors());
  276. }
  277. public function testHasNoErrors()
  278. {
  279. $this->assertFalse($this->form->hasErrors());
  280. }
  281. public function testHasChildren()
  282. {
  283. $this->form->add($this->getBuilder()->getForm());
  284. $this->assertTrue($this->form->hasChildren());
  285. }
  286. public function testHasNoChildren()
  287. {
  288. $this->assertFalse($this->form->hasChildren());
  289. }
  290. public function testAdd()
  291. {
  292. $child = $this->getBuilder('foo')->getForm();
  293. $this->form->add($child);
  294. $this->assertSame($this->form, $child->getParent());
  295. $this->assertSame(array('foo' => $child), $this->form->getChildren());
  296. }
  297. public function testRemove()
  298. {
  299. $child = $this->getBuilder('foo')->getForm();
  300. $this->form->add($child);
  301. $this->form->remove('foo');
  302. $this->assertNull($child->getParent());
  303. $this->assertFalse($this->form->hasChildren());
  304. }
  305. public function testRemoveIgnoresUnknownName()
  306. {
  307. $this->form->remove('notexisting');
  308. }
  309. public function testArrayAccess()
  310. {
  311. $child = $this->getBuilder('foo')->getForm();
  312. $this->form[] = $child;
  313. $this->assertTrue(isset($this->form['foo']));
  314. $this->assertSame($child, $this->form['foo']);
  315. unset($this->form['foo']);
  316. $this->assertFalse(isset($this->form['foo']));
  317. }
  318. public function testCountable()
  319. {
  320. $this->form->add($this->getBuilder('foo')->getForm());
  321. $this->form->add($this->getBuilder('bar')->getForm());
  322. $this->assertEquals(2, count($this->form));
  323. }
  324. public function testIterator()
  325. {
  326. $this->form->add($this->getBuilder('foo')->getForm());
  327. $this->form->add($this->getBuilder('bar')->getForm());
  328. $this->assertSame($this->form->getChildren(), iterator_to_array($this->form));
  329. }
  330. public function testBound()
  331. {
  332. $this->form->bind('foobar');
  333. $this->assertTrue($this->form->isBound());
  334. }
  335. public function testNotBound()
  336. {
  337. $this->assertFalse($this->form->isBound());
  338. }
  339. public function testSetDataExecutesTransformationChain()
  340. {
  341. // use real event dispatcher now
  342. $form = $this->getBuilder('name', new EventDispatcher())
  343. ->addEventSubscriber(new FixedFilterListener(array(
  344. 'onSetData' => array(
  345. 'app' => 'filtered',
  346. ),
  347. )))
  348. ->appendNormTransformer(new FixedDataTransformer(array(
  349. '' => '',
  350. 'filtered' => 'norm',
  351. )))
  352. ->appendClientTransformer(new FixedDataTransformer(array(
  353. '' => '',
  354. 'norm' => 'client',
  355. )))
  356. ->getForm();
  357. $form->setData('app');
  358. $this->assertEquals('filtered', $form->getData());
  359. $this->assertEquals('norm', $form->getNormData());
  360. $this->assertEquals('client', $form->getClientData());
  361. }
  362. public function testSetDataExecutesClientTransformersInOrder()
  363. {
  364. $form = $this->getBuilder()
  365. ->appendClientTransformer(new FixedDataTransformer(array(
  366. '' => '',
  367. 'first' => 'second',
  368. )))
  369. ->appendClientTransformer(new FixedDataTransformer(array(
  370. '' => '',
  371. 'second' => 'third',
  372. )))
  373. ->getForm();
  374. $form->setData('first');
  375. $this->assertEquals('third', $form->getClientData());
  376. }
  377. public function testSetDataExecutesNormTransformersInOrder()
  378. {
  379. $form = $this->getBuilder()
  380. ->appendNormTransformer(new FixedDataTransformer(array(
  381. '' => '',
  382. 'first' => 'second',
  383. )))
  384. ->appendNormTransformer(new FixedDataTransformer(array(
  385. '' => '',
  386. 'second' => 'third',
  387. )))
  388. ->getForm();
  389. $form->setData('first');
  390. $this->assertEquals('third', $form->getNormData());
  391. }
  392. /*
  393. * When there is no data transformer, the data must have the same format
  394. * in all three representations
  395. */
  396. public function testSetDataConvertsScalarToStringIfNoTransformer()
  397. {
  398. $form = $this->getBuilder()->getForm();
  399. $form->setData(1);
  400. $this->assertSame('1', $form->getData());
  401. $this->assertSame('1', $form->getNormData());
  402. $this->assertSame('1', $form->getClientData());
  403. }
  404. /*
  405. * Data in client format should, if possible, always be a string to
  406. * facilitate differentiation between '0' and ''
  407. */
  408. public function testSetDataConvertsScalarToStringIfOnlyNormTransformer()
  409. {
  410. $form = $this->getBuilder()
  411. ->appendNormTransformer(new FixedDataTransformer(array(
  412. '' => '',
  413. 1 => 23,
  414. )))
  415. ->getForm();
  416. $form->setData(1);
  417. $this->assertSame(1, $form->getData());
  418. $this->assertSame(23, $form->getNormData());
  419. $this->assertSame('23', $form->getClientData());
  420. }
  421. /*
  422. * NULL remains NULL in app and norm format to remove the need to treat
  423. * empty values and NULL explicitely in the application
  424. */
  425. public function testSetDataConvertsNullToStringIfNoTransformer()
  426. {
  427. $form = $this->getBuilder()->getForm();
  428. $form->setData(null);
  429. $this->assertNull($form->getData());
  430. $this->assertNull($form->getNormData());
  431. $this->assertSame('', $form->getClientData());
  432. }
  433. public function testBindConvertsEmptyToNullIfNoTransformer()
  434. {
  435. $form = $this->getBuilder()->getForm();
  436. $form->bind('');
  437. $this->assertNull($form->getData());
  438. $this->assertNull($form->getNormData());
  439. $this->assertSame('', $form->getClientData());
  440. }
  441. public function testBindExecutesTransformationChain()
  442. {
  443. // use real event dispatcher now
  444. $form = $this->getBuilder('name', new EventDispatcher())
  445. ->addEventSubscriber(new FixedFilterListener(array(
  446. 'onBindClientData' => array(
  447. 'client' => 'filteredclient',
  448. ),
  449. 'onBindNormData' => array(
  450. 'norm' => 'filterednorm',
  451. ),
  452. )))
  453. ->appendClientTransformer(new FixedDataTransformer(array(
  454. '' => '',
  455. // direction is reversed!
  456. 'norm' => 'filteredclient',
  457. 'filterednorm' => 'cleanedclient'
  458. )))
  459. ->appendNormTransformer(new FixedDataTransformer(array(
  460. '' => '',
  461. // direction is reversed!
  462. 'app' => 'filterednorm',
  463. )))
  464. ->getForm();
  465. $form->setData('app');
  466. $this->assertEquals('app', $form->getData());
  467. $this->assertEquals('filterednorm', $form->getNormData());
  468. $this->assertEquals('cleanedclient', $form->getClientData());
  469. }
  470. public function testBindExecutesClientTransformersInReverseOrder()
  471. {
  472. $form = $this->getBuilder()
  473. ->appendClientTransformer(new FixedDataTransformer(array(
  474. '' => '',
  475. 'third' => 'second',
  476. )))
  477. ->appendClientTransformer(new FixedDataTransformer(array(
  478. '' => '',
  479. 'second' => 'first',
  480. )))
  481. ->getForm();
  482. $form->bind('first');
  483. $this->assertEquals('third', $form->getNormData());
  484. }
  485. public function testBindExecutesNormTransformersInReverseOrder()
  486. {
  487. $form = $this->getBuilder()
  488. ->appendNormTransformer(new FixedDataTransformer(array(
  489. '' => '',
  490. 'third' => 'second',
  491. )))
  492. ->appendNormTransformer(new FixedDataTransformer(array(
  493. '' => '',
  494. 'second' => 'first',
  495. )))
  496. ->getForm();
  497. $form->bind('first');
  498. $this->assertEquals('third', $form->getData());
  499. }
  500. public function testSynchronizedByDefault()
  501. {
  502. $this->assertTrue($this->form->isSynchronized());
  503. }
  504. public function testSynchronizedAfterBinding()
  505. {
  506. $this->form->bind('foobar');
  507. $this->assertTrue($this->form->isSynchronized());
  508. }
  509. public function testNotSynchronizedIfTransformationFailed()
  510. {
  511. $transformer = $this->getDataTransformer();
  512. $transformer->expects($this->once())
  513. ->method('reverseTransform')
  514. ->will($this->throwException(new TransformationFailedException()));
  515. $form = $this->getBuilder()
  516. ->appendClientTransformer($transformer)
  517. ->getForm();
  518. $form->bind('foobar');
  519. $this->assertFalse($form->isSynchronized());
  520. }
  521. public function testEmptyDataCreatedBeforeTransforming()
  522. {
  523. $form = $this->getBuilder()
  524. ->setEmptyData('foo')
  525. ->appendClientTransformer(new FixedDataTransformer(array(
  526. '' => '',
  527. // direction is reversed!
  528. 'bar' => 'foo',
  529. )))
  530. ->getForm();
  531. $form->bind('');
  532. $this->assertEquals('bar', $form->getData());
  533. }
  534. public function testEmptyDataFromClosure()
  535. {
  536. $test = $this;
  537. $form = $this->getBuilder()
  538. ->setEmptyData(function ($form) use ($test) {
  539. // the form instance is passed to the closure to allow use
  540. // of form data when creating the empty value
  541. $test->assertInstanceOf('Symfony\Component\Form\FormInterface', $form);
  542. return 'foo';
  543. })
  544. ->appendClientTransformer(new FixedDataTransformer(array(
  545. '' => '',
  546. // direction is reversed!
  547. 'bar' => 'foo',
  548. )))
  549. ->getForm();
  550. $form->bind('');
  551. $this->assertEquals('bar', $form->getData());
  552. }
  553. public function testAddMapsClientDataToForm()
  554. {
  555. $mapper = $this->getDataMapper();
  556. $form = $this->getBuilder()
  557. ->setDataMapper($mapper)
  558. ->appendClientTransformer(new FixedDataTransformer(array(
  559. '' => '',
  560. 'foo' => 'bar',
  561. )))
  562. ->setData('foo')
  563. ->getForm();
  564. $child = $this->getBuilder()->getForm();
  565. $mapper->expects($this->once())
  566. ->method('mapDataToForm')
  567. ->with('bar', $child);
  568. $form->add($child);
  569. }
  570. public function testSetDataMapsClientDataToChildren()
  571. {
  572. $mapper = $this->getDataMapper();
  573. $form = $this->getBuilder()
  574. ->setDataMapper($mapper)
  575. ->appendClientTransformer(new FixedDataTransformer(array(
  576. '' => '',
  577. 'foo' => 'bar',
  578. )))
  579. ->getForm();
  580. $form->add($child1 = $this->getBuilder('firstName')->getForm());
  581. $form->add($child2 = $this->getBuilder('lastName')->getForm());
  582. $mapper->expects($this->once())
  583. ->method('mapDataToForms')
  584. ->with('bar', array('firstName' => $child1, 'lastName' => $child2));
  585. $form->setData('foo');
  586. }
  587. public function testBindMapsBoundChildrenOntoExistingClientData()
  588. {
  589. $test = $this;
  590. $mapper = $this->getDataMapper();
  591. $form = $this->getBuilder()
  592. ->setDataMapper($mapper)
  593. ->appendClientTransformer(new FixedDataTransformer(array(
  594. '' => '',
  595. 'foo' => 'bar',
  596. )))
  597. ->setData('foo')
  598. ->getForm();
  599. $form->add($child1 = $this->getBuilder('firstName')->getForm());
  600. $form->add($child2 = $this->getBuilder('lastName')->getForm());
  601. $mapper->expects($this->once())
  602. ->method('mapFormsToData')
  603. ->with(array('firstName' => $child1, 'lastName' => $child2), 'bar')
  604. ->will($this->returnCallback(function ($children, $bar) use ($test) {
  605. $test->assertEquals('Bernhard', $children['firstName']->getData());
  606. $test->assertEquals('Schussek', $children['lastName']->getData());
  607. }));
  608. $form->bind(array(
  609. 'firstName' => 'Bernhard',
  610. 'lastName' => 'Schussek',
  611. ));
  612. }
  613. public function testBindMapsBoundChildrenOntoEmptyData()
  614. {
  615. $test = $this;
  616. $mapper = $this->getDataMapper();
  617. $object = new \stdClass();
  618. $form = $this->getBuilder()
  619. ->setDataMapper($mapper)
  620. ->setEmptyData($object)
  621. ->setData(null)
  622. ->getForm();
  623. $form->add($child = $this->getBuilder('name')->getForm());
  624. $mapper->expects($this->once())
  625. ->method('mapFormsToData')
  626. ->with(array('name' => $child), $object);
  627. $form->bind(array(
  628. 'name' => 'Bernhard',
  629. ));
  630. }
  631. public function testBindValidatesAfterTransformation()
  632. {
  633. $test = $this;
  634. $validator = $this->getFormValidator();
  635. $form = $this->getBuilder()
  636. ->addValidator($validator)
  637. ->getForm();
  638. $validator->expects($this->once())
  639. ->method('validate')
  640. ->with($form)
  641. ->will($this->returnCallback(function ($form) use ($test) {
  642. $test->assertEquals('foobar', $form->getData());
  643. }));
  644. $form->bind('foobar');
  645. }
  646. public function requestMethodProvider()
  647. {
  648. return array(
  649. array('POST'),
  650. array('PUT'),
  651. );
  652. }
  653. /**
  654. * @dataProvider requestMethodProvider
  655. */
  656. public function testBindPostOrPutRequest($method)
  657. {
  658. $path = tempnam(sys_get_temp_dir(), 'sf2');
  659. touch($path);
  660. $values = array(
  661. 'author' => array(
  662. 'name' => 'Bernhard',
  663. 'image' => array('filename' => 'foobar.png'),
  664. ),
  665. );
  666. $files = array(
  667. 'author' => array(
  668. 'error' => array('image' => UPLOAD_ERR_OK),
  669. 'name' => array('image' => 'upload.png'),
  670. 'size' => array('image' => 123),
  671. 'tmp_name' => array('image' => $path),
  672. 'type' => array('image' => 'image/png'),
  673. ),
  674. );
  675. $request = new Request(array(), $values, array(), array(), $files, array(
  676. 'REQUEST_METHOD' => $method,
  677. ));
  678. $form = $this->getBuilder('author')->getForm();
  679. $form->add($this->getBuilder('name')->getForm());
  680. $form->add($this->getBuilder('image')->getForm());
  681. $form->bindRequest($request);
  682. $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  683. $this->assertEquals('Bernhard', $form['name']->getData());
  684. $this->assertEquals($file, $form['image']->getData());
  685. unlink($path);
  686. }
  687. public function testBindGetRequest()
  688. {
  689. $values = array(
  690. 'author' => array(
  691. 'firstName' => 'Bernhard',
  692. 'lastName' => 'Schussek',
  693. ),
  694. );
  695. $request = new Request($values, array(), array(), array(), array(), array(
  696. 'REQUEST_METHOD' => 'GET',
  697. ));
  698. $form = $this->getBuilder('author')->getForm();
  699. $form->add($this->getBuilder('firstName')->getForm());
  700. $form->add($this->getBuilder('lastName')->getForm());
  701. $form->bindRequest($request);
  702. $this->assertEquals('Bernhard', $form['firstName']->getData());
  703. $this->assertEquals('Schussek', $form['lastName']->getData());
  704. }
  705. public function testBindResetsErrors()
  706. {
  707. $form = $this->getBuilder()->getForm();
  708. $form->addError(new FormError('Error!'));
  709. $form->bind('foobar');
  710. $this->assertSame(array(), $form->getErrors());
  711. }
  712. public function testCreateView()
  713. {
  714. $test = $this;
  715. $type1 = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  716. $type1Extension = $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
  717. $type1->expects($this->any())
  718. ->method('getExtensions')
  719. ->will($this->returnValue(array($type1Extension)));
  720. $type2 = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  721. $type2Extension = $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
  722. $type2->expects($this->any())
  723. ->method('getExtensions')
  724. ->will($this->returnValue(array($type2Extension)));
  725. $calls = array();
  726. $type1->expects($this->once())
  727. ->method('buildView')
  728. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  729. $calls[] = 'type1::buildView';
  730. $test->assertTrue($view->hasParent());
  731. $test->assertFalse($view->hasChildren());
  732. }));
  733. $type1Extension->expects($this->once())
  734. ->method('buildView')
  735. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  736. $calls[] = 'type1ext::buildView';
  737. $test->assertTrue($view->hasParent());
  738. $test->assertFalse($view->hasChildren());
  739. }));
  740. $type2->expects($this->once())
  741. ->method('buildView')
  742. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  743. $calls[] = 'type2::buildView';
  744. $test->assertTrue($view->hasParent());
  745. $test->assertFalse($view->hasChildren());
  746. }));
  747. $type2Extension->expects($this->once())
  748. ->method('buildView')
  749. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  750. $calls[] = 'type2ext::buildView';
  751. $test->assertTrue($view->hasParent());
  752. $test->assertFalse($view->hasChildren());
  753. }));
  754. $type1->expects($this->once())
  755. ->method('buildViewBottomUp')
  756. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  757. $calls[] = 'type1::buildViewBottomUp';
  758. $test->assertTrue($view->hasChildren());
  759. }));
  760. $type1Extension->expects($this->once())
  761. ->method('buildViewBottomUp')
  762. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  763. $calls[] = 'type1ext::buildViewBottomUp';
  764. $test->assertTrue($view->hasChildren());
  765. }));
  766. $type2->expects($this->once())
  767. ->method('buildViewBottomUp')
  768. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  769. $calls[] = 'type2::buildViewBottomUp';
  770. $test->assertTrue($view->hasChildren());
  771. }));
  772. $type2Extension->expects($this->once())
  773. ->method('buildViewBottomUp')
  774. ->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
  775. $calls[] = 'type2ext::buildViewBottomUp';
  776. $test->assertTrue($view->hasChildren());
  777. }));
  778. $form = $this->getBuilder()->setTypes(array($type1, $type2))->getForm();
  779. $form->setParent($this->getBuilder()->getForm());
  780. $form->add($this->getBuilder()->getForm());
  781. $form->createView();
  782. $this->assertEquals(array(
  783. 0 => 'type1::buildView',
  784. 1 => 'type1ext::buildView',
  785. 2 => 'type2::buildView',
  786. 3 => 'type2ext::buildView',
  787. 4 => 'type1::buildViewBottomUp',
  788. 5 => 'type1ext::buildViewBottomUp',
  789. 6 => 'type2::buildViewBottomUp',
  790. 7 => 'type2ext::buildViewBottomUp',
  791. ), $calls);
  792. }
  793. public function testCreateViewAcceptsParent()
  794. {
  795. $parent = new FormView();
  796. $form = $this->getBuilder()->getForm();
  797. $view = $form->createView($parent);
  798. $this->assertSame($parent, $view->getParent());
  799. }
  800. protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
  801. {
  802. return new FormBuilder($name, $this->factory, $dispatcher ?: $this->dispatcher);
  803. }
  804. protected function getMockForm($name = 'name')
  805. {
  806. $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
  807. $form->expects($this->any())
  808. ->method('getName')
  809. ->will($this->returnValue($name));
  810. return $form;
  811. }
  812. protected function getValidForm($name)
  813. {
  814. $form = $this->getMockForm($name);
  815. $form->expects($this->any())
  816. ->method('isValid')
  817. ->will($this->returnValue(true));
  818. return $form;
  819. }
  820. protected function getInvalidForm($name)
  821. {
  822. $form = $this->getMockForm($name);
  823. $form->expects($this->any())
  824. ->method('isValid')
  825. ->will($this->returnValue(false));
  826. return $form;
  827. }
  828. protected function getDataMapper()
  829. {
  830. return $this->getMock('Symfony\Component\Form\DataMapperInterface');
  831. }
  832. protected function getDataTransformer()
  833. {
  834. return $this->getMock('Symfony\Component\Form\DataTransformerInterface');
  835. }
  836. protected function getFormValidator()
  837. {
  838. return $this->getMock('Symfony\Component\Form\FormValidatorInterface');
  839. }
  840. }