FieldTest.php 13 KB

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