FieldTest.php 16 KB

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