FieldTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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_noValueTransformer()
  168. {
  169. $this->field->setData(null);
  170. $this->assertSame(null, $this->field->getData());
  171. $this->assertSame('', $this->field->getDisplayedData());
  172. }
  173. public function testBoundValuesAreTransformedCorrectly()
  174. {
  175. $field = $this->getMock(
  176. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  177. array('processData'), // only mock processData()
  178. array('title')
  179. );
  180. // 1a. The value is converted to a string and passed to the value transformer
  181. $valueTransformer = $this->createMockTransformer();
  182. $valueTransformer->expects($this->once())
  183. ->method('reverseTransform')
  184. ->with($this->identicalTo('0'))
  185. ->will($this->returnValue('reverse[0]'));
  186. $field->setValueTransformer($valueTransformer);
  187. // 2. The output of the reverse transformation is passed to processData()
  188. // The processed data is accessible through getNormalizedData()
  189. $field->expects($this->once())
  190. ->method('processData')
  191. ->with($this->equalTo('reverse[0]'))
  192. ->will($this->returnValue('processed[reverse[0]]'));
  193. // 3. The processed data is denormalized and then accessible through
  194. // getData()
  195. $normTransformer = $this->createMockTransformer();
  196. $normTransformer->expects($this->once())
  197. ->method('reverseTransform')
  198. ->with($this->identicalTo('processed[reverse[0]]'))
  199. ->will($this->returnValue('denorm[processed[reverse[0]]]'));
  200. $field->setNormalizationTransformer($normTransformer);
  201. // 4. The processed data is transformed again and then accessible
  202. // through getDisplayedData()
  203. $valueTransformer->expects($this->once())
  204. ->method('transform')
  205. ->with($this->equalTo('processed[reverse[0]]'))
  206. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  207. $field->bind(0);
  208. $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData());
  209. $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData());
  210. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  211. }
  212. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  213. {
  214. $field = $this->getMock(
  215. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  216. array('processData'), // only mock processData()
  217. array('title')
  218. );
  219. // 1. Empty values are converted to NULL by convention
  220. $transformer = $this->createMockTransformer();
  221. $transformer->expects($this->once())
  222. ->method('reverseTransform')
  223. ->with($this->identicalTo(''))
  224. ->will($this->returnValue(null));
  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 converted to NULL by convention
  243. $transformer = $this->createMockTransformer();
  244. $transformer->expects($this->once())
  245. ->method('reverseTransform')
  246. ->with($this->identicalTo(''))
  247. ->will($this->returnValue(null));
  248. $this->field->setValueTransformer($transformer);
  249. // 2. The processed data is NULL and therefore transformed to an empty
  250. // string by convention
  251. $transformer->expects($this->once())
  252. ->method('transform')
  253. ->with($this->identicalTo(null))
  254. ->will($this->returnValue(''));
  255. $this->field->bind('');
  256. $this->assertSame(null, $this->field->getData());
  257. $this->assertEquals('', $this->field->getDisplayedData());
  258. }
  259. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  260. {
  261. $this->field->bind('');
  262. $this->assertSame(null, $this->field->getData());
  263. $this->assertEquals('', $this->field->getDisplayedData());
  264. }
  265. public function testValuesAreTransformedCorrectly()
  266. {
  267. // The value is first passed to the normalization transformer...
  268. $normTransformer = $this->createMockTransformer();
  269. $normTransformer->expects($this->once())
  270. ->method('transform')
  271. ->with($this->identicalTo(0))
  272. ->will($this->returnValue('norm[0]'));
  273. // ...and then to the value transformer
  274. $valueTransformer = $this->createMockTransformer();
  275. $valueTransformer->expects($this->once())
  276. ->method('transform')
  277. ->with($this->identicalTo('norm[0]'))
  278. ->will($this->returnValue('transform[norm[0]]'));
  279. $this->field->setNormalizationTransformer($normTransformer);
  280. $this->field->setValueTransformer($valueTransformer);
  281. $this->field->setData(0);
  282. $this->assertEquals(0, $this->field->getData());
  283. $this->assertEquals('norm[0]', $this->field->getNormalizedData());
  284. $this->assertEquals('transform[norm[0]]', $this->field->getDisplayedData());
  285. }
  286. public function testBoundValuesAreTrimmedBeforeTransforming()
  287. {
  288. // The value is passed to the value transformer
  289. $transformer = $this->createMockTransformer();
  290. $transformer->expects($this->once())
  291. ->method('reverseTransform')
  292. ->with($this->identicalTo('a'))
  293. ->will($this->returnValue('reverse[a]'));
  294. $transformer->expects($this->once())
  295. ->method('transform')
  296. ->with($this->identicalTo('reverse[a]'))
  297. ->will($this->returnValue('a'));
  298. $this->field->setValueTransformer($transformer);
  299. $this->field->bind(' a ');
  300. $this->assertEquals('a', $this->field->getDisplayedData());
  301. $this->assertEquals('reverse[a]', $this->field->getData());
  302. }
  303. public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
  304. {
  305. // The value is passed to the value transformer
  306. $transformer = $this->createMockTransformer();
  307. $transformer->expects($this->once())
  308. ->method('reverseTransform')
  309. ->with($this->identicalTo(' a '))
  310. ->will($this->returnValue('reverse[ a ]'));
  311. $transformer->expects($this->once())
  312. ->method('transform')
  313. ->with($this->identicalTo('reverse[ a ]'))
  314. ->will($this->returnValue(' a '));
  315. $field = new TestField('title', array('trim' => false));
  316. $field->setValueTransformer($transformer);
  317. $field->bind(' a ');
  318. $this->assertEquals(' a ', $field->getDisplayedData());
  319. $this->assertEquals('reverse[ a ]', $field->getData());
  320. }
  321. /*
  322. * The use case of this test is a field group with an empty property path.
  323. * Even if the field group itself is not associated to a specific property,
  324. * nested fields might be.
  325. */
  326. public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
  327. {
  328. $object = new Author();
  329. $object->firstName = 'Bernhard';
  330. $field = new TestField('firstName', array('property_path' => null));
  331. $field->updateFromObject($object);
  332. $this->assertEquals($object, $field->getData());
  333. }
  334. /*
  335. * This is important so that bind() can work even if setData() was not called
  336. * before
  337. */
  338. public function testUpdateObjectTreatsEmptyValuesAsArrays()
  339. {
  340. $array = null;
  341. $field = new TestField('firstName');
  342. $field->bind('Bernhard');
  343. $field->updateObject($array);
  344. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  345. }
  346. public function testUpdateObjectDoesNotUpdatePropertyIfPropertyPathIsEmpty()
  347. {
  348. $object = new Author();
  349. $field = new TestField('firstName', array('property_path' => null));
  350. $field->bind('Bernhard');
  351. $field->updateObject($object);
  352. $this->assertEquals(null, $object->firstName);
  353. }
  354. protected function createMockTransformer()
  355. {
  356. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  357. }
  358. protected function createMockTransformerTransformingTo($value)
  359. {
  360. $transformer = $this->createMockTransformer();
  361. $transformer->expects($this->any())
  362. ->method('reverseTransform')
  363. ->will($this->returnValue($value));
  364. return $transformer;
  365. }
  366. protected function createMockGroup()
  367. {
  368. return $this->getMock(
  369. 'Symfony\Component\Form\FieldGroup',
  370. array(),
  371. array(),
  372. '',
  373. false // don't call constructor
  374. );
  375. }
  376. protected function createMockGroupWithName($name)
  377. {
  378. $group = $this->createMockGroup();
  379. $group->expects($this->any())
  380. ->method('getName')
  381. ->will($this->returnValue($name));
  382. return $group;
  383. }
  384. protected function createMockGroupWithId($id)
  385. {
  386. $group = $this->createMockGroup();
  387. $group->expects($this->any())
  388. ->method('getId')
  389. ->will($this->returnValue($id));
  390. return $group;
  391. }
  392. }