FormTest.php 26 KB

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