FormTest.php 36 KB

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