FieldTest.php 16 KB

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