ChoiceFieldTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. use Symfony\Component\Form\ChoiceField;
  4. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  5. class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $choices = array(
  8. 'a' => 'Bernhard',
  9. 'b' => 'Fabien',
  10. 'c' => 'Kris',
  11. 'd' => 'Jon',
  12. 'e' => 'Roman',
  13. );
  14. protected $preferredChoices = array('d', 'e');
  15. protected $groupedChoices = array(
  16. 'Symfony' => array(
  17. 'a' => 'Bernhard',
  18. 'b' => 'Fabien',
  19. 'c' => 'Kris',
  20. ),
  21. 'Doctrine' => array(
  22. 'd' => 'Jon',
  23. 'e' => 'Roman',
  24. )
  25. );
  26. protected $numericChoices = array(
  27. 0 => 'Bernhard',
  28. 1 => 'Fabien',
  29. 2 => 'Kris',
  30. 3 => 'Jon',
  31. 4 => 'Roman',
  32. );
  33. /**
  34. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  35. */
  36. public function testConfigureChoicesWithNonArray()
  37. {
  38. $field = new ChoiceField('name', array(
  39. 'choices' => new \ArrayObject(),
  40. ));
  41. }
  42. /**
  43. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  44. */
  45. public function testConfigurePreferredChoicesWithNonArray()
  46. {
  47. $field = new ChoiceField('name', array(
  48. 'choices' => $this->choices,
  49. 'preferred_choices' => new \ArrayObject(),
  50. ));
  51. }
  52. public function testBindSingleNonExpanded()
  53. {
  54. $field = new ChoiceField('name', array(
  55. 'multiple' => false,
  56. 'expanded' => false,
  57. 'choices' => $this->choices,
  58. ));
  59. $field->bind('b');
  60. $this->assertEquals('b', $field->getData());
  61. $this->assertEquals('b', $field->getDisplayedData());
  62. }
  63. public function testBindMultipleNonExpanded()
  64. {
  65. $field = new ChoiceField('name', array(
  66. 'multiple' => true,
  67. 'expanded' => false,
  68. 'choices' => $this->choices,
  69. ));
  70. $field->bind(array('a', 'b'));
  71. $this->assertEquals(array('a', 'b'), $field->getData());
  72. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  73. }
  74. public function testBindSingleExpanded()
  75. {
  76. $field = new ChoiceField('name', array(
  77. 'multiple' => false,
  78. 'expanded' => true,
  79. 'choices' => $this->choices,
  80. ));
  81. $field->bind('b');
  82. $this->assertSame('b', $field->getData());
  83. $this->assertSame(null, $field['a']->getData());
  84. $this->assertSame(true, $field['b']->getData());
  85. $this->assertSame(null, $field['c']->getData());
  86. $this->assertSame(null, $field['d']->getData());
  87. $this->assertSame(null, $field['e']->getData());
  88. $this->assertSame('', $field['a']->getDisplayedData());
  89. $this->assertSame('1', $field['b']->getDisplayedData());
  90. $this->assertSame('', $field['c']->getDisplayedData());
  91. $this->assertSame('', $field['d']->getDisplayedData());
  92. $this->assertSame('', $field['e']->getDisplayedData());
  93. $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  94. }
  95. public function testBindSingleExpandedNumericChoices()
  96. {
  97. $field = new ChoiceField('name', array(
  98. 'multiple' => false,
  99. 'expanded' => true,
  100. 'choices' => $this->numericChoices,
  101. ));
  102. $field->bind('1');
  103. $this->assertSame(1, $field->getData());
  104. $this->assertSame(null, $field[0]->getData());
  105. $this->assertSame(true, $field[1]->getData());
  106. $this->assertSame(null, $field[2]->getData());
  107. $this->assertSame(null, $field[3]->getData());
  108. $this->assertSame(null, $field[4]->getData());
  109. $this->assertSame('', $field[0]->getDisplayedData());
  110. $this->assertSame('1', $field[1]->getDisplayedData());
  111. $this->assertSame('', $field[2]->getDisplayedData());
  112. $this->assertSame('', $field[3]->getDisplayedData());
  113. $this->assertSame('', $field[4]->getDisplayedData());
  114. $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
  115. }
  116. public function testBindMultipleExpanded()
  117. {
  118. $field = new ChoiceField('name', array(
  119. 'multiple' => true,
  120. 'expanded' => true,
  121. 'choices' => $this->choices,
  122. ));
  123. $field->bind(array('a' => 'a', 'b' => 'b'));
  124. $this->assertSame(array('a', 'b'), $field->getData());
  125. $this->assertSame(true, $field['a']->getData());
  126. $this->assertSame(true, $field['b']->getData());
  127. $this->assertSame(null, $field['c']->getData());
  128. $this->assertSame(null, $field['d']->getData());
  129. $this->assertSame(null, $field['e']->getData());
  130. $this->assertSame('1', $field['a']->getDisplayedData());
  131. $this->assertSame('1', $field['b']->getDisplayedData());
  132. $this->assertSame('', $field['c']->getDisplayedData());
  133. $this->assertSame('', $field['d']->getDisplayedData());
  134. $this->assertSame('', $field['e']->getDisplayedData());
  135. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  136. }
  137. public function testBindMultipleExpandedNumericChoices()
  138. {
  139. $field = new ChoiceField('name', array(
  140. 'multiple' => true,
  141. 'expanded' => true,
  142. 'choices' => $this->numericChoices,
  143. ));
  144. $field->bind(array(1 => 1, 2 => 2));
  145. $this->assertSame(array(1, 2), $field->getData());
  146. $this->assertSame(null, $field[0]->getData());
  147. $this->assertSame(true, $field[1]->getData());
  148. $this->assertSame(true, $field[2]->getData());
  149. $this->assertSame(null, $field[3]->getData());
  150. $this->assertSame(null, $field[4]->getData());
  151. $this->assertSame('', $field[0]->getDisplayedData());
  152. $this->assertSame('1', $field[1]->getDisplayedData());
  153. $this->assertSame('1', $field[2]->getDisplayedData());
  154. $this->assertSame('', $field[3]->getDisplayedData());
  155. $this->assertSame('', $field[4]->getDisplayedData());
  156. $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
  157. }
  158. }