ChoiceFieldTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * @dataProvider getChoicesVariants
  78. */
  79. public function testBindSingleNonExpanded($choices)
  80. {
  81. $field = new ChoiceField('name', array(
  82. 'multiple' => false,
  83. 'expanded' => false,
  84. 'choices' => $choices,
  85. ));
  86. $field->bind('b');
  87. $this->assertEquals('b', $field->getData());
  88. $this->assertEquals('b', $field->getDisplayedData());
  89. }
  90. /**
  91. * @dataProvider getChoicesVariants
  92. */
  93. public function testBindMultipleNonExpanded($choices)
  94. {
  95. $field = new ChoiceField('name', array(
  96. 'multiple' => true,
  97. 'expanded' => false,
  98. 'choices' => $choices,
  99. ));
  100. $field->bind(array('a', 'b'));
  101. $this->assertEquals(array('a', 'b'), $field->getData());
  102. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  103. }
  104. /**
  105. * @dataProvider getChoicesVariants
  106. */
  107. public function testBindSingleExpanded($choices)
  108. {
  109. $field = new ChoiceField('name', array(
  110. 'multiple' => false,
  111. 'expanded' => true,
  112. 'choices' => $choices,
  113. ));
  114. $field->bind('b');
  115. $this->assertSame('b', $field->getData());
  116. $this->assertSame(false, $field['a']->getData());
  117. $this->assertSame(true, $field['b']->getData());
  118. $this->assertSame(false, $field['c']->getData());
  119. $this->assertSame(false, $field['d']->getData());
  120. $this->assertSame(false, $field['e']->getData());
  121. $this->assertSame('', $field['a']->getDisplayedData());
  122. $this->assertSame('1', $field['b']->getDisplayedData());
  123. $this->assertSame('', $field['c']->getDisplayedData());
  124. $this->assertSame('', $field['d']->getDisplayedData());
  125. $this->assertSame('', $field['e']->getDisplayedData());
  126. $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  127. }
  128. /**
  129. * @dataProvider getNumericChoicesVariants
  130. */
  131. public function testBindSingleExpandedNumericChoices($choices)
  132. {
  133. $field = new ChoiceField('name', array(
  134. 'multiple' => false,
  135. 'expanded' => true,
  136. 'choices' => $choices,
  137. ));
  138. $field->bind('1');
  139. $this->assertSame(1, $field->getData());
  140. $this->assertSame(false, $field[0]->getData());
  141. $this->assertSame(true, $field[1]->getData());
  142. $this->assertSame(false, $field[2]->getData());
  143. $this->assertSame(false, $field[3]->getData());
  144. $this->assertSame(false, $field[4]->getData());
  145. $this->assertSame('', $field[0]->getDisplayedData());
  146. $this->assertSame('1', $field[1]->getDisplayedData());
  147. $this->assertSame('', $field[2]->getDisplayedData());
  148. $this->assertSame('', $field[3]->getDisplayedData());
  149. $this->assertSame('', $field[4]->getDisplayedData());
  150. $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
  151. }
  152. /**
  153. * @dataProvider getChoicesVariants
  154. */
  155. public function testBindMultipleExpanded($choices)
  156. {
  157. $field = new ChoiceField('name', array(
  158. 'multiple' => true,
  159. 'expanded' => true,
  160. 'choices' => $choices,
  161. ));
  162. $field->bind(array('a' => 'a', 'b' => 'b'));
  163. $this->assertSame(array('a', 'b'), $field->getData());
  164. $this->assertSame(true, $field['a']->getData());
  165. $this->assertSame(true, $field['b']->getData());
  166. $this->assertSame(false, $field['c']->getData());
  167. $this->assertSame(false, $field['d']->getData());
  168. $this->assertSame(false, $field['e']->getData());
  169. $this->assertSame('1', $field['a']->getDisplayedData());
  170. $this->assertSame('1', $field['b']->getDisplayedData());
  171. $this->assertSame('', $field['c']->getDisplayedData());
  172. $this->assertSame('', $field['d']->getDisplayedData());
  173. $this->assertSame('', $field['e']->getDisplayedData());
  174. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  175. }
  176. /**
  177. * @dataProvider getNumericChoicesVariants
  178. */
  179. public function testBindMultipleExpandedNumericChoices($choices)
  180. {
  181. $field = new ChoiceField('name', array(
  182. 'multiple' => true,
  183. 'expanded' => true,
  184. 'choices' => $choices,
  185. ));
  186. $field->bind(array(1 => 1, 2 => 2));
  187. $this->assertSame(array(1, 2), $field->getData());
  188. $this->assertSame(false, $field[0]->getData());
  189. $this->assertSame(true, $field[1]->getData());
  190. $this->assertSame(true, $field[2]->getData());
  191. $this->assertSame(false, $field[3]->getData());
  192. $this->assertSame(false, $field[4]->getData());
  193. $this->assertSame('', $field[0]->getDisplayedData());
  194. $this->assertSame('1', $field[1]->getDisplayedData());
  195. $this->assertSame('1', $field[2]->getDisplayedData());
  196. $this->assertSame('', $field[3]->getDisplayedData());
  197. $this->assertSame('', $field[4]->getDisplayedData());
  198. $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
  199. }
  200. }