FieldGroupTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. require_once __DIR__ . '/Fixtures/Author.php';
  4. require_once __DIR__ . '/Fixtures/TestField.php';
  5. require_once __DIR__ . '/Fixtures/TestFieldGroup.php';
  6. use Symfony\Component\Form\Field;
  7. use Symfony\Component\Form\FieldInterface;
  8. use Symfony\Component\Form\FieldGroup;
  9. use Symfony\Component\Form\PropertyPath;
  10. use Symfony\Tests\Component\Form\Fixtures\Author;
  11. use Symfony\Tests\Component\Form\Fixtures\TestField;
  12. use Symfony\Tests\Component\Form\Fixtures\TestFieldGroup;
  13. abstract class FieldGroupTest_Field extends TestField
  14. {
  15. public $locales = array();
  16. public function setLocale($locale)
  17. {
  18. $this->locales[] = $locale;
  19. }
  20. }
  21. class FieldGroupTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function testSupportsArrayAccess()
  24. {
  25. $group = new TestFieldGroup('author');
  26. $group->add($this->createMockField('firstName'));
  27. $this->assertEquals($group->get('firstName'), $group['firstName']);
  28. $this->assertTrue(isset($group['firstName']));
  29. }
  30. public function testSupportsUnset()
  31. {
  32. $group = new TestFieldGroup('author');
  33. $group->add($this->createMockField('firstName'));
  34. unset($group['firstName']);
  35. $this->assertFalse(isset($group['firstName']));
  36. }
  37. public function testDoesNotSupportAddingFields()
  38. {
  39. $group = new TestFieldGroup('author');
  40. $this->setExpectedException('LogicException');
  41. $group[] = $this->createMockField('lastName');
  42. }
  43. public function testSupportsCountable()
  44. {
  45. $group = new TestFieldGroup('group');
  46. $group->add($this->createMockField('firstName'));
  47. $group->add($this->createMockField('lastName'));
  48. $this->assertEquals(2, count($group));
  49. $group->add($this->createMockField('australian'));
  50. $this->assertEquals(3, count($group));
  51. }
  52. public function testSupportsIterable()
  53. {
  54. $group = new TestFieldGroup('group');
  55. $group->add($field1 = $this->createMockField('field1'));
  56. $group->add($field2 = $this->createMockField('field2'));
  57. $group->add($field3 = $this->createMockField('field3'));
  58. $expected = array(
  59. 'field1' => $field1,
  60. 'field2' => $field2,
  61. 'field3' => $field3,
  62. );
  63. $this->assertEquals($expected, iterator_to_array($group));
  64. }
  65. public function testIsBound()
  66. {
  67. $group = new TestFieldGroup('author');
  68. $this->assertFalse($group->isBound());
  69. $group->bind(array('firstName' => 'Bernhard'));
  70. $this->assertTrue($group->isBound());
  71. }
  72. public function testValidIfAllFieldsAreValid()
  73. {
  74. $group = new TestFieldGroup('author');
  75. $group->add($this->createValidMockField('firstName'));
  76. $group->add($this->createValidMockField('lastName'));
  77. $group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  78. $this->assertTrue($group->isValid());
  79. }
  80. public function testInvalidIfFieldIsInvalid()
  81. {
  82. $group = new TestFieldGroup('author');
  83. $group->add($this->createInvalidMockField('firstName'));
  84. $group->add($this->createValidMockField('lastName'));
  85. $group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  86. $this->assertFalse($group->isValid());
  87. }
  88. public function testInvalidIfBoundWithExtraFields()
  89. {
  90. $group = new TestFieldGroup('author');
  91. $group->add($this->createValidMockField('firstName'));
  92. $group->add($this->createValidMockField('lastName'));
  93. $group->bind(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  94. $this->assertTrue($group->isBoundWithExtraFields());
  95. }
  96. public function testHasNoErrorsIfOnlyFieldHasErrors()
  97. {
  98. $group = new TestFieldGroup('author');
  99. $group->add($this->createInvalidMockField('firstName'));
  100. $group->bind(array('firstName' => 'Bernhard'));
  101. $this->assertFalse($group->hasErrors());
  102. }
  103. public function testBindForwardsBoundValues()
  104. {
  105. $field = $this->createMockField('firstName');
  106. $field->expects($this->once())
  107. ->method('bind')
  108. ->with($this->equalTo('Bernhard'));
  109. $group = new TestFieldGroup('author');
  110. $group->add($field);
  111. $group->bind(array('firstName' => 'Bernhard'));
  112. }
  113. public function testBindForwardsNullIfValueIsMissing()
  114. {
  115. $field = $this->createMockField('firstName');
  116. $field->expects($this->once())
  117. ->method('bind')
  118. ->with($this->equalTo(null));
  119. $group = new TestFieldGroup('author');
  120. $group->add($field);
  121. $group->bind(array());
  122. }
  123. public function testAddErrorMapsFieldValidationErrorsOntoFields()
  124. {
  125. $field = $this->createMockField('firstName');
  126. $field->expects($this->once())
  127. ->method('addError')
  128. ->with($this->equalTo('Message'));
  129. $group = new TestFieldGroup('author');
  130. $group->add($field);
  131. $path = new PropertyPath('fields[firstName].data');
  132. $group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
  133. }
  134. public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups()
  135. {
  136. $field = $this->createMockField('firstName');
  137. $field->expects($this->once())
  138. ->method('addError')
  139. ->with($this->equalTo('Message'));
  140. $group = new TestFieldGroup('author');
  141. $innerGroup = new TestFieldGroup('names');
  142. $innerGroup->add($field);
  143. $group->add($innerGroup);
  144. $path = new PropertyPath('fields[names].fields[firstName].data');
  145. $group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
  146. }
  147. public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
  148. {
  149. $field = $this->createMockField('foo');
  150. $field->expects($this->never())
  151. ->method('addError');
  152. $group = new TestFieldGroup('author');
  153. $group->add($field);
  154. $path = new PropertyPath('fields[bar].data');
  155. $group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
  156. $this->assertEquals(array(array('Message', array())), $group->getErrors());
  157. }
  158. public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
  159. {
  160. $field = $this->createMockField('firstName');
  161. $field->expects($this->any())
  162. ->method('isHidden')
  163. ->will($this->returnValue(true));
  164. $field->expects($this->never())
  165. ->method('addError');
  166. $group = new TestFieldGroup('author');
  167. $group->add($field);
  168. $path = new PropertyPath('fields[firstName].data');
  169. $group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
  170. $this->assertEquals(array(array('Message', array())), $group->getErrors());
  171. }
  172. public function testAddErrorMapsDataValidationErrorsOntoFields()
  173. {
  174. // path is expected to point at "firstName"
  175. $expectedPath = new PropertyPath('firstName');
  176. $expectedPathIterator = $expectedPath->getIterator();
  177. $field = $this->createMockField('firstName');
  178. $field->expects($this->any())
  179. ->method('getPropertyPath')
  180. ->will($this->returnValue(new PropertyPath('firstName')));
  181. $field->expects($this->once())
  182. ->method('addError')
  183. ->with($this->equalTo('Message'), array(), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
  184. $group = new TestFieldGroup('author');
  185. $group->add($field);
  186. $path = new PropertyPath('firstName');
  187. $group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
  188. }
  189. public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
  190. {
  191. $field = $this->createMockField('foo');
  192. $field->expects($this->any())
  193. ->method('getPropertyPath')
  194. ->will($this->returnValue(new PropertyPath('foo')));
  195. $field->expects($this->never())
  196. ->method('addError');
  197. $group = new TestFieldGroup('author');
  198. $group->add($field);
  199. $path = new PropertyPath('bar');
  200. $group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
  201. }
  202. public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
  203. {
  204. $field = $this->createMockField('firstName');
  205. $field->expects($this->any())
  206. ->method('isHidden')
  207. ->will($this->returnValue(true));
  208. $field->expects($this->any())
  209. ->method('getPropertyPath')
  210. ->will($this->returnValue(new PropertyPath('firstName')));
  211. $field->expects($this->never())
  212. ->method('addError');
  213. $group = new TestFieldGroup('author');
  214. $group->add($field);
  215. $path = new PropertyPath('firstName');
  216. $group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
  217. }
  218. public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
  219. {
  220. // path is expected to point at "street"
  221. $expectedPath = new PropertyPath('address.street');
  222. $expectedPathIterator = $expectedPath->getIterator();
  223. $expectedPathIterator->next();
  224. $field = $this->createMockField('address');
  225. $field->expects($this->any())
  226. ->method('getPropertyPath')
  227. ->will($this->returnValue(new PropertyPath('address')));
  228. $field->expects($this->once())
  229. ->method('addError')
  230. ->with($this->equalTo('Message'), array(), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
  231. $group = new TestFieldGroup('author');
  232. $group->add($field);
  233. $path = new PropertyPath('address.street');
  234. $group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
  235. }
  236. public function testAddErrorMapsErrorsOntoFieldsInAnonymousGroups()
  237. {
  238. // path is expected to point at "address"
  239. $expectedPath = new PropertyPath('address');
  240. $expectedPathIterator = $expectedPath->getIterator();
  241. $field = $this->createMockField('address');
  242. $field->expects($this->any())
  243. ->method('getPropertyPath')
  244. ->will($this->returnValue(new PropertyPath('address')));
  245. $field->expects($this->once())
  246. ->method('addError')
  247. ->with($this->equalTo('Message'), array(), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
  248. $group = new TestFieldGroup('author');
  249. $group2 = new TestFieldGroup('anonymous', array('property_path' => null));
  250. $group2->add($field);
  251. $group->add($group2);
  252. $path = new PropertyPath('address');
  253. $group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
  254. }
  255. public function testAddThrowsExceptionIfAlreadyBound()
  256. {
  257. $group = new TestFieldGroup('author');
  258. $group->add($this->createMockField('firstName'));
  259. $group->bind(array('firstName' => 'Bernhard'));
  260. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException');
  261. $group->add($this->createMockField('lastName'));
  262. }
  263. public function testAddSetsFieldParent()
  264. {
  265. $group = new TestFieldGroup('author');
  266. $field = $this->createMockField('firstName');
  267. $field->expects($this->once())
  268. ->method('setParent');
  269. // PHPUnit fails to compare infinitely recursive objects
  270. //->with($this->equalTo($group));
  271. $group->add($field);
  272. }
  273. public function testRemoveUnsetsFieldParent()
  274. {
  275. $group = new TestFieldGroup('author');
  276. $field = $this->createMockField('firstName');
  277. $field->expects($this->exactly(2))
  278. ->method('setParent');
  279. // PHPUnit fails to compare subsequent method calls with different arguments
  280. $group->add($field);
  281. $group->remove('firstName');
  282. }
  283. public function testMergeAddsFieldsFromAnotherGroup()
  284. {
  285. $group1 = new TestFieldGroup('author');
  286. $group1->add($field1 = new TestField('firstName'));
  287. $group2 = new TestFieldGroup('publisher');
  288. $group2->add($field2 = new TestField('lastName'));
  289. $group1->merge($group2);
  290. $this->assertTrue($group1->has('lastName'));
  291. $this->assertEquals(new PropertyPath('publisher.lastName'), $group1->get('lastName')->getPropertyPath());
  292. }
  293. public function testMergeThrowsExceptionIfOtherGroupAlreadyBound()
  294. {
  295. $group1 = new TestFieldGroup('author');
  296. $group2 = new TestFieldGroup('publisher');
  297. $group2->add($this->createMockField('firstName'));
  298. $group2->bind(array('firstName' => 'Bernhard'));
  299. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException');
  300. $group1->merge($group2);
  301. }
  302. public function testAddUpdatesFieldFromTransformedData()
  303. {
  304. $originalAuthor = new Author();
  305. $transformedAuthor = new Author();
  306. // the authors should differ to make sure the test works
  307. $transformedAuthor->firstName = 'Foo';
  308. $group = new TestFieldGroup('author');
  309. $transformer = $this->createMockTransformer();
  310. $transformer->expects($this->once())
  311. ->method('transform')
  312. ->with($this->equalTo($originalAuthor))
  313. ->will($this->returnValue($transformedAuthor));
  314. $group->setValueTransformer($transformer);
  315. $group->setData($originalAuthor);
  316. $field = $this->createMockField('firstName');
  317. $field->expects($this->any())
  318. ->method('getPropertyPath')
  319. ->will($this->returnValue(new PropertyPath('firstName')));
  320. $field->expects($this->once())
  321. ->method('updateFromObject')
  322. ->with($this->equalTo($transformedAuthor));
  323. $group->add($field);
  324. }
  325. public function testAddDoesNotUpdateFieldsWithEmptyPropertyPath()
  326. {
  327. $group = new TestFieldGroup('author');
  328. $group->setData(new Author());
  329. $field = $this->createMockField('firstName');
  330. $field->expects($this->any())
  331. ->method('getPropertyPath')
  332. ->will($this->returnValue(null));
  333. $field->expects($this->never())
  334. ->method('updateFromObject');
  335. $group->add($field);
  336. }
  337. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  338. {
  339. $originalAuthor = new Author();
  340. $group = new TestFieldGroup('author');
  341. $transformer = $this->createMockTransformer();
  342. $transformer->expects($this->once())
  343. ->method('transform')
  344. ->with($this->equalTo($originalAuthor))
  345. ->will($this->returnValue(''));
  346. $group->setValueTransformer($transformer);
  347. $group->setData($originalAuthor);
  348. $field = $this->createMockField('firstName');
  349. $field->expects($this->never())
  350. ->method('updateFromObject');
  351. $group->add($field);
  352. }
  353. public function testSetDataUpdatesAllFieldsFromTransformedData()
  354. {
  355. $originalAuthor = new Author();
  356. $transformedAuthor = new Author();
  357. // the authors should differ to make sure the test works
  358. $transformedAuthor->firstName = 'Foo';
  359. $group = new TestFieldGroup('author');
  360. $transformer = $this->createMockTransformer();
  361. $transformer->expects($this->once())
  362. ->method('transform')
  363. ->with($this->equalTo($originalAuthor))
  364. ->will($this->returnValue($transformedAuthor));
  365. $group->setValueTransformer($transformer);
  366. $field = $this->createMockField('firstName');
  367. $field->expects($this->once())
  368. ->method('updateFromObject')
  369. ->with($this->equalTo($transformedAuthor));
  370. $group->add($field);
  371. $field = $this->createMockField('lastName');
  372. $field->expects($this->once())
  373. ->method('updateFromObject')
  374. ->with($this->equalTo($transformedAuthor));
  375. $group->add($field);
  376. $group->setData($originalAuthor);
  377. }
  378. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  379. {
  380. $group = new TestFieldGroup('author');
  381. $this->setExpectedException('InvalidArgumentException');
  382. $group->setData('foobar');
  383. }
  384. public function testBindUpdatesTransformedDataFromAllFields()
  385. {
  386. $originalAuthor = new Author();
  387. $transformedAuthor = new Author();
  388. // the authors should differ to make sure the test works
  389. $transformedAuthor->firstName = 'Foo';
  390. $group = new TestFieldGroup('author');
  391. $transformer = $this->createMockTransformer();
  392. $transformer->expects($this->exactly(2))
  393. ->method('transform')
  394. // the method is first called with NULL, then
  395. // with $originalAuthor -> not testable by PHPUnit
  396. // ->with($this->equalTo(null))
  397. // ->with($this->equalTo($originalAuthor))
  398. ->will($this->returnValue($transformedAuthor));
  399. $group->setValueTransformer($transformer);
  400. $group->setData($originalAuthor);
  401. $field = $this->createMockField('firstName');
  402. $field->expects($this->once())
  403. ->method('updateObject')
  404. ->with($this->equalTo($transformedAuthor));
  405. $group->add($field);
  406. $field = $this->createMockField('lastName');
  407. $field->expects($this->once())
  408. ->method('updateObject')
  409. ->with($this->equalTo($transformedAuthor));
  410. $group->add($field);
  411. $group->bind(array()); // irrelevant
  412. }
  413. public function testGetDataReturnsObject()
  414. {
  415. $group = new TestFieldGroup('author');
  416. $object = new \stdClass();
  417. $group->setData($object);
  418. $this->assertEquals($object, $group->getData());
  419. }
  420. public function testGetDisplayedDataForwardsCall()
  421. {
  422. $field = $this->createValidMockField('firstName');
  423. $field->expects($this->atLeastOnce())
  424. ->method('getDisplayedData')
  425. ->will($this->returnValue('Bernhard'));
  426. $group = new TestFieldGroup('author');
  427. $group->add($field);
  428. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getDisplayedData());
  429. }
  430. public function testIsMultipartIfAnyFieldIsMultipart()
  431. {
  432. $group = new TestFieldGroup('author');
  433. $group->add($this->createMultipartMockField('firstName'));
  434. $group->add($this->createNonMultipartMockField('lastName'));
  435. $this->assertTrue($group->isMultipart());
  436. }
  437. public function testIsNotMultipartIfNoFieldIsMultipart()
  438. {
  439. $group = new TestFieldGroup('author');
  440. $group->add($this->createNonMultipartMockField('firstName'));
  441. $group->add($this->createNonMultipartMockField('lastName'));
  442. $this->assertFalse($group->isMultipart());
  443. }
  444. public function testLocaleIsPassedToField_SetBeforeAddingTheField()
  445. {
  446. $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
  447. $field->expects($this->any())
  448. ->method('getKey')
  449. ->will($this->returnValue('firstName'));
  450. $field->expects($this->once())
  451. ->method('setLocale')
  452. ->with($this->equalTo('de_DE'));
  453. $group = new TestFieldGroup('author');
  454. $group->setLocale('de_DE');
  455. $group->add($field);
  456. }
  457. public function testLocaleIsPassedToField_SetAfterAddingTheField()
  458. {
  459. $field = $this->getMockForAbstractClass(__NAMESPACE__ . '\FieldGroupTest_Field', array(), '', false, false);
  460. $field->expects($this->any())
  461. ->method('getKey')
  462. ->will($this->returnValue('firstName'));
  463. // DOESN'T WORK!
  464. // $field = $this->getMock(__NAMESPACE__ . '\Fixtures\Field', array(), array(), '', false, false);
  465. // $field->expects($this->once())
  466. // ->method('setLocale')
  467. // ->with($this->equalTo('de_AT'));
  468. // $field->expects($this->once())
  469. // ->method('setLocale')
  470. // ->with($this->equalTo('de_DE'));
  471. $group = new TestFieldGroup('author');
  472. $group->add($field);
  473. $group->setLocale('de_DE');
  474. $this->assertEquals(array(class_exists('\Locale', false) ? \Locale::getDefault() : 'en', 'de_DE'), $field->locales);
  475. }
  476. public function testSupportsClone()
  477. {
  478. $group = new TestFieldGroup('author');
  479. $group->add($this->createMockField('firstName'));
  480. $clone = clone $group;
  481. $this->assertNotSame($clone['firstName'], $group['firstName']);
  482. }
  483. public function testBindWithoutPriorSetData()
  484. {
  485. return; // TODO
  486. $field = $this->createMockField('firstName');
  487. $field->expects($this->any())
  488. ->method('getData')
  489. ->will($this->returnValue('Bernhard'));
  490. $group = new TestFieldGroup('author');
  491. $group->add($field);
  492. $group->bind(array('firstName' => 'Bernhard'));
  493. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getData());
  494. }
  495. public function testGetHiddenFieldsReturnsOnlyHiddenFields()
  496. {
  497. $group = $this->getGroupWithBothVisibleAndHiddenField();
  498. $hiddenFields = $group->getHiddenFields(true, false);
  499. $this->assertSame(array($group['hiddenField']), $hiddenFields);
  500. }
  501. public function testGetVisibleFieldsReturnsOnlyVisibleFields()
  502. {
  503. $group = $this->getGroupWithBothVisibleAndHiddenField();
  504. $visibleFields = $group->getVisibleFields(true, false);
  505. $this->assertSame(array($group['visibleField']), $visibleFields);
  506. }
  507. /**
  508. * Create a group containing two fields, "visibleField" and "hiddenField"
  509. *
  510. * @return FieldGroup
  511. */
  512. protected function getGroupWithBothVisibleAndHiddenField()
  513. {
  514. $group = new TestFieldGroup('testGroup');
  515. // add a visible field
  516. $visibleField = $this->createMockField('visibleField');
  517. $visibleField->expects($this->once())
  518. ->method('isHidden')
  519. ->will($this->returnValue(false));
  520. $group->add($visibleField);
  521. // add a hidden field
  522. $hiddenField = $this->createMockField('hiddenField');
  523. $hiddenField->expects($this->once())
  524. ->method('isHidden')
  525. ->will($this->returnValue(true));
  526. $group->add($hiddenField);
  527. return $group;
  528. }
  529. protected function createMockField($key)
  530. {
  531. $field = $this->getMock(
  532. 'Symfony\Component\Form\FieldInterface',
  533. array(),
  534. array(),
  535. '',
  536. false, // don't use constructor
  537. false // don't call parent::__clone
  538. );
  539. $field->expects($this->any())
  540. ->method('getKey')
  541. ->will($this->returnValue($key));
  542. return $field;
  543. }
  544. protected function createInvalidMockField($key)
  545. {
  546. $field = $this->createMockField($key);
  547. $field->expects($this->any())
  548. ->method('isValid')
  549. ->will($this->returnValue(false));
  550. return $field;
  551. }
  552. protected function createValidMockField($key)
  553. {
  554. $field = $this->createMockField($key);
  555. $field->expects($this->any())
  556. ->method('isValid')
  557. ->will($this->returnValue(true));
  558. return $field;
  559. }
  560. protected function createNonMultipartMockField($key)
  561. {
  562. $field = $this->createMockField($key);
  563. $field->expects($this->any())
  564. ->method('isMultipart')
  565. ->will($this->returnValue(false));
  566. return $field;
  567. }
  568. protected function createMultipartMockField($key)
  569. {
  570. $field = $this->createMockField($key);
  571. $field->expects($this->any())
  572. ->method('isMultipart')
  573. ->will($this->returnValue(true));
  574. return $field;
  575. }
  576. protected function createMockTransformer()
  577. {
  578. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  579. }
  580. }