FieldGroupTest.php 27 KB

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