FormTest.php 30 KB

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