/vendor/symfony/symfony/src/Symfony/Bridge/Propel1/Tests/Form/ChoiceList/ModelChoiceListTest.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS · PHP · 325 lines · 248 code · 54 blank · 23 comment · 0 complexity · 8b98c538ad3751d62237c4a45491f408 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\Bridge\Propel1\Tests\Form\ChoiceList;
  11. use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
  12. use Symfony\Bridge\Propel1\Tests\Propel1TestCase;
  13. use Symfony\Bridge\Propel1\Tests\Fixtures\Item;
  14. use Symfony\Bridge\Propel1\Tests\Fixtures\ItemQuery;
  15. use Symfony\Bridge\Propel1\Tests\Fixtures\ReadOnlyItem;
  16. use Symfony\Component\Form\Extension\Core\View\ChoiceView;
  17. class ModelChoiceListTest extends Propel1TestCase
  18. {
  19. const ITEM_CLASS = '\Symfony\Bridge\Propel1\Tests\Fixtures\Item';
  20. protected function setUp()
  21. {
  22. ItemQuery::$result = array();
  23. }
  24. public function testEmptyChoicesReturnsEmpty()
  25. {
  26. $choiceList = new ModelChoiceList(
  27. self::ITEM_CLASS,
  28. 'value',
  29. array()
  30. );
  31. $this->assertSame(array(), $choiceList->getChoices());
  32. }
  33. public function testReadOnlyIsValidChoice()
  34. {
  35. $item = new ReadOnlyItem();
  36. $choiceList = new ModelChoiceList(
  37. '\Symfony\Bridge\Propel1\Tests\Fixtures\ReadOnlyItem',
  38. 'name',
  39. array(
  40. $item,
  41. )
  42. );
  43. $this->assertSame(array(42 => $item), $choiceList->getChoices());
  44. }
  45. public function testFlattenedChoices()
  46. {
  47. $item1 = new Item(1, 'Foo');
  48. $item2 = new Item(2, 'Bar');
  49. $choiceList = new ModelChoiceList(
  50. self::ITEM_CLASS,
  51. 'value',
  52. array(
  53. $item1,
  54. $item2,
  55. )
  56. );
  57. $this->assertSame(array(1 => $item1, 2 => $item2), $choiceList->getChoices());
  58. }
  59. public function testFlattenedPreferredChoices()
  60. {
  61. $item1 = new Item(1, 'Foo');
  62. $item2 = new Item(2, 'Bar');
  63. $choiceList = new ModelChoiceList(
  64. self::ITEM_CLASS,
  65. 'value',
  66. array(
  67. $item1,
  68. $item2,
  69. ),
  70. null,
  71. null,
  72. array(
  73. $item1,
  74. )
  75. );
  76. $this->assertSame(array(1 => $item1, 2 => $item2), $choiceList->getChoices());
  77. $this->assertEquals(array(1 => new ChoiceView($item1, '1', 'Foo')), $choiceList->getPreferredViews());
  78. }
  79. public function testNestedChoices()
  80. {
  81. $item1 = new Item(1, 'Foo');
  82. $item2 = new Item(2, 'Bar');
  83. $choiceList = new ModelChoiceList(
  84. self::ITEM_CLASS,
  85. 'value',
  86. array(
  87. 'group1' => array($item1),
  88. 'group2' => array($item2),
  89. )
  90. );
  91. $this->assertSame(array(1 => $item1, 2 => $item2), $choiceList->getChoices());
  92. $this->assertEquals(array(
  93. 'group1' => array(1 => new ChoiceView($item1, '1', 'Foo')),
  94. 'group2' => array(2 => new ChoiceView($item2, '2', 'Bar')),
  95. ), $choiceList->getRemainingViews());
  96. }
  97. public function testGroupBySupportsString()
  98. {
  99. $item1 = new Item(1, 'Foo', 'Group1');
  100. $item2 = new Item(2, 'Bar', 'Group1');
  101. $item3 = new Item(3, 'Baz', 'Group2');
  102. $item4 = new Item(4, 'Boo!', null);
  103. $choiceList = new ModelChoiceList(
  104. self::ITEM_CLASS,
  105. 'value',
  106. array(
  107. $item1,
  108. $item2,
  109. $item3,
  110. $item4,
  111. ),
  112. null,
  113. 'groupName'
  114. );
  115. $this->assertEquals(array(1 => $item1, 2 => $item2, 3 => $item3, 4 => $item4), $choiceList->getChoices());
  116. $this->assertEquals(array(
  117. 'Group1' => array(1 => new ChoiceView($item1, '1', 'Foo'), 2 => new ChoiceView($item2, '2', 'Bar')),
  118. 'Group2' => array(3 => new ChoiceView($item3, '3', 'Baz')),
  119. 4 => new ChoiceView($item4, '4', 'Boo!'),
  120. ), $choiceList->getRemainingViews());
  121. }
  122. public function testGroupByInvalidPropertyPathReturnsFlatChoices()
  123. {
  124. $item1 = new Item(1, 'Foo', 'Group1');
  125. $item2 = new Item(2, 'Bar', 'Group1');
  126. $choiceList = new ModelChoiceList(
  127. self::ITEM_CLASS,
  128. 'value',
  129. array(
  130. $item1,
  131. $item2,
  132. ),
  133. null,
  134. 'child.that.does.not.exist'
  135. );
  136. $this->assertEquals(array(
  137. 1 => $item1,
  138. 2 => $item2,
  139. ), $choiceList->getChoices());
  140. }
  141. public function testGetValuesForChoices()
  142. {
  143. $item1 = new Item(1, 'Foo');
  144. $item2 = new Item(2, 'Bar');
  145. ItemQuery::$result = array(
  146. $item1,
  147. $item2,
  148. );
  149. $choiceList = new ModelChoiceList(
  150. self::ITEM_CLASS,
  151. 'value',
  152. null,
  153. null,
  154. null,
  155. null
  156. );
  157. $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2)));
  158. }
  159. public function testDifferentEqualObjectsAreChoosen()
  160. {
  161. $item = new Item(1, 'Foo');
  162. ItemQuery::$result = array(
  163. $item,
  164. );
  165. $choiceList = new ModelChoiceList(
  166. self::ITEM_CLASS,
  167. 'value',
  168. array($item)
  169. );
  170. $choosenItem = new Item(1, 'Foo');
  171. $this->assertEquals(array('1'), $choiceList->getValuesForChoices(array($choosenItem)));
  172. }
  173. /**
  174. * @group legacy
  175. */
  176. public function testLegacygetIndicesForChoices()
  177. {
  178. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  179. $item1 = new Item(1, 'Foo');
  180. $item2 = new Item(2, 'Bar');
  181. ItemQuery::$result = array(
  182. $item1,
  183. $item2,
  184. );
  185. $choiceList = new ModelChoiceList(
  186. self::ITEM_CLASS,
  187. 'value',
  188. null,
  189. null,
  190. null,
  191. null
  192. );
  193. $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2)));
  194. }
  195. /**
  196. * @group legacy
  197. */
  198. public function testLegacyDifferentEqualObjectsAreChoosen()
  199. {
  200. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  201. $item = new Item(1, 'Foo');
  202. ItemQuery::$result = array(
  203. $item,
  204. );
  205. $choiceList = new ModelChoiceList(
  206. self::ITEM_CLASS,
  207. 'value',
  208. array($item)
  209. );
  210. $choosenItem = new Item(1, 'Foo');
  211. $this->assertEquals(array(1), $choiceList->getIndicesForChoices(array($choosenItem)));
  212. }
  213. /**
  214. * @group legacy
  215. */
  216. public function testLegacyGetIndicesForNullChoices()
  217. {
  218. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  219. $item = new Item(1, 'Foo');
  220. $choiceList = new ModelChoiceList(
  221. self::ITEM_CLASS,
  222. 'value',
  223. array($item)
  224. );
  225. $this->assertEquals(array(), $choiceList->getIndicesForChoices(array(null)));
  226. }
  227. public function testDontAllowInvalidChoiceValues()
  228. {
  229. $item = new Item(1, 'Foo');
  230. $choiceList = new ModelChoiceList(
  231. self::ITEM_CLASS,
  232. 'value',
  233. array($item)
  234. );
  235. $this->assertEquals(array(), $choiceList->getValuesForChoices(array(new Item(2, 'Bar'))));
  236. $this->assertEquals(array(), $choiceList->getChoicesForValues(array(2)));
  237. }
  238. /**
  239. * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
  240. */
  241. public function testEmptyClass()
  242. {
  243. new ModelChoiceList('');
  244. }
  245. /**
  246. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  247. */
  248. public function testInvalidClass()
  249. {
  250. new ModelChoiceList('Foo\Bar\DoesNotExistClass');
  251. }
  252. public function testCustomIdentifier()
  253. {
  254. $item1 = new Item(1, 'Foo', null, null, 'slug');
  255. $item2 = new Item(2, 'Bar', null, null, 'slug2');
  256. $choiceList = new ModelChoiceList(
  257. self::ITEM_CLASS,
  258. 'value',
  259. array(
  260. $item1,
  261. $item2,
  262. ),
  263. null,
  264. null,
  265. array(),
  266. null,
  267. 'slug'
  268. );
  269. $this->assertSame(array('slug' => $item1, 'slug2' => $item2), $choiceList->getChoices());
  270. }
  271. }