FieldTest.php 22 KB

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