PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php

http://github.com/symfony/symfony
PHP | 307 lines | 218 code | 71 blank | 18 comment | 0 complexity | 20cb8eef5cf901b6a6e39a18819dffb5 MD5 | raw file
  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\Extension\Core\Type;
  11. require_once __DIR__ . '/TypeTestCase.php';
  12. require_once __DIR__ . '/../../../Fixtures/Author.php';
  13. require_once __DIR__ . '/../../../Fixtures/FixedDataTransformer.php';
  14. require_once __DIR__ . '/../../../Fixtures/FixedFilterListener.php';
  15. use Symfony\Component\Form\Util\PropertyPath;
  16. use Symfony\Component\Form\Form;
  17. use Symfony\Tests\Component\Form\Fixtures\Author;
  18. use Symfony\Tests\Component\Form\Fixtures\FixedDataTransformer;
  19. use Symfony\Tests\Component\Form\Fixtures\FixedFilterListener;
  20. class FieldTypeTest extends TypeTestCase
  21. {
  22. public function testGetPropertyPathDefaultPath()
  23. {
  24. $form = $this->factory->createNamed('field', 'title');
  25. $this->assertEquals(new PropertyPath('title'), $form->getAttribute('property_path'));
  26. }
  27. public function testGetPropertyPathPathIsZero()
  28. {
  29. $form = $this->factory->create('field', null, array('property_path' => '0'));
  30. $this->assertEquals(new PropertyPath('0'), $form->getAttribute('property_path'));
  31. }
  32. public function testGetPropertyPathPathIsEmpty()
  33. {
  34. $form = $this->factory->create('field', null, array('property_path' => ''));
  35. $this->assertNull($form->getAttribute('property_path'));
  36. }
  37. public function testGetPropertyPathPathIsFalse()
  38. {
  39. $form = $this->factory->create('field', null, array('property_path' => false));
  40. $this->assertNull($form->getAttribute('property_path'));
  41. }
  42. public function testGetPropertyPathPathIsNull()
  43. {
  44. $form = $this->factory->createNamed('field', 'title', null, array('property_path' => null));
  45. $this->assertEquals(new PropertyPath('title'), $form->getAttribute('property_path'));
  46. }
  47. public function testPassRequiredAsOption()
  48. {
  49. $form = $this->factory->create('field', null, array('required' => false));
  50. $this->assertFalse($form->isRequired());
  51. $form = $this->factory->create('field', null, array('required' => true));
  52. $this->assertTrue($form->isRequired());
  53. }
  54. public function testPassDisabledAsOption()
  55. {
  56. $form = $this->factory->create('field', null, array('disabled' => true));
  57. $this->assertTrue($form->isDisabled());
  58. }
  59. public function testBoundDataIsTrimmedBeforeTransforming()
  60. {
  61. $form = $this->factory->createBuilder('field')
  62. ->appendClientTransformer(new FixedDataTransformer(array(
  63. null => '',
  64. 'reverse[a]' => 'a',
  65. )))
  66. ->getForm();
  67. $form->bind(' a ');
  68. $this->assertEquals('a', $form->getClientData());
  69. $this->assertEquals('reverse[a]', $form->getData());
  70. }
  71. public function testBoundDataIsNotTrimmedBeforeTransformingIfNoTrimming()
  72. {
  73. $form = $this->factory->createBuilder('field', null, array('trim' => false))
  74. ->appendClientTransformer(new FixedDataTransformer(array(
  75. null => '',
  76. 'reverse[ a ]' => ' a ',
  77. )))
  78. ->getForm();
  79. $form->bind(' a ');
  80. $this->assertEquals(' a ', $form->getClientData());
  81. $this->assertEquals('reverse[ a ]', $form->getData());
  82. }
  83. public function testPassIdAndNameToView()
  84. {
  85. $form = $this->factory->createNamed('field', 'name');
  86. $view = $form->createView();
  87. $this->assertEquals('name', $view->get('id'));
  88. $this->assertEquals('name', $view->get('name'));
  89. $this->assertEquals('name', $view->get('full_name'));
  90. }
  91. public function testStripLeadingUnderscoresAndDigitsFromId()
  92. {
  93. $form = $this->factory->createNamed('field', '_09name');
  94. $view = $form->createView();
  95. $this->assertEquals('name', $view->get('id'));
  96. $this->assertEquals('_09name', $view->get('name'));
  97. $this->assertEquals('_09name', $view->get('full_name'));
  98. }
  99. public function testPassIdAndNameToViewWithParent()
  100. {
  101. $parent = $this->factory->createNamed('field', 'parent');
  102. $parent->add($this->factory->createNamed('field', 'child'));
  103. $view = $parent->createView();
  104. $this->assertEquals('parent_child', $view['child']->get('id'));
  105. $this->assertEquals('child', $view['child']->get('name'));
  106. $this->assertEquals('parent[child]', $view['child']->get('full_name'));
  107. }
  108. public function testPassIdAndNameToViewWithGrandParent()
  109. {
  110. $parent = $this->factory->createNamed('field', 'parent');
  111. $parent->add($this->factory->createNamed('field', 'child'));
  112. $parent['child']->add($this->factory->createNamed('field', 'grand_child'));
  113. $view = $parent->createView();
  114. $this->assertEquals('parent_child_grand_child', $view['child']['grand_child']->get('id'));
  115. $this->assertEquals('grand_child', $view['child']['grand_child']->get('name'));
  116. $this->assertEquals('parent[child][grand_child]', $view['child']['grand_child']->get('full_name'));
  117. }
  118. public function testPassMaxLengthToView()
  119. {
  120. $form = $this->factory->create('field', null, array('max_length' => 10));
  121. $view = $form->createView();
  122. $this->assertSame(10, $view->get('max_length'));
  123. }
  124. public function testPassTranslationDomainToView()
  125. {
  126. $form = $this->factory->create('field', null, array('translation_domain' => 'test'));
  127. $view = $form->createView();
  128. $this->assertSame('test', $view->get('translation_domain'));
  129. }
  130. public function testDefaultTranslationDomain()
  131. {
  132. $form = $this->factory->create('field');
  133. $view = $form->createView();
  134. $this->assertSame('messages', $view->get('translation_domain'));
  135. }
  136. public function testBindWithEmptyDataCreatesObjectIfClassAvailable()
  137. {
  138. $form = $this->factory->create('form', null, array(
  139. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  140. 'required' => false,
  141. ));
  142. $form->add($this->factory->createNamed('field', 'firstName'));
  143. $form->add($this->factory->createNamed('field', 'lastName'));
  144. $form->setData(null);
  145. // partially empty, still an object is created
  146. $form->bind(array('firstName' => 'Bernhard', 'lastName' => ''));
  147. $author = new Author();
  148. $author->firstName = 'Bernhard';
  149. $author->setLastName('');
  150. $this->assertEquals($author, $form->getData());
  151. }
  152. public function testBindEmptyWithEmptyDataCreatesNoObjectIfNotRequired()
  153. {
  154. $form = $this->factory->create('form', null, array(
  155. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  156. 'required' => false,
  157. ));
  158. $form->add($this->factory->createNamed('field', 'firstName'));
  159. $form->add($this->factory->createNamed('field', 'lastName'));
  160. $form->setData(null);
  161. $form->bind(array('firstName' => '', 'lastName' => ''));
  162. $this->assertNull($form->getData());
  163. }
  164. public function testBindEmptyWithEmptyDataCreatesObjectIfRequired()
  165. {
  166. $form = $this->factory->create('form', null, array(
  167. 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
  168. 'required' => true,
  169. ));
  170. $form->add($this->factory->createNamed('field', 'firstName'));
  171. $form->add($this->factory->createNamed('field', 'lastName'));
  172. $form->setData(null);
  173. $form->bind(array('firstName' => '', 'lastName' => ''));
  174. $this->assertEquals(new Author(), $form->getData());
  175. }
  176. /*
  177. * We need something to write the field values into
  178. */
  179. public function testBindWithEmptyDataStoresArrayIfNoClassAvailable()
  180. {
  181. $form = $this->factory->create('form');
  182. $form->add($this->factory->createNamed('field', 'firstName'));
  183. $form->setData(null);
  184. $form->bind(array('firstName' => 'Bernhard'));
  185. $this->assertSame(array('firstName' => 'Bernhard'), $form->getData());
  186. }
  187. public function testBindWithEmptyDataUsesEmptyDataOption()
  188. {
  189. $author = new Author();
  190. $form = $this->factory->create('form', null, array(
  191. 'empty_data' => $author,
  192. ));
  193. $form->add($this->factory->createNamed('field', 'firstName'));
  194. $form->bind(array('firstName' => 'Bernhard'));
  195. $this->assertSame($author, $form->getData());
  196. $this->assertEquals('Bernhard', $author->firstName);
  197. }
  198. public function testGetAttributesIsEmpty()
  199. {
  200. $form = $this->factory->create('field', null, array('attr' => array()));
  201. $this->assertCount(0, $form->getAttribute('attr'));
  202. }
  203. /**
  204. * @see https://github.com/symfony/symfony/issues/1986
  205. */
  206. public function testSetDataThroughParamsWithZero()
  207. {
  208. $form = $this->factory->create('field', null, array('data' => 0));
  209. $view = $form->createView();
  210. $this->assertFalse($form->isEmpty());
  211. $this->assertSame('0', $view->get('value'));
  212. $this->assertSame('0', $form->getData());
  213. $form = $this->factory->create('field', null, array('data' => '0'));
  214. $view = $form->createView();
  215. $this->assertFalse($form->isEmpty());
  216. $this->assertSame('0', $view->get('value'));
  217. $this->assertSame('0', $form->getData());
  218. $form = $this->factory->create('field', null, array('data' => '00000'));
  219. $view = $form->createView();
  220. $this->assertFalse($form->isEmpty());
  221. $this->assertSame('00000', $view->get('value'));
  222. $this->assertSame('00000', $form->getData());
  223. }
  224. /**
  225. * @expectedException Symfony\Component\Form\Exception\FormException
  226. */
  227. public function testAttributesException()
  228. {
  229. $form = $this->factory->create('field', null, array('attr' => ''));
  230. }
  231. public function testNameCanBeEmptyString()
  232. {
  233. $form = $this->factory->createNamed('field', '');
  234. $this->assertEquals('', $form->getName());
  235. }
  236. }