FormTest.php 48 KB

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