ChoiceFieldTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 testBindSingleNonExpanded()
  61. {
  62. $field = new ChoiceField('name', array(
  63. 'multiple' => false,
  64. 'expanded' => false,
  65. 'choices' => $this->choices,
  66. ));
  67. $field->bind('b');
  68. $this->assertEquals('b', $field->getData());
  69. $this->assertEquals('b', $field->getDisplayedData());
  70. }
  71. public function testBindMultipleNonExpanded()
  72. {
  73. $field = new ChoiceField('name', array(
  74. 'multiple' => true,
  75. 'expanded' => false,
  76. 'choices' => $this->choices,
  77. ));
  78. $field->bind(array('a', 'b'));
  79. $this->assertEquals(array('a', 'b'), $field->getData());
  80. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  81. }
  82. public function testBindSingleExpanded()
  83. {
  84. $field = new ChoiceField('name', array(
  85. 'multiple' => false,
  86. 'expanded' => true,
  87. 'choices' => $this->choices,
  88. ));
  89. $field->bind('b');
  90. $this->assertSame('b', $field->getData());
  91. $this->assertSame(false, $field['a']->getData());
  92. $this->assertSame(true, $field['b']->getData());
  93. $this->assertSame(false, $field['c']->getData());
  94. $this->assertSame(false, $field['d']->getData());
  95. $this->assertSame(false, $field['e']->getData());
  96. $this->assertSame('', $field['a']->getDisplayedData());
  97. $this->assertSame('1', $field['b']->getDisplayedData());
  98. $this->assertSame('', $field['c']->getDisplayedData());
  99. $this->assertSame('', $field['d']->getDisplayedData());
  100. $this->assertSame('', $field['e']->getDisplayedData());
  101. $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  102. }
  103. public function testBindSingleExpandedNumericChoices()
  104. {
  105. $field = new ChoiceField('name', array(
  106. 'multiple' => false,
  107. 'expanded' => true,
  108. 'choices' => $this->numericChoices,
  109. ));
  110. $field->bind('1');
  111. $this->assertSame(1, $field->getData());
  112. $this->assertSame(false, $field[0]->getData());
  113. $this->assertSame(true, $field[1]->getData());
  114. $this->assertSame(false, $field[2]->getData());
  115. $this->assertSame(false, $field[3]->getData());
  116. $this->assertSame(false, $field[4]->getData());
  117. $this->assertSame('', $field[0]->getDisplayedData());
  118. $this->assertSame('1', $field[1]->getDisplayedData());
  119. $this->assertSame('', $field[2]->getDisplayedData());
  120. $this->assertSame('', $field[3]->getDisplayedData());
  121. $this->assertSame('', $field[4]->getDisplayedData());
  122. $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
  123. }
  124. public function testBindMultipleExpanded()
  125. {
  126. $field = new ChoiceField('name', array(
  127. 'multiple' => true,
  128. 'expanded' => true,
  129. 'choices' => $this->choices,
  130. ));
  131. $field->bind(array('a' => 'a', 'b' => 'b'));
  132. $this->assertSame(array('a', 'b'), $field->getData());
  133. $this->assertSame(true, $field['a']->getData());
  134. $this->assertSame(true, $field['b']->getData());
  135. $this->assertSame(false, $field['c']->getData());
  136. $this->assertSame(false, $field['d']->getData());
  137. $this->assertSame(false, $field['e']->getData());
  138. $this->assertSame('1', $field['a']->getDisplayedData());
  139. $this->assertSame('1', $field['b']->getDisplayedData());
  140. $this->assertSame('', $field['c']->getDisplayedData());
  141. $this->assertSame('', $field['d']->getDisplayedData());
  142. $this->assertSame('', $field['e']->getDisplayedData());
  143. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  144. }
  145. public function testBindMultipleExpandedNumericChoices()
  146. {
  147. $field = new ChoiceField('name', array(
  148. 'multiple' => true,
  149. 'expanded' => true,
  150. 'choices' => $this->numericChoices,
  151. ));
  152. $field->bind(array(1 => 1, 2 => 2));
  153. $this->assertSame(array(1, 2), $field->getData());
  154. $this->assertSame(false, $field[0]->getData());
  155. $this->assertSame(true, $field[1]->getData());
  156. $this->assertSame(true, $field[2]->getData());
  157. $this->assertSame(false, $field[3]->getData());
  158. $this->assertSame(false, $field[4]->getData());
  159. $this->assertSame('', $field[0]->getDisplayedData());
  160. $this->assertSame('1', $field[1]->getDisplayedData());
  161. $this->assertSame('1', $field[2]->getDisplayedData());
  162. $this->assertSame('', $field[3]->getDisplayedData());
  163. $this->assertSame('', $field[4]->getDisplayedData());
  164. $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
  165. }
  166. }