ArrayChoiceListTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\ArrayChoiceList;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. */
  15. class ArrayChoiceListTest extends AbstractChoiceListTest
  16. {
  17. private $object;
  18. protected function setUp()
  19. {
  20. $this->object = new \stdClass();
  21. parent::setUp();
  22. }
  23. protected function createChoiceList()
  24. {
  25. return new ArrayChoiceList($this->getChoices());
  26. }
  27. protected function getChoices()
  28. {
  29. return array(0, 1, 1.5, '1', 'a', false, true, $this->object, null);
  30. }
  31. protected function getValues()
  32. {
  33. return array('0', '1', '2', '3', '4', '5', '6', '7', '8');
  34. }
  35. /**
  36. * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
  37. */
  38. public function testFailIfKeyMismatch()
  39. {
  40. new ArrayChoiceList(array(0 => 'a', 1 => 'b'), array(1 => 'a', 2 => 'b'));
  41. }
  42. public function testCreateChoiceListWithValueCallback()
  43. {
  44. $callback = function ($choice) {
  45. return ':'.$choice;
  46. };
  47. $choiceList = new ArrayChoiceList(array(2 => 'foo', 7 => 'bar', 10 => 'baz'), $callback);
  48. $this->assertSame(array(':foo', ':bar', ':baz'), $choiceList->getValues());
  49. $this->assertSame(array(':foo' => 'foo', ':bar' => 'bar', ':baz' => 'baz'), $choiceList->getChoices());
  50. $this->assertSame(array(':foo' => 2, ':bar' => 7, ':baz' => 10), $choiceList->getOriginalKeys());
  51. $this->assertSame(array(1 => 'foo', 2 => 'baz'), $choiceList->getChoicesForValues(array(1 => ':foo', 2 => ':baz')));
  52. $this->assertSame(array(1 => ':foo', 2 => ':baz'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 'baz')));
  53. }
  54. public function testCreateChoiceListWithoutValueCallbackAndDuplicateFreeToStringChoices()
  55. {
  56. $choiceList = new ArrayChoiceList(array(2 => 'foo', 7 => 'bar', 10 => 123));
  57. $this->assertSame(array('foo', 'bar', '123'), $choiceList->getValues());
  58. $this->assertSame(array('foo' => 'foo', 'bar' => 'bar', '123' => 123), $choiceList->getChoices());
  59. $this->assertSame(array('foo' => 2, 'bar' => 7, '123' => 10), $choiceList->getOriginalKeys());
  60. $this->assertSame(array(1 => 'foo', 2 => 123), $choiceList->getChoicesForValues(array(1 => 'foo', 2 => '123')));
  61. $this->assertSame(array(1 => 'foo', 2 => '123'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 123)));
  62. }
  63. public function testCreateChoiceListWithoutValueCallbackAndToStringDuplicates()
  64. {
  65. $choiceList = new ArrayChoiceList(array(2 => 'foo', 7 => '123', 10 => 123));
  66. $this->assertSame(array('0', '1', '2'), $choiceList->getValues());
  67. $this->assertSame(array('0' => 'foo', '1' => '123', '2' => 123), $choiceList->getChoices());
  68. $this->assertSame(array('0' => 2, '1' => 7, '2' => 10), $choiceList->getOriginalKeys());
  69. $this->assertSame(array(1 => 'foo', 2 => 123), $choiceList->getChoicesForValues(array(1 => '0', 2 => '2')));
  70. $this->assertSame(array(1 => '0', 2 => '2'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 123)));
  71. }
  72. public function testCreateChoiceListWithoutValueCallbackAndMixedChoices()
  73. {
  74. $object = new \stdClass();
  75. $choiceList = new ArrayChoiceList(array(2 => 'foo', 5 => array(7 => '123'), 10 => $object));
  76. $this->assertSame(array('0', '1', '2'), $choiceList->getValues());
  77. $this->assertSame(array('0' => 'foo', '1' => '123', '2' => $object), $choiceList->getChoices());
  78. $this->assertSame(array('0' => 2, '1' => 7, '2' => 10), $choiceList->getOriginalKeys());
  79. $this->assertSame(array(1 => 'foo', 2 => $object), $choiceList->getChoicesForValues(array(1 => '0', 2 => '2')));
  80. $this->assertSame(array(1 => '0', 2 => '2'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => $object)));
  81. }
  82. public function testCreateChoiceListWithGroupedChoices()
  83. {
  84. $choiceList = new ArrayChoiceList(array(
  85. 'Group 1' => array('A' => 'a', 'B' => 'b'),
  86. 'Group 2' => array('C' => 'c', 'D' => 'd'),
  87. ));
  88. $this->assertSame(array('a', 'b', 'c', 'd'), $choiceList->getValues());
  89. $this->assertSame(array(
  90. 'Group 1' => array('A' => 'a', 'B' => 'b'),
  91. 'Group 2' => array('C' => 'c', 'D' => 'd'),
  92. ), $choiceList->getStructuredValues());
  93. $this->assertSame(array('a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd'), $choiceList->getChoices());
  94. $this->assertSame(array('a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D'), $choiceList->getOriginalKeys());
  95. $this->assertSame(array(1 => 'a', 2 => 'b'), $choiceList->getChoicesForValues(array(1 => 'a', 2 => 'b')));
  96. $this->assertSame(array(1 => 'a', 2 => 'b'), $choiceList->getValuesForChoices(array(1 => 'a', 2 => 'b')));
  97. }
  98. public function testCompareChoicesByIdentityByDefault()
  99. {
  100. $callback = function ($choice) {
  101. return $choice->value;
  102. };
  103. $obj1 = (object) array('value' => 'value1');
  104. $obj2 = (object) array('value' => 'value2');
  105. $choiceList = new ArrayChoiceList(array($obj1, $obj2), $callback);
  106. $this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => $obj2)));
  107. $this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => (object) array('value' => 'value2'))));
  108. }
  109. public function testGetChoicesForValuesWithContainingNull()
  110. {
  111. $choiceList = new ArrayChoiceList(array('Null' => null));
  112. $this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('0')));
  113. }
  114. public function testGetChoicesForValuesWithContainingFalseAndNull()
  115. {
  116. $choiceList = new ArrayChoiceList(array('False' => false, 'Null' => null));
  117. $this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('1')));
  118. $this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0')));
  119. }
  120. public function testGetChoicesForValuesWithContainingEmptyStringAndNull()
  121. {
  122. $choiceList = new ArrayChoiceList(array('Empty String' => '', 'Null' => null));
  123. $this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('0')));
  124. $this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('1')));
  125. }
  126. public function testGetChoicesForValuesWithContainingEmptyStringAndBooleans()
  127. {
  128. $choiceList = new ArrayChoiceList(array('Empty String' => '', 'True' => true, 'False' => false));
  129. $this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('')));
  130. $this->assertSame(array(0 => true), $choiceList->getChoicesForValues(array('1')));
  131. $this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0')));
  132. }
  133. public function testGetChoicesForValuesWithContainingEmptyStringAndFloats()
  134. {
  135. $choiceList = new ArrayChoiceList(array('Empty String' => '', '1/3' => 0.3, '1/2' => 0.5));
  136. $this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('')));
  137. $this->assertSame(array(0 => 0.3), $choiceList->getChoicesForValues(array('0.3')));
  138. $this->assertSame(array(0 => 0.5), $choiceList->getChoicesForValues(array('0.5')));
  139. }
  140. }