AbstractChoiceListTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 PHPUnit\Framework\TestCase;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. */
  15. abstract class AbstractChoiceListTest extends TestCase
  16. {
  17. /**
  18. * @var \Symfony\Component\Form\ChoiceList\ChoiceListInterface
  19. */
  20. protected $list;
  21. /**
  22. * @var array
  23. */
  24. protected $choices;
  25. /**
  26. * @var array
  27. */
  28. protected $values;
  29. /**
  30. * @var array
  31. */
  32. protected $structuredValues;
  33. /**
  34. * @var array
  35. */
  36. protected $keys;
  37. /**
  38. * @var mixed
  39. */
  40. protected $choice1;
  41. /**
  42. * @var mixed
  43. */
  44. protected $choice2;
  45. /**
  46. * @var mixed
  47. */
  48. protected $choice3;
  49. /**
  50. * @var mixed
  51. */
  52. protected $choice4;
  53. /**
  54. * @var string
  55. */
  56. protected $value1;
  57. /**
  58. * @var string
  59. */
  60. protected $value2;
  61. /**
  62. * @var string
  63. */
  64. protected $value3;
  65. /**
  66. * @var string
  67. */
  68. protected $value4;
  69. /**
  70. * @var string
  71. */
  72. protected $key1;
  73. /**
  74. * @var string
  75. */
  76. protected $key2;
  77. /**
  78. * @var string
  79. */
  80. protected $key3;
  81. /**
  82. * @var string
  83. */
  84. protected $key4;
  85. protected function setUp()
  86. {
  87. parent::setUp();
  88. $this->list = $this->createChoiceList();
  89. $choices = $this->getChoices();
  90. $this->values = $this->getValues();
  91. $this->structuredValues = array_combine(array_keys($choices), $this->values);
  92. $this->choices = array_combine($this->values, $choices);
  93. $this->keys = array_combine($this->values, array_keys($choices));
  94. // allow access to the individual entries without relying on their indices
  95. reset($this->choices);
  96. reset($this->values);
  97. reset($this->keys);
  98. for ($i = 1; $i <= 4; ++$i) {
  99. $this->{'choice'.$i} = current($this->choices);
  100. $this->{'value'.$i} = current($this->values);
  101. $this->{'key'.$i} = current($this->keys);
  102. next($this->choices);
  103. next($this->values);
  104. next($this->keys);
  105. }
  106. }
  107. public function testGetChoices()
  108. {
  109. $this->assertSame($this->choices, $this->list->getChoices());
  110. }
  111. public function testGetValues()
  112. {
  113. $this->assertSame($this->values, $this->list->getValues());
  114. }
  115. public function testGetStructuredValues()
  116. {
  117. $this->assertSame($this->values, $this->list->getStructuredValues());
  118. }
  119. public function testGetOriginalKeys()
  120. {
  121. $this->assertSame($this->keys, $this->list->getOriginalKeys());
  122. }
  123. public function testGetChoicesForValues()
  124. {
  125. $values = array($this->value1, $this->value2);
  126. $this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
  127. }
  128. public function testGetChoicesForValuesPreservesKeys()
  129. {
  130. $values = array(5 => $this->value1, 8 => $this->value2);
  131. $this->assertSame(array(5 => $this->choice1, 8 => $this->choice2), $this->list->getChoicesForValues($values));
  132. }
  133. public function testGetChoicesForValuesPreservesOrder()
  134. {
  135. $values = array($this->value2, $this->value1);
  136. $this->assertSame(array($this->choice2, $this->choice1), $this->list->getChoicesForValues($values));
  137. }
  138. public function testGetChoicesForValuesIgnoresNonExistingValues()
  139. {
  140. $values = array($this->value1, $this->value2, 'foobar');
  141. $this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
  142. }
  143. // https://github.com/symfony/symfony/issues/3446
  144. public function testGetChoicesForValuesEmpty()
  145. {
  146. $this->assertSame(array(), $this->list->getChoicesForValues(array()));
  147. }
  148. public function testGetValuesForChoices()
  149. {
  150. $choices = array($this->choice1, $this->choice2);
  151. $this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
  152. }
  153. public function testGetValuesForChoicesPreservesKeys()
  154. {
  155. $choices = array(5 => $this->choice1, 8 => $this->choice2);
  156. $this->assertSame(array(5 => $this->value1, 8 => $this->value2), $this->list->getValuesForChoices($choices));
  157. }
  158. public function testGetValuesForChoicesPreservesOrder()
  159. {
  160. $choices = array($this->choice2, $this->choice1);
  161. $this->assertSame(array($this->value2, $this->value1), $this->list->getValuesForChoices($choices));
  162. }
  163. public function testGetValuesForChoicesIgnoresNonExistingChoices()
  164. {
  165. $choices = array($this->choice1, $this->choice2, 'foobar');
  166. $this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
  167. }
  168. public function testGetValuesForChoicesEmpty()
  169. {
  170. $this->assertSame(array(), $this->list->getValuesForChoices(array()));
  171. }
  172. public function testGetChoicesForValuesWithNull()
  173. {
  174. $values = $this->list->getValuesForChoices(array(null));
  175. $this->assertNotEmpty($this->list->getChoicesForValues($values));
  176. }
  177. /**
  178. * @return \Symfony\Component\Form\ChoiceList\ChoiceListInterface
  179. */
  180. abstract protected function createChoiceList();
  181. abstract protected function getChoices();
  182. abstract protected function getValues();
  183. }