FieldTest.php 18 KB

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