ArrayKeyChoiceListTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\Form\Tests\ChoiceList;
  11. use Symfony\Component\Form\ChoiceList\ArrayKeyChoiceList;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. *
  15. * @group legacy
  16. */
  17. class ArrayKeyChoiceListTest extends AbstractChoiceListTest
  18. {
  19. private $object;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->object = new \stdClass();
  24. }
  25. protected function createChoiceList()
  26. {
  27. return new ArrayKeyChoiceList(array_flip($this->getChoices()));
  28. }
  29. protected function getChoices()
  30. {
  31. return array(0, 1, 'a', 'b', '');
  32. }
  33. protected function getValues()
  34. {
  35. return array('0', '1', 'a', 'b', '');
  36. }
  37. public function testUseChoicesAsValuesByDefault()
  38. {
  39. $list = new ArrayKeyChoiceList(array('' => 'Empty', 0 => 'Zero', 1 => 'One', '1.23' => 'Float'));
  40. $this->assertSame(array('', '0', '1', '1.23'), $list->getValues());
  41. $this->assertSame(array('' => '', 0 => 0, 1 => 1, '1.23' => '1.23'), $list->getChoices());
  42. $this->assertSame(array('' => 'Empty', 0 => 'Zero', 1 => 'One', '1.23' => 'Float'), $list->getOriginalKeys());
  43. }
  44. public function testNoChoices()
  45. {
  46. $list = new ArrayKeyChoiceList(array());
  47. $this->assertSame(array(), $list->getValues());
  48. }
  49. public function testGetChoicesForValuesConvertsValuesToStrings()
  50. {
  51. $this->assertSame(array(0), $this->list->getChoicesForValues(array(0)));
  52. $this->assertSame(array(0), $this->list->getChoicesForValues(array('0')));
  53. $this->assertSame(array(1), $this->list->getChoicesForValues(array(1)));
  54. $this->assertSame(array(1), $this->list->getChoicesForValues(array('1')));
  55. $this->assertSame(array('a'), $this->list->getChoicesForValues(array('a')));
  56. $this->assertSame(array('b'), $this->list->getChoicesForValues(array('b')));
  57. $this->assertSame(array(''), $this->list->getChoicesForValues(array('')));
  58. // "1" === (string) true
  59. $this->assertSame(array(1), $this->list->getChoicesForValues(array(true)));
  60. // "" === (string) false
  61. $this->assertSame(array(''), $this->list->getChoicesForValues(array(false)));
  62. // "" === (string) null
  63. $this->assertSame(array(''), $this->list->getChoicesForValues(array(null)));
  64. $this->assertSame(array(), $this->list->getChoicesForValues(array(1.23)));
  65. }
  66. public function testGetValuesForChoicesConvertsChoicesToArrayKeys()
  67. {
  68. $this->assertSame(array('0'), $this->list->getValuesForChoices(array(0)));
  69. $this->assertSame(array('0'), $this->list->getValuesForChoices(array('0')));
  70. $this->assertSame(array('1'), $this->list->getValuesForChoices(array(1)));
  71. $this->assertSame(array('1'), $this->list->getValuesForChoices(array('1')));
  72. $this->assertSame(array('a'), $this->list->getValuesForChoices(array('a')));
  73. $this->assertSame(array('b'), $this->list->getValuesForChoices(array('b')));
  74. // Always cast booleans to 0 and 1, because:
  75. // array(true => 'Yes', false => 'No') === array(1 => 'Yes', 0 => 'No')
  76. // see ChoiceTypeTest::testSetDataSingleNonExpandedAcceptsBoolean
  77. $this->assertSame(array('0'), $this->list->getValuesForChoices(array(false)));
  78. $this->assertSame(array('1'), $this->list->getValuesForChoices(array(true)));
  79. }
  80. /**
  81. * @dataProvider provideConvertibleChoices
  82. */
  83. public function testConvertChoicesIfNecessary(array $choices, array $converted)
  84. {
  85. $list = new ArrayKeyChoiceList($choices);
  86. $this->assertSame($converted, $list->getChoices());
  87. }
  88. public function provideConvertibleChoices()
  89. {
  90. return array(
  91. array(array(0 => 'Label'), array(0 => 0)),
  92. array(array(1 => 'Label'), array(1 => 1)),
  93. array(array('1.23' => 'Label'), array('1.23' => '1.23')),
  94. array(array('foobar' => 'Label'), array('foobar' => 'foobar')),
  95. // The default value of choice fields is NULL. It should be treated
  96. // like the empty value for this choice list type
  97. array(array(null => 'Label'), array('' => '')),
  98. array(array('1.23' => 'Label'), array('1.23' => '1.23')),
  99. // Always cast booleans to 0 and 1, because:
  100. // array(true => 'Yes', false => 'No') === array(1 => 'Yes', 0 => 'No')
  101. // see ChoiceTypeTest::testSetDataSingleNonExpandedAcceptsBoolean
  102. array(array(true => 'Label'), array(1 => 1)),
  103. array(array(false => 'Label'), array(0 => 0)),
  104. );
  105. }
  106. /**
  107. * @dataProvider provideInvalidChoices
  108. * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
  109. */
  110. public function testGetValuesForChoicesFailsIfInvalidChoices(array $choices)
  111. {
  112. $this->list->getValuesForChoices($choices);
  113. }
  114. public function provideInvalidChoices()
  115. {
  116. return array(
  117. array(array(new \stdClass())),
  118. array(array(array(1, 2))),
  119. );
  120. }
  121. /**
  122. * @dataProvider provideConvertibleValues
  123. */
  124. public function testConvertValuesToStrings($value, $converted)
  125. {
  126. $callback = function () use ($value) {
  127. return $value;
  128. };
  129. $list = new ArrayKeyChoiceList(array('choice' => 'Label'), $callback);
  130. $this->assertSame(array($converted), $list->getValues());
  131. }
  132. public function provideConvertibleValues()
  133. {
  134. return array(
  135. array(0, '0'),
  136. array(1, '1'),
  137. array('0', '0'),
  138. array('1', '1'),
  139. array('1.23', '1.23'),
  140. array('foobar', 'foobar'),
  141. array('', ''),
  142. );
  143. }
  144. public function testCreateChoiceListWithValueCallback()
  145. {
  146. $callback = function ($choice) {
  147. return ':'.$choice;
  148. };
  149. $choiceList = new ArrayKeyChoiceList(array('foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz'), $callback);
  150. $this->assertSame(array(':foo', ':bar', ':baz'), $choiceList->getValues());
  151. $this->assertSame(array(':foo' => 'foo', ':bar' => 'bar', ':baz' => 'baz'), $choiceList->getChoices());
  152. $this->assertSame(array(':foo' => 'Foo', ':bar' => 'Bar', ':baz' => 'Baz'), $choiceList->getOriginalKeys());
  153. $this->assertSame(array(1 => 'foo', 2 => 'baz'), $choiceList->getChoicesForValues(array(1 => ':foo', 2 => ':baz')));
  154. $this->assertSame(array(1 => ':foo', 2 => ':baz'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 'baz')));
  155. }
  156. }