ChoiceFieldTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. 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\InvalidOptionsException
  85. */
  86. public function testConfigureChoicesWithNonArray()
  87. {
  88. $field = new ChoiceField('name', array(
  89. 'choices' => new \ArrayObject(),
  90. ));
  91. }
  92. /**
  93. * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
  94. */
  95. public function testConfigurePreferredChoicesWithNonArray()
  96. {
  97. $field = new ChoiceField('name', array(
  98. 'choices' => $this->choices,
  99. 'preferred_choices' => new \ArrayObject(),
  100. ));
  101. }
  102. public function getChoicesVariants()
  103. {
  104. $choices = $this->choices;
  105. return array(
  106. array($choices),
  107. array(function () use ($choices) { return $choices; }),
  108. );
  109. }
  110. public function getNumericChoicesVariants()
  111. {
  112. $choices = $this->numericChoices;
  113. return array(
  114. array($choices),
  115. array(function () use ($choices) { return $choices; }),
  116. );
  117. }
  118. /**
  119. * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
  120. */
  121. public function testClosureShouldReturnArray()
  122. {
  123. $field = new ChoiceField('name', array(
  124. 'choices' => function () { return 'foobar'; },
  125. ));
  126. // trigger closure
  127. $field->getOtherChoices();
  128. }
  129. public function testNonRequiredContainsEmptyField()
  130. {
  131. $field = new ChoiceField('name', array(
  132. 'multiple' => false,
  133. 'expanded' => false,
  134. 'choices' => $this->choices,
  135. 'required' => false,
  136. ));
  137. $this->assertEquals(array('' => '') + $this->choices, $field->getOtherChoices());
  138. }
  139. public function testRequiredContainsNoEmptyField()
  140. {
  141. $field = new ChoiceField('name', array(
  142. 'multiple' => false,
  143. 'expanded' => false,
  144. 'choices' => $this->choices,
  145. 'required' => true,
  146. ));
  147. $this->assertEquals($this->choices, $field->getOtherChoices());
  148. }
  149. public function testEmptyValueConfiguresLabelOfEmptyField()
  150. {
  151. $field = new ChoiceField('name', array(
  152. 'multiple' => false,
  153. 'expanded' => false,
  154. 'choices' => $this->choices,
  155. 'required' => false,
  156. 'empty_value' => 'Foobar',
  157. ));
  158. $this->assertEquals(array('' => 'Foobar') + $this->choices, $field->getOtherChoices());
  159. }
  160. /**
  161. * @dataProvider getChoicesVariants
  162. */
  163. public function testSubmitSingleNonExpanded($choices)
  164. {
  165. $field = new ChoiceField('name', array(
  166. 'multiple' => false,
  167. 'expanded' => false,
  168. 'choices' => $choices,
  169. ));
  170. $field->submit('b');
  171. $this->assertEquals('b', $field->getData());
  172. $this->assertEquals('b', $field->getDisplayedData());
  173. }
  174. /**
  175. * @dataProvider getChoicesVariants
  176. */
  177. public function testSubmitMultipleNonExpanded($choices)
  178. {
  179. $field = new ChoiceField('name', array(
  180. 'multiple' => true,
  181. 'expanded' => false,
  182. 'choices' => $choices,
  183. ));
  184. $field->submit(array('a', 'b'));
  185. $this->assertEquals(array('a', 'b'), $field->getData());
  186. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  187. }
  188. /**
  189. * @dataProvider getChoicesVariants
  190. */
  191. public function testSubmitSingleExpanded($choices)
  192. {
  193. $field = new ChoiceField('name', array(
  194. 'multiple' => false,
  195. 'expanded' => true,
  196. 'choices' => $choices,
  197. ));
  198. $field->submit('b');
  199. $this->assertSame('b', $field->getData());
  200. $this->assertSame(false, $field['a']->getData());
  201. $this->assertSame(true, $field['b']->getData());
  202. $this->assertSame(false, $field['c']->getData());
  203. $this->assertSame(false, $field['d']->getData());
  204. $this->assertSame(false, $field['e']->getData());
  205. $this->assertSame('', $field['a']->getDisplayedData());
  206. $this->assertSame('1', $field['b']->getDisplayedData());
  207. $this->assertSame('', $field['c']->getDisplayedData());
  208. $this->assertSame('', $field['d']->getDisplayedData());
  209. $this->assertSame('', $field['e']->getDisplayedData());
  210. $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  211. }
  212. /**
  213. * @dataProvider getNumericChoicesVariants
  214. */
  215. public function testSubmitSingleExpandedNumericChoices($choices)
  216. {
  217. $field = new ChoiceField('name', array(
  218. 'multiple' => false,
  219. 'expanded' => true,
  220. 'choices' => $choices,
  221. ));
  222. $field->submit('1');
  223. $this->assertSame(1, $field->getData());
  224. $this->assertSame(false, $field[0]->getData());
  225. $this->assertSame(true, $field[1]->getData());
  226. $this->assertSame(false, $field[2]->getData());
  227. $this->assertSame(false, $field[3]->getData());
  228. $this->assertSame(false, $field[4]->getData());
  229. $this->assertSame('', $field[0]->getDisplayedData());
  230. $this->assertSame('1', $field[1]->getDisplayedData());
  231. $this->assertSame('', $field[2]->getDisplayedData());
  232. $this->assertSame('', $field[3]->getDisplayedData());
  233. $this->assertSame('', $field[4]->getDisplayedData());
  234. $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
  235. }
  236. /**
  237. * @dataProvider getChoicesVariants
  238. */
  239. public function testSubmitMultipleExpanded($choices)
  240. {
  241. $field = new ChoiceField('name', array(
  242. 'multiple' => true,
  243. 'expanded' => true,
  244. 'choices' => $choices,
  245. ));
  246. $field->submit(array('a' => 'a', 'b' => 'b'));
  247. $this->assertSame(array('a', 'b'), $field->getData());
  248. $this->assertSame(true, $field['a']->getData());
  249. $this->assertSame(true, $field['b']->getData());
  250. $this->assertSame(false, $field['c']->getData());
  251. $this->assertSame(false, $field['d']->getData());
  252. $this->assertSame(false, $field['e']->getData());
  253. $this->assertSame('1', $field['a']->getDisplayedData());
  254. $this->assertSame('1', $field['b']->getDisplayedData());
  255. $this->assertSame('', $field['c']->getDisplayedData());
  256. $this->assertSame('', $field['d']->getDisplayedData());
  257. $this->assertSame('', $field['e']->getDisplayedData());
  258. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  259. }
  260. /**
  261. * @dataProvider getNumericChoicesVariants
  262. */
  263. public function testSubmitMultipleExpandedNumericChoices($choices)
  264. {
  265. $field = new ChoiceField('name', array(
  266. 'multiple' => true,
  267. 'expanded' => true,
  268. 'choices' => $choices,
  269. ));
  270. $field->submit(array(1 => 1, 2 => 2));
  271. $this->assertSame(array(1, 2), $field->getData());
  272. $this->assertSame(false, $field[0]->getData());
  273. $this->assertSame(true, $field[1]->getData());
  274. $this->assertSame(true, $field[2]->getData());
  275. $this->assertSame(false, $field[3]->getData());
  276. $this->assertSame(false, $field[4]->getData());
  277. $this->assertSame('', $field[0]->getDisplayedData());
  278. $this->assertSame('1', $field[1]->getDisplayedData());
  279. $this->assertSame('1', $field[2]->getDisplayedData());
  280. $this->assertSame('', $field[3]->getDisplayedData());
  281. $this->assertSame('', $field[4]->getDisplayedData());
  282. $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
  283. }
  284. }