ChoiceFieldTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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->getChoiceList()->isChoiceSelected(0, $field->getDisplayedData()));
  51. $this->assertTrue($field->getChoiceList()->isChoiceSelected('0', $field->getDisplayedData()));
  52. $this->assertFalse($field->getChoiceList()->isChoiceSelected('', $field->getDisplayedData()));
  53. $field->submit('0');
  54. $this->assertTrue($field->getChoiceList()->isChoiceSelected(0, $field->getDisplayedData()));
  55. $this->assertTrue($field->getChoiceList()->isChoiceSelected('0', $field->getDisplayedData()));
  56. $this->assertFalse($field->getChoiceList()->isChoiceSelected('', $field->getDisplayedData()));
  57. $field->submit('');
  58. $this->assertFalse($field->getChoiceList()->isChoiceSelected(0, $field->getDisplayedData()));
  59. $this->assertFalse($field->getChoiceList()->isChoiceSelected('0', $field->getDisplayedData()));
  60. $this->assertTrue($field->getChoiceList()->isChoiceSelected('', $field->getDisplayedData()));
  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->getChoiceList()->isChoiceSelected(0, $field->getDisplayedData()));
  72. $this->assertTrue($field->getChoiceList()->isChoiceSelected('0', $field->getDisplayedData()));
  73. $this->assertFalse($field->getChoiceList()->isChoiceSelected('', $field->getDisplayedData()));
  74. $field->submit('0');
  75. $this->assertTrue($field->getChoiceList()->isChoiceSelected(0, $field->getDisplayedData()));
  76. $this->assertTrue($field->getChoiceList()->isChoiceSelected('0', $field->getDisplayedData()));
  77. $this->assertFalse($field->getChoiceList()->isChoiceSelected('', $field->getDisplayedData()));
  78. $field->submit('');
  79. $this->assertFalse($field->getChoiceList()->isChoiceSelected(0, $field->getDisplayedData()));
  80. $this->assertFalse($field->getChoiceList()->isChoiceSelected('0', $field->getDisplayedData()));
  81. $this->assertTrue($field->getChoiceList()->isChoiceSelected('', $field->getDisplayedData()));
  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->getChoiceList()->getOtherChoices();
  118. }
  119. /**
  120. * @dataProvider getChoicesVariants
  121. */
  122. public function testSubmitSingleNonExpanded($choices)
  123. {
  124. $field = new ChoiceField('name', array(
  125. 'multiple' => false,
  126. 'expanded' => false,
  127. 'choices' => $choices,
  128. ));
  129. $field->submit('b');
  130. $this->assertEquals('b', $field->getData());
  131. $this->assertEquals('b', $field->getDisplayedData());
  132. }
  133. /**
  134. * @dataProvider getChoicesVariants
  135. */
  136. public function testSubmitMultipleNonExpanded($choices)
  137. {
  138. $field = new ChoiceField('name', array(
  139. 'multiple' => true,
  140. 'expanded' => false,
  141. 'choices' => $choices,
  142. ));
  143. $field->submit(array('a', 'b'));
  144. $this->assertEquals(array('a', 'b'), $field->getData());
  145. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  146. }
  147. /**
  148. * @dataProvider getChoicesVariants
  149. */
  150. public function testSubmitSingleExpanded($choices)
  151. {
  152. $field = new ChoiceField('name', array(
  153. 'multiple' => false,
  154. 'expanded' => true,
  155. 'choices' => $choices,
  156. ));
  157. $field->submit('b');
  158. $this->assertSame('b', $field->getData());
  159. $this->assertSame(false, $field['a']->getData());
  160. $this->assertSame(true, $field['b']->getData());
  161. $this->assertSame(false, $field['c']->getData());
  162. $this->assertSame(false, $field['d']->getData());
  163. $this->assertSame(false, $field['e']->getData());
  164. $this->assertSame('', $field['a']->getDisplayedData());
  165. $this->assertSame('1', $field['b']->getDisplayedData());
  166. $this->assertSame('', $field['c']->getDisplayedData());
  167. $this->assertSame('', $field['d']->getDisplayedData());
  168. $this->assertSame('', $field['e']->getDisplayedData());
  169. $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  170. }
  171. /**
  172. * @dataProvider getNumericChoicesVariants
  173. */
  174. public function testSubmitSingleExpandedNumericChoices($choices)
  175. {
  176. $field = new ChoiceField('name', array(
  177. 'multiple' => false,
  178. 'expanded' => true,
  179. 'choices' => $choices,
  180. ));
  181. $field->submit('1');
  182. $this->assertSame(1, $field->getData());
  183. $this->assertSame(false, $field[0]->getData());
  184. $this->assertSame(true, $field[1]->getData());
  185. $this->assertSame(false, $field[2]->getData());
  186. $this->assertSame(false, $field[3]->getData());
  187. $this->assertSame(false, $field[4]->getData());
  188. $this->assertSame('', $field[0]->getDisplayedData());
  189. $this->assertSame('1', $field[1]->getDisplayedData());
  190. $this->assertSame('', $field[2]->getDisplayedData());
  191. $this->assertSame('', $field[3]->getDisplayedData());
  192. $this->assertSame('', $field[4]->getDisplayedData());
  193. $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
  194. }
  195. /**
  196. * @dataProvider getChoicesVariants
  197. */
  198. public function testSubmitMultipleExpanded($choices)
  199. {
  200. $field = new ChoiceField('name', array(
  201. 'multiple' => true,
  202. 'expanded' => true,
  203. 'choices' => $choices,
  204. ));
  205. $field->submit(array('a' => 'a', 'b' => 'b'));
  206. $this->assertSame(array('a', 'b'), $field->getData());
  207. $this->assertSame(true, $field['a']->getData());
  208. $this->assertSame(true, $field['b']->getData());
  209. $this->assertSame(false, $field['c']->getData());
  210. $this->assertSame(false, $field['d']->getData());
  211. $this->assertSame(false, $field['e']->getData());
  212. $this->assertSame('1', $field['a']->getDisplayedData());
  213. $this->assertSame('1', $field['b']->getDisplayedData());
  214. $this->assertSame('', $field['c']->getDisplayedData());
  215. $this->assertSame('', $field['d']->getDisplayedData());
  216. $this->assertSame('', $field['e']->getDisplayedData());
  217. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  218. }
  219. /**
  220. * @dataProvider getNumericChoicesVariants
  221. */
  222. public function testSubmitMultipleExpandedNumericChoices($choices)
  223. {
  224. $field = new ChoiceField('name', array(
  225. 'multiple' => true,
  226. 'expanded' => true,
  227. 'choices' => $choices,
  228. ));
  229. $field->submit(array(1 => 1, 2 => 2));
  230. $this->assertSame(array(1, 2), $field->getData());
  231. $this->assertSame(false, $field[0]->getData());
  232. $this->assertSame(true, $field[1]->getData());
  233. $this->assertSame(true, $field[2]->getData());
  234. $this->assertSame(false, $field[3]->getData());
  235. $this->assertSame(false, $field[4]->getData());
  236. $this->assertSame('', $field[0]->getDisplayedData());
  237. $this->assertSame('1', $field[1]->getDisplayedData());
  238. $this->assertSame('1', $field[2]->getDisplayedData());
  239. $this->assertSame('', $field[3]->getDisplayedData());
  240. $this->assertSame('', $field[4]->getDisplayedData());
  241. $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
  242. }
  243. }