FieldGroupTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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', array(), new PropertyPath('fields[firstName].data'), FieldGroup::FIELD_ERROR);
  123. }
  124. public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups()
  125. {
  126. $field = $this->createMockField('firstName');
  127. $field->expects($this->once())
  128. ->method('addError')
  129. ->with($this->equalTo('Message'));
  130. $group = new FieldGroup('author');
  131. $innerGroup = new FieldGroup('names');
  132. $innerGroup->add($field);
  133. $group->add($innerGroup);
  134. $group->addError('Message', array(), new PropertyPath('fields[names].fields[firstName].data'), FieldGroup::FIELD_ERROR);
  135. }
  136. public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
  137. {
  138. $field = $this->createMockField('foo');
  139. $field->expects($this->never())
  140. ->method('addError');
  141. $group = new FieldGroup('author');
  142. $group->add($field);
  143. $group->addError('Message', array(), new PropertyPath('fields[bar].data'), FieldGroup::FIELD_ERROR);
  144. $this->assertEquals(array(array('Message', array())), $group->getErrors());
  145. }
  146. public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
  147. {
  148. $field = $this->createMockField('firstName');
  149. $field->expects($this->any())
  150. ->method('isHidden')
  151. ->will($this->returnValue(true));
  152. $field->expects($this->never())
  153. ->method('addError');
  154. $group = new FieldGroup('author');
  155. $group->add($field);
  156. $group->addError('Message', array(), new PropertyPath('fields[firstName].data'), FieldGroup::FIELD_ERROR);
  157. $this->assertEquals(array(array('Message', array())), $group->getErrors());
  158. }
  159. public function testAddErrorMapsDataValidationErrorsOntoFields()
  160. {
  161. // path is expected to point at "firstName"
  162. $expectedPath = new PropertyPath('firstName');
  163. $field = $this->createMockField('firstName');
  164. $field->expects($this->any())
  165. ->method('getPropertyPath')
  166. ->will($this->returnValue(new PropertyPath('firstName')));
  167. $field->expects($this->once())
  168. ->method('addError')
  169. ->with($this->equalTo('Message'), array(), $this->equalTo($expectedPath), $this->equalTo(FieldGroup::DATA_ERROR));
  170. $group = new FieldGroup('author');
  171. $group->add($field);
  172. $group->addError('Message', array(), new PropertyPath('firstName'), FieldGroup::DATA_ERROR);
  173. }
  174. public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
  175. {
  176. $field = $this->createMockField('foo');
  177. $field->expects($this->any())
  178. ->method('getPropertyPath')
  179. ->will($this->returnValue(new PropertyPath('foo')));
  180. $field->expects($this->never())
  181. ->method('addError');
  182. $group = new FieldGroup('author');
  183. $group->add($field);
  184. $group->addError('Message', array(), new PropertyPath('bar'), FieldGroup::DATA_ERROR);
  185. }
  186. public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
  187. {
  188. $field = $this->createMockField('firstName');
  189. $field->expects($this->any())
  190. ->method('isHidden')
  191. ->will($this->returnValue(true));
  192. $field->expects($this->any())
  193. ->method('getPropertyPath')
  194. ->will($this->returnValue(new PropertyPath('firstName')));
  195. $field->expects($this->never())
  196. ->method('addError');
  197. $group = new FieldGroup('author');
  198. $group->add($field);
  199. $group->addError('Message', array(), new PropertyPath('firstName'), FieldGroup::DATA_ERROR);
  200. }
  201. public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
  202. {
  203. // path is expected to point at "street"
  204. $expectedPath = new PropertyPath('address.street');
  205. $expectedPath->next();
  206. $field = $this->createMockField('address');
  207. $field->expects($this->any())
  208. ->method('getPropertyPath')
  209. ->will($this->returnValue(new PropertyPath('address')));
  210. $field->expects($this->once())
  211. ->method('addError')
  212. ->with($this->equalTo('Message'), array(), $this->equalTo($expectedPath), $this->equalTo(FieldGroup::DATA_ERROR));
  213. $group = new FieldGroup('author');
  214. $group->add($field);
  215. $group->addError('Message', array(), new PropertyPath('address.street'), FieldGroup::DATA_ERROR);
  216. }
  217. public function testAddErrorMapsErrorsOntoFieldsInAnonymousGroups()
  218. {
  219. // path is expected to point at "address"
  220. $expectedPath = new PropertyPath('address');
  221. $field = $this->createMockField('address');
  222. $field->expects($this->any())
  223. ->method('getPropertyPath')
  224. ->will($this->returnValue(new PropertyPath('address')));
  225. $field->expects($this->once())
  226. ->method('addError')
  227. ->with($this->equalTo('Message'), array(), $this->equalTo($expectedPath), $this->equalTo(FieldGroup::DATA_ERROR));
  228. $group = new FieldGroup('author');
  229. $group2 = new FieldGroup('anonymous', array('property_path' => null));
  230. $group2->add($field);
  231. $group->add($group2);
  232. $group->addError('Message', array(), new PropertyPath('address'), FieldGroup::DATA_ERROR);
  233. }
  234. public function testAddThrowsExceptionIfAlreadyBound()
  235. {
  236. $group = new FieldGroup('author');
  237. $group->add($this->createMockField('firstName'));
  238. $group->bind(array('firstName' => 'Bernhard'));
  239. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException');
  240. $group->add($this->createMockField('lastName'));
  241. }
  242. public function testAddSetsFieldParent()
  243. {
  244. $group = new FieldGroup('author');
  245. $field = $this->createMockField('firstName');
  246. $field->expects($this->once())
  247. ->method('setParent');
  248. // PHPUnit fails to compare infinitely recursive objects
  249. //->with($this->equalTo($group));
  250. $group->add($field);
  251. }
  252. public function testRemoveUnsetsFieldParent()
  253. {
  254. $group = new FieldGroup('author');
  255. $field = $this->createMockField('firstName');
  256. $field->expects($this->exactly(2))
  257. ->method('setParent');
  258. // PHPUnit fails to compare subsequent method calls with different arguments
  259. $group->add($field);
  260. $group->remove('firstName');
  261. }
  262. public function testMergeAddsFieldsFromAnotherGroup()
  263. {
  264. $group1 = new FieldGroup('author');
  265. $group1->add($field1 = new TestField('firstName'));
  266. $group2 = new FieldGroup('publisher');
  267. $group2->add($field2 = new TestField('lastName'));
  268. $group1->merge($group2);
  269. $this->assertTrue($group1->has('lastName'));
  270. $this->assertEquals(new PropertyPath('publisher.lastName'), $group1->get('lastName')->getPropertyPath());
  271. }
  272. public function testMergeThrowsExceptionIfOtherGroupAlreadyBound()
  273. {
  274. $group1 = new FieldGroup('author');
  275. $group2 = new FieldGroup('publisher');
  276. $group2->add($this->createMockField('firstName'));
  277. $group2->bind(array('firstName' => 'Bernhard'));
  278. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException');
  279. $group1->merge($group2);
  280. }
  281. public function testAddUpdatesFieldFromTransformedData()
  282. {
  283. $originalAuthor = new Author();
  284. $transformedAuthor = new Author();
  285. // the authors should differ to make sure the test works
  286. $transformedAuthor->firstName = 'Foo';
  287. $group = new FieldGroup('author');
  288. $transformer = $this->createMockTransformer();
  289. $transformer->expects($this->once())
  290. ->method('transform')
  291. ->with($this->equalTo($originalAuthor))
  292. ->will($this->returnValue($transformedAuthor));
  293. $group->setValueTransformer($transformer);
  294. $group->setData($originalAuthor);
  295. $field = $this->createMockField('firstName');
  296. $field->expects($this->any())
  297. ->method('getPropertyPath')
  298. ->will($this->returnValue(new PropertyPath('firstName')));
  299. $field->expects($this->once())
  300. ->method('updateFromObject')
  301. ->with($this->equalTo($transformedAuthor));
  302. $group->add($field);
  303. }
  304. public function testAddDoesNotUpdateFieldsWithEmptyPropertyPath()
  305. {
  306. $group = new FieldGroup('author');
  307. $group->setData(new Author());
  308. $field = $this->createMockField('firstName');
  309. $field->expects($this->any())
  310. ->method('getPropertyPath')
  311. ->will($this->returnValue(null));
  312. $field->expects($this->never())
  313. ->method('updateFromObject');
  314. $group->add($field);
  315. }
  316. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  317. {
  318. $originalAuthor = new Author();
  319. $group = new FieldGroup('author');
  320. $transformer = $this->createMockTransformer();
  321. $transformer->expects($this->once())
  322. ->method('transform')
  323. ->with($this->equalTo($originalAuthor))
  324. ->will($this->returnValue(''));
  325. $group->setValueTransformer($transformer);
  326. $group->setData($originalAuthor);
  327. $field = $this->createMockField('firstName');
  328. $field->expects($this->never())
  329. ->method('updateFromObject');
  330. $group->add($field);
  331. }
  332. public function testSetDataUpdatesAllFieldsFromTransformedData()
  333. {
  334. $originalAuthor = new Author();
  335. $transformedAuthor = new Author();
  336. // the authors should differ to make sure the test works
  337. $transformedAuthor->firstName = 'Foo';
  338. $group = new FieldGroup('author');
  339. $transformer = $this->createMockTransformer();
  340. $transformer->expects($this->once())
  341. ->method('transform')
  342. ->with($this->equalTo($originalAuthor))
  343. ->will($this->returnValue($transformedAuthor));
  344. $group->setValueTransformer($transformer);
  345. $field = $this->createMockField('firstName');
  346. $field->expects($this->once())
  347. ->method('updateFromObject')
  348. ->with($this->equalTo($transformedAuthor));
  349. $group->add($field);
  350. $field = $this->createMockField('lastName');
  351. $field->expects($this->once())
  352. ->method('updateFromObject')
  353. ->with($this->equalTo($transformedAuthor));
  354. $group->add($field);
  355. $group->setData($originalAuthor);
  356. }
  357. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  358. {
  359. $group = new FieldGroup('author');
  360. $this->setExpectedException('InvalidArgumentException');
  361. $group->setData('foobar');
  362. }
  363. public function testBindUpdatesTransformedDataFromAllFields()
  364. {
  365. $originalAuthor = new Author();
  366. $transformedAuthor = new Author();
  367. // the authors should differ to make sure the test works
  368. $transformedAuthor->firstName = 'Foo';
  369. $group = new FieldGroup('author');
  370. $transformer = $this->createMockTransformer();
  371. $transformer->expects($this->once())
  372. ->method('transform')
  373. ->with($this->equalTo($originalAuthor))
  374. ->will($this->returnValue($transformedAuthor));
  375. $group->setValueTransformer($transformer);
  376. $group->setData($originalAuthor);
  377. $field = $this->createMockField('firstName');
  378. $field->expects($this->once())
  379. ->method('updateObject')
  380. ->with($this->equalTo($transformedAuthor));
  381. $group->add($field);
  382. $field = $this->createMockField('lastName');
  383. $field->expects($this->once())
  384. ->method('updateObject')
  385. ->with($this->equalTo($transformedAuthor));
  386. $group->add($field);
  387. $group->bind(array()); // irrelevant
  388. }
  389. public function testGetDataReturnsObject()
  390. {
  391. $group = new FieldGroup('author');
  392. $object = new \stdClass();
  393. $group->setData($object);
  394. $this->assertEquals($object, $group->getData());
  395. }
  396. public function testGetDisplayedDataForwardsCall()
  397. {
  398. $field = $this->createValidMockField('firstName');
  399. $field->expects($this->atLeastOnce())
  400. ->method('getDisplayedData')
  401. ->will($this->returnValue('Bernhard'));
  402. $group = new FieldGroup('author');
  403. $group->add($field);
  404. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getDisplayedData());
  405. }
  406. public function testIsMultipartIfAnyFieldIsMultipart()
  407. {
  408. $group = new FieldGroup('author');
  409. $group->add($this->createMultipartMockField('firstName'));
  410. $group->add($this->createNonMultipartMockField('lastName'));
  411. $this->assertTrue($group->isMultipart());
  412. }
  413. public function testIsNotMultipartIfNoFieldIsMultipart()
  414. {
  415. $group = new FieldGroup('author');
  416. $group->add($this->createNonMultipartMockField('firstName'));
  417. $group->add($this->createNonMultipartMockField('lastName'));
  418. $this->assertFalse($group->isMultipart());
  419. }
  420. public function testLocaleIsPassedToField_SetBeforeAddingTheField()
  421. {
  422. $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
  423. $field->expects($this->any())
  424. ->method('getKey')
  425. ->will($this->returnValue('firstName'));
  426. $field->expects($this->once())
  427. ->method('setLocale')
  428. ->with($this->equalTo('de_DE'));
  429. $group = new FieldGroup('author');
  430. $group->setLocale('de_DE');
  431. $group->add($field);
  432. }
  433. public function testLocaleIsPassedToField_SetAfterAddingTheField()
  434. {
  435. $field = $this->getMockForAbstractClass(__NAMESPACE__ . '\FieldGroupTest_Field', array(), '', false, false);
  436. $field->expects($this->any())
  437. ->method('getKey')
  438. ->will($this->returnValue('firstName'));
  439. // DOESN'T WORK!
  440. // $field = $this->getMock(__NAMESPACE__ . '\Fixtures\Field', array(), array(), '', false, false);
  441. // $field->expects($this->once())
  442. // ->method('setLocale')
  443. // ->with($this->equalTo('de_AT'));
  444. // $field->expects($this->once())
  445. // ->method('setLocale')
  446. // ->with($this->equalTo('de_DE'));
  447. $group = new FieldGroup('author');
  448. $group->add($field);
  449. $group->setLocale('de_DE');
  450. $this->assertEquals(array(class_exists('\Locale', false) ? \Locale::getDefault() : 'en', 'de_DE'), $field->locales);
  451. }
  452. public function testSupportsClone()
  453. {
  454. $group = new FieldGroup('author');
  455. $group->add($this->createMockField('firstName'));
  456. $clone = clone $group;
  457. $this->assertNotSame($clone['firstName'], $group['firstName']);
  458. }
  459. public function testBindWithoutPriorSetData()
  460. {
  461. return; // TODO
  462. $field = $this->createMockField('firstName');
  463. $field->expects($this->any())
  464. ->method('getData')
  465. ->will($this->returnValue('Bernhard'));
  466. $group = new FieldGroup('author');
  467. $group->add($field);
  468. $group->bind(array('firstName' => 'Bernhard'));
  469. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getData());
  470. }
  471. protected function createMockField($key)
  472. {
  473. $field = $this->getMock(
  474. 'Symfony\Component\Form\FieldInterface',
  475. array(),
  476. array(),
  477. '',
  478. false, // don't use constructor
  479. false // don't call parent::__clone
  480. );
  481. $field->expects($this->any())
  482. ->method('getKey')
  483. ->will($this->returnValue($key));
  484. return $field;
  485. }
  486. protected function createInvalidMockField($key)
  487. {
  488. $field = $this->createMockField($key);
  489. $field->expects($this->any())
  490. ->method('isValid')
  491. ->will($this->returnValue(false));
  492. return $field;
  493. }
  494. protected function createValidMockField($key)
  495. {
  496. $field = $this->createMockField($key);
  497. $field->expects($this->any())
  498. ->method('isValid')
  499. ->will($this->returnValue(true));
  500. return $field;
  501. }
  502. protected function createNonMultipartMockField($key)
  503. {
  504. $field = $this->createMockField($key);
  505. $field->expects($this->any())
  506. ->method('isMultipart')
  507. ->will($this->returnValue(false));
  508. return $field;
  509. }
  510. protected function createMultipartMockField($key)
  511. {
  512. $field = $this->createMockField($key);
  513. $field->expects($this->any())
  514. ->method('isMultipart')
  515. ->will($this->returnValue(true));
  516. return $field;
  517. }
  518. protected function createMockTransformer()
  519. {
  520. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  521. }
  522. }