FieldTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. //
  106. // $this->field->setLocale('de_DE');
  107. // $this->field->setValueTransformer($transformer);
  108. // }
  109. public function testLocaleIsPassedToValueTransformer_setLocaleCalledAfter()
  110. {
  111. $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
  112. $transformer->expects($this->exactly(2))
  113. ->method('setLocale'); // we can't test the params cause they differ :(
  114. $field = new TestField('title', array(
  115. 'value_transformer' => $transformer,
  116. ));
  117. $field->setLocale('de_DE');
  118. }
  119. public function testIsRequiredReturnsOwnValueIfNoParent()
  120. {
  121. $this->field->setRequired(true);
  122. $this->assertTrue($this->field->isRequired());
  123. $this->field->setRequired(false);
  124. $this->assertFalse($this->field->isRequired());
  125. }
  126. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  127. {
  128. $group = $this->createMockGroup();
  129. $group->expects($this->any())
  130. ->method('isRequired')
  131. ->will($this->returnValue(true));
  132. $this->field->setParent($group);
  133. $this->field->setRequired(true);
  134. $this->assertTrue($this->field->isRequired());
  135. $this->field->setRequired(false);
  136. $this->assertFalse($this->field->isRequired());
  137. }
  138. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  139. {
  140. $group = $this->createMockGroup();
  141. $group->expects($this->any())
  142. ->method('isRequired')
  143. ->will($this->returnValue(false));
  144. $this->field->setParent($group);
  145. $this->field->setRequired(true);
  146. $this->assertFalse($this->field->isRequired());
  147. }
  148. public function testExceptionIfUnknownOption()
  149. {
  150. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidOptionsException');
  151. new RequiredOptionsField('name', array('bar' => 'baz', 'moo' => 'maa'));
  152. }
  153. public function testExceptionIfMissingOption()
  154. {
  155. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  156. new RequiredOptionsField('name');
  157. }
  158. public function testIsBound()
  159. {
  160. $this->assertFalse($this->field->isBound());
  161. $this->field->bind('symfony');
  162. $this->assertTrue($this->field->isBound());
  163. }
  164. public function testDefaultValuesAreTransformedCorrectly()
  165. {
  166. $field = new TestField('name');
  167. $this->assertEquals(null, $this->field->getData());
  168. $this->assertEquals('', $this->field->getDisplayedData());
  169. }
  170. public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
  171. {
  172. $this->field->setData(null);
  173. $this->assertSame(null, $this->field->getData());
  174. $this->assertSame('', $this->field->getDisplayedData());
  175. }
  176. public function testBoundValuesAreTransformedCorrectly()
  177. {
  178. $valueTransformer = $this->createMockTransformer();
  179. $normTransformer = $this->createMockTransformer();
  180. $field = $this->getMock(
  181. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  182. array('processData'), // only mock processData()
  183. array('title', array(
  184. 'value_transformer' => $valueTransformer,
  185. 'normalization_transformer' => $normTransformer,
  186. ))
  187. );
  188. // 1a. The value is converted to a string and passed to the value transformer
  189. $valueTransformer->expects($this->once())
  190. ->method('reverseTransform')
  191. ->with($this->identicalTo('0'))
  192. ->will($this->returnValue('reverse[0]'));
  193. // 2. The output of the reverse transformation is passed to processData()
  194. // The processed data is accessible through getNormalizedData()
  195. $field->expects($this->once())
  196. ->method('processData')
  197. ->with($this->equalTo('reverse[0]'))
  198. ->will($this->returnValue('processed[reverse[0]]'));
  199. // 3. The processed data is denormalized and then accessible through
  200. // getData()
  201. $normTransformer->expects($this->once())
  202. ->method('reverseTransform')
  203. ->with($this->identicalTo('processed[reverse[0]]'))
  204. ->will($this->returnValue('denorm[processed[reverse[0]]]'));
  205. // 4. The processed data is transformed again and then accessible
  206. // through getDisplayedData()
  207. $valueTransformer->expects($this->once())
  208. ->method('transform')
  209. ->with($this->equalTo('processed[reverse[0]]'))
  210. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  211. $field->bind(0);
  212. $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData());
  213. $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData());
  214. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  215. }
  216. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  217. {
  218. $transformer = $this->createMockTransformer();
  219. $field = $this->getMock(
  220. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  221. array('processData'), // only mock processData()
  222. array('title', array(
  223. 'value_transformer' => $transformer,
  224. ))
  225. );
  226. // 1. Empty values are converted to NULL by convention
  227. $transformer->expects($this->once())
  228. ->method('reverseTransform')
  229. ->with($this->identicalTo(''))
  230. ->will($this->returnValue(null));
  231. // 2. NULL is passed to processData()
  232. $field->expects($this->once())
  233. ->method('processData')
  234. ->with($this->identicalTo(null))
  235. ->will($this->returnValue('processed'));
  236. // 3. The processed data is transformed (for displayed data)
  237. $transformer->expects($this->once())
  238. ->method('transform')
  239. ->with($this->equalTo('processed'))
  240. ->will($this->returnValue('transform[processed]'));
  241. $field->bind('');
  242. $this->assertSame('processed', $field->getData());
  243. $this->assertEquals('transform[processed]', $field->getDisplayedData());
  244. }
  245. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
  246. {
  247. $transformer = $this->createMockTransformer();
  248. $field = new TestField('title', array(
  249. 'value_transformer' => $transformer,
  250. ));
  251. // 1. Empty values are converted to NULL by convention
  252. $transformer->expects($this->once())
  253. ->method('reverseTransform')
  254. ->with($this->identicalTo(''))
  255. ->will($this->returnValue(null));
  256. // 2. The processed data is NULL and therefore transformed to an empty
  257. // string by convention
  258. $transformer->expects($this->once())
  259. ->method('transform')
  260. ->with($this->identicalTo(null))
  261. ->will($this->returnValue(''));
  262. $field->bind('');
  263. $this->assertSame(null, $field->getData());
  264. $this->assertEquals('', $field->getDisplayedData());
  265. }
  266. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  267. {
  268. $this->field->bind('');
  269. $this->assertSame(null, $this->field->getData());
  270. $this->assertEquals('', $this->field->getDisplayedData());
  271. }
  272. public function testValuesAreTransformedCorrectly()
  273. {
  274. // The value is first passed to the normalization transformer...
  275. $normTransformer = $this->createMockTransformer();
  276. $normTransformer->expects($this->exactly(2))
  277. ->method('transform')
  278. // Impossible to test with PHPUnit because called twice
  279. // ->with($this->identicalTo(0))
  280. ->will($this->returnValue('norm[0]'));
  281. // ...and then to the value transformer
  282. $valueTransformer = $this->createMockTransformer();
  283. $valueTransformer->expects($this->exactly(2))
  284. ->method('transform')
  285. // Impossible to test with PHPUnit because called twice
  286. // ->with($this->identicalTo('norm[0]'))
  287. ->will($this->returnValue('transform[norm[0]]'));
  288. $field = new TestField('title', array(
  289. 'value_transformer' => $valueTransformer,
  290. 'normalization_transformer' => $normTransformer,
  291. ));
  292. $field->setData(0);
  293. $this->assertEquals(0, $field->getData());
  294. $this->assertEquals('norm[0]', $field->getNormalizedData());
  295. $this->assertEquals('transform[norm[0]]', $field->getDisplayedData());
  296. }
  297. public function testBoundValuesAreTrimmedBeforeTransforming()
  298. {
  299. // The value is passed to the value transformer
  300. $transformer = $this->createMockTransformer();
  301. $transformer->expects($this->once())
  302. ->method('reverseTransform')
  303. ->with($this->identicalTo('a'))
  304. ->will($this->returnValue('reverse[a]'));
  305. $transformer->expects($this->exactly(2))
  306. ->method('transform')
  307. // Impossible to test with PHPUnit because called twice
  308. // ->with($this->identicalTo('reverse[a]'))
  309. ->will($this->returnValue('a'));
  310. $field = new TestField('title', array(
  311. 'value_transformer' => $transformer,
  312. ));
  313. $field->bind(' a ');
  314. $this->assertEquals('a', $field->getDisplayedData());
  315. $this->assertEquals('reverse[a]', $field->getData());
  316. }
  317. public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
  318. {
  319. // The value is passed to the value transformer
  320. $transformer = $this->createMockTransformer();
  321. $transformer->expects($this->once())
  322. ->method('reverseTransform')
  323. ->with($this->identicalTo(' a '))
  324. ->will($this->returnValue('reverse[ a ]'));
  325. $transformer->expects($this->exactly(2))
  326. ->method('transform')
  327. // Impossible to test with PHPUnit because called twice
  328. // ->with($this->identicalTo('reverse[ a ]'))
  329. ->will($this->returnValue(' a '));
  330. $field = new TestField('title', array(
  331. 'trim' => false,
  332. 'value_transformer' => $transformer,
  333. ));
  334. $field->bind(' a ');
  335. $this->assertEquals(' a ', $field->getDisplayedData());
  336. $this->assertEquals('reverse[ a ]', $field->getData());
  337. }
  338. /*
  339. * The use case of this test is a field group with an empty property path.
  340. * Even if the field group itself is not associated to a specific property,
  341. * nested fields might be.
  342. */
  343. public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
  344. {
  345. $object = new Author();
  346. $object->firstName = 'Bernhard';
  347. $field = new TestField('firstName', array('property_path' => null));
  348. $field->updateFromObject($object);
  349. $this->assertEquals($object, $field->getData());
  350. }
  351. /*
  352. * This is important so that bind() can work even if setData() was not called
  353. * before
  354. */
  355. public function testUpdateObjectTreatsEmptyValuesAsArrays()
  356. {
  357. $array = null;
  358. $field = new TestField('firstName');
  359. $field->bind('Bernhard');
  360. $field->updateObject($array);
  361. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  362. }
  363. public function testUpdateObjectDoesNotUpdatePropertyIfPropertyPathIsEmpty()
  364. {
  365. $object = new Author();
  366. $field = new TestField('firstName', array('property_path' => null));
  367. $field->bind('Bernhard');
  368. $field->updateObject($object);
  369. $this->assertEquals(null, $object->firstName);
  370. }
  371. protected function createMockTransformer()
  372. {
  373. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  374. }
  375. protected function createMockTransformerTransformingTo($value)
  376. {
  377. $transformer = $this->createMockTransformer();
  378. $transformer->expects($this->any())
  379. ->method('reverseTransform')
  380. ->will($this->returnValue($value));
  381. return $transformer;
  382. }
  383. protected function createMockGroup()
  384. {
  385. return $this->getMock(
  386. 'Symfony\Component\Form\FieldGroup',
  387. array(),
  388. array(),
  389. '',
  390. false // don't call constructor
  391. );
  392. }
  393. protected function createMockGroupWithName($name)
  394. {
  395. $group = $this->createMockGroup();
  396. $group->expects($this->any())
  397. ->method('getName')
  398. ->will($this->returnValue($name));
  399. return $group;
  400. }
  401. protected function createMockGroupWithId($id)
  402. {
  403. $group = $this->createMockGroup();
  404. $group->expects($this->any())
  405. ->method('getId')
  406. ->will($this->returnValue($id));
  407. return $group;
  408. }
  409. }