ChoiceFieldTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. use Symfony\Component\Form\ChoiceField;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $choices = array(
  16. 'a' => 'Bernhard',
  17. 'b' => 'Fabien',
  18. 'c' => 'Kris',
  19. 'd' => 'Jon',
  20. 'e' => 'Roman',
  21. );
  22. protected $preferredChoices = array('d', 'e');
  23. protected $groupedChoices = array(
  24. 'Symfony' => array(
  25. 'a' => 'Bernhard',
  26. 'b' => 'Fabien',
  27. 'c' => 'Kris',
  28. ),
  29. 'Doctrine' => array(
  30. 'd' => 'Jon',
  31. 'e' => 'Roman',
  32. )
  33. );
  34. protected $numericChoices = array(
  35. 0 => 'Bernhard',
  36. 1 => 'Fabien',
  37. 2 => 'Kris',
  38. 3 => 'Jon',
  39. 4 => 'Roman',
  40. );
  41. public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_integerZero()
  42. {
  43. $field = new ChoiceField('name', array(
  44. 'choices' => array(
  45. 0 => 'Foo',
  46. '' => 'Bar',
  47. )
  48. ));
  49. $field->submit(0);
  50. $this->assertTrue($field->isChoiceSelected(0));
  51. $this->assertTrue($field->isChoiceSelected('0'));
  52. $this->assertFalse($field->isChoiceSelected(''));
  53. $field->submit('0');
  54. $this->assertTrue($field->isChoiceSelected(0));
  55. $this->assertTrue($field->isChoiceSelected('0'));
  56. $this->assertFalse($field->isChoiceSelected(''));
  57. $field->submit('');
  58. $this->assertFalse($field->isChoiceSelected(0));
  59. $this->assertFalse($field->isChoiceSelected('0'));
  60. $this->assertTrue($field->isChoiceSelected(''));
  61. }
  62. public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_stringZero()
  63. {
  64. $field = new ChoiceField('name', array(
  65. 'choices' => array(
  66. '0' => 'Foo',
  67. '' => 'Bar',
  68. )
  69. ));
  70. $field->submit(0);
  71. $this->assertTrue($field->isChoiceSelected(0));
  72. $this->assertTrue($field->isChoiceSelected('0'));
  73. $this->assertFalse($field->isChoiceSelected(''));
  74. $field->submit('0');
  75. $this->assertTrue($field->isChoiceSelected(0));
  76. $this->assertTrue($field->isChoiceSelected('0'));
  77. $this->assertFalse($field->isChoiceSelected(''));
  78. $field->submit('');
  79. $this->assertFalse($field->isChoiceSelected(0));
  80. $this->assertFalse($field->isChoiceSelected('0'));
  81. $this->assertTrue($field->isChoiceSelected(''));
  82. }
  83. /**
  84. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  85. */
  86. public function testConfigureChoicesWithNonArray()
  87. {
  88. $field = new ChoiceField('name', array(
  89. 'choices' => new \ArrayObject(),
  90. ));
  91. }
  92. public function getChoicesVariants()
  93. {
  94. $choices = $this->choices;
  95. return array(
  96. array($choices),
  97. array(function () use ($choices) { return $choices; }),
  98. );
  99. }
  100. public function getNumericChoicesVariants()
  101. {
  102. $choices = $this->numericChoices;
  103. return array(
  104. array($choices),
  105. array(function () use ($choices) { return $choices; }),
  106. );
  107. }
  108. /**
  109. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  110. */
  111. public function testClosureShouldReturnArray()
  112. {
  113. $field = new ChoiceField('name', array(
  114. 'choices' => function () { return 'foobar'; },
  115. ));
  116. // trigger closure
  117. $field->getOtherChoices();
  118. }
  119. public function testNonRequiredContainsEmptyField()
  120. {
  121. $field = new ChoiceField('name', array(
  122. 'multiple' => false,
  123. 'expanded' => false,
  124. 'choices' => $this->choices,
  125. 'required' => false,
  126. ));
  127. $this->assertEquals(array('' => '') + $this->choices, $field->getOtherChoices());
  128. }
  129. public function testRequiredContainsNoEmptyField()
  130. {
  131. $field = new ChoiceField('name', array(
  132. 'multiple' => false,
  133. 'expanded' => false,
  134. 'choices' => $this->choices,
  135. 'required' => true,
  136. ));
  137. $this->assertEquals($this->choices, $field->getOtherChoices());
  138. }
  139. public function testEmptyValueConfiguresLabelOfEmptyField()
  140. {
  141. $field = new ChoiceField('name', array(
  142. 'multiple' => false,
  143. 'expanded' => false,
  144. 'choices' => $this->choices,
  145. 'required' => false,
  146. 'empty_value' => 'Foobar',
  147. ));
  148. $this->assertEquals(array('' => 'Foobar') + $this->choices, $field->getOtherChoices());
  149. }
  150. /**
  151. * @dataProvider getChoicesVariants
  152. */
  153. public function testSubmitSingleNonExpanded($choices)
  154. {
  155. $field = new ChoiceField('name', array(
  156. 'multiple' => false,
  157. 'expanded' => false,
  158. 'choices' => $choices,
  159. ));
  160. $field->submit('b');
  161. $this->assertEquals('b', $field->getData());
  162. $this->assertEquals('b', $field->getDisplayedData());
  163. }
  164. /**
  165. * @dataProvider getChoicesVariants
  166. */
  167. public function testSubmitMultipleNonExpanded($choices)
  168. {
  169. $field = new ChoiceField('name', array(
  170. 'multiple' => true,
  171. 'expanded' => false,
  172. 'choices' => $choices,
  173. ));
  174. $field->submit(array('a', 'b'));
  175. $this->assertEquals(array('a', 'b'), $field->getData());
  176. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  177. }
  178. /**
  179. * @dataProvider getChoicesVariants
  180. */
  181. public function testSubmitSingleExpanded($choices)
  182. {
  183. $field = new ChoiceField('name', array(
  184. 'multiple' => false,
  185. 'expanded' => true,
  186. 'choices' => $choices,
  187. ));
  188. $field->submit('b');
  189. $this->assertSame('b', $field->getData());
  190. $this->assertSame(false, $field['a']->getData());
  191. $this->assertSame(true, $field['b']->getData());
  192. $this->assertSame(false, $field['c']->getData());
  193. $this->assertSame(false, $field['d']->getData());
  194. $this->assertSame(false, $field['e']->getData());
  195. $this->assertSame('', $field['a']->getDisplayedData());
  196. $this->assertSame('1', $field['b']->getDisplayedData());
  197. $this->assertSame('', $field['c']->getDisplayedData());
  198. $this->assertSame('', $field['d']->getDisplayedData());
  199. $this->assertSame('', $field['e']->getDisplayedData());
  200. $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  201. }
  202. /**
  203. * @dataProvider getNumericChoicesVariants
  204. */
  205. public function testSubmitSingleExpandedNumericChoices($choices)
  206. {
  207. $field = new ChoiceField('name', array(
  208. 'multiple' => false,
  209. 'expanded' => true,
  210. 'choices' => $choices,
  211. ));
  212. $field->submit('1');
  213. $this->assertSame(1, $field->getData());
  214. $this->assertSame(false, $field[0]->getData());
  215. $this->assertSame(true, $field[1]->getData());
  216. $this->assertSame(false, $field[2]->getData());
  217. $this->assertSame(false, $field[3]->getData());
  218. $this->assertSame(false, $field[4]->getData());
  219. $this->assertSame('', $field[0]->getDisplayedData());
  220. $this->assertSame('1', $field[1]->getDisplayedData());
  221. $this->assertSame('', $field[2]->getDisplayedData());
  222. $this->assertSame('', $field[3]->getDisplayedData());
  223. $this->assertSame('', $field[4]->getDisplayedData());
  224. $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
  225. }
  226. /**
  227. * @dataProvider getChoicesVariants
  228. */
  229. public function testSubmitMultipleExpanded($choices)
  230. {
  231. $field = new ChoiceField('name', array(
  232. 'multiple' => true,
  233. 'expanded' => true,
  234. 'choices' => $choices,
  235. ));
  236. $field->submit(array('a' => 'a', 'b' => 'b'));
  237. $this->assertSame(array('a', 'b'), $field->getData());
  238. $this->assertSame(true, $field['a']->getData());
  239. $this->assertSame(true, $field['b']->getData());
  240. $this->assertSame(false, $field['c']->getData());
  241. $this->assertSame(false, $field['d']->getData());
  242. $this->assertSame(false, $field['e']->getData());
  243. $this->assertSame('1', $field['a']->getDisplayedData());
  244. $this->assertSame('1', $field['b']->getDisplayedData());
  245. $this->assertSame('', $field['c']->getDisplayedData());
  246. $this->assertSame('', $field['d']->getDisplayedData());
  247. $this->assertSame('', $field['e']->getDisplayedData());
  248. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  249. }
  250. /**
  251. * @dataProvider getNumericChoicesVariants
  252. */
  253. public function testSubmitMultipleExpandedNumericChoices($choices)
  254. {
  255. $field = new ChoiceField('name', array(
  256. 'multiple' => true,
  257. 'expanded' => true,
  258. 'choices' => $choices,
  259. ));
  260. $field->submit(array(1 => 1, 2 => 2));
  261. $this->assertSame(array(1, 2), $field->getData());
  262. $this->assertSame(false, $field[0]->getData());
  263. $this->assertSame(true, $field[1]->getData());
  264. $this->assertSame(true, $field[2]->getData());
  265. $this->assertSame(false, $field[3]->getData());
  266. $this->assertSame(false, $field[4]->getData());
  267. $this->assertSame('', $field[0]->getDisplayedData());
  268. $this->assertSame('1', $field[1]->getDisplayedData());
  269. $this->assertSame('1', $field[2]->getDisplayedData());
  270. $this->assertSame('', $field[3]->getDisplayedData());
  271. $this->assertSame('', $field[4]->getDisplayedData());
  272. $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
  273. }
  274. }