FieldGroupTest.php 27 KB

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