FormTest.php 37 KB

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