FieldTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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__ . '/TestCase.php';
  12. require_once __DIR__ . '/Fixtures/Author.php';
  13. require_once __DIR__ . '/Fixtures/InvalidField.php';
  14. require_once __DIR__ . '/Fixtures/FixedValueTransformer.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\Form;
  19. use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
  20. use Symfony\Tests\Component\Form\Fixtures\Author;
  21. use Symfony\Tests\Component\Form\Fixtures\InvalidField;
  22. use Symfony\Tests\Component\Form\Fixtures\FixedValueTransformer;
  23. class FieldTest extends TestCase
  24. {
  25. protected $field;
  26. protected function setUp()
  27. {
  28. parent::setUp();
  29. $this->field = $this->factory->getField('title');
  30. }
  31. public function testGetPropertyPath_defaultPath()
  32. {
  33. $field = $this->factory->getField('title');
  34. $this->assertEquals(new PropertyPath('title'), $field->getPropertyPath());
  35. }
  36. public function testGetPropertyPath_pathIsZero()
  37. {
  38. $field = $this->factory->getField('title', array('property_path' => '0'));
  39. $this->assertEquals(new PropertyPath('0'), $field->getPropertyPath());
  40. }
  41. public function testGetPropertyPath_pathIsEmpty()
  42. {
  43. $field = $this->factory->getField('title', array('property_path' => ''));
  44. $this->assertEquals(null, $field->getPropertyPath());
  45. }
  46. public function testGetPropertyPath_pathIsNull()
  47. {
  48. $field = $this->factory->getField('title', array('property_path' => null));
  49. $this->assertEquals(null, $field->getPropertyPath());
  50. }
  51. public function testPassRequiredAsOption()
  52. {
  53. $field = $this->factory->getField('title', array('required' => false));
  54. $this->assertFalse($field->isRequired());
  55. $field = $this->factory->getField('title', array('required' => true));
  56. $this->assertTrue($field->isRequired());
  57. }
  58. public function testPassDisabledAsOption()
  59. {
  60. $field = $this->factory->getField('title', array('disabled' => false));
  61. $this->assertFalse($field->isDisabled());
  62. $field = $this->factory->getField('title', array('disabled' => true));
  63. $this->assertTrue($field->isDisabled());
  64. }
  65. public function testFieldIsDisabledIfParentIsDisabled()
  66. {
  67. $field = $this->factory->getField('title', array('disabled' => false));
  68. $field->setParent($this->factory->getField('title', array('disabled' => true)));
  69. $this->assertTrue($field->isDisabled());
  70. }
  71. public function testFieldWithNoErrorsIsValid()
  72. {
  73. $this->field->submit('data');
  74. $this->assertTrue($this->field->isValid());
  75. }
  76. public function testFieldWithErrorsIsInvalid()
  77. {
  78. $this->field->submit('data');
  79. $this->field->addError(new FieldError('Some error'));
  80. $this->assertFalse($this->field->isValid());
  81. }
  82. public function testSubmitResetsErrors()
  83. {
  84. $this->field->addError(new FieldError('Some error'));
  85. $this->field->submit('data');
  86. $this->assertTrue($this->field->isValid());
  87. }
  88. public function testUnsubmittedFieldIsInvalid()
  89. {
  90. $this->assertFalse($this->field->isValid());
  91. }
  92. public function testIsRequiredReturnsOwnValueIfNoParent()
  93. {
  94. $this->field->setRequired(true);
  95. $this->assertTrue($this->field->isRequired());
  96. $this->field->setRequired(false);
  97. $this->assertFalse($this->field->isRequired());
  98. }
  99. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  100. {
  101. $group = $this->createMockGroup();
  102. $group->expects($this->any())
  103. ->method('isRequired')
  104. ->will($this->returnValue(true));
  105. $this->field->setParent($group);
  106. $this->field->setRequired(true);
  107. $this->assertTrue($this->field->isRequired());
  108. $this->field->setRequired(false);
  109. $this->assertFalse($this->field->isRequired());
  110. }
  111. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  112. {
  113. $group = $this->createMockGroup();
  114. $group->expects($this->any())
  115. ->method('isRequired')
  116. ->will($this->returnValue(false));
  117. $this->field->setParent($group);
  118. $this->field->setRequired(true);
  119. $this->assertFalse($this->field->isRequired());
  120. }
  121. public function testIsSubmitted()
  122. {
  123. $this->assertFalse($this->field->isSubmitted());
  124. $this->field->submit('symfony');
  125. $this->assertTrue($this->field->isSubmitted());
  126. }
  127. public function testDefaultValuesAreTransformedCorrectly()
  128. {
  129. $field = $this->factory->getField('name');
  130. $this->assertEquals(null, $this->field->getData());
  131. $this->assertEquals('', $this->field->getDisplayedData());
  132. }
  133. public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
  134. {
  135. $this->field->setData(null);
  136. $this->assertSame(null, $this->field->getData());
  137. $this->assertSame('', $this->field->getDisplayedData());
  138. }
  139. public function testValuesAreTransformedCorrectlyIfNotNull_noValueTransformer()
  140. {
  141. $this->field->setData(123);
  142. // The values are synchronized
  143. // Without value transformer, the field can't know that the data
  144. // should be casted to an integer when the field is bound
  145. // Even without binding, the data will thus be a string
  146. $this->assertSame('123', $this->field->getData());
  147. $this->assertSame('123', $this->field->getDisplayedData());
  148. }
  149. public function testSubmittedValuesAreTransformedCorrectly()
  150. {
  151. $valueTransformer = $this->createMockTransformer();
  152. $normTransformer = $this->createMockTransformer();
  153. $field = $this->getMock(
  154. 'Symfony\Component\Form\Field',
  155. array('processData'), // only mock processData()
  156. array('title')
  157. );
  158. $field->setValueTransformer($valueTransformer);
  159. $field->setNormalizationTransformer($normTransformer);
  160. // 1a. The value is converted to a string and passed to the value transformer
  161. $valueTransformer->expects($this->once())
  162. ->method('reverseTransform')
  163. ->with($this->identicalTo('0'))
  164. ->will($this->returnValue('reverse[0]'));
  165. // 2. The output of the reverse transformation is passed to processData()
  166. // The processed data is accessible through getNormalizedData()
  167. $field->expects($this->once())
  168. ->method('processData')
  169. ->with($this->equalTo('reverse[0]'))
  170. ->will($this->returnValue('processed[reverse[0]]'));
  171. // 3. The processed data is denormalized and then accessible through
  172. // getData()
  173. $normTransformer->expects($this->once())
  174. ->method('reverseTransform')
  175. ->with($this->identicalTo('processed[reverse[0]]'))
  176. ->will($this->returnValue('denorm[processed[reverse[0]]]'));
  177. // 4. The processed data is transformed again and then accessible
  178. // through getDisplayedData()
  179. $valueTransformer->expects($this->once())
  180. ->method('transform')
  181. ->with($this->equalTo('processed[reverse[0]]'))
  182. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  183. $field->submit(0);
  184. $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData());
  185. $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData());
  186. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  187. }
  188. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  189. {
  190. $transformer = $this->createMockTransformer();
  191. $field = $this->getMock(
  192. 'Symfony\Component\Form\Field',
  193. array('processData'), // only mock processData()
  194. array('title')
  195. );
  196. $field->setValueTransformer($transformer);
  197. // 1. Empty values are converted to NULL by convention
  198. $transformer->expects($this->once())
  199. ->method('reverseTransform')
  200. ->with($this->identicalTo(''))
  201. ->will($this->returnValue(null));
  202. // 2. NULL is passed to processData()
  203. $field->expects($this->once())
  204. ->method('processData')
  205. ->with($this->identicalTo(null))
  206. ->will($this->returnValue('processed'));
  207. // 3. The processed data is transformed (for displayed data)
  208. $transformer->expects($this->once())
  209. ->method('transform')
  210. ->with($this->equalTo('processed'))
  211. ->will($this->returnValue('transform[processed]'));
  212. $field->submit('');
  213. $this->assertSame('processed', $field->getData());
  214. $this->assertEquals('transform[processed]', $field->getDisplayedData());
  215. }
  216. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
  217. {
  218. $transformer = $this->createMockTransformer();
  219. $field = $this->factory->getField('title', array(
  220. 'value_transformer' => $transformer,
  221. ));
  222. // 1. Empty values are converted to NULL by convention
  223. $transformer->expects($this->once())
  224. ->method('reverseTransform')
  225. ->with($this->identicalTo(''))
  226. ->will($this->returnValue(null));
  227. // 2. The processed data is NULL and therefore transformed to an empty
  228. // string by convention
  229. $transformer->expects($this->once())
  230. ->method('transform')
  231. ->with($this->identicalTo(null))
  232. ->will($this->returnValue(''));
  233. $field->submit('');
  234. $this->assertSame(null, $field->getData());
  235. $this->assertEquals('', $field->getDisplayedData());
  236. }
  237. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  238. {
  239. $this->field->submit('');
  240. $this->assertSame(null, $this->field->getData());
  241. $this->assertEquals('', $this->field->getDisplayedData());
  242. }
  243. public function testValuesAreTransformedCorrectly()
  244. {
  245. $normTransformer = new FixedValueTransformer(array(
  246. null => '',
  247. 0 => 'norm[0]',
  248. ));
  249. $valueTransformer = new FixedValueTransformer(array(
  250. '' => '',
  251. 'norm[0]' => 'transform[norm[0]]',
  252. ));
  253. $field = $this->factory->getField('title', array(
  254. 'value_transformer' => $valueTransformer,
  255. 'normalization_transformer' => $normTransformer,
  256. ));
  257. $field->setData(0);
  258. $this->assertEquals(0, $field->getData());
  259. $this->assertEquals('norm[0]', $field->getNormalizedData());
  260. $this->assertEquals('transform[norm[0]]', $field->getDisplayedData());
  261. }
  262. public function testSubmittedValuesAreTrimmedBeforeTransforming()
  263. {
  264. $transformer = new FixedValueTransformer(array(
  265. null => '',
  266. 'reverse[a]' => 'a',
  267. ));
  268. $field = $this->factory->getField('title', array(
  269. 'value_transformer' => $transformer,
  270. ));
  271. $field->submit(' a ');
  272. $this->assertEquals('a', $field->getDisplayedData());
  273. $this->assertEquals('reverse[a]', $field->getData());
  274. }
  275. public function testSubmittedValuesAreNotTrimmedBeforeTransformingIfDisabled()
  276. {
  277. $transformer = new FixedValueTransformer(array(
  278. null => '',
  279. 'reverse[ a ]' => ' a ',
  280. ));
  281. $field = $this->factory->getField('title', array(
  282. 'trim' => false,
  283. 'value_transformer' => $transformer,
  284. ));
  285. $field->submit(' a ');
  286. $this->assertEquals(' a ', $field->getDisplayedData());
  287. $this->assertEquals('reverse[ a ]', $field->getData());
  288. }
  289. public function testWritePropertyDoesNotWritePropertyIfPropertyPathIsEmpty()
  290. {
  291. $object = new Author();
  292. $field = $this->factory->getField('firstName', array('property_path' => null));
  293. $field->submit('Bernhard');
  294. $field->writeProperty($object);
  295. $this->assertEquals(null, $object->firstName);
  296. }
  297. public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
  298. {
  299. $field = $this->factory->getField('title', array(
  300. 'trim' => false,
  301. ));
  302. $field->submit('a');
  303. $this->assertEquals('a', $field->getDisplayedData());
  304. $this->assertTrue($field->isTransformationSuccessful());
  305. }
  306. public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
  307. {
  308. // The value is passed to the value transformer
  309. $transformer = $this->createMockTransformer();
  310. $field = $this->factory->getField('title', array(
  311. 'trim' => false,
  312. 'value_transformer' => $transformer,
  313. ));
  314. $transformer->expects($this->once())
  315. ->method('reverseTransform')
  316. ->will($this->throwException(new TransformationFailedException()));
  317. $field->submit('a');
  318. $this->assertEquals('a', $field->getDisplayedData());
  319. $this->assertFalse($field->isTransformationSuccessful());
  320. }
  321. public function testGetRootReturnsRootOfParentIfSet()
  322. {
  323. $parent = $this->createMockGroup();
  324. $parent->expects($this->any())
  325. ->method('getRoot')
  326. ->will($this->returnValue('ROOT'));
  327. $this->field->setParent($parent);
  328. $this->assertEquals('ROOT', $this->field->getRoot());
  329. }
  330. public function testFieldsInitializedWithDataAreNotUpdatedWhenAddedToForms()
  331. {
  332. $author = new Author();
  333. $author->firstName = 'Bernhard';
  334. $field = $this->factory->getField('firstName', array(
  335. 'data' => 'foobar',
  336. ));
  337. $form = new Form('author', array(
  338. 'data' => $author,
  339. ));
  340. $form->add($field);
  341. $this->assertEquals('foobar', $field->getData());
  342. }
  343. public function testGetRootReturnsFieldIfNoParent()
  344. {
  345. $this->assertEquals($this->field, $this->field->getRoot());
  346. }
  347. public function testIsEmptyReturnsTrueIfNull()
  348. {
  349. $this->field->setData(null);
  350. $this->assertTrue($this->field->isEmpty());
  351. }
  352. public function testIsEmptyReturnsTrueIfEmptyString()
  353. {
  354. $this->field->setData('');
  355. $this->assertTrue($this->field->isEmpty());
  356. }
  357. public function testIsEmptyReturnsFalseIfZero()
  358. {
  359. $this->field->setData(0);
  360. $this->assertFalse($this->field->isEmpty());
  361. }
  362. protected function createMockTransformer()
  363. {
  364. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  365. }
  366. protected function createMockTransformerTransformingTo($value)
  367. {
  368. $transformer = $this->createMockTransformer();
  369. $transformer->expects($this->any())
  370. ->method('reverseTransform')
  371. ->will($this->returnValue($value));
  372. return $transformer;
  373. }
  374. protected function createMockGroup()
  375. {
  376. return $this->getMock(
  377. 'Symfony\Component\Form\Form',
  378. array(),
  379. array(),
  380. '',
  381. false // don't call constructor
  382. );
  383. }
  384. protected function createMockGroupWithName($name)
  385. {
  386. $group = $this->createMockGroup();
  387. $group->expects($this->any())
  388. ->method('getName')
  389. ->will($this->returnValue($name));
  390. return $group;
  391. }
  392. protected function createMockGroupWithId($id)
  393. {
  394. $group = $this->createMockGroup();
  395. $group->expects($this->any())
  396. ->method('getId')
  397. ->will($this->returnValue($id));
  398. return $group;
  399. }
  400. }