FormTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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/Author.php';
  12. require_once __DIR__ . '/Fixtures/TestField.php';
  13. require_once __DIR__ . '/Fixtures/TestForm.php';
  14. use Symfony\Component\Form\Form;
  15. use Symfony\Component\Form\FormContext;
  16. use Symfony\Component\Form\Field;
  17. use Symfony\Component\Form\FieldError;
  18. use Symfony\Component\Form\DataError;
  19. use Symfony\Component\Form\HiddenField;
  20. use Symfony\Component\Form\PropertyPath;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\File\UploadedFile;
  23. use Symfony\Component\Validator\ConstraintViolation;
  24. use Symfony\Component\Validator\ConstraintViolationList;
  25. use Symfony\Tests\Component\Form\Fixtures\Author;
  26. use Symfony\Tests\Component\Form\Fixtures\TestField;
  27. use Symfony\Tests\Component\Form\Fixtures\TestForm;
  28. class FormTest_PreconfiguredForm extends Form
  29. {
  30. protected function configure()
  31. {
  32. $this->add(new Field('firstName'));
  33. parent::configure();
  34. }
  35. }
  36. class TestSetDataBeforeConfigureForm extends Form
  37. {
  38. protected $testCase;
  39. protected $object;
  40. public function __construct($testCase, $name, $object, $validator)
  41. {
  42. $this->testCase = $testCase;
  43. $this->object = $object;
  44. parent::__construct($name, $object, $validator);
  45. }
  46. protected function configure()
  47. {
  48. $this->testCase->assertEquals($this->object, $this->getData());
  49. parent::configure();
  50. }
  51. }
  52. class FormTest extends \PHPUnit_Framework_TestCase
  53. {
  54. protected $validator;
  55. protected $form;
  56. public static function setUpBeforeClass()
  57. {
  58. @session_start();
  59. }
  60. protected function setUp()
  61. {
  62. $this->validator = $this->createMockValidator();
  63. $this->form = new Form('author', array('validator' => $this->validator));
  64. }
  65. public function testNoCsrfProtectionByDefault()
  66. {
  67. $form = new Form('author');
  68. $this->assertFalse($form->isCsrfProtected());
  69. }
  70. public function testCsrfProtectionCanBeEnabled()
  71. {
  72. $form = new Form('author', array(
  73. 'csrf_provider' => $this->createMockCsrfProvider(),
  74. ));
  75. $this->assertTrue($form->isCsrfProtected());
  76. }
  77. public function testCsrfFieldNameCanBeSet()
  78. {
  79. $form = new Form('author', array(
  80. 'csrf_provider' => $this->createMockCsrfProvider(),
  81. 'csrf_field_name' => 'foobar',
  82. ));
  83. $this->assertEquals('foobar', $form->getCsrfFieldName());
  84. }
  85. public function testCsrfProtectedFormsHaveExtraField()
  86. {
  87. $provider = $this->createMockCsrfProvider();
  88. $provider->expects($this->once())
  89. ->method('generateCsrfToken')
  90. ->with($this->equalTo('Symfony\Component\Form\Form'))
  91. ->will($this->returnValue('ABCDEF'));
  92. $form = new Form('author', array(
  93. 'csrf_provider' => $provider,
  94. ));
  95. $this->assertTrue($form->has($this->form->getCsrfFieldName()));
  96. $field = $form->get($form->getCsrfFieldName());
  97. $this->assertTrue($field instanceof HiddenField);
  98. $this->assertEquals('ABCDEF', $field->getDisplayedData());
  99. }
  100. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  101. {
  102. $this->form->submit(array());
  103. $this->assertTrue($this->form->isCsrfTokenValid());
  104. }
  105. public function testIsCsrfTokenValidPasses()
  106. {
  107. $provider = $this->createMockCsrfProvider();
  108. $provider->expects($this->once())
  109. ->method('isCsrfTokenValid')
  110. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  111. ->will($this->returnValue(true));
  112. $form = new Form('author', array(
  113. 'csrf_provider' => $provider,
  114. 'validator' => $this->validator,
  115. ));
  116. $field = $form->getCsrfFieldName();
  117. $form->submit(array($field => 'ABCDEF'));
  118. $this->assertTrue($form->isCsrfTokenValid());
  119. }
  120. public function testIsCsrfTokenValidFails()
  121. {
  122. $provider = $this->createMockCsrfProvider();
  123. $provider->expects($this->once())
  124. ->method('isCsrfTokenValid')
  125. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  126. ->will($this->returnValue(false));
  127. $form = new Form('author', array(
  128. 'csrf_provider' => $provider,
  129. 'validator' => $this->validator,
  130. ));
  131. $field = $form->getCsrfFieldName();
  132. $form->submit(array($field => 'ABCDEF'));
  133. $this->assertFalse($form->isCsrfTokenValid());
  134. }
  135. public function testGetValidator()
  136. {
  137. $this->assertSame($this->validator, $this->form->getValidator());
  138. }
  139. public function testValidationGroupNullByDefault()
  140. {
  141. $this->assertNull($this->form->getValidationGroups());
  142. }
  143. public function testValidationGroupsCanBeSetToString()
  144. {
  145. $form = new Form('author', array(
  146. 'validation_groups' => 'group',
  147. ));
  148. $this->assertEquals(array('group'), $form->getValidationGroups());
  149. }
  150. public function testValidationGroupsCanBeSetToArray()
  151. {
  152. $form = new Form('author', array(
  153. 'validation_groups' => array('group1', 'group2'),
  154. ));
  155. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  156. }
  157. public function testBindUsesValidationGroups()
  158. {
  159. $field = $this->createMockField('firstName');
  160. $form = new Form('author', array(
  161. 'validation_groups' => 'group',
  162. 'validator' => $this->validator,
  163. ));
  164. $form->add($field);
  165. $this->validator->expects($this->exactly(2))
  166. ->method('validate');
  167. // PHPUnit limitation here
  168. // // form data is validated in custom group
  169. // $this->validator->expects($this->once())
  170. // ->method('validate')
  171. // ->with($this->equalTo($form->getData()), $this->equalTo(array('group')));
  172. //
  173. // // form itself is validated in group "Default"
  174. // $this->validator->expects($this->once())
  175. // ->method('validate')
  176. // ->with($this->equalTo($form));
  177. // data is irrelevant
  178. $form->bind($this->createPostRequest());
  179. }
  180. public function testBindThrowsExceptionIfNoValidatorIsSet()
  181. {
  182. $field = $this->createMockField('firstName');
  183. $form = new Form('author');
  184. $form->add($field);
  185. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  186. // data is irrelevant
  187. $form->bind($this->createPostRequest());
  188. }
  189. public function testBindReadsRequestData()
  190. {
  191. $values = array(
  192. 'author' => array(
  193. 'name' => 'Bernhard',
  194. 'image' => array('filename' => 'foobar.png'),
  195. ),
  196. );
  197. $files = array(
  198. 'author' => array(
  199. 'error' => array('image' => array('file' => UPLOAD_ERR_OK)),
  200. 'name' => array('image' => array('file' => 'upload.png')),
  201. 'size' => array('image' => array('file' => 123)),
  202. 'tmp_name' => array('image' => array('file' => 'abcdef.png')),
  203. 'type' => array('image' => array('file' => 'image/png')),
  204. ),
  205. );
  206. $form = new Form('author', array('validator' => $this->validator));
  207. $form->add(new TestField('name'));
  208. $imageForm = new Form('image');
  209. $imageForm->add(new TestField('file'));
  210. $imageForm->add(new TestField('filename'));
  211. $form->add($imageForm);
  212. $form->bind($this->createPostRequest($values, $files));
  213. $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  214. $this->assertEquals('Bernhard', $form['name']->getData());
  215. $this->assertEquals('foobar.png', $form['image']['filename']->getData());
  216. $this->assertEquals($file, $form['image']['file']->getData());
  217. }
  218. public function testBindAcceptsObject()
  219. {
  220. $object = new \stdClass();
  221. $form = new Form('author', array('validator' => $this->validator));
  222. $form->bind(new Request(), $object);
  223. $this->assertSame($object, $form->getData());
  224. }
  225. public function testReadPropertyIsIgnoredIfPropertyPathIsNull()
  226. {
  227. $author = new Author();
  228. $author->child = new Author();
  229. $standaloneChild = new Author();
  230. $form = new Form('child');
  231. $form->setData($standaloneChild);
  232. $form->setPropertyPath(null);
  233. $form->readProperty($author);
  234. // should not be $author->child!!
  235. $this->assertSame($standaloneChild, $form->getData());
  236. }
  237. public function testWritePropertyIsIgnoredIfPropertyPathIsNull()
  238. {
  239. $author = new Author();
  240. $author->child = $child = new Author();
  241. $standaloneChild = new Author();
  242. $form = new Form('child');
  243. $form->setData($standaloneChild);
  244. $form->setPropertyPath(null);
  245. $form->writeProperty($author);
  246. // $author->child was not modified
  247. $this->assertSame($child, $author->child);
  248. }
  249. public function testSupportsArrayAccess()
  250. {
  251. $form = new Form('author');
  252. $form->add($this->createMockField('firstName'));
  253. $this->assertEquals($form->get('firstName'), $form['firstName']);
  254. $this->assertTrue(isset($form['firstName']));
  255. }
  256. public function testSupportsUnset()
  257. {
  258. $form = new Form('author');
  259. $form->add($this->createMockField('firstName'));
  260. unset($form['firstName']);
  261. $this->assertFalse(isset($form['firstName']));
  262. }
  263. public function testDoesNotSupportAddingFields()
  264. {
  265. $form = new Form('author');
  266. $this->setExpectedException('LogicException');
  267. $form[] = $this->createMockField('lastName');
  268. }
  269. public function testSupportsCountable()
  270. {
  271. $form = new Form('group');
  272. $form->add($this->createMockField('firstName'));
  273. $form->add($this->createMockField('lastName'));
  274. $this->assertEquals(2, count($form));
  275. $form->add($this->createMockField('australian'));
  276. $this->assertEquals(3, count($form));
  277. }
  278. public function testSupportsIterable()
  279. {
  280. $form = new Form('group');
  281. $form->add($field1 = $this->createMockField('field1'));
  282. $form->add($field2 = $this->createMockField('field2'));
  283. $form->add($field3 = $this->createMockField('field3'));
  284. $expected = array(
  285. 'field1' => $field1,
  286. 'field2' => $field2,
  287. 'field3' => $field3,
  288. );
  289. $this->assertEquals($expected, iterator_to_array($form));
  290. }
  291. public function testIsSubmitted()
  292. {
  293. $form = new Form('author', array('validator' => $this->validator));
  294. $this->assertFalse($form->isSubmitted());
  295. $form->submit(array('firstName' => 'Bernhard'));
  296. $this->assertTrue($form->isSubmitted());
  297. }
  298. public function testValidIfAllFieldsAreValid()
  299. {
  300. $form = new Form('author', array('validator' => $this->validator));
  301. $form->add($this->createValidMockField('firstName'));
  302. $form->add($this->createValidMockField('lastName'));
  303. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  304. $this->assertTrue($form->isValid());
  305. }
  306. public function testInvalidIfFieldIsInvalid()
  307. {
  308. $form = new Form('author', array('validator' => $this->validator));
  309. $form->add($this->createInvalidMockField('firstName'));
  310. $form->add($this->createValidMockField('lastName'));
  311. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  312. $this->assertFalse($form->isValid());
  313. }
  314. public function testInvalidIfSubmittedWithExtraFields()
  315. {
  316. $form = new Form('author', array('validator' => $this->validator));
  317. $form->add($this->createValidMockField('firstName'));
  318. $form->add($this->createValidMockField('lastName'));
  319. $form->submit(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  320. $this->assertTrue($form->isSubmittedWithExtraFields());
  321. }
  322. public function testHasNoErrorsIfOnlyFieldHasErrors()
  323. {
  324. $form = new Form('author', array('validator' => $this->validator));
  325. $form->add($this->createInvalidMockField('firstName'));
  326. $form->submit(array('firstName' => 'Bernhard'));
  327. $this->assertFalse($form->hasErrors());
  328. }
  329. public function testSubmitForwardsPreprocessedData()
  330. {
  331. $field = $this->createMockField('firstName');
  332. $form = $this->getMock(
  333. 'Symfony\Component\Form\Form',
  334. array('preprocessData'), // only mock preprocessData()
  335. array('author', array('validator' => $this->validator))
  336. );
  337. // The data array is prepared directly after binding
  338. $form->expects($this->once())
  339. ->method('preprocessData')
  340. ->with($this->equalTo(array('firstName' => 'Bernhard')))
  341. ->will($this->returnValue(array('firstName' => 'preprocessed[Bernhard]')));
  342. $form->add($field);
  343. // The preprocessed data is then forwarded to the fields
  344. $field->expects($this->once())
  345. ->method('submit')
  346. ->with($this->equalTo('preprocessed[Bernhard]'));
  347. $form->submit(array('firstName' => 'Bernhard'));
  348. }
  349. public function testSubmitForwardsNullIfValueIsMissing()
  350. {
  351. $field = $this->createMockField('firstName');
  352. $field->expects($this->once())
  353. ->method('submit')
  354. ->with($this->equalTo(null));
  355. $form = new Form('author', array('validator' => $this->validator));
  356. $form->add($field);
  357. $form->submit(array());
  358. }
  359. public function testAddErrorMapsFieldValidationErrorsOntoFields()
  360. {
  361. $error = new FieldError('Message');
  362. $field = $this->createMockField('firstName');
  363. $field->expects($this->once())
  364. ->method('addError')
  365. ->with($this->equalTo($error));
  366. $form = new Form('author');
  367. $form->add($field);
  368. $path = new PropertyPath('fields[firstName].data');
  369. $form->addError(new FieldError('Message'), $path->getIterator());
  370. }
  371. public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedForms()
  372. {
  373. $error = new FieldError('Message');
  374. $field = $this->createMockField('firstName');
  375. $field->expects($this->once())
  376. ->method('addError')
  377. ->with($this->equalTo($error));
  378. $form = new Form('author');
  379. $innerGroup = new Form('names');
  380. $innerGroup->add($field);
  381. $form->add($innerGroup);
  382. $path = new PropertyPath('fields[names].fields[firstName].data');
  383. $form->addError(new FieldError('Message'), $path->getIterator());
  384. }
  385. public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
  386. {
  387. $field = $this->createMockField('foo');
  388. $field->expects($this->never())
  389. ->method('addError');
  390. $form = new Form('author');
  391. $form->add($field);
  392. $path = new PropertyPath('fields[bar].data');
  393. $form->addError(new FieldError('Message'), $path->getIterator());
  394. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  395. }
  396. public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
  397. {
  398. $field = $this->createMockField('firstName');
  399. $field->expects($this->any())
  400. ->method('isHidden')
  401. ->will($this->returnValue(true));
  402. $field->expects($this->never())
  403. ->method('addError');
  404. $form = new Form('author');
  405. $form->add($field);
  406. $path = new PropertyPath('fields[firstName].data');
  407. $form->addError(new FieldError('Message'), $path->getIterator());
  408. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  409. }
  410. public function testAddErrorMapsDataValidationErrorsOntoFields()
  411. {
  412. $error = new DataError('Message');
  413. // path is expected to point at "firstName"
  414. $expectedPath = new PropertyPath('firstName');
  415. $expectedPathIterator = $expectedPath->getIterator();
  416. $field = $this->createMockField('firstName');
  417. $field->expects($this->any())
  418. ->method('getPropertyPath')
  419. ->will($this->returnValue(new PropertyPath('firstName')));
  420. $field->expects($this->once())
  421. ->method('addError')
  422. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  423. $form = new Form('author');
  424. $form->add($field);
  425. $path = new PropertyPath('firstName');
  426. $form->addError($error, $path->getIterator());
  427. }
  428. public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
  429. {
  430. $field = $this->createMockField('foo');
  431. $field->expects($this->any())
  432. ->method('getPropertyPath')
  433. ->will($this->returnValue(new PropertyPath('foo')));
  434. $field->expects($this->never())
  435. ->method('addError');
  436. $form = new Form('author');
  437. $form->add($field);
  438. $path = new PropertyPath('bar');
  439. $form->addError(new DataError('Message'), $path->getIterator());
  440. }
  441. public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
  442. {
  443. $field = $this->createMockField('firstName');
  444. $field->expects($this->any())
  445. ->method('isHidden')
  446. ->will($this->returnValue(true));
  447. $field->expects($this->any())
  448. ->method('getPropertyPath')
  449. ->will($this->returnValue(new PropertyPath('firstName')));
  450. $field->expects($this->never())
  451. ->method('addError');
  452. $form = new Form('author');
  453. $form->add($field);
  454. $path = new PropertyPath('firstName');
  455. $form->addError(new DataError('Message'), $path->getIterator());
  456. }
  457. public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
  458. {
  459. $error = new DataError('Message');
  460. // path is expected to point at "street"
  461. $expectedPath = new PropertyPath('address.street');
  462. $expectedPathIterator = $expectedPath->getIterator();
  463. $expectedPathIterator->next();
  464. $field = $this->createMockField('address');
  465. $field->expects($this->any())
  466. ->method('getPropertyPath')
  467. ->will($this->returnValue(new PropertyPath('address')));
  468. $field->expects($this->once())
  469. ->method('addError')
  470. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  471. $form = new Form('author');
  472. $form->add($field);
  473. $path = new PropertyPath('address.street');
  474. $form->addError($error, $path->getIterator());
  475. }
  476. public function testAddErrorMapsErrorsOntoFieldsInVirtualGroups()
  477. {
  478. $error = new DataError('Message');
  479. // path is expected to point at "address"
  480. $expectedPath = new PropertyPath('address');
  481. $expectedPathIterator = $expectedPath->getIterator();
  482. $field = $this->createMockField('address');
  483. $field->expects($this->any())
  484. ->method('getPropertyPath')
  485. ->will($this->returnValue(new PropertyPath('address')));
  486. $field->expects($this->once())
  487. ->method('addError')
  488. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  489. $form = new Form('author');
  490. $nestedForm = new Form('nested', array('virtual' => true));
  491. $nestedForm->add($field);
  492. $form->add($nestedForm);
  493. $path = new PropertyPath('address');
  494. $form->addError($error, $path->getIterator());
  495. }
  496. public function testAddThrowsExceptionIfAlreadySubmitted()
  497. {
  498. $form = new Form('author', array('validator' => $this->validator));
  499. $form->add($this->createMockField('firstName'));
  500. $form->submit(array());
  501. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadySubmittedException');
  502. $form->add($this->createMockField('lastName'));
  503. }
  504. public function testAddSetsFieldParent()
  505. {
  506. $form = new Form('author');
  507. $field = $this->createMockField('firstName');
  508. $field->expects($this->once())
  509. ->method('setParent')
  510. ->with($this->equalTo($form));
  511. $form->add($field);
  512. }
  513. public function testRemoveUnsetsFieldParent()
  514. {
  515. $form = new Form('author');
  516. $field = $this->createMockField('firstName');
  517. $field->expects($this->exactly(2))
  518. ->method('setParent');
  519. // PHPUnit fails to compare subsequent method calls with different arguments
  520. $form->add($field);
  521. $form->remove('firstName');
  522. }
  523. public function testAddUpdatesFieldFromTransformedData()
  524. {
  525. $originalAuthor = new Author();
  526. $transformedAuthor = new Author();
  527. // the authors should differ to make sure the test works
  528. $transformedAuthor->firstName = 'Foo';
  529. $form = new TestForm('author');
  530. $transformer = $this->createMockTransformer();
  531. $transformer->expects($this->once())
  532. ->method('transform')
  533. ->with($this->equalTo($originalAuthor))
  534. ->will($this->returnValue($transformedAuthor));
  535. $form->setValueTransformer($transformer);
  536. $form->setData($originalAuthor);
  537. $field = $this->createMockField('firstName');
  538. $field->expects($this->any())
  539. ->method('getPropertyPath')
  540. ->will($this->returnValue(new PropertyPath('firstName')));
  541. $field->expects($this->once())
  542. ->method('readProperty')
  543. ->with($this->equalTo($transformedAuthor));
  544. $form->add($field);
  545. }
  546. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  547. {
  548. $originalAuthor = new Author();
  549. $form = new TestForm('author');
  550. $transformer = $this->createMockTransformer();
  551. $transformer->expects($this->once())
  552. ->method('transform')
  553. ->with($this->equalTo($originalAuthor))
  554. ->will($this->returnValue(''));
  555. $form->setValueTransformer($transformer);
  556. $form->setData($originalAuthor);
  557. $field = $this->createMockField('firstName');
  558. $field->expects($this->never())
  559. ->method('readProperty');
  560. $form->add($field);
  561. }
  562. /**
  563. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  564. */
  565. public function testAddThrowsExceptionIfNoFieldOrString()
  566. {
  567. $form = new Form('author');
  568. $form->add(1234);
  569. }
  570. /**
  571. * @expectedException Symfony\Component\Form\Exception\FieldDefinitionException
  572. */
  573. public function testAddThrowsExceptionIfAnonymousField()
  574. {
  575. $form = new Form('author');
  576. $field = $this->createMockField('');
  577. $form->add($field);
  578. }
  579. /**
  580. * @expectedException Symfony\Component\Form\Exception\FormException
  581. */
  582. public function testAddThrowsExceptionIfStringButNoFieldFactory()
  583. {
  584. $form = new Form('author', array('data_class' => 'Application\Entity'));
  585. $form->add('firstName');
  586. }
  587. /**
  588. * @expectedException Symfony\Component\Form\Exception\FormException
  589. */
  590. public function testAddThrowsExceptionIfStringButNoClass()
  591. {
  592. $form = new Form('author', array('field_factory' => new \stdClass()));
  593. $form->add('firstName');
  594. }
  595. public function testAddUsesFieldFromFactoryIfStringIsGiven()
  596. {
  597. $author = new \stdClass();
  598. $field = $this->createMockField('firstName');
  599. $factory = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryInterface');
  600. $factory->expects($this->once())
  601. ->method('getInstance')
  602. ->with($this->equalTo('stdClass'), $this->equalTo('firstName'), $this->equalTo(array('foo' => 'bar')))
  603. ->will($this->returnValue($field));
  604. $form = new Form('author', array(
  605. 'data' => $author,
  606. 'data_class' => 'stdClass',
  607. 'field_factory' => $factory,
  608. ));
  609. $form->add('firstName', array('foo' => 'bar'));
  610. $this->assertSame($field, $form['firstName']);
  611. }
  612. public function testSetDataUpdatesAllFieldsFromTransformedData()
  613. {
  614. $originalAuthor = new Author();
  615. $transformedAuthor = new Author();
  616. // the authors should differ to make sure the test works
  617. $transformedAuthor->firstName = 'Foo';
  618. $form = new TestForm('author');
  619. $transformer = $this->createMockTransformer();
  620. $transformer->expects($this->once())
  621. ->method('transform')
  622. ->with($this->equalTo($originalAuthor))
  623. ->will($this->returnValue($transformedAuthor));
  624. $form->setValueTransformer($transformer);
  625. $field = $this->createMockField('firstName');
  626. $field->expects($this->once())
  627. ->method('readProperty')
  628. ->with($this->equalTo($transformedAuthor));
  629. $form->add($field);
  630. $field = $this->createMockField('lastName');
  631. $field->expects($this->once())
  632. ->method('readProperty')
  633. ->with($this->equalTo($transformedAuthor));
  634. $form->add($field);
  635. $form->setData($originalAuthor);
  636. }
  637. /**
  638. * The use case for this test are groups whose fields should be mapped
  639. * directly onto properties of the form's object.
  640. *
  641. * Example:
  642. *
  643. * <code>
  644. * $dateRangeField = new Form('dateRange');
  645. * $dateRangeField->add(new DateField('startDate'));
  646. * $dateRangeField->add(new DateField('endDate'));
  647. * $form->add($dateRangeField);
  648. * </code>
  649. *
  650. * If $dateRangeField is not virtual, the property "dateRange" must be
  651. * present on the form's object. In this property, an object or array
  652. * with the properties "startDate" and "endDate" is expected.
  653. *
  654. * If $dateRangeField is virtual though, it's children are mapped directly
  655. * onto the properties "startDate" and "endDate" of the form's object.
  656. */
  657. public function testSetDataSkipsVirtualForms()
  658. {
  659. $author = new Author();
  660. $author->firstName = 'Foo';
  661. $form = new Form('author');
  662. $nestedForm = new Form('personal_data', array(
  663. 'virtual' => true,
  664. ));
  665. // both fields are in the nested group but receive the object of the
  666. // top-level group because the nested group is virtual
  667. $field = $this->createMockField('firstName');
  668. $field->expects($this->once())
  669. ->method('readProperty')
  670. ->with($this->equalTo($author));
  671. $nestedForm->add($field);
  672. $field = $this->createMockField('lastName');
  673. $field->expects($this->once())
  674. ->method('readProperty')
  675. ->with($this->equalTo($author));
  676. $nestedForm->add($field);
  677. $form->add($nestedForm);
  678. $form->setData($author);
  679. }
  680. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  681. {
  682. $form = new Form('author');
  683. $this->setExpectedException('InvalidArgumentException');
  684. $form->setData('foobar');
  685. }
  686. /**
  687. * @expectedException Symfony\Component\Form\Exception\FormException
  688. */
  689. public function testSetDataMatchesAgainstDataClass_fails()
  690. {
  691. $form = new Form('author', array(
  692. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  693. ));
  694. $form->setData(new \stdClass());
  695. }
  696. public function testSetDataMatchesAgainstDataClass_succeeds()
  697. {
  698. $form = new Form('author', array(
  699. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  700. ));
  701. $form->setData(new Author());
  702. }
  703. public function testSubmitUpdatesTransformedDataFromAllFields()
  704. {
  705. $originalAuthor = new Author();
  706. $transformedAuthor = new Author();
  707. // the authors should differ to make sure the test works
  708. $transformedAuthor->firstName = 'Foo';
  709. $form = new TestForm('author', array('validator' => $this->validator));
  710. $transformer = $this->createMockTransformer();
  711. $transformer->expects($this->exactly(2))
  712. ->method('transform')
  713. // the method is first called with NULL, then
  714. // with $originalAuthor -> not testable by PHPUnit
  715. // ->with($this->equalTo(null))
  716. // ->with($this->equalTo($originalAuthor))
  717. ->will($this->returnValue($transformedAuthor));
  718. $form->setValueTransformer($transformer);
  719. $form->setData($originalAuthor);
  720. $field = $this->createMockField('firstName');
  721. $field->expects($this->once())
  722. ->method('writeProperty')
  723. ->with($this->equalTo($transformedAuthor));
  724. $form->add($field);
  725. $field = $this->createMockField('lastName');
  726. $field->expects($this->once())
  727. ->method('writeProperty')
  728. ->with($this->equalTo($transformedAuthor));
  729. $form->add($field);
  730. $form->submit(array()); // irrelevant
  731. }
  732. public function testGetDataReturnsObject()
  733. {
  734. $form = new Form('author');
  735. $object = new \stdClass();
  736. $form->setData($object);
  737. $this->assertEquals($object, $form->getData());
  738. }
  739. public function testGetDisplayedDataForwardsCall()
  740. {
  741. $field = $this->createValidMockField('firstName');
  742. $field->expects($this->atLeastOnce())
  743. ->method('getDisplayedData')
  744. ->will($this->returnValue('Bernhard'));
  745. $form = new Form('author');
  746. $form->add($field);
  747. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getDisplayedData());
  748. }
  749. public function testIsMultipartIfAnyFieldIsMultipart()
  750. {
  751. $form = new Form('author');
  752. $form->add($this->createMultipartMockField('firstName'));
  753. $form->add($this->createNonMultipartMockField('lastName'));
  754. $this->assertTrue($form->isMultipart());
  755. }
  756. public function testIsNotMultipartIfNoFieldIsMultipart()
  757. {
  758. $form = new Form('author');
  759. $form->add($this->createNonMultipartMockField('firstName'));
  760. $form->add($this->createNonMultipartMockField('lastName'));
  761. $this->assertFalse($form->isMultipart());
  762. }
  763. public function testSupportsClone()
  764. {
  765. $form = new Form('author');
  766. $form->add($this->createMockField('firstName'));
  767. $clone = clone $form;
  768. $this->assertNotSame($clone['firstName'], $form['firstName']);
  769. }
  770. public function testSubmitWithoutPriorSetData()
  771. {
  772. return; // TODO
  773. $field = $this->createMockField('firstName');
  774. $field->expects($this->any())
  775. ->method('getData')
  776. ->will($this->returnValue('Bernhard'));
  777. $form = new Form('author');
  778. $form->add($field);
  779. $form->submit(array('firstName' => 'Bernhard'));
  780. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getData());
  781. }
  782. public function testGetHiddenFieldsReturnsOnlyHiddenFields()
  783. {
  784. $form = $this->getGroupWithBothVisibleAndHiddenField();
  785. $hiddenFields = $form->getHiddenFields(true, false);
  786. $this->assertSame(array($form['hiddenField']), $hiddenFields);
  787. }
  788. public function testGetVisibleFieldsReturnsOnlyVisibleFields()
  789. {
  790. $form = $this->getGroupWithBothVisibleAndHiddenField();
  791. $visibleFields = $form->getVisibleFields(true, false);
  792. $this->assertSame(array($form['visibleField']), $visibleFields);
  793. }
  794. /**
  795. * Create a group containing two fields, "visibleField" and "hiddenField"
  796. *
  797. * @return Form
  798. */
  799. protected function getGroupWithBothVisibleAndHiddenField()
  800. {
  801. $form = new Form('testGroup');
  802. // add a visible field
  803. $visibleField = $this->createMockField('visibleField');
  804. $visibleField->expects($this->once())
  805. ->method('isHidden')
  806. ->will($this->returnValue(false));
  807. $form->add($visibleField);
  808. // add a hidden field
  809. $hiddenField = $this->createMockField('hiddenField');
  810. $hiddenField->expects($this->once())
  811. ->method('isHidden')
  812. ->will($this->returnValue(true));
  813. $form->add($hiddenField);
  814. return $form;
  815. }
  816. protected function createMockField($key)
  817. {
  818. $field = $this->getMock(
  819. 'Symfony\Component\Form\FieldInterface',
  820. array(),
  821. array(),
  822. '',
  823. false, // don't use constructor
  824. false // don't call parent::__clone
  825. );
  826. $field->expects($this->any())
  827. ->method('getKey')
  828. ->will($this->returnValue($key));
  829. return $field;
  830. }
  831. protected function createMockForm()
  832. {
  833. $form = $this->getMock(
  834. 'Symfony\Component\Form\Form',
  835. array(),
  836. array(),
  837. '',
  838. false, // don't use constructor
  839. false // don't call parent::__clone)
  840. );
  841. $form->expects($this->any())
  842. ->method('getRoot')
  843. ->will($this->returnValue($form));
  844. return $form;
  845. }
  846. protected function createInvalidMockField($key)
  847. {
  848. $field = $this->createMockField($key);
  849. $field->expects($this->any())
  850. ->method('isValid')
  851. ->will($this->returnValue(false));
  852. return $field;
  853. }
  854. protected function createValidMockField($key)
  855. {
  856. $field = $this->createMockField($key);
  857. $field->expects($this->any())
  858. ->method('isValid')
  859. ->will($this->returnValue(true));
  860. return $field;
  861. }
  862. protected function createNonMultipartMockField($key)
  863. {
  864. $field = $this->createMockField($key);
  865. $field->expects($this->any())
  866. ->method('isMultipart')
  867. ->will($this->returnValue(false));
  868. return $field;
  869. }
  870. protected function createMultipartMockField($key)
  871. {
  872. $field = $this->createMockField($key);
  873. $field->expects($this->any())
  874. ->method('isMultipart')
  875. ->will($this->returnValue(true));
  876. return $field;
  877. }
  878. protected function createMockTransformer()
  879. {
  880. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  881. }
  882. protected function createMockValidator()
  883. {
  884. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  885. }
  886. protected function createMockCsrfProvider()
  887. {
  888. return $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface');
  889. }
  890. protected function createPostRequest(array $values = array(), array $files = array())
  891. {
  892. $server = array('REQUEST_METHOD' => 'POST');
  893. return new Request(array(), $values, array(), array(), $files, $server);
  894. }
  895. }