FieldGroupTest.php 27 KB

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