FormTest.php 47 KB

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