FormTest.php 47 KB

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