FieldTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. require_once __DIR__ . '/Fixtures/FixedFilterListener.php';
  16. use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
  17. use Symfony\Component\Form\PropertyPath;
  18. use Symfony\Component\Form\FieldError;
  19. use Symfony\Component\Form\Form;
  20. use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
  21. use Symfony\Tests\Component\Form\Fixtures\Author;
  22. use Symfony\Tests\Component\Form\Fixtures\InvalidField;
  23. use Symfony\Tests\Component\Form\Fixtures\FixedValueTransformer;
  24. use Symfony\Tests\Component\Form\Fixtures\FixedFilterListener;
  25. class FieldTest extends TestCase
  26. {
  27. protected $field;
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->field = $this->factory->getInstance('field', 'title');
  32. }
  33. public function testGetPropertyPath_defaultPath()
  34. {
  35. $field = $this->factory->getInstance('field', 'title');
  36. $this->assertEquals(new PropertyPath('title'), $field->getPropertyPath());
  37. }
  38. public function testGetPropertyPath_pathIsZero()
  39. {
  40. $field = $this->factory->getInstance('field', 'title', array('property_path' => '0'));
  41. $this->assertEquals(new PropertyPath('0'), $field->getPropertyPath());
  42. }
  43. public function testGetPropertyPath_pathIsEmpty()
  44. {
  45. $field = $this->factory->getInstance('field', 'title', array('property_path' => ''));
  46. $this->assertEquals(null, $field->getPropertyPath());
  47. }
  48. public function testGetPropertyPath_pathIsNull()
  49. {
  50. $field = $this->factory->getInstance('field', 'title', array('property_path' => null));
  51. $this->assertEquals(null, $field->getPropertyPath());
  52. }
  53. public function testPassRequiredAsOption()
  54. {
  55. $field = $this->factory->getInstance('field', 'title', array('required' => false));
  56. $this->assertFalse($field->isRequired());
  57. $field = $this->factory->getInstance('field', 'title', array('required' => true));
  58. $this->assertTrue($field->isRequired());
  59. }
  60. public function testPassDisabledAsOption()
  61. {
  62. $field = $this->factory->getInstance('field', 'title', array('disabled' => false));
  63. $this->assertFalse($field->isDisabled());
  64. $field = $this->factory->getInstance('field', 'title', array('disabled' => true));
  65. $this->assertTrue($field->isDisabled());
  66. }
  67. public function testFieldIsDisabledIfParentIsDisabled()
  68. {
  69. $field = $this->factory->getInstance('field', 'title', array('disabled' => false));
  70. $field->setParent($this->factory->getInstance('field', 'title', array('disabled' => true)));
  71. $this->assertTrue($field->isDisabled());
  72. }
  73. public function testFieldWithNoErrorsIsValid()
  74. {
  75. $this->field->bind('data');
  76. $this->assertTrue($this->field->isValid());
  77. }
  78. public function testFieldWithErrorsIsInvalid()
  79. {
  80. $this->field->bind('data');
  81. $this->field->addError(new FieldError('Some error'));
  82. $this->assertFalse($this->field->isValid());
  83. }
  84. public function testSubmitResetsErrors()
  85. {
  86. $this->field->addError(new FieldError('Some error'));
  87. $this->field->bind('data');
  88. $this->assertTrue($this->field->isValid());
  89. }
  90. public function testUnboundFieldIsInvalid()
  91. {
  92. $this->assertFalse($this->field->isValid());
  93. }
  94. public function testIsRequiredReturnsOwnValueIfNoParent()
  95. {
  96. $this->field->setRequired(true);
  97. $this->assertTrue($this->field->isRequired());
  98. $this->field->setRequired(false);
  99. $this->assertFalse($this->field->isRequired());
  100. }
  101. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  102. {
  103. $group = $this->createMockGroup();
  104. $group->expects($this->any())
  105. ->method('isRequired')
  106. ->will($this->returnValue(true));
  107. $this->field->setParent($group);
  108. $this->field->setRequired(true);
  109. $this->assertTrue($this->field->isRequired());
  110. $this->field->setRequired(false);
  111. $this->assertFalse($this->field->isRequired());
  112. }
  113. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  114. {
  115. $group = $this->createMockGroup();
  116. $group->expects($this->any())
  117. ->method('isRequired')
  118. ->will($this->returnValue(false));
  119. $this->field->setParent($group);
  120. $this->field->setRequired(true);
  121. $this->assertFalse($this->field->isRequired());
  122. }
  123. public function testIsBound()
  124. {
  125. $this->assertFalse($this->field->isBound());
  126. $this->field->bind('symfony');
  127. $this->assertTrue($this->field->isBound());
  128. }
  129. public function testDefaultDataIsTransformedCorrectly()
  130. {
  131. $field = $this->factory->getInstance('field', 'name');
  132. $this->assertEquals(null, $this->field->getData());
  133. $this->assertEquals('', $this->field->getTransformedData());
  134. }
  135. public function testDataIsTransformedCorrectlyIfNull_noValueTransformer()
  136. {
  137. $this->field->setData(null);
  138. $this->assertSame(null, $this->field->getData());
  139. $this->assertSame('', $this->field->getTransformedData());
  140. }
  141. public function testDataIsTransformedCorrectlyIfNotNull_noValueTransformer()
  142. {
  143. $this->field->setData(123);
  144. // The values are synchronized
  145. // Without value transformer, the field can't know that the data
  146. // should be casted to an integer when the field is bound
  147. // Even without binding, the data will thus be a string
  148. $this->assertSame('123', $this->field->getData());
  149. $this->assertSame('123', $this->field->getTransformedData());
  150. }
  151. public function testBoundDataIsTransformedCorrectly()
  152. {
  153. $filter = new FixedFilterListener(array(
  154. 'filterBoundDataFromClient' => array(
  155. // 1. The value is converted to a string and passed to the
  156. // first filter
  157. '0' => 'filter1[0]',
  158. ),
  159. 'filterBoundData' => array(
  160. // 3. The normalized value is passed to the second filter
  161. 'norm[filter1[0]]' => 'filter2[norm[filter1[0]]]',
  162. ),
  163. ));
  164. $valueTransformer = new FixedValueTransformer(array(
  165. // 2. The filtered value is normalized
  166. 'norm[filter1[0]]' => 'filter1[0]',
  167. // 4a. The filtered normalized value is converted to client
  168. // representation
  169. 'filter2[norm[filter1[0]]]' => 'client[filter2[norm[filter1[0]]]]'
  170. ));
  171. $normTransformer = new FixedValueTransformer(array(
  172. // 4b. The filtered normalized value is converted to app
  173. // representation
  174. 'app[filter2[norm[filter1[0]]]]' => 'filter2[norm[filter1[0]]]',
  175. ));
  176. $this->field->addEventSubscriber($filter);
  177. $this->field->setValueTransformer($valueTransformer);
  178. $this->field->setNormalizationTransformer($normTransformer);
  179. $this->field->bind(0);
  180. $this->assertEquals('app[filter2[norm[filter1[0]]]]', $this->field->getData());
  181. $this->assertEquals('filter2[norm[filter1[0]]]', $this->field->getNormalizedData());
  182. $this->assertEquals('client[filter2[norm[filter1[0]]]]', $this->field->getTransformedData());
  183. }
  184. public function testBoundDataIsTransformedCorrectlyIfEmpty_noValueTransformer()
  185. {
  186. $this->field->bind('');
  187. $this->assertSame(null, $this->field->getData());
  188. $this->assertEquals('', $this->field->getTransformedData());
  189. }
  190. public function testSetDataIsTransformedCorrectly()
  191. {
  192. $normTransformer = new FixedValueTransformer(array(
  193. null => '',
  194. 0 => 'norm[0]',
  195. ));
  196. $valueTransformer = new FixedValueTransformer(array(
  197. '' => '',
  198. 'norm[0]' => 'transform[norm[0]]',
  199. ));
  200. $field = $this->factory->getInstance('field', 'title', array(
  201. 'value_transformer' => $valueTransformer,
  202. 'normalization_transformer' => $normTransformer,
  203. ));
  204. $field->setData(0);
  205. $this->assertEquals(0, $field->getData());
  206. $this->assertEquals('norm[0]', $field->getNormalizedData());
  207. $this->assertEquals('transform[norm[0]]', $field->getTransformedData());
  208. }
  209. public function testBoundDataIsTrimmedBeforeTransforming()
  210. {
  211. $transformer = new FixedValueTransformer(array(
  212. null => '',
  213. 'reverse[a]' => 'a',
  214. ));
  215. $field = $this->factory->getInstance('field', 'title', array(
  216. 'value_transformer' => $transformer,
  217. ));
  218. $field->bind(' a ');
  219. $this->assertEquals('a', $field->getTransformedData());
  220. $this->assertEquals('reverse[a]', $field->getData());
  221. }
  222. public function testBoundDataIsNotTrimmedBeforeTransformingIfDisabled()
  223. {
  224. $transformer = new FixedValueTransformer(array(
  225. null => '',
  226. 'reverse[ a ]' => ' a ',
  227. ));
  228. $field = $this->factory->getInstance('field', 'title', array(
  229. 'trim' => false,
  230. 'value_transformer' => $transformer,
  231. ));
  232. $field->bind(' a ');
  233. $this->assertEquals(' a ', $field->getTransformedData());
  234. $this->assertEquals('reverse[ a ]', $field->getData());
  235. }
  236. public function testWritePropertyDoesNotWritePropertyIfPropertyPathIsEmpty()
  237. {
  238. $object = new Author();
  239. $field = $this->factory->getInstance('field', 'firstName', array('property_path' => null));
  240. $field->bind('Bernhard');
  241. $field->writeProperty($object);
  242. $this->assertEquals(null, $object->firstName);
  243. }
  244. public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
  245. {
  246. $field = $this->factory->getInstance('field', 'title', array(
  247. 'trim' => false,
  248. ));
  249. $field->bind('a');
  250. $this->assertEquals('a', $field->getTransformedData());
  251. $this->assertTrue($field->isTransformationSuccessful());
  252. }
  253. public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
  254. {
  255. // The value is passed to the value transformer
  256. $transformer = $this->createMockTransformer();
  257. $field = $this->factory->getInstance('field', 'title', array(
  258. 'trim' => false,
  259. 'value_transformer' => $transformer,
  260. ));
  261. $transformer->expects($this->once())
  262. ->method('reverseTransform')
  263. ->will($this->throwException(new TransformationFailedException()));
  264. $field->bind('a');
  265. $this->assertEquals('a', $field->getTransformedData());
  266. $this->assertFalse($field->isTransformationSuccessful());
  267. }
  268. public function testGetRootReturnsRootOfParentIfSet()
  269. {
  270. $parent = $this->createMockGroup();
  271. $parent->expects($this->any())
  272. ->method('getRoot')
  273. ->will($this->returnValue('ROOT'));
  274. $this->field->setParent($parent);
  275. $this->assertEquals('ROOT', $this->field->getRoot());
  276. }
  277. public function testGetRootReturnsFieldIfNoParent()
  278. {
  279. $this->assertEquals($this->field, $this->field->getRoot());
  280. }
  281. public function testIsEmptyReturnsTrueIfNull()
  282. {
  283. $this->field->setData(null);
  284. $this->assertTrue($this->field->isEmpty());
  285. }
  286. public function testIsEmptyReturnsTrueIfEmptyString()
  287. {
  288. $this->field->setData('');
  289. $this->assertTrue($this->field->isEmpty());
  290. }
  291. public function testIsEmptyReturnsFalseIfZero()
  292. {
  293. $this->field->setData(0);
  294. $this->assertFalse($this->field->isEmpty());
  295. }
  296. protected function createMockTransformer()
  297. {
  298. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  299. }
  300. protected function createMockTransformerTransformingTo($value)
  301. {
  302. $transformer = $this->createMockTransformer();
  303. $transformer->expects($this->any())
  304. ->method('reverseTransform')
  305. ->will($this->returnValue($value));
  306. return $transformer;
  307. }
  308. protected function createMockGroup()
  309. {
  310. return $this->getMock(
  311. 'Symfony\Component\Form\Form',
  312. array(),
  313. array(),
  314. '',
  315. false // don't call constructor
  316. );
  317. }
  318. protected function createMockGroupWithName($name)
  319. {
  320. $group = $this->createMockGroup();
  321. $group->expects($this->any())
  322. ->method('getName')
  323. ->will($this->returnValue($name));
  324. return $group;
  325. }
  326. protected function createMockGroupWithId($id)
  327. {
  328. $group = $this->createMockGroup();
  329. $group->expects($this->any())
  330. ->method('getId')
  331. ->will($this->returnValue($id));
  332. return $group;
  333. }
  334. }