/vendor/symfony/tests/Symfony/Tests/Component/Form/Extension/Core/Type/ChoiceTypeTest.php

https://github.com/proclamo/txinbometro · PHP · 439 lines · 352 code · 66 blank · 21 comment · 0 complexity · 04fc45ce78a4f2ca1c1b7c2df74e026f 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. use Symfony\Component\Form\ChoiceField;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. class ChoiceTypeTest extends TypeTestCase
  14. {
  15. private $choices = array(
  16. 'a' => 'Bernhard',
  17. 'b' => 'Fabien',
  18. 'c' => 'Kris',
  19. 'd' => 'Jon',
  20. 'e' => 'Roman',
  21. );
  22. private $numericChoices = array(
  23. 0 => 'Bernhard',
  24. 1 => 'Fabien',
  25. 2 => 'Kris',
  26. 3 => 'Jon',
  27. 4 => 'Roman',
  28. );
  29. private $stringButNumericChoices = array(
  30. '0' => 'Bernhard',
  31. '1' => 'Fabien',
  32. '2' => 'Kris',
  33. '3' => 'Jon',
  34. '4' => 'Roman',
  35. );
  36. protected $groupedChoices = array(
  37. 'Symfony' => array(
  38. 'a' => 'Bernhard',
  39. 'b' => 'Fabien',
  40. 'c' => 'Kris',
  41. ),
  42. 'Doctrine' => array(
  43. 'd' => 'Jon',
  44. 'e' => 'Roman',
  45. )
  46. );
  47. /**
  48. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  49. */
  50. public function testChoicesOptionExpectsArray()
  51. {
  52. $form = $this->factory->create('choice', null, array(
  53. 'choices' => new \ArrayObject(),
  54. ));
  55. }
  56. /**
  57. * @expectedException Symfony\Component\Form\Exception\FormException
  58. */
  59. public function testChoiceListOptionExpectsChoiceListInterface()
  60. {
  61. $form = $this->factory->create('choice', null, array(
  62. 'choice_list' => array('foo' => 'foo'),
  63. ));
  64. }
  65. public function testExpandedCheckboxesAreNeverRequired()
  66. {
  67. $form = $this->factory->create('choice', null, array(
  68. 'multiple' => true,
  69. 'expanded' => true,
  70. 'required' => true,
  71. 'choices' => $this->choices,
  72. ));
  73. foreach ($form as $child) {
  74. $this->assertFalse($child->isRequired());
  75. }
  76. }
  77. public function testExpandedRadiosAreRequiredIfChoiceFieldIsRequired()
  78. {
  79. $form = $this->factory->create('choice', null, array(
  80. 'multiple' => false,
  81. 'expanded' => true,
  82. 'required' => true,
  83. 'choices' => $this->choices,
  84. ));
  85. foreach ($form as $child) {
  86. $this->assertTrue($child->isRequired());
  87. }
  88. }
  89. public function testExpandedRadiosAreNotRequiredIfChoiceFieldIsNotRequired()
  90. {
  91. $form = $this->factory->create('choice', null, array(
  92. 'multiple' => false,
  93. 'expanded' => true,
  94. 'required' => false,
  95. 'choices' => $this->choices,
  96. ));
  97. foreach ($form as $child) {
  98. $this->assertFalse($child->isRequired());
  99. }
  100. }
  101. public function testBindSingleNonExpanded()
  102. {
  103. $form = $this->factory->create('choice', null, array(
  104. 'multiple' => false,
  105. 'expanded' => false,
  106. 'choices' => $this->choices,
  107. ));
  108. $form->bind('b');
  109. $this->assertEquals('b', $form->getData());
  110. $this->assertEquals('b', $form->getClientData());
  111. }
  112. public function testBindMultipleNonExpanded()
  113. {
  114. $form = $this->factory->create('choice', null, array(
  115. 'multiple' => true,
  116. 'expanded' => false,
  117. 'choices' => $this->choices,
  118. ));
  119. $form->bind(array('a', 'b'));
  120. $this->assertEquals(array('a', 'b'), $form->getData());
  121. $this->assertEquals(array('a', 'b'), $form->getClientData());
  122. }
  123. public function testBindSingleExpanded()
  124. {
  125. $form = $this->factory->create('choice', null, array(
  126. 'multiple' => false,
  127. 'expanded' => true,
  128. 'choices' => $this->choices,
  129. ));
  130. $form->bind('b');
  131. $this->assertSame('b', $form->getData());
  132. $this->assertSame(false, $form['a']->getData());
  133. $this->assertSame(true, $form['b']->getData());
  134. $this->assertSame(false, $form['c']->getData());
  135. $this->assertSame(false, $form['d']->getData());
  136. $this->assertSame(false, $form['e']->getData());
  137. $this->assertSame('', $form['a']->getClientData());
  138. $this->assertSame('1', $form['b']->getClientData());
  139. $this->assertSame('', $form['c']->getClientData());
  140. $this->assertSame('', $form['d']->getClientData());
  141. $this->assertSame('', $form['e']->getClientData());
  142. }
  143. public function testBindSingleExpandedWithFalseDoesNotHaveExtraFields()
  144. {
  145. $form = $this->factory->create('choice', null, array(
  146. 'multiple' => false,
  147. 'expanded' => true,
  148. 'choices' => $this->choices,
  149. ));
  150. $form->bind(false);
  151. $this->assertEmpty($form->getExtraData());
  152. $this->assertNull($form->getData());
  153. }
  154. public function testBindSingleExpandedNumericChoices()
  155. {
  156. $form = $this->factory->create('choice', null, array(
  157. 'multiple' => false,
  158. 'expanded' => true,
  159. 'choices' => $this->numericChoices,
  160. ));
  161. $form->bind('1');
  162. $this->assertSame('1', $form->getData());
  163. $this->assertSame(false, $form[0]->getData());
  164. $this->assertSame(true, $form[1]->getData());
  165. $this->assertSame(false, $form[2]->getData());
  166. $this->assertSame(false, $form[3]->getData());
  167. $this->assertSame(false, $form[4]->getData());
  168. $this->assertSame('', $form[0]->getClientData());
  169. $this->assertSame('1', $form[1]->getClientData());
  170. $this->assertSame('', $form[2]->getClientData());
  171. $this->assertSame('', $form[3]->getClientData());
  172. $this->assertSame('', $form[4]->getClientData());
  173. }
  174. public function testBindSingleExpandedStringsButNumericChoices()
  175. {
  176. $form = $this->factory->create('choice', null, array(
  177. 'multiple' => false,
  178. 'expanded' => true,
  179. 'choices' => $this->stringButNumericChoices,
  180. ));
  181. $form->bind('1');
  182. $this->assertSame('1', $form->getData());
  183. $this->assertSame(false, $form[0]->getData());
  184. $this->assertSame(true, $form[1]->getData());
  185. $this->assertSame(false, $form[2]->getData());
  186. $this->assertSame(false, $form[3]->getData());
  187. $this->assertSame(false, $form[4]->getData());
  188. $this->assertSame('', $form[0]->getClientData());
  189. $this->assertSame('1', $form[1]->getClientData());
  190. $this->assertSame('', $form[2]->getClientData());
  191. $this->assertSame('', $form[3]->getClientData());
  192. $this->assertSame('', $form[4]->getClientData());
  193. }
  194. public function testBindMultipleExpanded()
  195. {
  196. $form = $this->factory->create('choice', null, array(
  197. 'multiple' => true,
  198. 'expanded' => true,
  199. 'choices' => $this->choices,
  200. ));
  201. $form->bind(array('a' => 'a', 'b' => 'b'));
  202. $this->assertSame(array('a', 'b'), $form->getData());
  203. $this->assertSame(true, $form['a']->getData());
  204. $this->assertSame(true, $form['b']->getData());
  205. $this->assertSame(false, $form['c']->getData());
  206. $this->assertSame(false, $form['d']->getData());
  207. $this->assertSame(false, $form['e']->getData());
  208. $this->assertSame('1', $form['a']->getClientData());
  209. $this->assertSame('1', $form['b']->getClientData());
  210. $this->assertSame('', $form['c']->getClientData());
  211. $this->assertSame('', $form['d']->getClientData());
  212. $this->assertSame('', $form['e']->getClientData());
  213. }
  214. public function testBindMultipleExpandedNumericChoices()
  215. {
  216. $form = $this->factory->create('choice', null, array(
  217. 'multiple' => true,
  218. 'expanded' => true,
  219. 'choices' => $this->numericChoices,
  220. ));
  221. $form->bind(array(1 => 1, 2 => 2));
  222. $this->assertSame(array(1, 2), $form->getData());
  223. $this->assertSame(false, $form[0]->getData());
  224. $this->assertSame(true, $form[1]->getData());
  225. $this->assertSame(true, $form[2]->getData());
  226. $this->assertSame(false, $form[3]->getData());
  227. $this->assertSame(false, $form[4]->getData());
  228. $this->assertSame('', $form[0]->getClientData());
  229. $this->assertSame('1', $form[1]->getClientData());
  230. $this->assertSame('1', $form[2]->getClientData());
  231. $this->assertSame('', $form[3]->getClientData());
  232. $this->assertSame('', $form[4]->getClientData());
  233. }
  234. /*
  235. * We need this functionality to create choice fields for Boolean types,
  236. * e.g. false => 'No', true => 'Yes'
  237. */
  238. public function testSetDataSingleNonExpandedAcceptsBoolean()
  239. {
  240. $form = $this->factory->create('choice', null, array(
  241. 'multiple' => false,
  242. 'expanded' => false,
  243. 'choices' => $this->numericChoices,
  244. ));
  245. $form->setData(false);
  246. $this->assertEquals(false, $form->getData());
  247. $this->assertEquals('0', $form->getClientData());
  248. }
  249. public function testSetDataMultipleNonExpandedAcceptsBoolean()
  250. {
  251. $form = $this->factory->create('choice', null, array(
  252. 'multiple' => true,
  253. 'expanded' => false,
  254. 'choices' => $this->numericChoices,
  255. ));
  256. $form->setData(array(false, true));
  257. $this->assertEquals(array(false, true), $form->getData());
  258. $this->assertEquals(array('0', '1'), $form->getClientData());
  259. }
  260. public function testPassRequiredToView()
  261. {
  262. $form = $this->factory->create('choice', null, array(
  263. 'choices' => $this->choices,
  264. ));
  265. $view = $form->createView();
  266. $this->assertTrue($view->get('required'));
  267. }
  268. public function testPassNonRequiredToView()
  269. {
  270. $form = $this->factory->create('choice', null, array(
  271. 'required' => false,
  272. 'choices' => $this->choices,
  273. ));
  274. $view = $form->createView();
  275. $this->assertFalse($view->get('required'));
  276. }
  277. public function testPassMultipleToView()
  278. {
  279. $form = $this->factory->create('choice', null, array(
  280. 'multiple' => true,
  281. 'choices' => $this->choices,
  282. ));
  283. $view = $form->createView();
  284. $this->assertTrue($view->get('multiple'));
  285. }
  286. public function testPassExpandedToView()
  287. {
  288. $form = $this->factory->create('choice', null, array(
  289. 'expanded' => true,
  290. 'choices' => $this->choices,
  291. ));
  292. $view = $form->createView();
  293. $this->assertTrue($view->get('expanded'));
  294. }
  295. public function testNotPassedEmptyValueToViewIsNull()
  296. {
  297. $form = $this->factory->create('choice', null, array(
  298. 'multiple' => false,
  299. 'choices' => $this->choices,
  300. ));
  301. $view = $form->createView();
  302. $this->assertNull($view->get('empty_value'));
  303. }
  304. public function testPassEmptyValueToViewIsEmpty()
  305. {
  306. $form = $this->factory->create('choice', null, array(
  307. 'multiple' => false,
  308. 'required' => false,
  309. 'choices' => $this->choices,
  310. ));
  311. $view = $form->createView();
  312. $this->assertEmpty($view->get('empty_value'));
  313. }
  314. /**
  315. * @dataProvider getOptionsWithEmptyValue
  316. */
  317. public function testPassEmptyValueToView($multiple, $expanded, $required, $emptyValue, $viewValue)
  318. {
  319. $form = $this->factory->create('choice', null, array(
  320. 'multiple' => $multiple,
  321. 'expanded' => $expanded,
  322. 'required' => $required,
  323. 'empty_value' => $emptyValue,
  324. 'choices' => $this->choices,
  325. ));
  326. $view = $form->createView();
  327. $this->assertEquals($viewValue, $view->get('empty_value'));
  328. }
  329. public function getOptionsWithEmptyValue()
  330. {
  331. return array(
  332. array(false, false, false, 'foobar', 'foobar'),
  333. array(true, false, false, 'foobar', null),
  334. array(false, true, false, 'foobar', null),
  335. array(false, false, true, 'foobar', 'foobar'),
  336. array(false, false, true, '', ''),
  337. array(false, false, true, null, null),
  338. array(false, true, true, 'foobar', null),
  339. array(true, true, false, 'foobar', null),
  340. array(true, true, true, 'foobar', null),
  341. );
  342. }
  343. public function testPassChoicesToView()
  344. {
  345. $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D');
  346. $form = $this->factory->create('choice', null, array(
  347. 'choices' => $choices,
  348. ));
  349. $view = $form->createView();
  350. $this->assertSame($choices, $view->get('choices'));
  351. }
  352. public function testPassPreferredChoicesToView()
  353. {
  354. $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D');
  355. $form = $this->factory->create('choice', null, array(
  356. 'choices' => $choices,
  357. 'preferred_choices' => array('b', 'd'),
  358. ));
  359. $view = $form->createView();
  360. $this->assertSame(array('a' => 'A', 'c' => 'C'), $view->get('choices'));
  361. $this->assertSame(array('b' => 'B', 'd' => 'D'), $view->get('preferred_choices'));
  362. }
  363. public function testAdjustFullNameForMultipleNonExpanded()
  364. {
  365. $form = $this->factory->createNamed('choice', 'name', null, array(
  366. 'multiple' => true,
  367. 'expanded' => false,
  368. 'choices' => $this->choices,
  369. ));
  370. $view = $form->createView();
  371. $this->assertSame('name[]', $view->get('full_name'));
  372. }
  373. }