/vendor/symfony/symfony/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/AbstractChoiceListTest.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS · PHP · 345 lines · 168 code · 66 blank · 111 comment · 1 complexity · 04a1aeb327fe0a95fe9b50aad4b0787a MD5 · raw file

  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\Extension\Core\ChoiceList;
  11. /**
  12. * @author Bernhard Schussek <bschussek@gmail.com>
  13. */
  14. abstract class AbstractChoiceListTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
  18. */
  19. protected $list;
  20. /**
  21. * @var array
  22. */
  23. protected $choices;
  24. /**
  25. * @var array
  26. */
  27. protected $values;
  28. /**
  29. * @var array
  30. */
  31. protected $indices;
  32. /**
  33. * @var array
  34. */
  35. protected $labels;
  36. /**
  37. * @var mixed
  38. */
  39. protected $choice1;
  40. /**
  41. * @var mixed
  42. */
  43. protected $choice2;
  44. /**
  45. * @var mixed
  46. */
  47. protected $choice3;
  48. /**
  49. * @var mixed
  50. */
  51. protected $choice4;
  52. /**
  53. * @var string
  54. */
  55. protected $value1;
  56. /**
  57. * @var string
  58. */
  59. protected $value2;
  60. /**
  61. * @var string
  62. */
  63. protected $value3;
  64. /**
  65. * @var string
  66. */
  67. protected $value4;
  68. /**
  69. * @var int|string
  70. */
  71. protected $index1;
  72. /**
  73. * @var int|string
  74. */
  75. protected $index2;
  76. /**
  77. * @var int|string
  78. */
  79. protected $index3;
  80. /**
  81. * @var int|string
  82. */
  83. protected $index4;
  84. /**
  85. * @var string
  86. */
  87. protected $label1;
  88. /**
  89. * @var string
  90. */
  91. protected $label2;
  92. /**
  93. * @var string
  94. */
  95. protected $label3;
  96. /**
  97. * @var string
  98. */
  99. protected $label4;
  100. protected function setUp()
  101. {
  102. parent::setUp();
  103. $this->list = $this->createChoiceList();
  104. $this->choices = $this->getChoices();
  105. $this->indices = $this->getIndices();
  106. $this->values = $this->getValues();
  107. $this->labels = $this->getLabels();
  108. // allow access to the individual entries without relying on their indices
  109. reset($this->choices);
  110. reset($this->indices);
  111. reset($this->values);
  112. reset($this->labels);
  113. for ($i = 1; $i <= 4; ++$i) {
  114. $this->{'choice'.$i} = current($this->choices);
  115. $this->{'index'.$i} = current($this->indices);
  116. $this->{'value'.$i} = current($this->values);
  117. $this->{'label'.$i} = current($this->labels);
  118. next($this->choices);
  119. next($this->indices);
  120. next($this->values);
  121. next($this->labels);
  122. }
  123. }
  124. public function testGetChoices()
  125. {
  126. $this->assertSame($this->choices, $this->list->getChoices());
  127. }
  128. public function testGetValues()
  129. {
  130. $this->assertSame($this->values, $this->list->getValues());
  131. }
  132. /**
  133. * @group legacy
  134. */
  135. public function testLegacyGetIndicesForChoices()
  136. {
  137. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  138. $choices = array($this->choice1, $this->choice2);
  139. $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
  140. }
  141. /**
  142. * @group legacy
  143. */
  144. public function testLegacyGetIndicesForChoicesPreservesKeys()
  145. {
  146. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  147. $choices = array(5 => $this->choice1, 8 => $this->choice2);
  148. $this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForChoices($choices));
  149. }
  150. /**
  151. * @group legacy
  152. */
  153. public function testLegacyGetIndicesForChoicesPreservesOrder()
  154. {
  155. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  156. $choices = array($this->choice2, $this->choice1);
  157. $this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForChoices($choices));
  158. }
  159. /**
  160. * @group legacy
  161. */
  162. public function testLegacyGetIndicesForChoicesIgnoresNonExistingChoices()
  163. {
  164. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  165. $choices = array($this->choice1, $this->choice2, 'foobar');
  166. $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
  167. }
  168. /**
  169. * @group legacy
  170. */
  171. public function testLegacyGetIndicesForChoicesEmpty()
  172. {
  173. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  174. $this->assertSame(array(), $this->list->getIndicesForChoices(array()));
  175. }
  176. /**
  177. * @group legacy
  178. */
  179. public function testLegacyGetIndicesForValues()
  180. {
  181. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  182. // values and indices are always the same
  183. $values = array($this->value1, $this->value2);
  184. $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
  185. }
  186. /**
  187. * @group legacy
  188. */
  189. public function testLegacyGetIndicesForValuesPreservesKeys()
  190. {
  191. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  192. // values and indices are always the same
  193. $values = array(5 => $this->value1, 8 => $this->value2);
  194. $this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForValues($values));
  195. }
  196. /**
  197. * @group legacy
  198. */
  199. public function testLegacyGetIndicesForValuesPreservesOrder()
  200. {
  201. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  202. $values = array($this->value2, $this->value1);
  203. $this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForValues($values));
  204. }
  205. /**
  206. * @group legacy
  207. */
  208. public function testLegacyGetIndicesForValuesIgnoresNonExistingValues()
  209. {
  210. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  211. $values = array($this->value1, $this->value2, 'foobar');
  212. $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
  213. }
  214. /**
  215. * @group legacy
  216. */
  217. public function testLegacyGetIndicesForValuesEmpty()
  218. {
  219. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  220. $this->assertSame(array(), $this->list->getIndicesForValues(array()));
  221. }
  222. public function testGetChoicesForValues()
  223. {
  224. $values = array($this->value1, $this->value2);
  225. $this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
  226. }
  227. public function testGetChoicesForValuesPreservesKeys()
  228. {
  229. $values = array(5 => $this->value1, 8 => $this->value2);
  230. $this->assertSame(array(5 => $this->choice1, 8 => $this->choice2), $this->list->getChoicesForValues($values));
  231. }
  232. public function testGetChoicesForValuesPreservesOrder()
  233. {
  234. $values = array($this->value2, $this->value1);
  235. $this->assertSame(array($this->choice2, $this->choice1), $this->list->getChoicesForValues($values));
  236. }
  237. public function testGetChoicesForValuesIgnoresNonExistingValues()
  238. {
  239. $values = array($this->value1, $this->value2, 'foobar');
  240. $this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
  241. }
  242. // https://github.com/symfony/symfony/issues/3446
  243. public function testGetChoicesForValuesEmpty()
  244. {
  245. $this->assertSame(array(), $this->list->getChoicesForValues(array()));
  246. }
  247. public function testGetValuesForChoices()
  248. {
  249. $choices = array($this->choice1, $this->choice2);
  250. $this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
  251. }
  252. public function testGetValuesForChoicesPreservesKeys()
  253. {
  254. $choices = array(5 => $this->choice1, 8 => $this->choice2);
  255. $this->assertSame(array(5 => $this->value1, 8 => $this->value2), $this->list->getValuesForChoices($choices));
  256. }
  257. public function testGetValuesForChoicesPreservesOrder()
  258. {
  259. $choices = array($this->choice2, $this->choice1);
  260. $this->assertSame(array($this->value2, $this->value1), $this->list->getValuesForChoices($choices));
  261. }
  262. public function testGetValuesForChoicesIgnoresNonExistingChoices()
  263. {
  264. $choices = array($this->choice1, $this->choice2, 'foobar');
  265. $this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
  266. }
  267. public function testGetValuesForChoicesEmpty()
  268. {
  269. $this->assertSame(array(), $this->list->getValuesForChoices(array()));
  270. }
  271. /**
  272. * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
  273. */
  274. abstract protected function createChoiceList();
  275. abstract protected function getChoices();
  276. abstract protected function getLabels();
  277. abstract protected function getValues();
  278. abstract protected function getIndices();
  279. }