FormTest.php 26 KB

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