FieldGroupTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. require_once __DIR__ . '/Fixtures/Author.php';
  4. require_once __DIR__ . '/Fixtures/TestField.php';
  5. require_once __DIR__ . '/Fixtures/TestFieldGroup.php';
  6. use Symfony\Component\Form\Field;
  7. use Symfony\Component\Form\FieldError;
  8. use Symfony\Component\Form\FieldInterface;
  9. use Symfony\Component\Form\FieldGroup;
  10. use Symfony\Component\Form\PropertyPath;
  11. use Symfony\Tests\Component\Form\Fixtures\Author;
  12. use Symfony\Tests\Component\Form\Fixtures\TestField;
  13. use Symfony\Tests\Component\Form\Fixtures\TestFieldGroup;
  14. class FieldGroupTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testSupportsArrayAccess()
  17. {
  18. $group = new TestFieldGroup('author');
  19. $group->add($this->createMockField('firstName'));
  20. $this->assertEquals($group->get('firstName'), $group['firstName']);
  21. $this->assertTrue(isset($group['firstName']));
  22. }
  23. public function testSupportsUnset()
  24. {
  25. $group = new TestFieldGroup('author');
  26. $group->add($this->createMockField('firstName'));
  27. unset($group['firstName']);
  28. $this->assertFalse(isset($group['firstName']));
  29. }
  30. public function testDoesNotSupportAddingFields()
  31. {
  32. $group = new TestFieldGroup('author');
  33. $this->setExpectedException('LogicException');
  34. $group[] = $this->createMockField('lastName');
  35. }
  36. public function testSupportsCountable()
  37. {
  38. $group = new TestFieldGroup('group');
  39. $group->add($this->createMockField('firstName'));
  40. $group->add($this->createMockField('lastName'));
  41. $this->assertEquals(2, count($group));
  42. $group->add($this->createMockField('australian'));
  43. $this->assertEquals(3, count($group));
  44. }
  45. public function testSupportsIterable()
  46. {
  47. $group = new TestFieldGroup('group');
  48. $group->add($field1 = $this->createMockField('field1'));
  49. $group->add($field2 = $this->createMockField('field2'));
  50. $group->add($field3 = $this->createMockField('field3'));
  51. $expected = array(
  52. 'field1' => $field1,
  53. 'field2' => $field2,
  54. 'field3' => $field3,
  55. );
  56. $this->assertEquals($expected, iterator_to_array($group));
  57. }
  58. public function testIsBound()
  59. {
  60. $group = new TestFieldGroup('author');
  61. $this->assertFalse($group->isBound());
  62. $group->bind(array('firstName' => 'Bernhard'));
  63. $this->assertTrue($group->isBound());
  64. }
  65. public function testValidIfAllFieldsAreValid()
  66. {
  67. $group = new TestFieldGroup('author');
  68. $group->add($this->createValidMockField('firstName'));
  69. $group->add($this->createValidMockField('lastName'));
  70. $group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  71. $this->assertTrue($group->isValid());
  72. }
  73. public function testInvalidIfFieldIsInvalid()
  74. {
  75. $group = new TestFieldGroup('author');
  76. $group->add($this->createInvalidMockField('firstName'));
  77. $group->add($this->createValidMockField('lastName'));
  78. $group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  79. $this->assertFalse($group->isValid());
  80. }
  81. public function testInvalidIfBoundWithExtraFields()
  82. {
  83. $group = new TestFieldGroup('author');
  84. $group->add($this->createValidMockField('firstName'));
  85. $group->add($this->createValidMockField('lastName'));
  86. $group->bind(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
  87. $this->assertTrue($group->isBoundWithExtraFields());
  88. }
  89. public function testHasNoErrorsIfOnlyFieldHasErrors()
  90. {
  91. $group = new TestFieldGroup('author');
  92. $group->add($this->createInvalidMockField('firstName'));
  93. $group->bind(array('firstName' => 'Bernhard'));
  94. $this->assertFalse($group->hasErrors());
  95. }
  96. public function testBindForwardsPreprocessedData()
  97. {
  98. $field = $this->createMockField('firstName');
  99. $group = $this->getMock(
  100. 'Symfony\Tests\Component\Form\Fixtures\TestFieldGroup',
  101. array('preprocessData'), // only mock preprocessData()
  102. array('author')
  103. );
  104. // The data array is prepared directly after binding
  105. $group->expects($this->once())
  106. ->method('preprocessData')
  107. ->with($this->equalTo(array('firstName' => 'Bernhard')))
  108. ->will($this->returnValue(array('firstName' => 'preprocessed[Bernhard]')));
  109. $group->add($field);
  110. // The preprocessed data is then forwarded to the fields
  111. $field->expects($this->once())
  112. ->method('bind')
  113. ->with($this->equalTo('preprocessed[Bernhard]'));
  114. $group->bind(array('firstName' => 'Bernhard'));
  115. }
  116. public function testBindForwardsNullIfValueIsMissing()
  117. {
  118. $field = $this->createMockField('firstName');
  119. $field->expects($this->once())
  120. ->method('bind')
  121. ->with($this->equalTo(null));
  122. $group = new TestFieldGroup('author');
  123. $group->add($field);
  124. $group->bind(array());
  125. }
  126. public function testAddErrorMapsFieldValidationErrorsOntoFields()
  127. {
  128. $error = new FieldError('Message');
  129. $field = $this->createMockField('firstName');
  130. $field->expects($this->once())
  131. ->method('addError')
  132. ->with($this->equalTo($error));
  133. $group = new TestFieldGroup('author');
  134. $group->add($field);
  135. $path = new PropertyPath('fields[firstName].data');
  136. $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
  137. }
  138. public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups()
  139. {
  140. $error = new FieldError('Message');
  141. $field = $this->createMockField('firstName');
  142. $field->expects($this->once())
  143. ->method('addError')
  144. ->with($this->equalTo($error));
  145. $group = new TestFieldGroup('author');
  146. $innerGroup = new TestFieldGroup('names');
  147. $innerGroup->add($field);
  148. $group->add($innerGroup);
  149. $path = new PropertyPath('fields[names].fields[firstName].data');
  150. $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
  151. }
  152. public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
  153. {
  154. $error = new FieldError('Message');
  155. $field = $this->createMockField('foo');
  156. $field->expects($this->never())
  157. ->method('addError');
  158. $group = new TestFieldGroup('author');
  159. $group->add($field);
  160. $path = new PropertyPath('fields[bar].data');
  161. $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
  162. $this->assertEquals(array($error), $group->getErrors());
  163. }
  164. public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
  165. {
  166. $error = new FieldError('Message');
  167. $field = $this->createMockField('firstName');
  168. $field->expects($this->any())
  169. ->method('isHidden')
  170. ->will($this->returnValue(true));
  171. $field->expects($this->never())
  172. ->method('addError');
  173. $group = new TestFieldGroup('author');
  174. $group->add($field);
  175. $path = new PropertyPath('fields[firstName].data');
  176. $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
  177. $this->assertEquals(array($error), $group->getErrors());
  178. }
  179. public function testAddErrorMapsDataValidationErrorsOntoFields()
  180. {
  181. $error = new FieldError('Message');
  182. // path is expected to point at "firstName"
  183. $expectedPath = new PropertyPath('firstName');
  184. $expectedPathIterator = $expectedPath->getIterator();
  185. $field = $this->createMockField('firstName');
  186. $field->expects($this->any())
  187. ->method('getPropertyPath')
  188. ->will($this->returnValue(new PropertyPath('firstName')));
  189. $field->expects($this->once())
  190. ->method('addError')
  191. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
  192. $group = new TestFieldGroup('author');
  193. $group->add($field);
  194. $path = new PropertyPath('firstName');
  195. $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
  196. }
  197. public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
  198. {
  199. $error = new FieldError('Message');
  200. $field = $this->createMockField('foo');
  201. $field->expects($this->any())
  202. ->method('getPropertyPath')
  203. ->will($this->returnValue(new PropertyPath('foo')));
  204. $field->expects($this->never())
  205. ->method('addError');
  206. $group = new TestFieldGroup('author');
  207. $group->add($field);
  208. $path = new PropertyPath('bar');
  209. $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
  210. }
  211. public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
  212. {
  213. $error = new FieldError('Message');
  214. $field = $this->createMockField('firstName');
  215. $field->expects($this->any())
  216. ->method('isHidden')
  217. ->will($this->returnValue(true));
  218. $field->expects($this->any())
  219. ->method('getPropertyPath')
  220. ->will($this->returnValue(new PropertyPath('firstName')));
  221. $field->expects($this->never())
  222. ->method('addError');
  223. $group = new TestFieldGroup('author');
  224. $group->add($field);
  225. $path = new PropertyPath('firstName');
  226. $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
  227. }
  228. public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
  229. {
  230. $error = new FieldError('Message');
  231. // path is expected to point at "street"
  232. $expectedPath = new PropertyPath('address.street');
  233. $expectedPathIterator = $expectedPath->getIterator();
  234. $expectedPathIterator->next();
  235. $field = $this->createMockField('address');
  236. $field->expects($this->any())
  237. ->method('getPropertyPath')
  238. ->will($this->returnValue(new PropertyPath('address')));
  239. $field->expects($this->once())
  240. ->method('addError')
  241. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
  242. $group = new TestFieldGroup('author');
  243. $group->add($field);
  244. $path = new PropertyPath('address.street');
  245. $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
  246. }
  247. public function testAddErrorMapsErrorsOntoFieldsInAnonymousGroups()
  248. {
  249. $error = new FieldError('Message');
  250. // path is expected to point at "address"
  251. $expectedPath = new PropertyPath('address');
  252. $expectedPathIterator = $expectedPath->getIterator();
  253. $field = $this->createMockField('address');
  254. $field->expects($this->any())
  255. ->method('getPropertyPath')
  256. ->will($this->returnValue(new PropertyPath('address')));
  257. $field->expects($this->once())
  258. ->method('addError')
  259. ->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
  260. $group = new TestFieldGroup('author');
  261. $group2 = new TestFieldGroup('anonymous', array('property_path' => null));
  262. $group2->add($field);
  263. $group->add($group2);
  264. $path = new PropertyPath('address');
  265. $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
  266. }
  267. public function testAddThrowsExceptionIfAlreadyBound()
  268. {
  269. $group = new TestFieldGroup('author');
  270. $group->add($this->createMockField('firstName'));
  271. $group->bind(array('firstName' => 'Bernhard'));
  272. $this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException');
  273. $group->add($this->createMockField('lastName'));
  274. }
  275. public function testAddSetsFieldParent()
  276. {
  277. $group = new TestFieldGroup('author');
  278. $field = $this->createMockField('firstName');
  279. $field->expects($this->once())
  280. ->method('setParent');
  281. // PHPUnit fails to compare infinitely recursive objects
  282. //->with($this->equalTo($group));
  283. $group->add($field);
  284. }
  285. public function testRemoveUnsetsFieldParent()
  286. {
  287. $group = new TestFieldGroup('author');
  288. $field = $this->createMockField('firstName');
  289. $field->expects($this->exactly(2))
  290. ->method('setParent');
  291. // PHPUnit fails to compare subsequent method calls with different arguments
  292. $group->add($field);
  293. $group->remove('firstName');
  294. }
  295. public function testAddUpdatesFieldFromTransformedData()
  296. {
  297. $originalAuthor = new Author();
  298. $transformedAuthor = new Author();
  299. // the authors should differ to make sure the test works
  300. $transformedAuthor->firstName = 'Foo';
  301. $group = new TestFieldGroup('author');
  302. $transformer = $this->createMockTransformer();
  303. $transformer->expects($this->once())
  304. ->method('transform')
  305. ->with($this->equalTo($originalAuthor))
  306. ->will($this->returnValue($transformedAuthor));
  307. $group->setValueTransformer($transformer);
  308. $group->setData($originalAuthor);
  309. $field = $this->createMockField('firstName');
  310. $field->expects($this->any())
  311. ->method('getPropertyPath')
  312. ->will($this->returnValue(new PropertyPath('firstName')));
  313. $field->expects($this->once())
  314. ->method('updateFromProperty')
  315. ->with($this->equalTo($transformedAuthor));
  316. $group->add($field);
  317. }
  318. public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
  319. {
  320. $originalAuthor = new Author();
  321. $group = new TestFieldGroup('author');
  322. $transformer = $this->createMockTransformer();
  323. $transformer->expects($this->once())
  324. ->method('transform')
  325. ->with($this->equalTo($originalAuthor))
  326. ->will($this->returnValue(''));
  327. $group->setValueTransformer($transformer);
  328. $group->setData($originalAuthor);
  329. $field = $this->createMockField('firstName');
  330. $field->expects($this->never())
  331. ->method('updateFromProperty');
  332. $group->add($field);
  333. }
  334. public function testSetDataUpdatesAllFieldsFromTransformedData()
  335. {
  336. $originalAuthor = new Author();
  337. $transformedAuthor = new Author();
  338. // the authors should differ to make sure the test works
  339. $transformedAuthor->firstName = 'Foo';
  340. $group = new TestFieldGroup('author');
  341. $transformer = $this->createMockTransformer();
  342. $transformer->expects($this->once())
  343. ->method('transform')
  344. ->with($this->equalTo($originalAuthor))
  345. ->will($this->returnValue($transformedAuthor));
  346. $group->setValueTransformer($transformer);
  347. $field = $this->createMockField('firstName');
  348. $field->expects($this->once())
  349. ->method('updateFromProperty')
  350. ->with($this->equalTo($transformedAuthor));
  351. $group->add($field);
  352. $field = $this->createMockField('lastName');
  353. $field->expects($this->once())
  354. ->method('updateFromProperty')
  355. ->with($this->equalTo($transformedAuthor));
  356. $group->add($field);
  357. $group->setData($originalAuthor);
  358. }
  359. public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
  360. {
  361. $group = new TestFieldGroup('author');
  362. $this->setExpectedException('InvalidArgumentException');
  363. $group->setData('foobar');
  364. }
  365. public function testBindUpdatesTransformedDataFromAllFields()
  366. {
  367. $originalAuthor = new Author();
  368. $transformedAuthor = new Author();
  369. // the authors should differ to make sure the test works
  370. $transformedAuthor->firstName = 'Foo';
  371. $group = new TestFieldGroup('author');
  372. $transformer = $this->createMockTransformer();
  373. $transformer->expects($this->exactly(2))
  374. ->method('transform')
  375. // the method is first called with NULL, then
  376. // with $originalAuthor -> not testable by PHPUnit
  377. // ->with($this->equalTo(null))
  378. // ->with($this->equalTo($originalAuthor))
  379. ->will($this->returnValue($transformedAuthor));
  380. $group->setValueTransformer($transformer);
  381. $group->setData($originalAuthor);
  382. $field = $this->createMockField('firstName');
  383. $field->expects($this->once())
  384. ->method('updateProperty')
  385. ->with($this->equalTo($transformedAuthor));
  386. $group->add($field);
  387. $field = $this->createMockField('lastName');
  388. $field->expects($this->once())
  389. ->method('updateProperty')
  390. ->with($this->equalTo($transformedAuthor));
  391. $group->add($field);
  392. $group->bind(array()); // irrelevant
  393. }
  394. public function testGetDataReturnsObject()
  395. {
  396. $group = new TestFieldGroup('author');
  397. $object = new \stdClass();
  398. $group->setData($object);
  399. $this->assertEquals($object, $group->getData());
  400. }
  401. public function testGetDisplayedDataForwardsCall()
  402. {
  403. $field = $this->createValidMockField('firstName');
  404. $field->expects($this->atLeastOnce())
  405. ->method('getDisplayedData')
  406. ->will($this->returnValue('Bernhard'));
  407. $group = new TestFieldGroup('author');
  408. $group->add($field);
  409. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getDisplayedData());
  410. }
  411. public function testIsMultipartIfAnyFieldIsMultipart()
  412. {
  413. $group = new TestFieldGroup('author');
  414. $group->add($this->createMultipartMockField('firstName'));
  415. $group->add($this->createNonMultipartMockField('lastName'));
  416. $this->assertTrue($group->isMultipart());
  417. }
  418. public function testIsNotMultipartIfNoFieldIsMultipart()
  419. {
  420. $group = new TestFieldGroup('author');
  421. $group->add($this->createNonMultipartMockField('firstName'));
  422. $group->add($this->createNonMultipartMockField('lastName'));
  423. $this->assertFalse($group->isMultipart());
  424. }
  425. public function testSupportsClone()
  426. {
  427. $group = new TestFieldGroup('author');
  428. $group->add($this->createMockField('firstName'));
  429. $clone = clone $group;
  430. $this->assertNotSame($clone['firstName'], $group['firstName']);
  431. }
  432. public function testBindWithoutPriorSetData()
  433. {
  434. return; // TODO
  435. $field = $this->createMockField('firstName');
  436. $field->expects($this->any())
  437. ->method('getData')
  438. ->will($this->returnValue('Bernhard'));
  439. $group = new TestFieldGroup('author');
  440. $group->add($field);
  441. $group->bind(array('firstName' => 'Bernhard'));
  442. $this->assertEquals(array('firstName' => 'Bernhard'), $group->getData());
  443. }
  444. public function testGetHiddenFieldsReturnsOnlyHiddenFields()
  445. {
  446. $group = $this->getGroupWithBothVisibleAndHiddenField();
  447. $hiddenFields = $group->getHiddenFields(true, false);
  448. $this->assertSame(array($group['hiddenField']), $hiddenFields);
  449. }
  450. public function testGetVisibleFieldsReturnsOnlyVisibleFields()
  451. {
  452. $group = $this->getGroupWithBothVisibleAndHiddenField();
  453. $visibleFields = $group->getVisibleFields(true, false);
  454. $this->assertSame(array($group['visibleField']), $visibleFields);
  455. }
  456. /**
  457. * Create a group containing two fields, "visibleField" and "hiddenField"
  458. *
  459. * @return FieldGroup
  460. */
  461. protected function getGroupWithBothVisibleAndHiddenField()
  462. {
  463. $group = new TestFieldGroup('testGroup');
  464. // add a visible field
  465. $visibleField = $this->createMockField('visibleField');
  466. $visibleField->expects($this->once())
  467. ->method('isHidden')
  468. ->will($this->returnValue(false));
  469. $group->add($visibleField);
  470. // add a hidden field
  471. $hiddenField = $this->createMockField('hiddenField');
  472. $hiddenField->expects($this->once())
  473. ->method('isHidden')
  474. ->will($this->returnValue(true));
  475. $group->add($hiddenField);
  476. return $group;
  477. }
  478. protected function createMockField($key)
  479. {
  480. $field = $this->getMock(
  481. 'Symfony\Component\Form\FieldInterface',
  482. array(),
  483. array(),
  484. '',
  485. false, // don't use constructor
  486. false // don't call parent::__clone
  487. );
  488. $field->expects($this->any())
  489. ->method('getKey')
  490. ->will($this->returnValue($key));
  491. return $field;
  492. }
  493. protected function createInvalidMockField($key)
  494. {
  495. $field = $this->createMockField($key);
  496. $field->expects($this->any())
  497. ->method('isValid')
  498. ->will($this->returnValue(false));
  499. return $field;
  500. }
  501. protected function createValidMockField($key)
  502. {
  503. $field = $this->createMockField($key);
  504. $field->expects($this->any())
  505. ->method('isValid')
  506. ->will($this->returnValue(true));
  507. return $field;
  508. }
  509. protected function createNonMultipartMockField($key)
  510. {
  511. $field = $this->createMockField($key);
  512. $field->expects($this->any())
  513. ->method('isMultipart')
  514. ->will($this->returnValue(false));
  515. return $field;
  516. }
  517. protected function createMultipartMockField($key)
  518. {
  519. $field = $this->createMockField($key);
  520. $field->expects($this->any())
  521. ->method('isMultipart')
  522. ->will($this->returnValue(true));
  523. return $field;
  524. }
  525. protected function createMockTransformer()
  526. {
  527. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  528. }
  529. }