FormTest.php 30 KB

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