FieldTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 testLocaleIsPassedToValueTransformer()
  111. {
  112. FormContext::setLocale('de_DE');
  113. $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
  114. $transformer->expects($this->exactly(1))
  115. ->method('setLocale')
  116. ->with($this->equalTo('de_DE'));
  117. $field = new TestField('title', array(
  118. 'value_transformer' => $transformer,
  119. ));
  120. }
  121. public function testIsRequiredReturnsOwnValueIfNoParent()
  122. {
  123. $this->field->setRequired(true);
  124. $this->assertTrue($this->field->isRequired());
  125. $this->field->setRequired(false);
  126. $this->assertFalse($this->field->isRequired());
  127. }
  128. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  129. {
  130. $group = $this->createMockGroup();
  131. $group->expects($this->any())
  132. ->method('isRequired')
  133. ->will($this->returnValue(true));
  134. $this->field->setParent($group);
  135. $this->field->setRequired(true);
  136. $this->assertTrue($this->field->isRequired());
  137. $this->field->setRequired(false);
  138. $this->assertFalse($this->field->isRequired());
  139. }
  140. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  141. {
  142. $group = $this->createMockGroup();
  143. $group->expects($this->any())
  144. ->method('isRequired')
  145. ->will($this->returnValue(false));
  146. $this->field->setParent($group);
  147. $this->field->setRequired(true);
  148. $this->assertFalse($this->field->isRequired());
  149. }
  150. public function testExceptionIfUnknownOption()
  151. {
  152. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidOptionsException');
  153. new RequiredOptionsField('name', array('bar' => 'baz', 'moo' => 'maa'));
  154. }
  155. public function testExceptionIfMissingOption()
  156. {
  157. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  158. new RequiredOptionsField('name');
  159. }
  160. public function testIsBound()
  161. {
  162. $this->assertFalse($this->field->isBound());
  163. $this->field->bind('symfony');
  164. $this->assertTrue($this->field->isBound());
  165. }
  166. public function testDefaultValuesAreTransformedCorrectly()
  167. {
  168. $field = new TestField('name');
  169. $this->assertEquals(null, $this->field->getData());
  170. $this->assertEquals('', $this->field->getDisplayedData());
  171. }
  172. public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
  173. {
  174. $this->field->setData(null);
  175. $this->assertSame(null, $this->field->getData());
  176. $this->assertSame('', $this->field->getDisplayedData());
  177. }
  178. public function testBoundValuesAreTransformedCorrectly()
  179. {
  180. $valueTransformer = $this->createMockTransformer();
  181. $normTransformer = $this->createMockTransformer();
  182. $field = $this->getMock(
  183. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  184. array('processData'), // only mock processData()
  185. array('title', array(
  186. 'value_transformer' => $valueTransformer,
  187. 'normalization_transformer' => $normTransformer,
  188. ))
  189. );
  190. // 1a. The value is converted to a string and passed to the value transformer
  191. $valueTransformer->expects($this->once())
  192. ->method('reverseTransform')
  193. ->with($this->identicalTo('0'))
  194. ->will($this->returnValue('reverse[0]'));
  195. // 2. The output of the reverse transformation is passed to processData()
  196. // The processed data is accessible through getNormalizedData()
  197. $field->expects($this->once())
  198. ->method('processData')
  199. ->with($this->equalTo('reverse[0]'))
  200. ->will($this->returnValue('processed[reverse[0]]'));
  201. // 3. The processed data is denormalized and then accessible through
  202. // getData()
  203. $normTransformer->expects($this->once())
  204. ->method('reverseTransform')
  205. ->with($this->identicalTo('processed[reverse[0]]'))
  206. ->will($this->returnValue('denorm[processed[reverse[0]]]'));
  207. // 4. The processed data is transformed again and then accessible
  208. // through getDisplayedData()
  209. $valueTransformer->expects($this->once())
  210. ->method('transform')
  211. ->with($this->equalTo('processed[reverse[0]]'))
  212. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  213. $field->bind(0);
  214. $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData());
  215. $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData());
  216. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  217. }
  218. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  219. {
  220. $transformer = $this->createMockTransformer();
  221. $field = $this->getMock(
  222. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  223. array('processData'), // only mock processData()
  224. array('title', array(
  225. 'value_transformer' => $transformer,
  226. ))
  227. );
  228. // 1. Empty values are converted to NULL by convention
  229. $transformer->expects($this->once())
  230. ->method('reverseTransform')
  231. ->with($this->identicalTo(''))
  232. ->will($this->returnValue(null));
  233. // 2. NULL is passed to processData()
  234. $field->expects($this->once())
  235. ->method('processData')
  236. ->with($this->identicalTo(null))
  237. ->will($this->returnValue('processed'));
  238. // 3. The processed data is transformed (for displayed data)
  239. $transformer->expects($this->once())
  240. ->method('transform')
  241. ->with($this->equalTo('processed'))
  242. ->will($this->returnValue('transform[processed]'));
  243. $field->bind('');
  244. $this->assertSame('processed', $field->getData());
  245. $this->assertEquals('transform[processed]', $field->getDisplayedData());
  246. }
  247. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
  248. {
  249. $transformer = $this->createMockTransformer();
  250. $field = new TestField('title', array(
  251. 'value_transformer' => $transformer,
  252. ));
  253. // 1. Empty values are converted to NULL by convention
  254. $transformer->expects($this->once())
  255. ->method('reverseTransform')
  256. ->with($this->identicalTo(''))
  257. ->will($this->returnValue(null));
  258. // 2. The processed data is NULL and therefore transformed to an empty
  259. // string by convention
  260. $transformer->expects($this->once())
  261. ->method('transform')
  262. ->with($this->identicalTo(null))
  263. ->will($this->returnValue(''));
  264. $field->bind('');
  265. $this->assertSame(null, $field->getData());
  266. $this->assertEquals('', $field->getDisplayedData());
  267. }
  268. public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  269. {
  270. $this->field->bind('');
  271. $this->assertSame(null, $this->field->getData());
  272. $this->assertEquals('', $this->field->getDisplayedData());
  273. }
  274. public function testValuesAreTransformedCorrectly()
  275. {
  276. // The value is first passed to the normalization transformer...
  277. $normTransformer = $this->createMockTransformer();
  278. $normTransformer->expects($this->exactly(2))
  279. ->method('transform')
  280. // Impossible to test with PHPUnit because called twice
  281. // ->with($this->identicalTo(0))
  282. ->will($this->returnValue('norm[0]'));
  283. // ...and then to the value transformer
  284. $valueTransformer = $this->createMockTransformer();
  285. $valueTransformer->expects($this->exactly(2))
  286. ->method('transform')
  287. // Impossible to test with PHPUnit because called twice
  288. // ->with($this->identicalTo('norm[0]'))
  289. ->will($this->returnValue('transform[norm[0]]'));
  290. $field = new TestField('title', array(
  291. 'value_transformer' => $valueTransformer,
  292. 'normalization_transformer' => $normTransformer,
  293. ));
  294. $field->setData(0);
  295. $this->assertEquals(0, $field->getData());
  296. $this->assertEquals('norm[0]', $field->getNormalizedData());
  297. $this->assertEquals('transform[norm[0]]', $field->getDisplayedData());
  298. }
  299. public function testBoundValuesAreTrimmedBeforeTransforming()
  300. {
  301. // The value is passed to the value transformer
  302. $transformer = $this->createMockTransformer();
  303. $transformer->expects($this->once())
  304. ->method('reverseTransform')
  305. ->with($this->identicalTo('a'))
  306. ->will($this->returnValue('reverse[a]'));
  307. $transformer->expects($this->exactly(2))
  308. ->method('transform')
  309. // Impossible to test with PHPUnit because called twice
  310. // ->with($this->identicalTo('reverse[a]'))
  311. ->will($this->returnValue('a'));
  312. $field = new TestField('title', array(
  313. 'value_transformer' => $transformer,
  314. ));
  315. $field->bind(' a ');
  316. $this->assertEquals('a', $field->getDisplayedData());
  317. $this->assertEquals('reverse[a]', $field->getData());
  318. }
  319. public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
  320. {
  321. // The value is passed to the value transformer
  322. $transformer = $this->createMockTransformer();
  323. $transformer->expects($this->once())
  324. ->method('reverseTransform')
  325. ->with($this->identicalTo(' a '))
  326. ->will($this->returnValue('reverse[ a ]'));
  327. $transformer->expects($this->exactly(2))
  328. ->method('transform')
  329. // Impossible to test with PHPUnit because called twice
  330. // ->with($this->identicalTo('reverse[ a ]'))
  331. ->will($this->returnValue(' a '));
  332. $field = new TestField('title', array(
  333. 'trim' => false,
  334. 'value_transformer' => $transformer,
  335. ));
  336. $field->bind(' a ');
  337. $this->assertEquals(' a ', $field->getDisplayedData());
  338. $this->assertEquals('reverse[ a ]', $field->getData());
  339. }
  340. /*
  341. * This is important so that bind() can work even if setData() was not called
  342. * before
  343. */
  344. public function testUpdatePropertyTreatsEmptyValuesAsArrays()
  345. {
  346. $array = null;
  347. $field = new TestField('firstName');
  348. $field->bind('Bernhard');
  349. $field->updateProperty($array);
  350. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  351. }
  352. public function testUpdatePropertyDoesNotUpdatePropertyIfPropertyPathIsEmpty()
  353. {
  354. $object = new Author();
  355. $field = new TestField('firstName', array('property_path' => null));
  356. $field->bind('Bernhard');
  357. $field->updateProperty($object);
  358. $this->assertEquals(null, $object->firstName);
  359. }
  360. public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
  361. {
  362. $field = new TestField('title', array(
  363. 'trim' => false,
  364. ));
  365. $field->bind('a');
  366. $this->assertEquals('a', $field->getDisplayedData());
  367. $this->assertTrue($field->isTransformationSuccessful());
  368. }
  369. public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
  370. {
  371. // The value is passed to the value transformer
  372. $transformer = $this->createMockTransformer();
  373. $transformer->expects($this->once())
  374. ->method('reverseTransform')
  375. ->will($this->throwException(new TransformationFailedException()));
  376. $field = new TestField('title', array(
  377. 'trim' => false,
  378. 'value_transformer' => $transformer,
  379. ));
  380. $field->bind('a');
  381. $this->assertEquals('a', $field->getDisplayedData());
  382. $this->assertFalse($field->isTransformationSuccessful());
  383. }
  384. public function testGetRootReturnsRootOfParentIfSet()
  385. {
  386. $parent = $this->createMockGroup();
  387. $parent->expects($this->any())
  388. ->method('getRoot')
  389. ->will($this->returnValue('ROOT'));
  390. $this->field->setParent($parent);
  391. $this->assertEquals('ROOT', $this->field->getRoot());
  392. }
  393. public function testGetRootReturnsFieldIfNoParent()
  394. {
  395. $this->assertEquals($this->field, $this->field->getRoot());
  396. }
  397. protected function createMockTransformer()
  398. {
  399. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  400. }
  401. protected function createMockTransformerTransformingTo($value)
  402. {
  403. $transformer = $this->createMockTransformer();
  404. $transformer->expects($this->any())
  405. ->method('reverseTransform')
  406. ->will($this->returnValue($value));
  407. return $transformer;
  408. }
  409. protected function createMockGroup()
  410. {
  411. return $this->getMock(
  412. 'Symfony\Component\Form\FieldGroup',
  413. array(),
  414. array(),
  415. '',
  416. false // don't call constructor
  417. );
  418. }
  419. protected function createMockGroupWithName($name)
  420. {
  421. $group = $this->createMockGroup();
  422. $group->expects($this->any())
  423. ->method('getName')
  424. ->will($this->returnValue($name));
  425. return $group;
  426. }
  427. protected function createMockGroupWithId($id)
  428. {
  429. $group = $this->createMockGroup();
  430. $group->expects($this->any())
  431. ->method('getId')
  432. ->will($this->returnValue($id));
  433. return $group;
  434. }
  435. }