FieldTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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/InvalidField.php';
  6. require_once __DIR__ . '/Fixtures/RequiredOptionsField.php';
  7. use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
  8. use Symfony\Component\Form\PropertyPath;
  9. use Symfony\Tests\Component\Form\Fixtures\Author;
  10. use Symfony\Tests\Component\Form\Fixtures\TestField;
  11. use Symfony\Tests\Component\Form\Fixtures\InvalidField;
  12. use Symfony\Tests\Component\Form\Fixtures\RequiredOptionsField;
  13. class FieldTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $field;
  16. protected function setUp()
  17. {
  18. $this->field = new TestField('title');
  19. }
  20. public function testGetPropertyPath_defaultPath()
  21. {
  22. $field = new TestField('title');
  23. $this->assertEquals(new PropertyPath('title'), $field->getPropertyPath());
  24. }
  25. public function testGetPropertyPath_pathIsZero()
  26. {
  27. $field = new TestField('title', array('property_path' => '0'));
  28. $this->assertEquals(new PropertyPath('0'), $field->getPropertyPath());
  29. }
  30. public function testGetPropertyPath_pathIsEmpty()
  31. {
  32. $field = new TestField('title', array('property_path' => ''));
  33. $this->assertEquals(null, $field->getPropertyPath());
  34. }
  35. public function testGetPropertyPath_pathIsNull()
  36. {
  37. $field = new TestField('title', array('property_path' => null));
  38. $this->assertEquals(null, $field->getPropertyPath());
  39. }
  40. public function testPassRequiredAsOption()
  41. {
  42. $field = new TestField('title', array('required' => false));
  43. $this->assertFalse($field->isRequired());
  44. $field = new TestField('title', array('required' => true));
  45. $this->assertTrue($field->isRequired());
  46. }
  47. public function testPassDisabledAsOption()
  48. {
  49. $field = new TestField('title', array('disabled' => false));
  50. $this->assertFalse($field->isDisabled());
  51. $field = new TestField('title', array('disabled' => true));
  52. $this->assertTrue($field->isDisabled());
  53. }
  54. public function testFieldIsDisabledIfParentIsDisabled()
  55. {
  56. $field = new TestField('title', array('disabled' => false));
  57. $field->setParent(new TestField('title', array('disabled' => true)));
  58. $this->assertTrue($field->isDisabled());
  59. }
  60. public function testFieldWithNoErrorsIsValid()
  61. {
  62. $this->field->bind('data');
  63. $this->assertTrue($this->field->isValid());
  64. }
  65. public function testFieldWithErrorsIsInvalid()
  66. {
  67. $this->field->bind('data');
  68. $this->field->addError('Some error');
  69. $this->assertFalse($this->field->isValid());
  70. }
  71. public function testBindResetsErrors()
  72. {
  73. $this->field->addError('Some error');
  74. $this->field->bind('data');
  75. $this->assertTrue($this->field->isValid());
  76. }
  77. public function testUnboundFieldIsInvalid()
  78. {
  79. $this->assertFalse($this->field->isValid());
  80. }
  81. public function testGetNameReturnsKey()
  82. {
  83. $this->assertEquals('title', $this->field->getName());
  84. }
  85. public function testGetNameIncludesParent()
  86. {
  87. $this->field->setParent($this->createMockGroupWithName('news[article]'));
  88. $this->assertEquals('news[article][title]', $this->field->getName());
  89. }
  90. public function testGetIdReturnsKey()
  91. {
  92. $this->assertEquals('title', $this->field->getId());
  93. }
  94. public function testGetIdIncludesParent()
  95. {
  96. $this->field->setParent($this->createMockGroupWithId('news_article'));
  97. $this->assertEquals('news_article_title', $this->field->getId());
  98. }
  99. public function testLocaleIsPassedToLocalizableValueTransformer_setLocaleCalledBefore()
  100. {
  101. $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
  102. $transformer->expects($this->once())
  103. ->method('setLocale')
  104. ->with($this->equalTo('de_DE'));
  105. $this->field->setLocale('de_DE');
  106. $this->field->setValueTransformer($transformer);
  107. }
  108. public function testLocaleIsPassedToValueTransformer_setLocaleCalledAfter()
  109. {
  110. $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
  111. $transformer->expects($this->exactly(2))
  112. ->method('setLocale'); // we can't test the params cause they differ :(
  113. $this->field->setValueTransformer($transformer);
  114. $this->field->setLocale('de_DE');
  115. }
  116. public function testIsRequiredReturnsOwnValueIfNoParent()
  117. {
  118. $this->field->setRequired(true);
  119. $this->assertTrue($this->field->isRequired());
  120. $this->field->setRequired(false);
  121. $this->assertFalse($this->field->isRequired());
  122. }
  123. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  124. {
  125. $group = $this->createMockGroup();
  126. $group->expects($this->any())
  127. ->method('isRequired')
  128. ->will($this->returnValue(true));
  129. $this->field->setParent($group);
  130. $this->field->setRequired(true);
  131. $this->assertTrue($this->field->isRequired());
  132. $this->field->setRequired(false);
  133. $this->assertFalse($this->field->isRequired());
  134. }
  135. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  136. {
  137. $group = $this->createMockGroup();
  138. $group->expects($this->any())
  139. ->method('isRequired')
  140. ->will($this->returnValue(false));
  141. $this->field->setParent($group);
  142. $this->field->setRequired(true);
  143. $this->assertFalse($this->field->isRequired());
  144. }
  145. public function testExceptionIfUnknownOption()
  146. {
  147. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidOptionsException');
  148. new RequiredOptionsField('name', array('bar' => 'baz', 'moo' => 'maa'));
  149. }
  150. public function testExceptionIfMissingOption()
  151. {
  152. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  153. new RequiredOptionsField('name');
  154. }
  155. public function testIsBound()
  156. {
  157. $this->assertFalse($this->field->isBound());
  158. $this->field->bind('symfony');
  159. $this->assertTrue($this->field->isBound());
  160. }
  161. public function testDefaultValuesAreTransformedCorrectly()
  162. {
  163. $field = new TestField('name');
  164. $this->assertEquals(null, $this->field->getData());
  165. $this->assertEquals('', $this->field->getDisplayedData());
  166. }
  167. public function testValuesAreTransformedCorrectlyIfNull()
  168. {
  169. // The value is converted to an empty string and NOT passed to the
  170. // value transformer
  171. $transformer = $this->createMockTransformer();
  172. $transformer->expects($this->never())
  173. ->method('transform');
  174. $this->field->setValueTransformer($transformer);
  175. $this->field->setData(null);
  176. $this->assertSame(null, $this->field->getData());
  177. $this->assertSame('', $this->field->getDisplayedData());
  178. }
  179. public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
  180. {
  181. $this->field->setData(null);
  182. $this->assertSame(null, $this->field->getData());
  183. $this->assertSame('', $this->field->getDisplayedData());
  184. }
  185. public function testBoundValuesAreTransformedCorrectly()
  186. {
  187. $field = $this->getMock(
  188. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  189. array('processData'), // only mock processData()
  190. array('title')
  191. );
  192. // 1. The value is converted to a string and passed to the value transformer
  193. $transformer = $this->createMockTransformer();
  194. $transformer->expects($this->once())
  195. ->method('reverseTransform')
  196. ->with($this->identicalTo('0'))
  197. ->will($this->returnValue('reverse[0]'));
  198. $field->setValueTransformer($transformer);
  199. // 2. The output of the reverse transformation is passed to processData()
  200. $field->expects($this->once())
  201. ->method('processData')
  202. ->with($this->equalTo('reverse[0]'))
  203. ->will($this->returnValue('processed[reverse[0]]'));
  204. // 3. The processed data is transformed again (for displayed data)
  205. $transformer->expects($this->once())
  206. ->method('transform')
  207. ->with($this->equalTo('processed[reverse[0]]'))
  208. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  209. $field->bind(0);
  210. $this->assertEquals('processed[reverse[0]]', $field->getData());
  211. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  212. }
  213. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  214. {
  215. $field = $this->getMock(
  216. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  217. array('processData'), // only mock processData()
  218. array('title')
  219. );
  220. // 1. Empty values are always converted to NULL. They are never passed to
  221. // the value transformer
  222. $transformer = $this->createMockTransformer();
  223. $transformer->expects($this->never())
  224. ->method('reverseTransform');
  225. $field->setValueTransformer($transformer);
  226. // 2. NULL is passed to processData()
  227. $field->expects($this->once())
  228. ->method('processData')
  229. ->with($this->identicalTo(null))
  230. ->will($this->returnValue('processed'));
  231. // 3. The processed data is transformed (for displayed data)
  232. $transformer->expects($this->once())
  233. ->method('transform')
  234. ->with($this->equalTo('processed'))
  235. ->will($this->returnValue('transform[processed]'));
  236. $field->bind('');
  237. $this->assertSame('processed', $field->getData());
  238. $this->assertEquals('transform[processed]', $field->getDisplayedData());
  239. }
  240. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
  241. {
  242. // 1. Empty values are always converted to NULL. They are never passed to
  243. // the value transformer
  244. $transformer = $this->createMockTransformer();
  245. $transformer->expects($this->never())
  246. ->method('reverseTransform');
  247. $this->field->setValueTransformer($transformer);
  248. // 2. The processed data is NULL and therefore transformed to an empty
  249. // string. It is NOT passed to the value transformer
  250. $transformer->expects($this->never())
  251. ->method('transform');
  252. $this->field->bind('');
  253. $this->assertSame(null, $this->field->getData());
  254. $this->assertEquals('', $this->field->getDisplayedData());
  255. }
  256. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  257. {
  258. $this->field->bind('');
  259. $this->assertSame(null, $this->field->getData());
  260. $this->assertEquals('', $this->field->getDisplayedData());
  261. }
  262. public function testValuesAreTransformedCorrectly()
  263. {
  264. // The value is passed to the value transformer
  265. $transformer = $this->createMockTransformer();
  266. $transformer->expects($this->once())
  267. ->method('transform')
  268. ->with($this->identicalTo(0))
  269. ->will($this->returnValue('transform[0]'));
  270. $this->field->setValueTransformer($transformer);
  271. $this->field->setData(0);
  272. $this->assertEquals(0, $this->field->getData());
  273. $this->assertEquals('transform[0]', $this->field->getDisplayedData());
  274. }
  275. public function testBoundValuesAreTrimmedBeforeTransforming()
  276. {
  277. // The value is passed to the value transformer
  278. $transformer = $this->createMockTransformer();
  279. $transformer->expects($this->once())
  280. ->method('reverseTransform')
  281. ->with($this->identicalTo('a'))
  282. ->will($this->returnValue('reverse[a]'));
  283. $transformer->expects($this->once())
  284. ->method('transform')
  285. ->with($this->identicalTo('reverse[a]'))
  286. ->will($this->returnValue('a'));
  287. $this->field->setValueTransformer($transformer);
  288. $this->field->bind(' a ');
  289. $this->assertEquals('a', $this->field->getDisplayedData());
  290. $this->assertEquals('reverse[a]', $this->field->getData());
  291. }
  292. public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
  293. {
  294. // The value is passed to the value transformer
  295. $transformer = $this->createMockTransformer();
  296. $transformer->expects($this->once())
  297. ->method('reverseTransform')
  298. ->with($this->identicalTo(' a '))
  299. ->will($this->returnValue('reverse[ a ]'));
  300. $transformer->expects($this->once())
  301. ->method('transform')
  302. ->with($this->identicalTo('reverse[ a ]'))
  303. ->will($this->returnValue(' a '));
  304. $field = new TestField('title', array('trim' => false));
  305. $field->setValueTransformer($transformer);
  306. $field->bind(' a ');
  307. $this->assertEquals(' a ', $field->getDisplayedData());
  308. $this->assertEquals('reverse[ a ]', $field->getData());
  309. }
  310. public function testUpdateFromObjectReadsArray()
  311. {
  312. $array = array('firstName' => 'Bernhard');
  313. $field = new TestField('firstName');
  314. $field->updateFromObject($array);
  315. $this->assertEquals('Bernhard', $field->getData());
  316. }
  317. public function testUpdateFromObjectReadsArrayWithCustomPropertyPath()
  318. {
  319. $array = array('child' => array('index' => array('firstName' => 'Bernhard')));
  320. $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
  321. $field->updateFromObject($array);
  322. $this->assertEquals('Bernhard', $field->getData());
  323. }
  324. public function testUpdateFromObjectReadsProperty()
  325. {
  326. $object = new Author();
  327. $object->firstName = 'Bernhard';
  328. $field = new TestField('firstName');
  329. $field->updateFromObject($object);
  330. $this->assertEquals('Bernhard', $field->getData());
  331. }
  332. public function testUpdateFromObjectReadsPropertyWithCustomPropertyPath()
  333. {
  334. $object = new Author();
  335. $object->child = array();
  336. $object->child['index'] = new Author();
  337. $object->child['index']->firstName = 'Bernhard';
  338. $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
  339. $field->updateFromObject($object);
  340. $this->assertEquals('Bernhard', $field->getData());
  341. }
  342. public function testUpdateFromObjectReadsArrayAccess()
  343. {
  344. $object = new \ArrayObject();
  345. $object['firstName'] = 'Bernhard';
  346. $field = new TestField('firstName', array('property_path' => '[firstName]'));
  347. $field->updateFromObject($object);
  348. $this->assertEquals('Bernhard', $field->getData());
  349. }
  350. public function testUpdateFromObjectThrowsExceptionIfArrayAccessExpected()
  351. {
  352. $field = new TestField('firstName', array('property_path' => '[firstName]'));
  353. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  354. $field->updateFromObject(new Author());
  355. }
  356. /*
  357. * The use case of this test is a field group with an empty property path.
  358. * Even if the field group itself is not associated to a specific property,
  359. * nested fields might be.
  360. */
  361. public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
  362. {
  363. $object = new Author();
  364. $object->firstName = 'Bernhard';
  365. $field = new TestField('firstName', array('property_path' => null));
  366. $field->updateFromObject($object);
  367. $this->assertEquals($object, $field->getData());
  368. }
  369. public function testUpdateFromObjectThrowsExceptionIfPropertyIsNotPublic()
  370. {
  371. $field = new TestField('privateProperty');
  372. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  373. $field->updateFromObject(new Author());
  374. }
  375. public function testUpdateFromObjectReadsGetters()
  376. {
  377. $object = new Author();
  378. $object->setLastName('Schussek');
  379. $field = new TestField('lastName');
  380. $field->updateFromObject($object);
  381. $this->assertEquals('Schussek', $field->getData());
  382. }
  383. public function testUpdateFromObjectThrowsExceptionIfGetterIsNotPublic()
  384. {
  385. $field = new TestField('privateGetter');
  386. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  387. $field->updateFromObject(new Author());
  388. }
  389. public function testUpdateFromObjectReadsIssers()
  390. {
  391. $object = new Author();
  392. $object->setAustralian(false);
  393. $field = new TestField('australian');
  394. $field->updateFromObject($object);
  395. $this->assertSame(false, $field->getData());
  396. }
  397. public function testUpdateFromObjectThrowsExceptionIfIsserIsNotPublic()
  398. {
  399. $field = new TestField('privateIsser');
  400. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  401. $field->updateFromObject(new Author());
  402. }
  403. public function testUpdateFromObjectThrowsExceptionIfPropertyDoesNotExist()
  404. {
  405. $field = new TestField('foobar');
  406. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  407. $field->updateFromObject(new Author());
  408. }
  409. public function testUpdateObjectUpdatesArrays()
  410. {
  411. $array = array();
  412. $field = new TestField('firstName');
  413. $field->bind('Bernhard');
  414. $field->updateObject($array);
  415. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  416. }
  417. public function testUpdateObjectUpdatesArraysWithCustomPropertyPath()
  418. {
  419. $array = array();
  420. $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
  421. $field->bind('Bernhard');
  422. $field->updateObject($array);
  423. $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
  424. }
  425. /*
  426. * This is important so that bind() can work even if setData() was not called
  427. * before
  428. */
  429. public function testUpdateObjectTreatsEmptyValuesAsArrays()
  430. {
  431. $array = null;
  432. $field = new TestField('firstName');
  433. $field->bind('Bernhard');
  434. $field->updateObject($array);
  435. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  436. }
  437. public function testUpdateObjectUpdatesProperties()
  438. {
  439. $object = new Author();
  440. $field = new TestField('firstName');
  441. $field->bind('Bernhard');
  442. $field->updateObject($object);
  443. $this->assertEquals('Bernhard', $object->firstName);
  444. }
  445. public function testUpdateObjectUpdatesPropertiesWithCustomPropertyPath()
  446. {
  447. $object = new Author();
  448. $object->child = array();
  449. $object->child['index'] = new Author();
  450. $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
  451. $field->bind('Bernhard');
  452. $field->updateObject($object);
  453. $this->assertEquals('Bernhard', $object->child['index']->firstName);
  454. }
  455. public function testUpdateObjectUpdatesArrayAccess()
  456. {
  457. $object = new \ArrayObject();
  458. $field = new TestField('firstName', array('property_path' => '[firstName]'));
  459. $field->bind('Bernhard');
  460. $field->updateObject($object);
  461. $this->assertEquals('Bernhard', $object['firstName']);
  462. }
  463. public function testUpdateObjectThrowsExceptionIfArrayAccessExpected()
  464. {
  465. $field = new TestField('firstName', array('property_path' => '[firstName]'));
  466. $field->bind('Bernhard');
  467. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  468. $field->updateObject(new Author());
  469. }
  470. public function testUpdateObjectDoesNotUpdatePropertyIfPropertyPathIsEmpty()
  471. {
  472. $object = new Author();
  473. $field = new TestField('firstName', array('property_path' => null));
  474. $field->bind('Bernhard');
  475. $field->updateObject($object);
  476. $this->assertEquals(null, $object->firstName);
  477. }
  478. public function testUpdateObjectUpdatesSetters()
  479. {
  480. $object = new Author();
  481. $field = new TestField('lastName');
  482. $field->bind('Schussek');
  483. $field->updateObject($object);
  484. $this->assertEquals('Schussek', $object->getLastName());
  485. }
  486. public function testUpdateObjectThrowsExceptionIfGetterIsNotPublic()
  487. {
  488. $field = new TestField('privateSetter');
  489. $field->bind('foobar');
  490. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  491. $field->updateObject(new Author());
  492. }
  493. protected function createMockTransformer()
  494. {
  495. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  496. }
  497. protected function createMockTransformerTransformingTo($value)
  498. {
  499. $transformer = $this->createMockTransformer();
  500. $transformer->expects($this->any())
  501. ->method('reverseTransform')
  502. ->will($this->returnValue($value));
  503. return $transformer;
  504. }
  505. protected function createMockGroup()
  506. {
  507. return $this->getMock(
  508. 'Symfony\Component\Form\FieldGroup',
  509. array(),
  510. array(),
  511. '',
  512. false // don't call constructor
  513. );
  514. }
  515. protected function createMockGroupWithName($name)
  516. {
  517. $group = $this->createMockGroup();
  518. $group->expects($this->any())
  519. ->method('getName')
  520. ->will($this->returnValue($name));
  521. return $group;
  522. }
  523. protected function createMockGroupWithId($id)
  524. {
  525. $group = $this->createMockGroup();
  526. $group->expects($this->any())
  527. ->method('getId')
  528. ->will($this->returnValue($id));
  529. return $group;
  530. }
  531. }