FormTest.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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->getInstance('field', '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->getInstance('form', 'author', array('validator' => $this->validator));
  92. }
  93. public function testNoCsrfProtectionByDefault()
  94. {
  95. $this->markTestSkipped('CSRF protection needs to be fixed');
  96. $form = $this->factory->getInstance('form', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', 'author', array(
  182. 'validation_groups' => 'group',
  183. ));
  184. $this->assertEquals(array('group'), $form->getValidationGroups());
  185. }
  186. public function testValidationGroupsCanBeSetToArray()
  187. {
  188. $form = $this->factory->getInstance('form', '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->getInstance('form', 'parent', array(
  196. 'validation_groups' => 'group',
  197. ));
  198. $childForm = $this->factory->getInstance('form', 'child');
  199. $parentForm->add($childForm);
  200. $this->assertEquals(array('group'), $childForm->getValidationGroups());
  201. }
  202. public function testValidationGroupsAreNotInheritedFromParentIfSet()
  203. {
  204. $parentForm = $this->factory->getInstance('form', 'parent', array(
  205. 'validation_groups' => 'group1',
  206. ));
  207. $childForm = $this->factory->getInstance('form', '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->getInstance('form', null, array('validator' => $this->createMockValidator()));
  219. $form->bind($this->createPostRequest());
  220. }
  221. public function testBindValidatesData()
  222. {
  223. $form = $this->factory->getInstance('form', 'author', array(
  224. 'validation_groups' => 'group',
  225. 'validator' => $this->validator,
  226. ));
  227. $form->add($this->factory->getInstance('field', '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->getInstance('form', 'author', array(
  237. 'validator' => $this->validator,
  238. ));
  239. $form->add($this->factory->getInstance('field', '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->getInstance('form', 'author', array('validator' => $this->validator));
  266. $form->add($this->factory->getInstance('field', 'name'));
  267. $imageForm = $this->factory->getInstance('form', 'image');
  268. $imageForm->add($this->factory->getInstance('field', 'file'));
  269. $imageForm->add($this->factory->getInstance('field', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', '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->getInstance('form', 'author');
  325. $this->setExpectedException('LogicException');
  326. $form[] = $this->createMockField('lastName');
  327. }
  328. public function testSupportsCountable()
  329. {
  330. $form = $this->factory->getInstance('form', 'group', array(
  331. 'csrf_protection' => false,
  332. ));
  333. $form->add($this->createMockField('firstName'));
  334. $form->add($this->createMockField('lastName'));
  335. $this->assertEquals(2, count($form));
  336. $form->add($this->createMockField('australian'));
  337. $this->assertEquals(3, count($form));
  338. }
  339. public function testSupportsIterable()
  340. {
  341. $form = $this->factory->getInstance('form', 'group', array(
  342. 'csrf_protection' => false,
  343. ));
  344. $form->add($field1 = $this->createMockField('field1'));
  345. $form->add($field2 = $this->createMockField('field2'));
  346. $form->add($field3 = $this->createMockField('field3'));
  347. $expected = array(
  348. 'field1' => $field1,
  349. 'field2' => $field2,
  350. 'field3' => $field3,
  351. );
  352. $this->assertEquals($expected, iterator_to_array($form));
  353. }
  354. public function testIsSubmitted()
  355. {
  356. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->validator));
  357. $this->assertFalse($form->isSubmitted());
  358. $form->submit(array('firstName' => 'Bernhard'));
  359. $this->assertTrue($form->isSubmitted());
  360. }
  361. public function testValidIfAllFieldsAreValid()
  362. {
  363. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->validator));
  364. $form->add($this->createValidMockField('firstName'));
  365. $form->add($this->createValidMockField('lastName'));
  366. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  367. $this->assertTrue($form->isValid());
  368. }
  369. public function testInvalidIfFieldIsInvalid()
  370. {
  371. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->validator));
  372. $form->add($this->createInvalidMockField('firstName'));
  373. $form->add($this->createValidMockField('lastName'));
  374. $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  375. $this->assertFalse($form->isValid());
  376. }
  377. public function testInvalidIfSubmittedWithExtraFields()
  378. {
  379. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->validator));
  380. $form->add($this->createValidMockField('firstName'));
  381. $form->add($this->createValidMockField('lastName'));
  382. $form->submit(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  383. $this->assertTrue($form->isSubmittedWithExtraFields());
  384. }
  385. public function testHasNoErrorsIfOnlyFieldHasErrors()
  386. {
  387. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->validator));
  388. $form->add($this->createInvalidMockField('firstName'));
  389. $form->submit(array('firstName' => 'Bernhard'));
  390. $this->assertFalse($form->hasErrors());
  391. }
  392. public function testSubmitForwardsNullIfValueIsMissing()
  393. {
  394. $field = $this->createMockField('firstName');
  395. $field->expects($this->once())
  396. ->method('submit')
  397. ->with($this->equalTo(null));
  398. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->validator));
  399. $form->add($field);
  400. $form->submit(array());
  401. }
  402. public function testAddErrorMapsFieldValidationErrorsOntoFields()
  403. {
  404. $error = new FieldError('Message');
  405. $field = $this->createMockField('firstName');
  406. $field->expects($this->once())
  407. ->method('addError')
  408. ->with($this->equalTo($error));
  409. $form = $this->factory->getInstance('form', 'author');
  410. $form->add($field);
  411. $path = new PropertyPath('fields[firstName].data');
  412. $form->addError(new FieldError('Message'), $path->getIterator());
  413. }
  414. public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedForms()
  415. {
  416. $error = new FieldError('Message');
  417. $field = $this->createMockField('firstName');
  418. $field->expects($this->once())
  419. ->method('addError')
  420. ->with($this->equalTo($error));
  421. $form = $this->factory->getInstance('form', 'author');
  422. $innerGroup = $this->factory->getInstance('form', 'names');
  423. $innerGroup->add($field);
  424. $form->add($innerGroup);
  425. $path = new PropertyPath('fields[names].fields[firstName].data');
  426. $form->addError(new FieldError('Message'), $path->getIterator());
  427. }
  428. public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
  429. {
  430. $field = $this->createMockField('foo');
  431. $field->expects($this->never())
  432. ->method('addError');
  433. $form = $this->factory->getInstance('form', 'author');
  434. $form->add($field);
  435. $path = new PropertyPath('fields[bar].data');
  436. $form->addError(new FieldError('Message'), $path->getIterator());
  437. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  438. }
  439. public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
  440. {
  441. $field = $this->createMockField('firstName');
  442. $field->expects($this->any())
  443. ->method('isHidden')
  444. ->will($this->returnValue(true));
  445. $field->expects($this->never())
  446. ->method('addError');
  447. $form = $this->factory->getInstance('form', 'author');
  448. $form->add($field);
  449. $path = new PropertyPath('fields[firstName].data');
  450. $form->addError(new FieldError('Message'), $path->getIterator());
  451. $this->assertEquals(array(new FieldError('Message')), $form->getErrors());
  452. }
  453. public function testAddErrorMapsDataValidationErrorsOntoFields()
  454. {
  455. $error = new DataError('Message');
  456. // path is expected to point at "firstName"
  457. $expectedPath = new PropertyPath('firstName');
  458. $expectedPathIterator = $expectedPath->getIterator();
  459. $field = $this->createMockField('firstName');
  460. $field->expects($this->any())
  461. ->method('getPropertyPath')
  462. ->will($this->returnValue(new PropertyPath('firstName')));
  463. $field->expects($this->once())
  464. ->method('addError')
  465. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  466. $form = $this->factory->getInstance('form', 'author');
  467. $form->add($field);
  468. $path = new PropertyPath('firstName');
  469. $form->addError($error, $path->getIterator());
  470. }
  471. public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
  472. {
  473. $field = $this->createMockField('foo');
  474. $field->expects($this->any())
  475. ->method('getPropertyPath')
  476. ->will($this->returnValue(new PropertyPath('foo')));
  477. $field->expects($this->never())
  478. ->method('addError');
  479. $form = $this->factory->getInstance('form', 'author');
  480. $form->add($field);
  481. $path = new PropertyPath('bar');
  482. $form->addError(new DataError('Message'), $path->getIterator());
  483. }
  484. public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
  485. {
  486. $field = $this->createMockField('firstName');
  487. $field->expects($this->any())
  488. ->method('isHidden')
  489. ->will($this->returnValue(true));
  490. $field->expects($this->any())
  491. ->method('getPropertyPath')
  492. ->will($this->returnValue(new PropertyPath('firstName')));
  493. $field->expects($this->never())
  494. ->method('addError');
  495. $form = $this->factory->getInstance('form', 'author');
  496. $form->add($field);
  497. $path = new PropertyPath('firstName');
  498. $form->addError(new DataError('Message'), $path->getIterator());
  499. }
  500. public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
  501. {
  502. $error = new DataError('Message');
  503. // path is expected to point at "street"
  504. $expectedPath = new PropertyPath('address.street');
  505. $expectedPathIterator = $expectedPath->getIterator();
  506. $expectedPathIterator->next();
  507. $field = $this->createMockField('address');
  508. $field->expects($this->any())
  509. ->method('getPropertyPath')
  510. ->will($this->returnValue(new PropertyPath('address')));
  511. $field->expects($this->once())
  512. ->method('addError')
  513. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  514. $form = $this->factory->getInstance('form', 'author');
  515. $form->add($field);
  516. $path = new PropertyPath('address.street');
  517. $form->addError($error, $path->getIterator());
  518. }
  519. public function testAddErrorMapsErrorsOntoFieldsInVirtualGroups()
  520. {
  521. $error = new DataError('Message');
  522. // path is expected to point at "address"
  523. $expectedPath = new PropertyPath('address');
  524. $expectedPathIterator = $expectedPath->getIterator();
  525. $field = $this->createMockField('address');
  526. $field->expects($this->any())
  527. ->method('getPropertyPath')
  528. ->will($this->returnValue(new PropertyPath('address')));
  529. $field->expects($this->once())
  530. ->method('addError')
  531. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator));
  532. $form = $this->factory->getInstance('form', 'author');
  533. $nestedForm = $this->factory->getInstance('form', 'nested', array('virtual' => true));
  534. $nestedForm->add($field);
  535. $form->add($nestedForm);
  536. $path = new PropertyPath('address');
  537. $form->addError($error, $path->getIterator());
  538. }
  539. public function testAddThrowsExceptionIfAlreadySubmitted()
  540. {
  541. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->validator));
  542. $form->add($this->createMockField('firstName'));
  543. $form->submit(array());
  544. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadySubmittedException');
  545. $form->add($this->createMockField('lastName'));
  546. }
  547. public function testAddSetsFieldParent()
  548. {
  549. $form = $this->factory->getInstance('form', 'author');
  550. $field = $this->createMockField('firstName');
  551. $field->expects($this->once())
  552. ->method('setParent')
  553. ->with($this->equalTo($form));
  554. $form->add($field);
  555. }
  556. public function testRemoveUnsetsFieldParent()
  557. {
  558. $form = $this->factory->getInstance('form', 'author');
  559. $field = $this->createMockField('firstName');
  560. $field->expects($this->exactly(2))
  561. ->method('setParent');
  562. // PHPUnit fails to compare subsequent method calls with different arguments
  563. $form->add($field);
  564. $form->remove('firstName');
  565. }
  566. public function testAddUpdatesFieldFromTransformedData()
  567. {
  568. $originalAuthor = new Author();
  569. $transformedAuthor = new Author();
  570. // the authors should differ to make sure the test works
  571. $transformedAuthor->firstName = 'Foo';
  572. $form = $this->factory->getInstance('form', 'author');
  573. $transformer = $this->createMockTransformer();
  574. $transformer->expects($this->once())
  575. ->method('transform')
  576. ->with($this->equalTo($originalAuthor))
  577. ->will($this->returnValue($transformedAuthor));
  578. $form->setValueTransformer($transformer);
  579. $form->setData($originalAuthor);
  580. $field = $this->createMockField('firstName');
  581. $field->expects($this->any())
  582. ->method('getPropertyPath')
  583. ->will($this->returnValue(new PropertyPath('firstName')));
  584. $field->expects($this->once())
  585. ->method('readProperty')
  586. ->with($this->equalTo($transformedAuthor));
  587. $form->add($field);
  588. }
  589. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  590. {
  591. $originalAuthor = new Author();
  592. $form = $this->factory->getInstance('form', 'author');
  593. $transformer = $this->createMockTransformer();
  594. $transformer->expects($this->once())
  595. ->method('transform')
  596. ->with($this->equalTo($originalAuthor))
  597. ->will($this->returnValue(''));
  598. $form->setValueTransformer($transformer);
  599. $form->setData($originalAuthor);
  600. $field = $this->createMockField('firstName');
  601. $field->expects($this->never())
  602. ->method('readProperty');
  603. $form->add($field);
  604. }
  605. /**
  606. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  607. */
  608. public function testAddThrowsExceptionIfNoFieldOrString()
  609. {
  610. $form = $this->factory->getInstance('form', 'author');
  611. $form->add(1234);
  612. }
  613. /**
  614. * @expectedException Symfony\Component\Form\Exception\FieldDefinitionException
  615. */
  616. public function testAddThrowsExceptionIfAnonymousField()
  617. {
  618. $form = $this->factory->getInstance('form', 'author');
  619. $field = $this->createMockField('');
  620. $form->add($field);
  621. }
  622. /**
  623. * @expectedException Symfony\Component\Form\Exception\FormException
  624. */
  625. public function testAddThrowsExceptionIfStringButNoFieldFactory()
  626. {
  627. $form = $this->factory->getInstance('form', 'author');
  628. $form->add('firstName');
  629. }
  630. public function testSetDataUpdatesAllFieldsFromTransformedData()
  631. {
  632. $originalAuthor = new Author();
  633. $transformedAuthor = new Author();
  634. // the authors should differ to make sure the test works
  635. $transformedAuthor->firstName = 'Foo';
  636. $form = $this->factory->getInstance('form', 'author');
  637. $transformer = $this->createMockTransformer();
  638. $transformer->expects($this->once())
  639. ->method('transform')
  640. ->with($this->equalTo($originalAuthor))
  641. ->will($this->returnValue($transformedAuthor));
  642. $form->setValueTransformer($transformer);
  643. $field = $this->createMockField('firstName');
  644. $field->expects($this->once())
  645. ->method('readProperty')
  646. ->with($this->equalTo($transformedAuthor));
  647. $form->add($field);
  648. $field = $this->createMockField('lastName');
  649. $field->expects($this->once())
  650. ->method('readProperty')
  651. ->with($this->equalTo($transformedAuthor));
  652. $form->add($field);
  653. $form->setData($originalAuthor);
  654. }
  655. /**
  656. * The use case for this test are groups whose fields should be mapped
  657. * directly onto properties of the form's object.
  658. *
  659. * Example:
  660. *
  661. * <code>
  662. * $dateRangeField = $this->factory->getInstance('form', 'dateRange');
  663. * $dateRangeField->add(new DateField('startDate'));
  664. * $dateRangeField->add(new DateField('endDate'));
  665. * $form->add($dateRangeField);
  666. * </code>
  667. *
  668. * If $dateRangeField is not virtual, the property "dateRange" must be
  669. * present on the form's object. In this property, an object or array
  670. * with the properties "startDate" and "endDate" is expected.
  671. *
  672. * If $dateRangeField is virtual though, it's children are mapped directly
  673. * onto the properties "startDate" and "endDate" of the form's object.
  674. */
  675. public function testSetDataSkipsVirtualForms()
  676. {
  677. $author = new Author();
  678. $author->firstName = 'Foo';
  679. $form = $this->factory->getInstance('form', 'author');
  680. $nestedForm = $this->factory->getInstance('form', 'personal_data', array(
  681. 'virtual' => true,
  682. ));
  683. // both fields are in the nested group but receive the object of the
  684. // top-level group because the nested group is virtual
  685. $field = $this->createMockField('firstName');
  686. $field->expects($this->once())
  687. ->method('readProperty')
  688. ->with($this->equalTo($author));
  689. $nestedForm->add($field);
  690. $field = $this->createMockField('lastName');
  691. $field->expects($this->once())
  692. ->method('readProperty')
  693. ->with($this->equalTo($author));
  694. $nestedForm->add($field);
  695. $form->add($nestedForm);
  696. $form->setData($author);
  697. }
  698. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  699. {
  700. $form = $this->factory->getInstance('form', 'author');
  701. $this->setExpectedException('InvalidArgumentException');
  702. $form->setData('foobar');
  703. }
  704. /**
  705. * @expectedException Symfony\Component\Form\Exception\FormException
  706. */
  707. public function testSetDataMatchesAgainstDataClass_fails()
  708. {
  709. $form = $this->factory->getInstance('form', 'author', array(
  710. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  711. ));
  712. $form->setData(new \stdClass());
  713. }
  714. public function testSetDataMatchesAgainstDataClass_succeeds()
  715. {
  716. $form = $this->factory->getInstance('form', 'author', array(
  717. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  718. ));
  719. $form->setData(new Author());
  720. }
  721. public function testSetDataToNullCreatesObjectIfClassAvailable()
  722. {
  723. $form = $this->factory->getInstance('form', 'author', array(
  724. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  725. ));
  726. $form->setData(null);
  727. $this->assertEquals(new Author(), $form->getData());
  728. }
  729. public function testSetDataToNullUsesDataConstructorOption()
  730. {
  731. $author = new Author();
  732. $form = $this->factory->getInstance('form', 'author', array(
  733. 'data_constructor' => function () use ($author) {
  734. return $author;
  735. }
  736. ));
  737. $form->setData(null);
  738. $this->assertSame($author, $form->getData());
  739. }
  740. /*
  741. * We need something to write the field values into
  742. */
  743. public function testSetDataToNullCreatesArrayIfNoDataClassOrConstructor()
  744. {
  745. $author = new Author();
  746. $form = $this->factory->getInstance('form', 'author');
  747. $form->setData(null);
  748. $this->assertSame(array(), $form->getData());
  749. }
  750. public function testSubmitUpdatesTransformedDataFromAllFields()
  751. {
  752. $originalAuthor = new Author();
  753. $transformedAuthor = new Author();
  754. // the authors should differ to make sure the test works
  755. $transformedAuthor->firstName = 'Foo';
  756. $form = $this->factory->getInstance('form', 'author', array(
  757. 'validator' => $this->validator,
  758. ));
  759. $transformer = $this->createMockTransformer();
  760. $transformer->expects($this->exactly(2))
  761. ->method('transform')
  762. // the method is first called with NULL, then
  763. // with $originalAuthor -> not testable by PHPUnit
  764. // ->with($this->equalTo(null))
  765. // ->with($this->equalTo($originalAuthor))
  766. ->will($this->returnValue($transformedAuthor));
  767. $form->setValueTransformer($transformer);
  768. $form->setData($originalAuthor);
  769. $field = $this->createMockField('firstName');
  770. $field->expects($this->once())
  771. ->method('writeProperty')
  772. ->with($this->equalTo($transformedAuthor));
  773. $form->add($field);
  774. $field = $this->createMockField('lastName');
  775. $field->expects($this->once())
  776. ->method('writeProperty')
  777. ->with($this->equalTo($transformedAuthor));
  778. $form->add($field);
  779. $form->submit(array()); // irrelevant
  780. }
  781. public function testGetDataReturnsObject()
  782. {
  783. $form = $this->factory->getInstance('form', 'author');
  784. $object = new \stdClass();
  785. $form->setData($object);
  786. $this->assertEquals($object, $form->getData());
  787. }
  788. public function testGetDisplayedDataForwardsCall()
  789. {
  790. $field = $this->createValidMockField('firstName');
  791. $field->expects($this->atLeastOnce())
  792. ->method('getDisplayedData')
  793. ->will($this->returnValue('Bernhard'));
  794. $form = $this->factory->getInstance('form', 'author', array(
  795. 'csrf_protection' => false,
  796. ));
  797. $form->add($field);
  798. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getDisplayedData());
  799. }
  800. public function testIsMultipartIfAnyFieldIsMultipart()
  801. {
  802. $form = $this->factory->getInstance('form', 'author');
  803. $form->add($this->createMultipartMockField('firstName'));
  804. $form->add($this->createNonMultipartMockField('lastName'));
  805. $this->assertTrue($form->isMultipart());
  806. }
  807. public function testIsNotMultipartIfNoFieldIsMultipart()
  808. {
  809. $form = $this->factory->getInstance('form', 'author');
  810. $form->add($this->createNonMultipartMockField('firstName'));
  811. $form->add($this->createNonMultipartMockField('lastName'));
  812. $this->assertFalse($form->isMultipart());
  813. }
  814. public function testSubmitWithoutPriorSetData()
  815. {
  816. return; // TODO
  817. $field = $this->createMockField('firstName');
  818. $field->expects($this->any())
  819. ->method('getData')
  820. ->will($this->returnValue('Bernhard'));
  821. $form = $this->factory->getInstance('form', 'author');
  822. $form->add($field);
  823. $form->submit(array('firstName' => 'Bernhard'));
  824. $this->assertEquals(array('firstName' => 'Bernhard'), $form->getData());
  825. }
  826. public function testValidateData()
  827. {
  828. $graphWalker = $this->createMockGraphWalker();
  829. $metadataFactory = $this->createMockMetadataFactory();
  830. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  831. $object = $this->getMock('\stdClass');
  832. $form = $this->factory->getInstance('form', 'author', array('validation_groups' => array(
  833. 'group1',
  834. 'group2',
  835. )));
  836. $graphWalker->expects($this->exactly(2))
  837. ->method('walkReference')
  838. ->with($object,
  839. // should test for groups - PHPUnit limitation
  840. $this->anything(),
  841. 'data',
  842. true);
  843. $form->setData($object);
  844. $form->validateData($context);
  845. }
  846. public function testValidateDataAppendsPropertyPath()
  847. {
  848. $graphWalker = $this->createMockGraphWalker();
  849. $metadataFactory = $this->createMockMetadataFactory();
  850. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  851. $context->setPropertyPath('path');
  852. $object = $this->getMock('\stdClass');
  853. $form = $this->factory->getInstance('form', 'author');
  854. $graphWalker->expects($this->once())
  855. ->method('walkReference')
  856. ->with($object,
  857. null,
  858. 'path.data',
  859. true);
  860. $form->setData($object);
  861. $form->validateData($context);
  862. }
  863. public function testValidateDataSetsCurrentPropertyToData()
  864. {
  865. $graphWalker = $this->createMockGraphWalker();
  866. $metadataFactory = $this->createMockMetadataFactory();
  867. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  868. $object = $this->getMock('\stdClass');
  869. $form = $this->factory->getInstance('form', 'author');
  870. $test = $this;
  871. $graphWalker->expects($this->once())
  872. ->method('walkReference')
  873. ->will($this->returnCallback(function () use ($context, $test) {
  874. $test->assertEquals('data', $context->getCurrentProperty());
  875. }));
  876. $form->setData($object);
  877. $form->validateData($context);
  878. }
  879. public function testValidateDataDoesNotWalkScalars()
  880. {
  881. $graphWalker = $this->createMockGraphWalker();
  882. $metadataFactory = $this->createMockMetadataFactory();
  883. $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
  884. $valueTransformer = $this->createMockTransformer();
  885. $form = $this->factory->getInstance('form', 'author', array('value_transformer' => $valueTransformer));
  886. $graphWalker->expects($this->never())
  887. ->method('walkReference');
  888. $valueTransformer->expects($this->atLeastOnce())
  889. ->method('reverseTransform')
  890. ->will($this->returnValue('foobar'));
  891. $form->submit(array('foo' => 'bar')); // reverse transformed to "foobar"
  892. $form->validateData($context);
  893. }
  894. public function testSubformDoesntCallSetters()
  895. {
  896. $author = new FormTest_AuthorWithoutRefSetter(new Author());
  897. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->createMockValidator()));
  898. $form->setData($author);
  899. $refForm = $this->factory->getInstance('form', 'reference');
  900. $refForm->add($this->factory->getInstance('field', 'firstName'));
  901. $form->add($refForm);
  902. $form->bind($this->createPostRequest(array(
  903. 'author' => array(
  904. // reference has a getter, but not setter
  905. 'reference' => array(
  906. 'firstName' => 'Foo',
  907. )
  908. )
  909. )));
  910. $this->assertEquals('Foo', $author->getReference()->firstName);
  911. }
  912. public function testSubformCallsSettersIfTheObjectChanged()
  913. {
  914. // no reference
  915. $author = new FormTest_AuthorWithoutRefSetter(null);
  916. $newReference = new Author();
  917. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->createMockValidator()));
  918. $form->setData($author);
  919. $refForm = $this->factory->getInstance('form', 'referenceCopy');
  920. $refForm->add($this->factory->getInstance('field', 'firstName'));
  921. $form->add($refForm);
  922. $refForm->setData($newReference); // new author object
  923. $form->bind($this->createPostRequest(array(
  924. 'author' => array(
  925. // referenceCopy has a getter that returns a copy
  926. 'referenceCopy' => array(
  927. 'firstName' => 'Foo',
  928. )
  929. )
  930. )));
  931. $this->assertEquals('Foo', $author->getReferenceCopy()->firstName);
  932. }
  933. public function testSubformCallsSettersIfByReferenceIsFalse()
  934. {
  935. $author = new FormTest_AuthorWithoutRefSetter(new Author());
  936. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->createMockValidator()));
  937. $form->setData($author);
  938. $refForm = $this->factory->getInstance('form', 'referenceCopy', array('by_reference' => false));
  939. $refForm->add($this->factory->getInstance('field', 'firstName'));
  940. $form->add($refForm);
  941. $form->bind($this->createPostRequest(array(
  942. 'author' => array(
  943. // referenceCopy has a getter that returns a copy
  944. 'referenceCopy' => array(
  945. 'firstName' => 'Foo',
  946. )
  947. )
  948. )));
  949. // firstName can only be updated if setReferenceCopy() was called
  950. $this->assertEquals('Foo', $author->getReferenceCopy()->firstName);
  951. }
  952. public function testSubformCallsSettersIfReferenceIsScalar()
  953. {
  954. $author = new FormTest_AuthorWithoutRefSetter('scalar');
  955. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->createMockValidator()));
  956. $form->setData($author);
  957. $form->add($this->factory->getInstance('form', 'referenceCopy', array(
  958. 'value_transformer' => new CallbackTransformer(
  959. function () {},
  960. function ($value) { // reverseTransform
  961. return 'foobar';
  962. }
  963. )
  964. )));
  965. $form->bind($this->createPostRequest(array(
  966. 'author' => array(
  967. 'referenceCopy' => array(), // doesn't matter actually
  968. )
  969. )));
  970. // firstName can only be updated if setReferenceCopy() was called
  971. $this->assertEquals('foobar', $author->getReferenceCopy());
  972. }
  973. public function testSubformAlwaysInsertsIntoArrays()
  974. {
  975. $ref1 = new Author();
  976. $ref2 = new Author();
  977. $author = array('referenceCopy' => $ref1);
  978. $form = $this->factory->getInstance('form', 'author', array('validator' => $this->createMockValidator()));
  979. $form->setData($author);
  980. $form->add($this->factory->getInstance('form', 'referenceCopy', array(
  981. 'value_transformer' => new CallbackTransformer(
  982. function () {},
  983. function ($value) use ($ref2) { // reverseTransform
  984. return $ref2;
  985. }
  986. )
  987. )));
  988. $form->bind($this->createPostRequest(array(
  989. 'author' => array(
  990. 'referenceCopy' => array('a' => 'b'), // doesn't matter actually
  991. )
  992. )));
  993. // the new reference was inserted into the array
  994. $author = $form->getData();
  995. $this->assertSame($ref2, $author['referenceCopy']);
  996. }
  997. public function testIsEmptyReturnsTrueIfAllFieldsAreEmpty()
  998. {
  999. $form = $this->factory->getInstance('form', 'name');
  1000. $field1 = $this->factory->getInstance('field', 'foo');
  1001. $field1->setData('');
  1002. $field2 = $this->factory->getInstance('field', 'bar');
  1003. $field2->setData(null);
  1004. $form->add($field1);
  1005. $form->add($field2);
  1006. $this->assertTrue($form->isEmpty());
  1007. }
  1008. public function testIsEmptyReturnsFalseIfAnyFieldIsFilled()
  1009. {
  1010. $form = $this->factory->getInstance('form', 'name');
  1011. $field1 = $this->factory->getInstance('field', 'foo');
  1012. $field1->setData('baz');
  1013. $field2 = $this->factory->getInstance('field', 'bar');
  1014. $field2->setData(null);
  1015. $form->add($field1);
  1016. $form->add($field2);
  1017. $this->assertFalse($form->isEmpty());
  1018. }
  1019. /**
  1020. * Create a group containing two fields, "visibleField" and "hiddenField"
  1021. *
  1022. * @return Form
  1023. */
  1024. protected function getGroupWithBothVisibleAndHiddenField()
  1025. {
  1026. $form = $this->factory->getInstance('form', 'testGroup');
  1027. // add a visible field
  1028. $visibleField = $this->createMockField('visibleField');
  1029. $visibleField->expects($this->once())
  1030. ->method('isHidden')
  1031. ->will($this->returnValue(false));
  1032. $form->add($visibleField);
  1033. // add a hidden field
  1034. $hiddenField = $this->createMockField('hiddenField');
  1035. $hiddenField->expects($this->once())
  1036. ->method('isHidden')
  1037. ->will($this->returnValue(true));
  1038. $form->add($hiddenField);
  1039. return $form;
  1040. }
  1041. protected function createMockField($key)
  1042. {
  1043. $field = $this->getMock(
  1044. 'Symfony\Component\Form\FieldInterface',
  1045. array(),
  1046. array(),
  1047. '',
  1048. false, // don't use constructor
  1049. false // don't call parent::__clone
  1050. );
  1051. $field->expects($this->any())
  1052. ->method('getName')
  1053. ->will($this->returnValue($key));
  1054. return $field;
  1055. }
  1056. protected function createMockForm()
  1057. {
  1058. $form = $this->getMock(
  1059. 'Symfony\Component\Form\Form',
  1060. array(),
  1061. array(),
  1062. '',
  1063. false, // don't use constructor
  1064. false // don't call parent::__clone)
  1065. );
  1066. $form->expects($this->any())
  1067. ->method('getRoot')
  1068. ->will($this->returnValue($form));
  1069. return $form;
  1070. }
  1071. protected function createInvalidMockField($key)
  1072. {
  1073. $field = $this->createMockField($key);
  1074. $field->expects($this->any())
  1075. ->method('isValid')
  1076. ->will($this->returnValue(false));
  1077. return $field;
  1078. }
  1079. protected function createValidMockField($key)
  1080. {
  1081. $field = $this->createMockField($key);
  1082. $field->expects($this->any())
  1083. ->method('isValid')
  1084. ->will($this->returnValue(true));
  1085. return $field;
  1086. }
  1087. protected function createNonMultipartMockField($key)
  1088. {
  1089. $field = $this->createMockField($key);
  1090. $field->expects($this->any())
  1091. ->method('isMultipart')
  1092. ->will($this->returnValue(false));
  1093. return $field;
  1094. }
  1095. protected function createMultipartMockField($key)
  1096. {
  1097. $field = $this->createMockField($key);
  1098. $field->expects($this->any())
  1099. ->method('isMultipart')
  1100. ->will($this->returnValue(true));
  1101. return $field;
  1102. }
  1103. protected function createMockTransformer()
  1104. {
  1105. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  1106. }
  1107. protected function createMockValidator()
  1108. {
  1109. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  1110. }
  1111. protected function createMockCsrfProvider()
  1112. {
  1113. return $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface');
  1114. }
  1115. protected function createMockGraphWalker()
  1116. {
  1117. return $this->getMockBuilder('Symfony\Component\Validator\GraphWalker')
  1118. ->disableOriginalConstructor()
  1119. ->getMock();
  1120. }
  1121. protected function createMockMetadataFactory()
  1122. {
  1123. return $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  1124. }
  1125. protected function createPostRequest(array $values = array(), array $files = array())
  1126. {
  1127. $server = array('REQUEST_METHOD' => 'POST');
  1128. return new Request(array(), $values, array(), array(), $files, $server);
  1129. }
  1130. }