ChoiceFieldTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /**
  42. * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
  43. */
  44. public function testConfigureChoicesWithNonArray()
  45. {
  46. $field = new ChoiceField('name', array(
  47. 'choices' => new \ArrayObject(),
  48. ));
  49. }
  50. /**
  51. * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
  52. */
  53. public function testConfigurePreferredChoicesWithNonArray()
  54. {
  55. $field = new ChoiceField('name', array(
  56. 'choices' => $this->choices,
  57. 'preferred_choices' => new \ArrayObject(),
  58. ));
  59. }
  60. public function getChoicesVariants()
  61. {
  62. $choices = $this->choices;
  63. return array(
  64. array($choices),
  65. array(function () use ($choices) { return $choices; }),
  66. );
  67. }
  68. public function getNumericChoicesVariants()
  69. {
  70. $choices = $this->numericChoices;
  71. return array(
  72. array($choices),
  73. array(function () use ($choices) { return $choices; }),
  74. );
  75. }
  76. /**
  77. * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
  78. */
  79. public function testClosureShouldReturnArray()
  80. {
  81. $field = new ChoiceField('name', array(
  82. 'choices' => function () { return 'foobar'; },
  83. ));
  84. // trigger closure
  85. $field->getOtherChoices();
  86. }
  87. public function testNonRequiredContainsEmptyField()
  88. {
  89. $field = new ChoiceField('name', array(
  90. 'multiple' => false,
  91. 'expanded' => false,
  92. 'choices' => $this->choices,
  93. 'required' => false,
  94. ));
  95. $this->assertEquals(array('' => '') + $this->choices, $field->getOtherChoices());
  96. }
  97. public function testRequiredContainsNoEmptyField()
  98. {
  99. $field = new ChoiceField('name', array(
  100. 'multiple' => false,
  101. 'expanded' => false,
  102. 'choices' => $this->choices,
  103. 'required' => true,
  104. ));
  105. $this->assertEquals($this->choices, $field->getOtherChoices());
  106. }
  107. public function testEmptyValueConfiguresLabelOfEmptyField()
  108. {
  109. $field = new ChoiceField('name', array(
  110. 'multiple' => false,
  111. 'expanded' => false,
  112. 'choices' => $this->choices,
  113. 'required' => false,
  114. 'empty_value' => 'Foobar',
  115. ));
  116. $this->assertEquals(array('' => 'Foobar') + $this->choices, $field->getOtherChoices());
  117. }
  118. /**
  119. * @dataProvider getChoicesVariants
  120. */
  121. public function testSubmitSingleNonExpanded($choices)
  122. {
  123. $field = new ChoiceField('name', array(
  124. 'multiple' => false,
  125. 'expanded' => false,
  126. 'choices' => $choices,
  127. ));
  128. $field->submit('b');
  129. $this->assertEquals('b', $field->getData());
  130. $this->assertEquals('b', $field->getDisplayedData());
  131. }
  132. /**
  133. * @dataProvider getChoicesVariants
  134. */
  135. public function testSubmitMultipleNonExpanded($choices)
  136. {
  137. $field = new ChoiceField('name', array(
  138. 'multiple' => true,
  139. 'expanded' => false,
  140. 'choices' => $choices,
  141. ));
  142. $field->submit(array('a', 'b'));
  143. $this->assertEquals(array('a', 'b'), $field->getData());
  144. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  145. }
  146. /**
  147. * @dataProvider getChoicesVariants
  148. */
  149. public function testSubmitSingleExpanded($choices)
  150. {
  151. $field = new ChoiceField('name', array(
  152. 'multiple' => false,
  153. 'expanded' => true,
  154. 'choices' => $choices,
  155. ));
  156. $field->submit('b');
  157. $this->assertSame('b', $field->getData());
  158. $this->assertSame(false, $field['a']->getData());
  159. $this->assertSame(true, $field['b']->getData());
  160. $this->assertSame(false, $field['c']->getData());
  161. $this->assertSame(false, $field['d']->getData());
  162. $this->assertSame(false, $field['e']->getData());
  163. $this->assertSame('', $field['a']->getDisplayedData());
  164. $this->assertSame('1', $field['b']->getDisplayedData());
  165. $this->assertSame('', $field['c']->getDisplayedData());
  166. $this->assertSame('', $field['d']->getDisplayedData());
  167. $this->assertSame('', $field['e']->getDisplayedData());
  168. $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  169. }
  170. /**
  171. * @dataProvider getNumericChoicesVariants
  172. */
  173. public function testSubmitSingleExpandedNumericChoices($choices)
  174. {
  175. $field = new ChoiceField('name', array(
  176. 'multiple' => false,
  177. 'expanded' => true,
  178. 'choices' => $choices,
  179. ));
  180. $field->submit('1');
  181. $this->assertSame(1, $field->getData());
  182. $this->assertSame(false, $field[0]->getData());
  183. $this->assertSame(true, $field[1]->getData());
  184. $this->assertSame(false, $field[2]->getData());
  185. $this->assertSame(false, $field[3]->getData());
  186. $this->assertSame(false, $field[4]->getData());
  187. $this->assertSame('', $field[0]->getDisplayedData());
  188. $this->assertSame('1', $field[1]->getDisplayedData());
  189. $this->assertSame('', $field[2]->getDisplayedData());
  190. $this->assertSame('', $field[3]->getDisplayedData());
  191. $this->assertSame('', $field[4]->getDisplayedData());
  192. $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
  193. }
  194. /**
  195. * @dataProvider getChoicesVariants
  196. */
  197. public function testSubmitMultipleExpanded($choices)
  198. {
  199. $field = new ChoiceField('name', array(
  200. 'multiple' => true,
  201. 'expanded' => true,
  202. 'choices' => $choices,
  203. ));
  204. $field->submit(array('a' => 'a', 'b' => 'b'));
  205. $this->assertSame(array('a', 'b'), $field->getData());
  206. $this->assertSame(true, $field['a']->getData());
  207. $this->assertSame(true, $field['b']->getData());
  208. $this->assertSame(false, $field['c']->getData());
  209. $this->assertSame(false, $field['d']->getData());
  210. $this->assertSame(false, $field['e']->getData());
  211. $this->assertSame('1', $field['a']->getDisplayedData());
  212. $this->assertSame('1', $field['b']->getDisplayedData());
  213. $this->assertSame('', $field['c']->getDisplayedData());
  214. $this->assertSame('', $field['d']->getDisplayedData());
  215. $this->assertSame('', $field['e']->getDisplayedData());
  216. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  217. }
  218. /**
  219. * @dataProvider getNumericChoicesVariants
  220. */
  221. public function testSubmitMultipleExpandedNumericChoices($choices)
  222. {
  223. $field = new ChoiceField('name', array(
  224. 'multiple' => true,
  225. 'expanded' => true,
  226. 'choices' => $choices,
  227. ));
  228. $field->submit(array(1 => 1, 2 => 2));
  229. $this->assertSame(array(1, 2), $field->getData());
  230. $this->assertSame(false, $field[0]->getData());
  231. $this->assertSame(true, $field[1]->getData());
  232. $this->assertSame(true, $field[2]->getData());
  233. $this->assertSame(false, $field[3]->getData());
  234. $this->assertSame(false, $field[4]->getData());
  235. $this->assertSame('', $field[0]->getDisplayedData());
  236. $this->assertSame('1', $field[1]->getDisplayedData());
  237. $this->assertSame('1', $field[2]->getDisplayedData());
  238. $this->assertSame('', $field[3]->getDisplayedData());
  239. $this->assertSame('', $field[4]->getDisplayedData());
  240. $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
  241. }
  242. }