FieldTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/FixedDataTransformer.php';
  15. require_once __DIR__ . '/Fixtures/FixedFilterListener.php';
  16. use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
  17. use Symfony\Component\Form\PropertyPath;
  18. use Symfony\Component\Form\FieldError;
  19. use Symfony\Component\Form\Form;
  20. use Symfony\Component\Form\DataTransformer\TransformationFailedException;
  21. use Symfony\Tests\Component\Form\Fixtures\Author;
  22. use Symfony\Tests\Component\Form\Fixtures\InvalidField;
  23. use Symfony\Tests\Component\Form\Fixtures\FixedDataTransformer;
  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 testPassReadOnlyAsOption()
  61. {
  62. $field = $this->factory->create('field', 'title', array('read_only' => false));
  63. $this->assertFalse($field->isReadOnly());
  64. $field = $this->factory->create('field', 'title', array('read_only' => true));
  65. $this->assertTrue($field->isReadOnly());
  66. }
  67. public function testFieldIsReadOnlyIfParentIsReadOnly()
  68. {
  69. $field = $this->factory->create('field', 'title', array('read_only' => false));
  70. $field->setParent($this->factory->create('field', 'title', array('read_only' => true)));
  71. $this->assertTrue($field->isReadOnly());
  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->getClientData());
  145. }
  146. public function testDataIsTransformedCorrectlyIfNull_noDataTransformer()
  147. {
  148. $this->field->setData(null);
  149. $this->assertSame(null, $this->field->getData());
  150. $this->assertSame('', $this->field->getClientData());
  151. }
  152. public function testDataIsTransformedCorrectlyIfNotNull_noDataTransformer()
  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->getClientData());
  161. }
  162. public function testBoundDataIsTransformedCorrectly()
  163. {
  164. $filter = new FixedFilterListener(array(
  165. 'filterBoundClientData' => array(
  166. // 1. The value is converted to a string and passed to the
  167. // first filter
  168. '0' => 'filter1[0]',
  169. ),
  170. 'filterBoundNormData' => array(
  171. // 3. The normalized value is passed to the second filter
  172. 'norm[filter1[0]]' => 'filter2[norm[filter1[0]]]',
  173. ),
  174. ));
  175. $clientTransformer = new FixedDataTransformer(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 FixedDataTransformer(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->setClientTransformer($clientTransformer);
  193. $this->builder->setNormTransformer($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->getNormData());
  198. $this->assertEquals('client[filter2[norm[filter1[0]]]]', $field->getClientData());
  199. }
  200. public function testBoundDataIsTransformedCorrectlyIfEmpty_noDataTransformer()
  201. {
  202. $this->field->bind('');
  203. $this->assertSame(null, $this->field->getData());
  204. $this->assertEquals('', $this->field->getClientData());
  205. }
  206. public function testSetDataIsTransformedCorrectly()
  207. {
  208. $normTransformer = new FixedDataTransformer(array(
  209. null => '',
  210. 0 => 'norm[0]',
  211. ));
  212. $clientTransformer = new FixedDataTransformer(array(
  213. '' => '',
  214. 'norm[0]' => 'transform[norm[0]]',
  215. ));
  216. $builder = $this->factory->createBuilder('field', 'title');
  217. $builder->setNormTransformer($normTransformer);
  218. $builder->setClientTransformer($clientTransformer);
  219. $field = $builder->getInstance();
  220. $field->setData(0);
  221. $this->assertEquals(0, $field->getData());
  222. $this->assertEquals('norm[0]', $field->getNormData());
  223. $this->assertEquals('transform[norm[0]]', $field->getClientData());
  224. }
  225. public function testBoundDataIsTrimmedBeforeTransforming()
  226. {
  227. $clientTransformer = new FixedDataTransformer(array(
  228. null => '',
  229. 'reverse[a]' => 'a',
  230. ));
  231. $builder = $this->factory->createBuilder('field', 'title');
  232. $builder->setClientTransformer($clientTransformer);
  233. $field = $builder->getInstance();
  234. $field->bind(' a ');
  235. $this->assertEquals('a', $field->getClientData());
  236. $this->assertEquals('reverse[a]', $field->getData());
  237. }
  238. public function testBoundDataIsNotTrimmedBeforeTransformingIfReadOnly()
  239. {
  240. $clientTransformer = new FixedDataTransformer(array(
  241. null => '',
  242. 'reverse[ a ]' => ' a ',
  243. ));
  244. $builder = $this->factory->createBuilder('field', 'title', array(
  245. 'trim' => false,
  246. ));
  247. $builder->setClientTransformer($clientTransformer);
  248. $field = $builder->getInstance();
  249. $field->bind(' a ');
  250. $this->assertEquals(' a ', $field->getClientData());
  251. $this->assertEquals('reverse[ a ]', $field->getData());
  252. }
  253. public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
  254. {
  255. $field = $this->factory->create('field', 'title', array(
  256. 'trim' => false,
  257. ));
  258. $field->bind('a');
  259. $this->assertEquals('a', $field->getClientData());
  260. $this->assertTrue($field->isTransformationSuccessful());
  261. }
  262. public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
  263. {
  264. // The value is passed to the value transformer
  265. $clientTransformer = $this->createMockTransformer();
  266. $builder = $this->factory->createBuilder('field', 'title', array(
  267. 'trim' => false,
  268. ));
  269. $builder->setClientTransformer($clientTransformer);
  270. $field = $builder->getInstance();
  271. $clientTransformer->expects($this->once())
  272. ->method('reverseTransform')
  273. ->will($this->throwException(new TransformationFailedException()));
  274. $field->bind('a');
  275. $this->assertEquals('a', $field->getClientData());
  276. $this->assertFalse($field->isTransformationSuccessful());
  277. }
  278. public function testGetRootReturnsRootOfParentIfSet()
  279. {
  280. $parent = $this->createMockGroup();
  281. $parent->expects($this->any())
  282. ->method('getRoot')
  283. ->will($this->returnValue('ROOT'));
  284. $this->field->setParent($parent);
  285. $this->assertEquals('ROOT', $this->field->getRoot());
  286. }
  287. public function testGetRootReturnsFieldIfNoParent()
  288. {
  289. $this->assertEquals($this->field, $this->field->getRoot());
  290. }
  291. public function testIsEmptyReturnsTrueIfNull()
  292. {
  293. $this->field->setData(null);
  294. $this->assertTrue($this->field->isEmpty());
  295. }
  296. public function testIsEmptyReturnsTrueIfEmptyString()
  297. {
  298. $this->field->setData('');
  299. $this->assertTrue($this->field->isEmpty());
  300. }
  301. public function testIsEmptyReturnsFalseIfZero()
  302. {
  303. $this->field->setData(0);
  304. $this->assertFalse($this->field->isEmpty());
  305. }
  306. protected function createMockTransformer()
  307. {
  308. return $this->getMock('Symfony\Component\Form\DataTransformer\DataTransformerInterface', array(), array(), '', false, false);
  309. }
  310. protected function createMockTransformerTransformingTo($value)
  311. {
  312. $clientTransformer = $this->createMockTransformer();
  313. $clientTransformer->expects($this->any())
  314. ->method('reverseTransform')
  315. ->will($this->returnValue($value));
  316. return $clientTransformer;
  317. }
  318. protected function createMockGroup()
  319. {
  320. return $this->getMock(
  321. 'Symfony\Component\Form\Form',
  322. array(),
  323. array(),
  324. '',
  325. false // don't call constructor
  326. );
  327. }
  328. protected function createMockGroupWithName($name)
  329. {
  330. $group = $this->createMockGroup();
  331. $group->expects($this->any())
  332. ->method('getName')
  333. ->will($this->returnValue($name));
  334. return $group;
  335. }
  336. protected function createMockGroupWithId($id)
  337. {
  338. $group = $this->createMockGroup();
  339. $group->expects($this->any())
  340. ->method('getId')
  341. ->will($this->returnValue($id));
  342. return $group;
  343. }
  344. }