FieldGroupTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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__ . '/Fixtures/Author.php';
  12. require_once __DIR__ . '/Fixtures/TestField.php';
  13. require_once __DIR__ . '/Fixtures/TestFieldGroup.php';
  14. use Symfony\Component\Form\Field;
  15. use Symfony\Component\Form\FieldError;
  16. use Symfony\Component\Form\FieldInterface;
  17. use Symfony\Component\Form\FieldGroup;
  18. use Symfony\Component\Form\PropertyPath;
  19. use Symfony\Tests\Component\Form\Fixtures\Author;
  20. use Symfony\Tests\Component\Form\Fixtures\TestField;
  21. use Symfony\Tests\Component\Form\Fixtures\TestFieldGroup;
  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 testAddErrorMapsErrorsOntoFieldsInVirtualGroups()
  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. $nestedGroup = new TestFieldGroup('nested', array('virtual' => true));
  270. $nestedGroup->add($field);
  271. $group->add($nestedGroup);
  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 testAddUpdatesFieldFromTransformedData()
  304. {
  305. $originalAuthor = new Author();
  306. $transformedAuthor = new Author();
  307. // the authors should differ to make sure the test works
  308. $transformedAuthor->firstName = 'Foo';
  309. $group = new TestFieldGroup('author');
  310. $transformer = $this->createMockTransformer();
  311. $transformer->expects($this->once())
  312. ->method('transform')
  313. ->with($this->equalTo($originalAuthor))
  314. ->will($this->returnValue($transformedAuthor));
  315. $group->setValueTransformer($transformer);
  316. $group->setData($originalAuthor);
  317. $field = $this->createMockField('firstName');
  318. $field->expects($this->any())
  319. ->method('getPropertyPath')
  320. ->will($this->returnValue(new PropertyPath('firstName')));
  321. $field->expects($this->once())
  322. ->method('updateFromProperty')
  323. ->with($this->equalTo($transformedAuthor));
  324. $group->add($field);
  325. }
  326. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  327. {
  328. $originalAuthor = new Author();
  329. $group = new TestFieldGroup('author');
  330. $transformer = $this->createMockTransformer();
  331. $transformer->expects($this->once())
  332. ->method('transform')
  333. ->with($this->equalTo($originalAuthor))
  334. ->will($this->returnValue(''));
  335. $group->setValueTransformer($transformer);
  336. $group->setData($originalAuthor);
  337. $field = $this->createMockField('firstName');
  338. $field->expects($this->never())
  339. ->method('updateFromProperty');
  340. $group->add($field);
  341. }
  342. /**
  343. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  344. */
  345. public function testAddThrowsExceptionIfNoFieldOrString()
  346. {
  347. $group = new TestFieldGroup('author');
  348. $group->add(1234);
  349. }
  350. /**
  351. * @expectedException Symfony\Component\Form\Exception\DanglingFieldException
  352. */
  353. public function testAddThrowsExceptionIfStringButNoRootForm()
  354. {
  355. $group = new TestFieldGroup('author');
  356. $group->add('firstName');
  357. }
  358. public function testAddThrowsExceptionIfStringButNoFieldFactory()
  359. {
  360. $form = $this->createMockForm();
  361. $form->expects($this->once())
  362. ->method('getFieldFactory')
  363. ->will($this->returnValue(null));
  364. $group = new TestFieldGroup('author');
  365. $group->setParent($form);
  366. $this->setExpectedException('\LogicException');
  367. $group->add('firstName');
  368. }
  369. public function testAddUsesFieldFromFactoryIfStringIsGiven()
  370. {
  371. $author = new \stdClass();
  372. $field = $this->createMockField('firstName');
  373. $factory = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryInterface');
  374. $factory->expects($this->once())
  375. ->method('getInstance')
  376. ->with($this->equalTo($author), $this->equalTo('firstName'), $this->equalTo(array('foo' => 'bar')))
  377. ->will($this->returnValue($field));
  378. $form = $this->createMockForm();
  379. $form->expects($this->once())
  380. ->method('getFieldFactory')
  381. ->will($this->returnValue($factory));
  382. $group = new TestFieldGroup('author');
  383. $group->setParent($form);
  384. $group->setData($author);
  385. $group->add('firstName', array('foo' => 'bar'));
  386. $this->assertSame($field, $group['firstName']);
  387. }
  388. public function testSetDataUpdatesAllFieldsFromTransformedData()
  389. {
  390. $originalAuthor = new Author();
  391. $transformedAuthor = new Author();
  392. // the authors should differ to make sure the test works
  393. $transformedAuthor->firstName = 'Foo';
  394. $group = new TestFieldGroup('author');
  395. $transformer = $this->createMockTransformer();
  396. $transformer->expects($this->once())
  397. ->method('transform')
  398. ->with($this->equalTo($originalAuthor))
  399. ->will($this->returnValue($transformedAuthor));
  400. $group->setValueTransformer($transformer);
  401. $field = $this->createMockField('firstName');
  402. $field->expects($this->once())
  403. ->method('updateFromProperty')
  404. ->with($this->equalTo($transformedAuthor));
  405. $group->add($field);
  406. $field = $this->createMockField('lastName');
  407. $field->expects($this->once())
  408. ->method('updateFromProperty')
  409. ->with($this->equalTo($transformedAuthor));
  410. $group->add($field);
  411. $group->setData($originalAuthor);
  412. }
  413. /**
  414. * The use case for this test are groups whose fields should be mapped
  415. * directly onto properties of the form's object.
  416. *
  417. * Example:
  418. *
  419. * <code>
  420. * $dateRangeField = new FieldGroup('dateRange');
  421. * $dateRangeField->add(new DateField('startDate'));
  422. * $dateRangeField->add(new DateField('endDate'));
  423. * $form->add($dateRangeField);
  424. * </code>
  425. *
  426. * If $dateRangeField is not virtual, the property "dateRange" must be
  427. * present on the form's object. In this property, an object or array
  428. * with the properties "startDate" and "endDate" is expected.
  429. *
  430. * If $dateRangeField is virtual though, it's children are mapped directly
  431. * onto the properties "startDate" and "endDate" of the form's object.
  432. */
  433. public function testSetDataSkipsVirtualFieldGroups()
  434. {
  435. $author = new Author();
  436. $author->firstName = 'Foo';
  437. $group = new TestFieldGroup('author');
  438. $nestedGroup = new TestFieldGroup('personal_data', array(
  439. 'virtual' => true,
  440. ));
  441. // both fields are in the nested group but receive the object of the
  442. // top-level group because the nested group is virtual
  443. $field = $this->createMockField('firstName');
  444. $field->expects($this->once())
  445. ->method('updateFromProperty')
  446. ->with($this->equalTo($author));
  447. $nestedGroup->add($field);
  448. $field = $this->createMockField('lastName');
  449. $field->expects($this->once())
  450. ->method('updateFromProperty')
  451. ->with($this->equalTo($author));
  452. $nestedGroup->add($field);
  453. $group->add($nestedGroup);
  454. $group->setData($author);
  455. }
  456. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  457. {
  458. $group = new TestFieldGroup('author');
  459. $this->setExpectedException('InvalidArgumentException');
  460. $group->setData('foobar');
  461. }
  462. public function testBindUpdatesTransformedDataFromAllFields()
  463. {
  464. $originalAuthor = new Author();
  465. $transformedAuthor = new Author();
  466. // the authors should differ to make sure the test works
  467. $transformedAuthor->firstName = 'Foo';
  468. $group = new TestFieldGroup('author');
  469. $transformer = $this->createMockTransformer();
  470. $transformer->expects($this->exactly(2))
  471. ->method('transform')
  472. // the method is first called with NULL, then
  473. // with $originalAuthor -> not testable by PHPUnit
  474. // ->with($this->equalTo(null))
  475. // ->with($this->equalTo($originalAuthor))
  476. ->will($this->returnValue($transformedAuthor));
  477. $group->setValueTransformer($transformer);
  478. $group->setData($originalAuthor);
  479. $field = $this->createMockField('firstName');
  480. $field->expects($this->once())
  481. ->method('updateProperty')
  482. ->with($this->equalTo($transformedAuthor));
  483. $group->add($field);
  484. $field = $this->createMockField('lastName');
  485. $field->expects($this->once())
  486. ->method('updateProperty')
  487. ->with($this->equalTo($transformedAuthor));
  488. $group->add($field);
  489. $group->bind(array()); // irrelevant
  490. }
  491. public function testGetDataReturnsObject()
  492. {
  493. $group = new TestFieldGroup('author');
  494. $object = new \stdClass();
  495. $group->setData($object);
  496. $this->assertEquals($object, $group->getData());
  497. }
  498. public function testGetDisplayedDataForwardsCall()
  499. {
  500. $field = $this->createValidMockField('firstName');
  501. $field->expects($this->atLeastOnce())
  502. ->method('getDisplayedData')
  503. ->will($this->returnValue('Bernhard'));
  504. $group = new TestFieldGroup('author');
  505. $group->add($field);
  506. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getDisplayedData());
  507. }
  508. public function testIsMultipartIfAnyFieldIsMultipart()
  509. {
  510. $group = new TestFieldGroup('author');
  511. $group->add($this->createMultipartMockField('firstName'));
  512. $group->add($this->createNonMultipartMockField('lastName'));
  513. $this->assertTrue($group->isMultipart());
  514. }
  515. public function testIsNotMultipartIfNoFieldIsMultipart()
  516. {
  517. $group = new TestFieldGroup('author');
  518. $group->add($this->createNonMultipartMockField('firstName'));
  519. $group->add($this->createNonMultipartMockField('lastName'));
  520. $this->assertFalse($group->isMultipart());
  521. }
  522. public function testSupportsClone()
  523. {
  524. $group = new TestFieldGroup('author');
  525. $group->add($this->createMockField('firstName'));
  526. $clone = clone $group;
  527. $this->assertNotSame($clone['firstName'], $group['firstName']);
  528. }
  529. public function testBindWithoutPriorSetData()
  530. {
  531. return; // TODO
  532. $field = $this->createMockField('firstName');
  533. $field->expects($this->any())
  534. ->method('getData')
  535. ->will($this->returnValue('Bernhard'));
  536. $group = new TestFieldGroup('author');
  537. $group->add($field);
  538. $group->bind(array('firstName' => 'Bernhard'));
  539. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getData());
  540. }
  541. public function testGetHiddenFieldsReturnsOnlyHiddenFields()
  542. {
  543. $group = $this->getGroupWithBothVisibleAndHiddenField();
  544. $hiddenFields = $group->getHiddenFields(true, false);
  545. $this->assertSame(array($group['hiddenField']), $hiddenFields);
  546. }
  547. public function testGetVisibleFieldsReturnsOnlyVisibleFields()
  548. {
  549. $group = $this->getGroupWithBothVisibleAndHiddenField();
  550. $visibleFields = $group->getVisibleFields(true, false);
  551. $this->assertSame(array($group['visibleField']), $visibleFields);
  552. }
  553. /**
  554. * Create a group containing two fields, "visibleField" and "hiddenField"
  555. *
  556. * @return FieldGroup
  557. */
  558. protected function getGroupWithBothVisibleAndHiddenField()
  559. {
  560. $group = new TestFieldGroup('testGroup');
  561. // add a visible field
  562. $visibleField = $this->createMockField('visibleField');
  563. $visibleField->expects($this->once())
  564. ->method('isHidden')
  565. ->will($this->returnValue(false));
  566. $group->add($visibleField);
  567. // add a hidden field
  568. $hiddenField = $this->createMockField('hiddenField');
  569. $hiddenField->expects($this->once())
  570. ->method('isHidden')
  571. ->will($this->returnValue(true));
  572. $group->add($hiddenField);
  573. return $group;
  574. }
  575. protected function createMockField($key)
  576. {
  577. $field = $this->getMock(
  578. 'Symfony\Component\Form\FieldInterface',
  579. array(),
  580. array(),
  581. '',
  582. false, // don't use constructor
  583. false // don't call parent::__clone
  584. );
  585. $field->expects($this->any())
  586. ->method('getKey')
  587. ->will($this->returnValue($key));
  588. return $field;
  589. }
  590. protected function createMockForm()
  591. {
  592. $form = $this->getMock(
  593. 'Symfony\Component\Form\Form',
  594. array(),
  595. array(),
  596. '',
  597. false, // don't use constructor
  598. false // don't call parent::__clone)
  599. );
  600. $form->expects($this->any())
  601. ->method('getRoot')
  602. ->will($this->returnValue($form));
  603. return $form;
  604. }
  605. protected function createInvalidMockField($key)
  606. {
  607. $field = $this->createMockField($key);
  608. $field->expects($this->any())
  609. ->method('isValid')
  610. ->will($this->returnValue(false));
  611. return $field;
  612. }
  613. protected function createValidMockField($key)
  614. {
  615. $field = $this->createMockField($key);
  616. $field->expects($this->any())
  617. ->method('isValid')
  618. ->will($this->returnValue(true));
  619. return $field;
  620. }
  621. protected function createNonMultipartMockField($key)
  622. {
  623. $field = $this->createMockField($key);
  624. $field->expects($this->any())
  625. ->method('isMultipart')
  626. ->will($this->returnValue(false));
  627. return $field;
  628. }
  629. protected function createMultipartMockField($key)
  630. {
  631. $field = $this->createMockField($key);
  632. $field->expects($this->any())
  633. ->method('isMultipart')
  634. ->will($this->returnValue(true));
  635. return $field;
  636. }
  637. protected function createMockTransformer()
  638. {
  639. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  640. }
  641. }