FieldGroupTest.php 27 KB

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