FieldTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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\Component\Form\FieldError;
  10. use Symfony\Tests\Component\Form\Fixtures\Author;
  11. use Symfony\Tests\Component\Form\Fixtures\TestField;
  12. use Symfony\Tests\Component\Form\Fixtures\InvalidField;
  13. use Symfony\Tests\Component\Form\Fixtures\RequiredOptionsField;
  14. class FieldTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected $field;
  17. protected function setUp()
  18. {
  19. $this->field = new TestField('title');
  20. }
  21. public function testGetPropertyPath_defaultPath()
  22. {
  23. $field = new TestField('title');
  24. $this->assertEquals(new PropertyPath('title'), $field->getPropertyPath());
  25. }
  26. public function testGetPropertyPath_pathIsZero()
  27. {
  28. $field = new TestField('title', array('property_path' => '0'));
  29. $this->assertEquals(new PropertyPath('0'), $field->getPropertyPath());
  30. }
  31. public function testGetPropertyPath_pathIsEmpty()
  32. {
  33. $field = new TestField('title', array('property_path' => ''));
  34. $this->assertEquals(null, $field->getPropertyPath());
  35. }
  36. public function testGetPropertyPath_pathIsNull()
  37. {
  38. $field = new TestField('title', array('property_path' => null));
  39. $this->assertEquals(null, $field->getPropertyPath());
  40. }
  41. public function testPassRequiredAsOption()
  42. {
  43. $field = new TestField('title', array('required' => false));
  44. $this->assertFalse($field->isRequired());
  45. $field = new TestField('title', array('required' => true));
  46. $this->assertTrue($field->isRequired());
  47. }
  48. public function testPassDisabledAsOption()
  49. {
  50. $field = new TestField('title', array('disabled' => false));
  51. $this->assertFalse($field->isDisabled());
  52. $field = new TestField('title', array('disabled' => true));
  53. $this->assertTrue($field->isDisabled());
  54. }
  55. public function testFieldIsDisabledIfParentIsDisabled()
  56. {
  57. $field = new TestField('title', array('disabled' => false));
  58. $field->setParent(new TestField('title', array('disabled' => true)));
  59. $this->assertTrue($field->isDisabled());
  60. }
  61. public function testFieldWithNoErrorsIsValid()
  62. {
  63. $this->field->bind('data');
  64. $this->assertTrue($this->field->isValid());
  65. }
  66. public function testFieldWithErrorsIsInvalid()
  67. {
  68. $this->field->bind('data');
  69. $this->field->addError(new FieldError('Some error'));
  70. $this->assertFalse($this->field->isValid());
  71. }
  72. public function testBindResetsErrors()
  73. {
  74. $this->field->addError(new FieldError('Some error'));
  75. $this->field->bind('data');
  76. $this->assertTrue($this->field->isValid());
  77. }
  78. public function testUnboundFieldIsInvalid()
  79. {
  80. $this->assertFalse($this->field->isValid());
  81. }
  82. public function testGetNameReturnsKey()
  83. {
  84. $this->assertEquals('title', $this->field->getName());
  85. }
  86. public function testGetNameIncludesParent()
  87. {
  88. $this->field->setParent($this->createMockGroupWithName('news[article]'));
  89. $this->assertEquals('news[article][title]', $this->field->getName());
  90. }
  91. public function testGetIdReturnsKey()
  92. {
  93. $this->assertEquals('title', $this->field->getId());
  94. }
  95. public function testGetIdIncludesParent()
  96. {
  97. $this->field->setParent($this->createMockGroupWithId('news_article'));
  98. $this->assertEquals('news_article_title', $this->field->getId());
  99. }
  100. // public function testLocaleIsPassedToLocalizableValueTransformer_setLocaleCalledBefore()
  101. // {
  102. // $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
  103. // $transformer->expects($this->once())
  104. // ->method('setLocale')
  105. // ->with($this->equalTo('de_DE'));
  106. //
  107. // $this->field->setLocale('de_DE');
  108. // $this->field->setValueTransformer($transformer);
  109. // }
  110. public function testLocaleIsPassedToValueTransformer_setLocaleCalledAfter()
  111. {
  112. $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
  113. $transformer->expects($this->exactly(2))
  114. ->method('setLocale'); // we can't test the params cause they differ :(
  115. $field = new TestField('title', array(
  116. 'value_transformer' => $transformer,
  117. ));
  118. $field->setLocale('de_DE');
  119. }
  120. public function testIsRequiredReturnsOwnValueIfNoParent()
  121. {
  122. $this->field->setRequired(true);
  123. $this->assertTrue($this->field->isRequired());
  124. $this->field->setRequired(false);
  125. $this->assertFalse($this->field->isRequired());
  126. }
  127. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  128. {
  129. $group = $this->createMockGroup();
  130. $group->expects($this->any())
  131. ->method('isRequired')
  132. ->will($this->returnValue(true));
  133. $this->field->setParent($group);
  134. $this->field->setRequired(true);
  135. $this->assertTrue($this->field->isRequired());
  136. $this->field->setRequired(false);
  137. $this->assertFalse($this->field->isRequired());
  138. }
  139. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  140. {
  141. $group = $this->createMockGroup();
  142. $group->expects($this->any())
  143. ->method('isRequired')
  144. ->will($this->returnValue(false));
  145. $this->field->setParent($group);
  146. $this->field->setRequired(true);
  147. $this->assertFalse($this->field->isRequired());
  148. }
  149. public function testExceptionIfUnknownOption()
  150. {
  151. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidOptionsException');
  152. new RequiredOptionsField('name', array('bar' => 'baz', 'moo' => 'maa'));
  153. }
  154. public function testExceptionIfMissingOption()
  155. {
  156. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  157. new RequiredOptionsField('name');
  158. }
  159. public function testIsBound()
  160. {
  161. $this->assertFalse($this->field->isBound());
  162. $this->field->bind('symfony');
  163. $this->assertTrue($this->field->isBound());
  164. }
  165. public function testDefaultValuesAreTransformedCorrectly()
  166. {
  167. $field = new TestField('name');
  168. $this->assertEquals(null, $this->field->getData());
  169. $this->assertEquals('', $this->field->getDisplayedData());
  170. }
  171. public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
  172. {
  173. $this->field->setData(null);
  174. $this->assertSame(null, $this->field->getData());
  175. $this->assertSame('', $this->field->getDisplayedData());
  176. }
  177. public function testBoundValuesAreTransformedCorrectly()
  178. {
  179. $valueTransformer = $this->createMockTransformer();
  180. $normTransformer = $this->createMockTransformer();
  181. $field = $this->getMock(
  182. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  183. array('processData'), // only mock processData()
  184. array('title', array(
  185. 'value_transformer' => $valueTransformer,
  186. 'normalization_transformer' => $normTransformer,
  187. ))
  188. );
  189. // 1a. The value is converted to a string and passed to the value transformer
  190. $valueTransformer->expects($this->once())
  191. ->method('reverseTransform')
  192. ->with($this->identicalTo('0'))
  193. ->will($this->returnValue('reverse[0]'));
  194. // 2. The output of the reverse transformation is passed to processData()
  195. // The processed data is accessible through getNormalizedData()
  196. $field->expects($this->once())
  197. ->method('processData')
  198. ->with($this->equalTo('reverse[0]'))
  199. ->will($this->returnValue('processed[reverse[0]]'));
  200. // 3. The processed data is denormalized and then accessible through
  201. // getData()
  202. $normTransformer->expects($this->once())
  203. ->method('reverseTransform')
  204. ->with($this->identicalTo('processed[reverse[0]]'))
  205. ->will($this->returnValue('denorm[processed[reverse[0]]]'));
  206. // 4. The processed data is transformed again and then accessible
  207. // through getDisplayedData()
  208. $valueTransformer->expects($this->once())
  209. ->method('transform')
  210. ->with($this->equalTo('processed[reverse[0]]'))
  211. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  212. $field->bind(0);
  213. $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData());
  214. $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData());
  215. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  216. }
  217. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  218. {
  219. $transformer = $this->createMockTransformer();
  220. $field = $this->getMock(
  221. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  222. array('processData'), // only mock processData()
  223. array('title', array(
  224. 'value_transformer' => $transformer,
  225. ))
  226. );
  227. // 1. Empty values are converted to NULL by convention
  228. $transformer->expects($this->once())
  229. ->method('reverseTransform')
  230. ->with($this->identicalTo(''))
  231. ->will($this->returnValue(null));
  232. // 2. NULL is passed to processData()
  233. $field->expects($this->once())
  234. ->method('processData')
  235. ->with($this->identicalTo(null))
  236. ->will($this->returnValue('processed'));
  237. // 3. The processed data is transformed (for displayed data)
  238. $transformer->expects($this->once())
  239. ->method('transform')
  240. ->with($this->equalTo('processed'))
  241. ->will($this->returnValue('transform[processed]'));
  242. $field->bind('');
  243. $this->assertSame('processed', $field->getData());
  244. $this->assertEquals('transform[processed]', $field->getDisplayedData());
  245. }
  246. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
  247. {
  248. $transformer = $this->createMockTransformer();
  249. $field = new TestField('title', array(
  250. 'value_transformer' => $transformer,
  251. ));
  252. // 1. Empty values are converted to NULL by convention
  253. $transformer->expects($this->once())
  254. ->method('reverseTransform')
  255. ->with($this->identicalTo(''))
  256. ->will($this->returnValue(null));
  257. // 2. The processed data is NULL and therefore transformed to an empty
  258. // string by convention
  259. $transformer->expects($this->once())
  260. ->method('transform')
  261. ->with($this->identicalTo(null))
  262. ->will($this->returnValue(''));
  263. $field->bind('');
  264. $this->assertSame(null, $field->getData());
  265. $this->assertEquals('', $field->getDisplayedData());
  266. }
  267. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  268. {
  269. $this->field->bind('');
  270. $this->assertSame(null, $this->field->getData());
  271. $this->assertEquals('', $this->field->getDisplayedData());
  272. }
  273. public function testValuesAreTransformedCorrectly()
  274. {
  275. // The value is first passed to the normalization transformer...
  276. $normTransformer = $this->createMockTransformer();
  277. $normTransformer->expects($this->exactly(2))
  278. ->method('transform')
  279. // Impossible to test with PHPUnit because called twice
  280. // ->with($this->identicalTo(0))
  281. ->will($this->returnValue('norm[0]'));
  282. // ...and then to the value transformer
  283. $valueTransformer = $this->createMockTransformer();
  284. $valueTransformer->expects($this->exactly(2))
  285. ->method('transform')
  286. // Impossible to test with PHPUnit because called twice
  287. // ->with($this->identicalTo('norm[0]'))
  288. ->will($this->returnValue('transform[norm[0]]'));
  289. $field = new TestField('title', array(
  290. 'value_transformer' => $valueTransformer,
  291. 'normalization_transformer' => $normTransformer,
  292. ));
  293. $field->setData(0);
  294. $this->assertEquals(0, $field->getData());
  295. $this->assertEquals('norm[0]', $field->getNormalizedData());
  296. $this->assertEquals('transform[norm[0]]', $field->getDisplayedData());
  297. }
  298. public function testBoundValuesAreTrimmedBeforeTransforming()
  299. {
  300. // The value is passed to the value transformer
  301. $transformer = $this->createMockTransformer();
  302. $transformer->expects($this->once())
  303. ->method('reverseTransform')
  304. ->with($this->identicalTo('a'))
  305. ->will($this->returnValue('reverse[a]'));
  306. $transformer->expects($this->exactly(2))
  307. ->method('transform')
  308. // Impossible to test with PHPUnit because called twice
  309. // ->with($this->identicalTo('reverse[a]'))
  310. ->will($this->returnValue('a'));
  311. $field = new TestField('title', array(
  312. 'value_transformer' => $transformer,
  313. ));
  314. $field->bind(' a ');
  315. $this->assertEquals('a', $field->getDisplayedData());
  316. $this->assertEquals('reverse[a]', $field->getData());
  317. }
  318. public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
  319. {
  320. // The value is passed to the value transformer
  321. $transformer = $this->createMockTransformer();
  322. $transformer->expects($this->once())
  323. ->method('reverseTransform')
  324. ->with($this->identicalTo(' a '))
  325. ->will($this->returnValue('reverse[ a ]'));
  326. $transformer->expects($this->exactly(2))
  327. ->method('transform')
  328. // Impossible to test with PHPUnit because called twice
  329. // ->with($this->identicalTo('reverse[ a ]'))
  330. ->will($this->returnValue(' a '));
  331. $field = new TestField('title', array(
  332. 'trim' => false,
  333. 'value_transformer' => $transformer,
  334. ));
  335. $field->bind(' a ');
  336. $this->assertEquals(' a ', $field->getDisplayedData());
  337. $this->assertEquals('reverse[ a ]', $field->getData());
  338. }
  339. /*
  340. * The use case of this test is a field group with an empty property path.
  341. * Even if the field group itself is not associated to a specific property,
  342. * nested fields might be.
  343. */
  344. public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
  345. {
  346. $object = new Author();
  347. $object->firstName = 'Bernhard';
  348. $field = new TestField('firstName', array('property_path' => null));
  349. $field->updateFromObject($object);
  350. $this->assertEquals($object, $field->getData());
  351. }
  352. /*
  353. * This is important so that bind() can work even if setData() was not called
  354. * before
  355. */
  356. public function testUpdateObjectTreatsEmptyValuesAsArrays()
  357. {
  358. $array = null;
  359. $field = new TestField('firstName');
  360. $field->bind('Bernhard');
  361. $field->updateObject($array);
  362. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  363. }
  364. public function testUpdateObjectDoesNotUpdatePropertyIfPropertyPathIsEmpty()
  365. {
  366. $object = new Author();
  367. $field = new TestField('firstName', array('property_path' => null));
  368. $field->bind('Bernhard');
  369. $field->updateObject($object);
  370. $this->assertEquals(null, $object->firstName);
  371. }
  372. protected function createMockTransformer()
  373. {
  374. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  375. }
  376. protected function createMockTransformerTransformingTo($value)
  377. {
  378. $transformer = $this->createMockTransformer();
  379. $transformer->expects($this->any())
  380. ->method('reverseTransform')
  381. ->will($this->returnValue($value));
  382. return $transformer;
  383. }
  384. protected function createMockGroup()
  385. {
  386. return $this->getMock(
  387. 'Symfony\Component\Form\FieldGroup',
  388. array(),
  389. array(),
  390. '',
  391. false // don't call constructor
  392. );
  393. }
  394. protected function createMockGroupWithName($name)
  395. {
  396. $group = $this->createMockGroup();
  397. $group->expects($this->any())
  398. ->method('getName')
  399. ->will($this->returnValue($name));
  400. return $group;
  401. }
  402. protected function createMockGroupWithId($id)
  403. {
  404. $group = $this->createMockGroup();
  405. $group->expects($this->any())
  406. ->method('getId')
  407. ->will($this->returnValue($id));
  408. return $group;
  409. }
  410. }