FormTest.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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__ . '/TestCase.php';
  12. require_once __DIR__ . '/Fixtures/Author.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\Component\Validator\ExecutionContext;
  26. use Symfony\Tests\Component\Form\Fixtures\Author;
  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. // behaves like a form with a value transformer that transforms into
  37. // a specific format
  38. class FormTest_FormThatReturns extends Form
  39. {
  40. protected $returnValue;
  41. public function setReturnValue($returnValue)
  42. {
  43. $this->returnValue = $returnValue;
  44. }
  45. public function setData($data)
  46. {
  47. }
  48. public function getData()
  49. {
  50. return $this->returnValue;
  51. }
  52. }
  53. class FormTest_AuthorWithoutRefSetter
  54. {
  55. protected $reference;
  56. protected $referenceCopy;
  57. public function __construct($reference)
  58. {
  59. $this->reference = $reference;
  60. $this->referenceCopy = $reference;
  61. }
  62. // The returned object should be modified by reference without having
  63. // to provide a setReference() method
  64. public function getReference()
  65. {
  66. return $this->reference;
  67. }
  68. // The returned object is a copy, so setReferenceCopy() must be used
  69. // to update it
  70. public function getReferenceCopy()
  71. {
  72. return is_object($this->referenceCopy) ? clone $this->referenceCopy : $this->referenceCopy;
  73. }
  74. public function setReferenceCopy($reference)
  75. {
  76. $this->referenceCopy = $reference;
  77. }
  78. }
  79. class TestSetDataBeforeConfigureForm extends Form
  80. {
  81. protected $testCase;
  82. protected $object;
  83. public function __construct($testCase, $name, $object, $validator)
  84. {
  85. $this->testCase = $testCase;
  86. $this->object = $object;
  87. parent::__construct($name, $object, $validator);
  88. }
  89. protected function configure()
  90. {
  91. $this->testCase->assertEquals($this->object, $this->getData());
  92. parent::configure();
  93. }
  94. }
  95. class FormTest extends TestCase
  96. {
  97. protected $validator;
  98. protected $form;
  99. public static function setUpBeforeClass()
  100. {
  101. @session_start();
  102. }
  103. protected function setUp()
  104. {
  105. parent::setUp();
  106. $this->validator = $this->createMockValidator();
  107. $this->form = $this->factory->getForm('author', array('validator' => $this->validator));
  108. }
  109. public function testNoCsrfProtectionByDefault()
  110. {
  111. $form = $this->factory->getForm('author');
  112. $this->assertFalse($form->isCsrfProtected());
  113. }
  114. public function testCsrfProtectionCanBeEnabled()
  115. {
  116. $form = $this->factory->getForm('author', array(
  117. 'csrf_protection' => true,
  118. 'csrf_provider' => $this->createMockCsrfProvider(),
  119. ));
  120. $this->assertTrue($form->isCsrfProtected());
  121. }
  122. public function testCsrfFieldNameCanBeSet()
  123. {
  124. $form = $this->factory->getForm('author', array(
  125. 'csrf_provider' => $this->createMockCsrfProvider(),
  126. 'csrf_field_name' => 'foobar',
  127. ));
  128. $this->assertEquals('foobar', $form->getCsrfFieldName());
  129. }
  130. public function testCsrfProtectedFormsHaveExtraField()
  131. {
  132. $provider = $this->createMockCsrfProvider();
  133. $provider->expects($this->once())
  134. ->method('generateCsrfToken')
  135. ->with($this->equalTo('Symfony\Component\Form\Form'))
  136. ->will($this->returnValue('ABCDEF'));
  137. $form = $this->factory->getForm('author', array(
  138. 'csrf_provider' => $provider,
  139. ));
  140. $this->assertTrue($form->has($this->form->getCsrfFieldName()));
  141. $field = $form->get($form->getCsrfFieldName());
  142. $this->assertTrue($field instanceof HiddenField);
  143. $this->assertEquals('ABCDEF', $field->getDisplayedData());
  144. }
  145. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  146. {
  147. $this->form->submit(array());
  148. $this->assertTrue($this->form->isCsrfTokenValid());
  149. }
  150. public function testIsCsrfTokenValidPasses()
  151. {
  152. $provider = $this->createMockCsrfProvider();
  153. $provider->expects($this->once())
  154. ->method('isCsrfTokenValid')
  155. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  156. ->will($this->returnValue(true));
  157. $form = $this->factory->getForm('author', array(
  158. 'csrf_provider' => $provider,
  159. 'validator' => $this->validator,
  160. ));
  161. $field = $form->getCsrfFieldName();
  162. $form->submit(array($field => 'ABCDEF'));
  163. $this->assertTrue($form->isCsrfTokenValid());
  164. }
  165. public function testIsCsrfTokenValidFails()
  166. {
  167. $provider = $this->createMockCsrfProvider();
  168. $provider->expects($this->once())
  169. ->method('isCsrfTokenValid')
  170. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  171. ->will($this->returnValue(false));
  172. $form = $this->factory->getForm('author', array(
  173. 'csrf_provider' => $provider,
  174. 'validator' => $this->validator,
  175. ));
  176. $field = $form->getCsrfFieldName();
  177. $form->submit(array($field => 'ABCDEF'));
  178. $this->assertFalse($form->isCsrfTokenValid());
  179. }
  180. public function testGetValidator()
  181. {
  182. $this->assertSame($this->validator, $this->form->getValidator());
  183. }
  184. public function testValidationGroupNullByDefault()
  185. {
  186. $this->assertNull($this->form->getValidationGroups());
  187. }
  188. public function testValidationGroupsCanBeSetToString()
  189. {
  190. $form = $this->factory->getForm('author', array(
  191. 'validation_groups' => 'group',
  192. ));
  193. $this->assertEquals(array('group'), $form->getValidationGroups());
  194. }
  195. public function testValidationGroupsCanBeSetToArray()
  196. {
  197. $form = $this->factory->getForm('author', array(
  198. 'validation_groups' => array('group1', 'group2'),
  199. ));
  200. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  201. }
  202. public function testValidationGroupsAreInheritedFromParentIfEmpty()
  203. {
  204. $parentForm = $this->factory->getForm('parent', array(
  205. 'validation_groups' => 'group',
  206. ));
  207. $childForm = $this->factory->getForm('child');
  208. $parentForm->add($childForm);
  209. $this->assertEquals(array('group'), $childForm->getValidationGroups());
  210. }
  211. public function testValidationGroupsAreNotInheritedFromParentIfSet()
  212. {
  213. $parentForm = $this->factory->getForm('parent', array(
  214. 'validation_groups' => 'group1',
  215. ));
  216. $childForm = $this->factory->getForm('child', array(
  217. 'validation_groups' => 'group2',
  218. ));
  219. $parentForm->add($childForm);
  220. $this->assertEquals(array('group2'), $childForm->getValidationGroups());
  221. }
  222. /**
  223. * @expectedException Symfony\Component\Form\Exception\FormException
  224. */
  225. public function testBindThrowsExceptionIfAnonymous()
  226. {
  227. $form = $this->factory->getForm(null, array('validator' => $this->createMockValidator()));
  228. $form->bind($this->createPostRequest());
  229. }
  230. public function testBindValidatesData()
  231. {
  232. $form = $this->factory->getForm('author', array(
  233. 'validation_groups' => 'group',
  234. 'validator' => $this->validator,
  235. ));
  236. $form->add(new Field('firstName'));
  237. $this->validator->expects($this->once())
  238. ->method('validate')
  239. ->with($this->equalTo($form));
  240. // concrete request is irrelevant
  241. $form->bind($this->createPostRequest());
  242. }
  243. public function testBindDoesNotValidateArrays()
  244. {
  245. $form = $this->factory->getForm('author', array(
  246. 'validator' => $this->validator,
  247. ));
  248. $form->add(new Field('firstName'));
  249. // only the form is validated
  250. $this->validator->expects($this->once())
  251. ->method('validate')
  252. ->with($this->equalTo($form));
  253. // concrete request is irrelevant
  254. // data is an array
  255. $form->bind($this->createPostRequest(), array());
  256. }
  257. public function testBindThrowsExceptionIfNoValidatorIsSet()
  258. {
  259. $field = $this->createMockField('firstName');
  260. $form = $this->factory->getForm('author');
  261. $form->add($field);
  262. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  263. // data is irrelevant
  264. $form->bind($this->createPostRequest());
  265. }
  266. public function testBindReadsRequestData()
  267. {
  268. $values = array(
  269. 'author' => array(
  270. 'name' => 'Bernhard',
  271. 'image' => array('filename' => 'foobar.png'),
  272. ),
  273. );
  274. $files = array(
  275. 'author' => array(
  276. 'error' => array('image' => array('file' => UPLOAD_ERR_OK)),
  277. 'name' => array('image' => array('file' => 'upload.png')),
  278. 'size' => array('image' => array('file' => 123)),
  279. 'tmp_name' => array('image' => array('file' => 'abcdef.png')),
  280. 'type' => array('image' => array('file' => 'image/png')),
  281. ),
  282. );
  283. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  284. $form->add(new Field('name'));
  285. $imageForm = $this->factory->getForm('image');
  286. $imageForm->add(new Field('file'));
  287. $imageForm->add(new Field('filename'));
  288. $form->add($imageForm);
  289. $form->bind($this->createPostRequest($values, $files));
  290. $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  291. $this->assertEquals('Bernhard', $form['name']->getData());
  292. $this->assertEquals('foobar.png', $form['image']['filename']->getData());
  293. $this->assertEquals($file, $form['image']['file']->getData());
  294. }
  295. public function testBindAcceptsObject()
  296. {
  297. $object = new \stdClass();
  298. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  299. $form->bind(new Request(), $object);
  300. $this->assertSame($object, $form->getData());
  301. }
  302. public function testReadPropertyIsIgnoredIfPropertyPathIsNull()
  303. {
  304. $author = new Author();
  305. $author->child = new Author();
  306. $standaloneChild = new Author();
  307. $form = $this->factory->getForm('child');
  308. $form->setData($standaloneChild);
  309. $form->setPropertyPath(null);
  310. $form->readProperty($author);
  311. // should not be $author->child!!
  312. $this->assertSame($standaloneChild, $form->getData());
  313. }
  314. public function testWritePropertyIsIgnoredIfPropertyPathIsNull()
  315. {
  316. $author = new Author();
  317. $author->child = $child = new Author();
  318. $standaloneChild = new Author();
  319. $form = $this->factory->getForm('child');
  320. $form->setData($standaloneChild);
  321. $form->setPropertyPath(null);
  322. $form->writeProperty($author);
  323. // $author->child was not modified
  324. $this->assertSame($child, $author->child);
  325. }
  326. public function testSupportsArrayAccess()
  327. {
  328. $form = $this->factory->getForm('author');
  329. $form->add($this->createMockField('firstName'));
  330. $this->assertEquals($form->get('firstName'), $form['firstName']);
  331. $this->assertTrue(isset($form['firstName']));
  332. }
  333. public function testSupportsUnset()
  334. {
  335. $form = $this->factory->getForm('author');
  336. $form->add($this->createMockField('firstName'));
  337. unset($form['firstName']);
  338. $this->assertFalse(isset($form['firstName']));
  339. }
  340. public function testDoesNotSupportAddingFields()
  341. {
  342. $form = $this->factory->getForm('author');
  343. $this->setExpectedException('LogicException');
  344. $form[] = $this->createMockField('lastName');
  345. }
  346. public function testSupportsCountable()
  347. {
  348. $form = $this->factory->getForm('group');
  349. $form->add($this->createMockField('firstName'));
  350. $form->add($this->createMockField('lastName'));
  351. $this->assertEquals(2, count($form));
  352. $form->add($this->createMockField('australian'));
  353. $this->assertEquals(3, count($form));
  354. }
  355. public function testSupportsIterable()
  356. {
  357. $form = $this->factory->getForm('group');
  358. $form->add($field1 = $this->createMockField('field1'));
  359. $form->add($field2 = $this->createMockField('field2'));
  360. $form->add($field3 = $this->createMockField('field3'));
  361. $expected = array(
  362. 'field1' => $field1,
  363. 'field2' => $field2,
  364. 'field3' => $field3,
  365. );
  366. $this->assertEquals($expected, iterator_to_array($form));
  367. }
  368. public function testIsSubmitted()
  369. {
  370. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  371. $this->assertFalse($form->isSubmitted());
  372. $form->submit(array('firstName' => 'Bernhard'));
  373. $this->assertTrue($form->isSubmitted());
  374. }
  375. public function testValidIfAllFieldsAreValid()
  376. {
  377. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  378. $form->add($this->createValidMockField('firstName'));
  379. $form->add($this->createValidMockField('lastName'));
  380. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  381. $this->assertTrue($form->isValid());
  382. }
  383. public function testInvalidIfFieldIsInvalid()
  384. {
  385. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  386. $form->add($this->createInvalidMockField('firstName'));
  387. $form->add($this->createValidMockField('lastName'));
  388. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  389. $this->assertFalse($form->isValid());
  390. }
  391. public function testInvalidIfSubmittedWithExtraFields()
  392. {
  393. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  394. $form->add($this->createValidMockField('firstName'));
  395. $form->add($this->createValidMockField('lastName'));
  396. $form->submit(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  397. $this->assertTrue($form->isSubmittedWithExtraFields());
  398. }
  399. public function testHasNoErrorsIfOnlyFieldHasErrors()
  400. {
  401. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  402. $form->add($this->createInvalidMockField('firstName'));
  403. $form->submit(array('firstName' => 'Bernhard'));
  404. $this->assertFalse($form->hasErrors());
  405. }
  406. public function testSubmitForwardsPreprocessedData()
  407. {
  408. $field = $this->createMockField('firstName');
  409. $form = $this->getMock(
  410. 'Symfony\Component\Form\Form',
  411. array('preprocessData'), // only mock preprocessData()
  412. array('author', array('validator' => $this->validator))
  413. );
  414. // The data array is prepared directly after binding
  415. $form->expects($this->once())
  416. ->method('preprocessData')
  417. ->with($this->equalTo(array('firstName' => 'Bernhard')))
  418. ->will($this->returnValue(array('firstName' => 'preprocessed[Bernhard]')));
  419. $form->add($field);
  420. // The preprocessed data is then forwarded to the fields
  421. $field->expects($this->once())
  422. ->method('submit')
  423. ->with($this->equalTo('preprocessed[Bernhard]'));
  424. $form->submit(array('firstName' => 'Bernhard'));
  425. }
  426. public function testSubmitForwardsNullIfValueIsMissing()
  427. {
  428. $field = $this->createMockField('firstName');
  429. $field->expects($this->once())
  430. ->method('submit')
  431. ->with($this->equalTo(null));
  432. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  433. $form->add($field);
  434. $form->submit(array());
  435. }
  436. public function testAddErrorMapsFieldValidationErrorsOntoFields()
  437. {
  438. $error = new FieldError('Message');
  439. $field = $this->createMockField('firstName');
  440. $field->expects($this->once())
  441. ->method('addError')
  442. ->with($this->equalTo($error));
  443. $form = $this->factory->getForm('author');
  444. $form->add($field);
  445. $path = new PropertyPath('fields[firstName].data');
  446. $form->addError(new FieldError('Message'), $path->getIterator());
  447. }
  448. public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedForms()
  449. {
  450. $error = new FieldError('Message');
  451. $field = $this->createMockField('firstName');
  452. $field->expects($this->once())
  453. ->method('addError')
  454. ->with($this->equalTo($error));
  455. $form = $this->factory->getForm('author');
  456. $innerGroup = $this->factory->getForm('names');
  457. $innerGroup->add($field);
  458. $form->add($innerGroup);
  459. $path = new PropertyPath('fields[names].fields[firstName].data');
  460. $form->addError(new FieldError('Message'), $path->getIterator());
  461. }
  462. public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
  463. {
  464. $field = $this->createMockField('foo');
  465. $field->expects($this->never())
  466. ->method('addError');
  467. $form = $this->factory->getForm('author');
  468. $form->add($field);
  469. $path = new PropertyPath('fields[bar].data');
  470. $form->addError(new FieldError('Message'), $path->getIterator());
  471. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  472. }
  473. public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
  474. {
  475. $field = $this->createMockField('firstName');
  476. $field->expects($this->any())
  477. ->method('isHidden')
  478. ->will($this->returnValue(true));
  479. $field->expects($this->never())
  480. ->method('addError');
  481. $form = $this->factory->getForm('author');
  482. $form->add($field);
  483. $path = new PropertyPath('fields[firstName].data');
  484. $form->addError(new FieldError('Message'), $path->getIterator());
  485. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  486. }
  487. public function testAddErrorMapsDataValidationErrorsOntoFields()
  488. {
  489. $error = new DataError('Message');
  490. // path is expected to point at "firstName"
  491. $expectedPath = new PropertyPath('firstName');
  492. $expectedPathIterator = $expectedPath->getIterator();
  493. $field = $this->createMockField('firstName');
  494. $field->expects($this->any())
  495. ->method('getPropertyPath')
  496. ->will($this->returnValue(new PropertyPath('firstName')));
  497. $field->expects($this->once())
  498. ->method('addError')
  499. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  500. $form = $this->factory->getForm('author');
  501. $form->add($field);
  502. $path = new PropertyPath('firstName');
  503. $form->addError($error, $path->getIterator());
  504. }
  505. public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
  506. {
  507. $field = $this->createMockField('foo');
  508. $field->expects($this->any())
  509. ->method('getPropertyPath')
  510. ->will($this->returnValue(new PropertyPath('foo')));
  511. $field->expects($this->never())
  512. ->method('addError');
  513. $form = $this->factory->getForm('author');
  514. $form->add($field);
  515. $path = new PropertyPath('bar');
  516. $form->addError(new DataError('Message'), $path->getIterator());
  517. }
  518. public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
  519. {
  520. $field = $this->createMockField('firstName');
  521. $field->expects($this->any())
  522. ->method('isHidden')
  523. ->will($this->returnValue(true));
  524. $field->expects($this->any())
  525. ->method('getPropertyPath')
  526. ->will($this->returnValue(new PropertyPath('firstName')));
  527. $field->expects($this->never())
  528. ->method('addError');
  529. $form = $this->factory->getForm('author');
  530. $form->add($field);
  531. $path = new PropertyPath('firstName');
  532. $form->addError(new DataError('Message'), $path->getIterator());
  533. }
  534. public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
  535. {
  536. $error = new DataError('Message');
  537. // path is expected to point at "street"
  538. $expectedPath = new PropertyPath('address.street');
  539. $expectedPathIterator = $expectedPath->getIterator();
  540. $expectedPathIterator->next();
  541. $field = $this->createMockField('address');
  542. $field->expects($this->any())
  543. ->method('getPropertyPath')
  544. ->will($this->returnValue(new PropertyPath('address')));
  545. $field->expects($this->once())
  546. ->method('addError')
  547. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  548. $form = $this->factory->getForm('author');
  549. $form->add($field);
  550. $path = new PropertyPath('address.street');
  551. $form->addError($error, $path->getIterator());
  552. }
  553. public function testAddErrorMapsErrorsOntoFieldsInVirtualGroups()
  554. {
  555. $error = new DataError('Message');
  556. // path is expected to point at "address"
  557. $expectedPath = new PropertyPath('address');
  558. $expectedPathIterator = $expectedPath->getIterator();
  559. $field = $this->createMockField('address');
  560. $field->expects($this->any())
  561. ->method('getPropertyPath')
  562. ->will($this->returnValue(new PropertyPath('address')));
  563. $field->expects($this->once())
  564. ->method('addError')
  565. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  566. $form = $this->factory->getForm('author');
  567. $nestedForm = $this->factory->getForm('nested', array('virtual' => true));
  568. $nestedForm->add($field);
  569. $form->add($nestedForm);
  570. $path = new PropertyPath('address');
  571. $form->addError($error, $path->getIterator());
  572. }
  573. public function testAddThrowsExceptionIfAlreadySubmitted()
  574. {
  575. $form = $this->factory->getForm('author', array('validator' => $this->validator));
  576. $form->add($this->createMockField('firstName'));
  577. $form->submit(array());
  578. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadySubmittedException');
  579. $form->add($this->createMockField('lastName'));
  580. }
  581. public function testAddSetsFieldParent()
  582. {
  583. $form = $this->factory->getForm('author');
  584. $field = $this->createMockField('firstName');
  585. $field->expects($this->once())
  586. ->method('setParent')
  587. ->with($this->equalTo($form));
  588. $form->add($field);
  589. }
  590. public function testRemoveUnsetsFieldParent()
  591. {
  592. $form = $this->factory->getForm('author');
  593. $field = $this->createMockField('firstName');
  594. $field->expects($this->exactly(2))
  595. ->method('setParent');
  596. // PHPUnit fails to compare subsequent method calls with different arguments
  597. $form->add($field);
  598. $form->remove('firstName');
  599. }
  600. public function testAddUpdatesFieldFromTransformedData()
  601. {
  602. $originalAuthor = new Author();
  603. $transformedAuthor = new Author();
  604. // the authors should differ to make sure the test works
  605. $transformedAuthor->firstName = 'Foo';
  606. $form = new TestForm('author');
  607. $transformer = $this->createMockTransformer();
  608. $transformer->expects($this->once())
  609. ->method('transform')
  610. ->with($this->equalTo($originalAuthor))
  611. ->will($this->returnValue($transformedAuthor));
  612. $form->setValueTransformer($transformer);
  613. $form->setData($originalAuthor);
  614. $field = $this->createMockField('firstName');
  615. $field->expects($this->any())
  616. ->method('getPropertyPath')
  617. ->will($this->returnValue(new PropertyPath('firstName')));
  618. $field->expects($this->once())
  619. ->method('readProperty')
  620. ->with($this->equalTo($transformedAuthor));
  621. $form->add($field);
  622. }
  623. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  624. {
  625. $originalAuthor = new Author();
  626. $form = new TestForm('author');
  627. $transformer = $this->createMockTransformer();
  628. $transformer->expects($this->once())
  629. ->method('transform')
  630. ->with($this->equalTo($originalAuthor))
  631. ->will($this->returnValue(''));
  632. $form->setValueTransformer($transformer);
  633. $form->setData($originalAuthor);
  634. $field = $this->createMockField('firstName');
  635. $field->expects($this->never())
  636. ->method('readProperty');
  637. $form->add($field);
  638. }
  639. /**
  640. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  641. */
  642. public function testAddThrowsExceptionIfNoFieldOrString()
  643. {
  644. $form = $this->factory->getForm('author');
  645. $form->add(1234);
  646. }
  647. /**
  648. * @expectedException Symfony\Component\Form\Exception\FieldDefinitionException
  649. */
  650. public function testAddThrowsExceptionIfAnonymousField()
  651. {
  652. $form = $this->factory->getForm('author');
  653. $field = $this->createMockField('');
  654. $form->add($field);
  655. }
  656. /**
  657. * @expectedException Symfony\Component\Form\Exception\FormException
  658. */
  659. public function testAddThrowsExceptionIfStringButNoFieldFactory()
  660. {
  661. $form = $this->factory->getForm('author', array('data_class' => 'Application\Entity'));
  662. $form->add('firstName');
  663. }
  664. /**
  665. * @expectedException Symfony\Component\Form\Exception\FormException
  666. */
  667. public function testAddThrowsExceptionIfStringButNoClass()
  668. {
  669. $form = $this->factory->getForm('author', array('field_factory' => new \stdClass()));
  670. $form->add('firstName');
  671. }
  672. public function testAddUsesFieldFromFactoryIfStringIsGiven()
  673. {
  674. $author = new \stdClass();
  675. $field = $this->createMockField('firstName');
  676. $factory = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryInterface');
  677. $factory->expects($this->once())
  678. ->method('getInstance')
  679. ->with($this->equalTo('stdClass'), $this->equalTo('firstName'), $this->equalTo(array('foo' => 'bar')))
  680. ->will($this->returnValue($field));
  681. $form = $this->factory->getForm('author', array(
  682. 'data' => $author,
  683. 'data_class' => 'stdClass',
  684. 'field_factory' => $factory,
  685. ));
  686. $form->add('firstName', array('foo' => 'bar'));
  687. $this->assertSame($field, $form['firstName']);
  688. }
  689. public function testSetDataUpdatesAllFieldsFromTransformedData()
  690. {
  691. $originalAuthor = new Author();
  692. $transformedAuthor = new Author();
  693. // the authors should differ to make sure the test works
  694. $transformedAuthor->firstName = 'Foo';
  695. $form = new TestForm('author');
  696. $transformer = $this->createMockTransformer();
  697. $transformer->expects($this->once())
  698. ->method('transform')
  699. ->with($this->equalTo($originalAuthor))
  700. ->will($this->returnValue($transformedAuthor));
  701. $form->setValueTransformer($transformer);
  702. $field = $this->createMockField('firstName');
  703. $field->expects($this->once())
  704. ->method('readProperty')
  705. ->with($this->equalTo($transformedAuthor));
  706. $form->add($field);
  707. $field = $this->createMockField('lastName');
  708. $field->expects($this->once())
  709. ->method('readProperty')
  710. ->with($this->equalTo($transformedAuthor));
  711. $form->add($field);
  712. $form->setData($originalAuthor);
  713. }
  714. /**
  715. * The use case for this test are groups whose fields should be mapped
  716. * directly onto properties of the form's object.
  717. *
  718. * Example:
  719. *
  720. * <code>
  721. * $dateRangeField = $this->factory->getForm('dateRange');
  722. * $dateRangeField->add(new DateField('startDate'));
  723. * $dateRangeField->add(new DateField('endDate'));
  724. * $form->add($dateRangeField);
  725. * </code>
  726. *
  727. * If $dateRangeField is not virtual, the property "dateRange" must be
  728. * present on the form's object. In this property, an object or array
  729. * with the properties "startDate" and "endDate" is expected.
  730. *
  731. * If $dateRangeField is virtual though, it's children are mapped directly
  732. * onto the properties "startDate" and "endDate" of the form's object.
  733. */
  734. public function testSetDataSkipsVirtualForms()
  735. {
  736. $author = new Author();
  737. $author->firstName = 'Foo';
  738. $form = $this->factory->getForm('author');
  739. $nestedForm = $this->factory->getForm('personal_data', array(
  740. 'virtual' => true,
  741. ));
  742. // both fields are in the nested group but receive the object of the
  743. // top-level group because the nested group is virtual
  744. $field = $this->createMockField('firstName');
  745. $field->expects($this->once())
  746. ->method('readProperty')
  747. ->with($this->equalTo($author));
  748. $nestedForm->add($field);
  749. $field = $this->createMockField('lastName');
  750. $field->expects($this->once())
  751. ->method('readProperty')
  752. ->with($this->equalTo($author));
  753. $nestedForm->add($field);
  754. $form->add($nestedForm);
  755. $form->setData($author);
  756. }
  757. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  758. {
  759. $form = $this->factory->getForm('author');
  760. $this->setExpectedException('InvalidArgumentException');
  761. $form->setData('foobar');
  762. }
  763. /**
  764. * @expectedException Symfony\Component\Form\Exception\FormException
  765. */
  766. public function testSetDataMatchesAgainstDataClass_fails()
  767. {
  768. $form = $this->factory->getForm('author', array(
  769. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  770. ));
  771. $form->setData(new \stdClass());
  772. }
  773. public function testSetDataMatchesAgainstDataClass_succeeds()
  774. {
  775. $form = $this->factory->getForm('author', array(
  776. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  777. ));
  778. $form->setData(new Author());
  779. }
  780. public function testSetDataToNull()
  781. {
  782. $form = $this->factory->getForm('author');
  783. $form->setData(null);
  784. $this->assertNull($form->getData());
  785. }
  786. public function testSetDataToNullCreatesObjectIfClassAvailable()
  787. {
  788. $form = $this->factory->getForm('author', array(
  789. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  790. ));
  791. $form->setData(null);
  792. $this->assertEquals(new Author(), $form->getData());
  793. }
  794. public function testSetDataToNullUsesDataConstructorOption()
  795. {
  796. $author = new Author();
  797. $form = $this->factory->getForm('author', array(
  798. 'data_constructor' => function () use ($author) {
  799. return $author;
  800. }
  801. ));
  802. $form->setData(null);
  803. $this->assertSame($author, $form->getData());
  804. }
  805. public function testSubmitUpdatesTransformedDataFromAllFields()
  806. {
  807. $originalAuthor = new Author();
  808. $transformedAuthor = new Author();
  809. // the authors should differ to make sure the test works
  810. $transformedAuthor->firstName = 'Foo';
  811. $form = new TestForm('author', array('validator' => $this->validator));
  812. $transformer = $this->createMockTransformer();
  813. $transformer->expects($this->exactly(2))
  814. ->method('transform')
  815. // the method is first called with NULL, then
  816. // with $originalAuthor -> not testable by PHPUnit
  817. // ->with($this->equalTo(null))
  818. // ->with($this->equalTo($originalAuthor))
  819. ->will($this->returnValue($transformedAuthor));
  820. $form->setValueTransformer($transformer);
  821. $form->setData($originalAuthor);
  822. $field = $this->createMockField('firstName');
  823. $field->expects($this->once())
  824. ->method('writeProperty')
  825. ->with($this->equalTo($transformedAuthor));
  826. $form->add($field);
  827. $field = $this->createMockField('lastName');
  828. $field->expects($this->once())
  829. ->method('writeProperty')
  830. ->with($this->equalTo($transformedAuthor));
  831. $form->add($field);
  832. $form->submit(array()); // irrelevant
  833. }
  834. public function testGetDataReturnsObject()
  835. {
  836. $form = $this->factory->getForm('author');
  837. $object = new \stdClass();
  838. $form->setData($object);
  839. $this->assertEquals($object, $form->getData());
  840. }
  841. public function testGetDisplayedDataForwardsCall()
  842. {
  843. $field = $this->createValidMockField('firstName');
  844. $field->expects($this->atLeastOnce())
  845. ->method('getDisplayedData')
  846. ->will($this->returnValue('Bernhard'));
  847. $form = $this->factory->getForm('author');
  848. $form->add($field);
  849. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getDisplayedData());
  850. }
  851. public function testIsMultipartIfAnyFieldIsMultipart()
  852. {
  853. $form = $this->factory->getForm('author');
  854. $form->add($this->createMultipartMockField('firstName'));
  855. $form->add($this->createNonMultipartMockField('lastName'));
  856. $this->assertTrue($form->isMultipart());
  857. }
  858. public function testIsNotMultipartIfNoFieldIsMultipart()
  859. {
  860. $form = $this->factory->getForm('author');
  861. $form->add($this->createNonMultipartMockField('firstName'));
  862. $form->add($this->createNonMultipartMockField('lastName'));
  863. $this->assertFalse($form->isMultipart());
  864. }
  865. public function testSupportsClone()
  866. {
  867. $form = $this->factory->getForm('author');
  868. $form->add($this->createMockField('firstName'));
  869. $clone = clone $form;
  870. $this->assertNotSame($clone['firstName'], $form['firstName']);
  871. }
  872. public function testSubmitWithoutPriorSetData()
  873. {
  874. return; // TODO
  875. $field = $this->createMockField('firstName');
  876. $field->expects($this->any())
  877. ->method('getData')
  878. ->will($this->returnValue('Bernhard'));
  879. $form = $this->factory->getForm('author');
  880. $form->add($field);
  881. $form->submit(array('firstName' => 'Bernhard'));
  882. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getData());
  883. }
  884. public function testGetHiddenFieldsReturnsOnlyHiddenFields()
  885. {
  886. $form = $this->getGroupWithBothVisibleAndHiddenField();
  887. $hiddenFields = $form->getHiddenFields(true, false);
  888. $this->assertSame(array($form['hiddenField']), $hiddenFields);
  889. }
  890. public function testGetVisibleFieldsReturnsOnlyVisibleFields()
  891. {
  892. $form = $this->getGroupWithBothVisibleAndHiddenField();
  893. $visibleFields = $form->getVisibleFields(true, false);
  894. $this->assertSame(array($form['visibleField']), $visibleFields);
  895. }
  896. public function testValidateData()
  897. {
  898. $graphWalker = $this->createMockGraphWalker();
  899. $metadataFactory = $this->createMockMetadataFactory();
  900. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  901. $object = $this->getMock('\stdClass');
  902. $form = $this->factory->getForm('author', array('validation_groups' => array(
  903. 'group1',
  904. 'group2',
  905. )));
  906. $graphWalker->expects($this->exactly(2))
  907. ->method('walkReference')
  908. ->with($object,
  909. // should test for groups - PHPUnit limitation
  910. $this->anything(),
  911. 'data',
  912. true);
  913. $form->setData($object);
  914. $form->validateData($context);
  915. }
  916. public function testValidateDataAppendsPropertyPath()
  917. {
  918. $graphWalker = $this->createMockGraphWalker();
  919. $metadataFactory = $this->createMockMetadataFactory();
  920. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  921. $context->setPropertyPath('path');
  922. $object = $this->getMock('\stdClass');
  923. $form = $this->factory->getForm('author');
  924. $graphWalker->expects($this->once())
  925. ->method('walkReference')
  926. ->with($object,
  927. null,
  928. 'path.data',
  929. true);
  930. $form->setData($object);
  931. $form->validateData($context);
  932. }
  933. public function testValidateDataSetsCurrentPropertyToData()
  934. {
  935. $graphWalker = $this->createMockGraphWalker();
  936. $metadataFactory = $this->createMockMetadataFactory();
  937. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  938. $object = $this->getMock('\stdClass');
  939. $form = $this->factory->getForm('author');
  940. $test = $this;
  941. $graphWalker->expects($this->once())
  942. ->method('walkReference')
  943. ->will($this->returnCallback(function () use ($context, $test) {
  944. $test->assertEquals('data', $context->getCurrentProperty());
  945. }));
  946. $form->setData($object);
  947. $form->validateData($context);
  948. }
  949. public function testValidateDataDoesNotWalkScalars()
  950. {
  951. $graphWalker = $this->createMockGraphWalker();
  952. $metadataFactory = $this->createMockMetadataFactory();
  953. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  954. $valueTransformer = $this->createMockTransformer();
  955. $form = $this->factory->getForm('author', array('value_transformer' => $valueTransformer));
  956. $graphWalker->expects($this->never())
  957. ->method('walkReference');
  958. $valueTransformer->expects($this->atLeastOnce())
  959. ->method('reverseTransform')
  960. ->will($this->returnValue('foobar'));
  961. $form->submit(array('foo' => 'bar')); // reverse transformed to "foobar"
  962. $form->validateData($context);
  963. }
  964. public function testSubformDoesntCallSetters()
  965. {
  966. $author = new FormTest_AuthorWithoutRefSetter(new Author());
  967. $form = $this->factory->getForm('author', array('validator' => $this->createMockValidator()));
  968. $form->setData($author);
  969. $refForm = $this->factory->getForm('reference');
  970. $refForm->add(new Field('firstName'));
  971. $form->add($refForm);
  972. $form->bind($this->createPostRequest(array(
  973. 'author' => array(
  974. // reference has a getter, but not setter
  975. 'reference' => array(
  976. 'firstName' => 'Foo',
  977. )
  978. )
  979. )));
  980. $this->assertEquals('Foo', $author->getReference()->firstName);
  981. }
  982. public function testSubformCallsSettersIfTheObjectChanged()
  983. {
  984. // no reference
  985. $author = new FormTest_AuthorWithoutRefSetter(null);
  986. $newReference = new Author();
  987. $form = $this->factory->getForm('author', array('validator' => $this->createMockValidator()));
  988. $form->setData($author);
  989. $refForm = $this->factory->getForm('referenceCopy');
  990. $refForm->add(new Field('firstName'));
  991. $form->add($refForm);
  992. $refForm->setData($newReference); // new author object
  993. $form->bind($this->createPostRequest(array(
  994. 'author' => array(
  995. // referenceCopy has a getter that returns a copy
  996. 'referenceCopy' => array(
  997. 'firstName' => 'Foo',
  998. )
  999. )
  1000. )));
  1001. $this->assertEquals('Foo', $author->getReferenceCopy()->firstName);
  1002. }
  1003. public function testSubformCallsSettersIfByReferenceIsFalse()
  1004. {
  1005. $author = new FormTest_AuthorWithoutRefSetter(new Author());
  1006. $form = $this->factory->getForm('author', array('validator' => $this->createMockValidator()));
  1007. $form->setData($author);
  1008. $refForm = $this->factory->getForm('referenceCopy', array('by_reference' => false));
  1009. $refForm->add(new Field('firstName'));
  1010. $form->add($refForm);
  1011. $form->bind($this->createPostRequest(array(
  1012. 'author' => array(
  1013. // referenceCopy has a getter that returns a copy
  1014. 'referenceCopy' => array(
  1015. 'firstName' => 'Foo',
  1016. )
  1017. )
  1018. )));
  1019. // firstName can only be updated if setReferenceCopy() was called
  1020. $this->assertEquals('Foo', $author->getReferenceCopy()->firstName);
  1021. }
  1022. public function testSubformCallsSettersIfReferenceIsScalar()
  1023. {
  1024. $author = new FormTest_AuthorWithoutRefSetter('scalar');
  1025. $form = $this->factory->getForm('author', array('validator' => $this->createMockValidator()));
  1026. $form->setData($author);
  1027. $refForm = new FormTest_FormThatReturns('referenceCopy');
  1028. $refForm->setReturnValue('foobar');
  1029. $form->add($refForm);
  1030. $form->bind($this->createPostRequest(array(
  1031. 'author' => array(
  1032. 'referenceCopy' => array(), // doesn't matter actually
  1033. )
  1034. )));
  1035. // firstName can only be updated if setReferenceCopy() was called
  1036. $this->assertEquals('foobar', $author->getReferenceCopy());
  1037. }
  1038. public function testSubformAlwaysInsertsIntoArrays()
  1039. {
  1040. $ref1 = new Author();
  1041. $ref2 = new Author();
  1042. $author = array('referenceCopy' => $ref1);
  1043. $form = $this->factory->getForm('author', array('validator' => $this->createMockValidator()));
  1044. $form->setData($author);
  1045. $refForm = new FormTest_FormThatReturns('referenceCopy');
  1046. $refForm->setReturnValue($ref2);
  1047. $form->add($refForm);
  1048. $form->bind($this->createPostRequest(array(
  1049. 'author' => array(
  1050. 'referenceCopy' => array(), // doesn't matter actually
  1051. )
  1052. )));
  1053. // the new reference was inserted into the array
  1054. $author = $form->getData();
  1055. $this->assertSame($ref2, $author['referenceCopy']);
  1056. }
  1057. public function testIsEmptyReturnsTrueIfAllFieldsAreEmpty()
  1058. {
  1059. $form = $this->factory->getForm();
  1060. $field1 = new Field('foo');
  1061. $field1->setData('');
  1062. $field2 = new Field('bar');
  1063. $field2->setData(null);
  1064. $form->add($field1);
  1065. $form->add($field2);
  1066. $this->assertTrue($form->isEmpty());
  1067. }
  1068. public function testIsEmptyReturnsFalseIfAnyFieldIsFilled()
  1069. {
  1070. $form = $this->factory->getForm();
  1071. $field1 = new Field('foo');
  1072. $field1->setData('baz');
  1073. $field2 = new Field('bar');
  1074. $field2->setData(null);
  1075. $form->add($field1);
  1076. $form->add($field2);
  1077. $this->assertFalse($form->isEmpty());
  1078. }
  1079. /**
  1080. * Create a group containing two fields, "visibleField" and "hiddenField"
  1081. *
  1082. * @return Form
  1083. */
  1084. protected function getGroupWithBothVisibleAndHiddenField()
  1085. {
  1086. $form = $this->factory->getForm('testGroup');
  1087. // add a visible field
  1088. $visibleField = $this->createMockField('visibleField');
  1089. $visibleField->expects($this->once())
  1090. ->method('isHidden')
  1091. ->will($this->returnValue(false));
  1092. $form->add($visibleField);
  1093. // add a hidden field
  1094. $hiddenField = $this->createMockField('hiddenField');
  1095. $hiddenField->expects($this->once())
  1096. ->method('isHidden')
  1097. ->will($this->returnValue(true));
  1098. $form->add($hiddenField);
  1099. return $form;
  1100. }
  1101. protected function createMockField($key)
  1102. {
  1103. $field = $this->getMock(
  1104. 'Symfony\Component\Form\FieldInterface',
  1105. array(),
  1106. array(),
  1107. '',
  1108. false, // don't use constructor
  1109. false // don't call parent::__clone
  1110. );
  1111. $field->expects($this->any())
  1112. ->method('getKey')
  1113. ->will($this->returnValue($key));
  1114. return $field;
  1115. }
  1116. protected function createMockForm()
  1117. {
  1118. $form = $this->getMock(
  1119. 'Symfony\Component\Form\Form',
  1120. array(),
  1121. array(),
  1122. '',
  1123. false, // don't use constructor
  1124. false // don't call parent::__clone)
  1125. );
  1126. $form->expects($this->any())
  1127. ->method('getRoot')
  1128. ->will($this->returnValue($form));
  1129. return $form;
  1130. }
  1131. protected function createInvalidMockField($key)
  1132. {
  1133. $field = $this->createMockField($key);
  1134. $field->expects($this->any())
  1135. ->method('isValid')
  1136. ->will($this->returnValue(false));
  1137. return $field;
  1138. }
  1139. protected function createValidMockField($key)
  1140. {
  1141. $field = $this->createMockField($key);
  1142. $field->expects($this->any())
  1143. ->method('isValid')
  1144. ->will($this->returnValue(true));
  1145. return $field;
  1146. }
  1147. protected function createNonMultipartMockField($key)
  1148. {
  1149. $field = $this->createMockField($key);
  1150. $field->expects($this->any())
  1151. ->method('isMultipart')
  1152. ->will($this->returnValue(false));
  1153. return $field;
  1154. }
  1155. protected function createMultipartMockField($key)
  1156. {
  1157. $field = $this->createMockField($key);
  1158. $field->expects($this->any())
  1159. ->method('isMultipart')
  1160. ->will($this->returnValue(true));
  1161. return $field;
  1162. }
  1163. protected function createMockTransformer()
  1164. {
  1165. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  1166. }
  1167. protected function createMockValidator()
  1168. {
  1169. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  1170. }
  1171. protected function createMockCsrfProvider()
  1172. {
  1173. return $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface');
  1174. }
  1175. protected function createMockGraphWalker()
  1176. {
  1177. return $this->getMockBuilder('Symfony\Component\Validator\GraphWalker')
  1178. ->disableOriginalConstructor()
  1179. ->getMock();
  1180. }
  1181. protected function createMockMetadataFactory()
  1182. {
  1183. return $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  1184. }
  1185. protected function createPostRequest(array $values = array(), array $files = array())
  1186. {
  1187. $server = array('REQUEST_METHOD' => 'POST');
  1188. return new Request(array(), $values, array(), array(), $files, $server);
  1189. }
  1190. }