FormTest.php 46 KB

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