FieldGroupTest.php 25 KB

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