FieldGroupTest.php 26 KB

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