FieldTest.php 22 KB

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