/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS · PHP · 290 lines · 211 code · 52 blank · 27 comment · 0 complexity · 114c47e9ec28303f691dd73ead188b65 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\Doctrine\Tests\Form\ChoiceList;
  11. use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
  12. use Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity;
  13. use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
  14. use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
  15. use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
  16. use Symfony\Component\Form\Extension\Core\View\ChoiceView;
  17. use Doctrine\ORM\Tools\SchemaTool;
  18. class GenericEntityChoiceListTest extends \PHPUnit_Framework_TestCase
  19. {
  20. const SINGLE_INT_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity';
  21. const SINGLE_STRING_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity';
  22. const COMPOSITE_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity';
  23. const GROUPABLE_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity';
  24. /**
  25. * @var \Doctrine\ORM\EntityManager
  26. */
  27. private $em;
  28. protected function setUp()
  29. {
  30. $this->em = DoctrineTestHelper::createTestEntityManager();
  31. $schemaTool = new SchemaTool($this->em);
  32. $classes = array(
  33. $this->em->getClassMetadata(self::SINGLE_INT_ID_CLASS),
  34. $this->em->getClassMetadata(self::SINGLE_STRING_ID_CLASS),
  35. $this->em->getClassMetadata(self::COMPOSITE_ID_CLASS),
  36. $this->em->getClassMetadata(self::GROUPABLE_CLASS),
  37. );
  38. try {
  39. $schemaTool->dropSchema($classes);
  40. } catch (\Exception $e) {
  41. }
  42. try {
  43. $schemaTool->createSchema($classes);
  44. } catch (\Exception $e) {
  45. }
  46. parent::setUp();
  47. }
  48. protected function tearDown()
  49. {
  50. parent::tearDown();
  51. $this->em = null;
  52. }
  53. /**
  54. * @expectedException \Symfony\Component\Form\Exception\StringCastException
  55. * @expectedMessage Entity "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).
  56. */
  57. public function testEntitiesMustHaveAToStringMethod()
  58. {
  59. $entity1 = new SingleIntIdNoToStringEntity(1, 'Foo');
  60. $entity2 = new SingleIntIdNoToStringEntity(2, 'Bar');
  61. // Persist for managed state
  62. $this->em->persist($entity1);
  63. $this->em->persist($entity2);
  64. $choiceList = new EntityChoiceList(
  65. $this->em,
  66. self::SINGLE_INT_ID_CLASS,
  67. null,
  68. null,
  69. array(
  70. $entity1,
  71. $entity2,
  72. )
  73. );
  74. $choiceList->getValues();
  75. }
  76. /**
  77. * @expectedException \Symfony\Component\Form\Exception\RuntimeException
  78. */
  79. public function testChoicesMustBeManaged()
  80. {
  81. $entity1 = new SingleIntIdEntity(1, 'Foo');
  82. $entity2 = new SingleIntIdEntity(2, 'Bar');
  83. // no persist here!
  84. $choiceList = new EntityChoiceList(
  85. $this->em,
  86. self::SINGLE_INT_ID_CLASS,
  87. 'name',
  88. null,
  89. array(
  90. $entity1,
  91. $entity2,
  92. )
  93. );
  94. // triggers loading -> exception
  95. $choiceList->getChoices();
  96. }
  97. public function testInitExplicitChoices()
  98. {
  99. $entity1 = new SingleIntIdEntity(1, 'Foo');
  100. $entity2 = new SingleIntIdEntity(2, 'Bar');
  101. // Persist for managed state
  102. $this->em->persist($entity1);
  103. $this->em->persist($entity2);
  104. $choiceList = new EntityChoiceList(
  105. $this->em,
  106. self::SINGLE_INT_ID_CLASS,
  107. 'name',
  108. null,
  109. array(
  110. $entity1,
  111. $entity2,
  112. )
  113. );
  114. $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices());
  115. }
  116. public function testInitEmptyChoices()
  117. {
  118. $entity1 = new SingleIntIdEntity(1, 'Foo');
  119. $entity2 = new SingleIntIdEntity(2, 'Bar');
  120. // Persist for managed state
  121. $this->em->persist($entity1);
  122. $this->em->persist($entity2);
  123. $choiceList = new EntityChoiceList(
  124. $this->em,
  125. self::SINGLE_INT_ID_CLASS,
  126. 'name',
  127. null,
  128. array()
  129. );
  130. $this->assertSame(array(), $choiceList->getChoices());
  131. }
  132. public function testInitNestedChoices()
  133. {
  134. $entity1 = new SingleIntIdEntity(1, 'Foo');
  135. $entity2 = new SingleIntIdEntity(2, 'Bar');
  136. // Oh yeah, we're persisting with fire now!
  137. $this->em->persist($entity1);
  138. $this->em->persist($entity2);
  139. $choiceList = new EntityChoiceList(
  140. $this->em,
  141. self::SINGLE_INT_ID_CLASS,
  142. 'name',
  143. null,
  144. array(
  145. 'group1' => array($entity1),
  146. 'group2' => array($entity2),
  147. ),
  148. array()
  149. );
  150. $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices());
  151. $this->assertEquals(array(
  152. 'group1' => array(1 => new ChoiceView($entity1, '1', 'Foo')),
  153. 'group2' => array(2 => new ChoiceView($entity2, '2', 'Bar')),
  154. ), $choiceList->getRemainingViews());
  155. }
  156. public function testGroupByPropertyPath()
  157. {
  158. $item1 = new GroupableEntity(1, 'Foo', 'Group1');
  159. $item2 = new GroupableEntity(2, 'Bar', 'Group1');
  160. $item3 = new GroupableEntity(3, 'Baz', 'Group2');
  161. $item4 = new GroupableEntity(4, 'Boo!', null);
  162. $this->em->persist($item1);
  163. $this->em->persist($item2);
  164. $this->em->persist($item3);
  165. $this->em->persist($item4);
  166. $choiceList = new EntityChoiceList(
  167. $this->em,
  168. self::GROUPABLE_CLASS,
  169. 'name',
  170. null,
  171. array(
  172. $item1,
  173. $item2,
  174. $item3,
  175. $item4,
  176. ),
  177. array(),
  178. 'groupName'
  179. );
  180. $this->assertEquals(array(1 => $item1, 2 => $item2, 3 => $item3, 4 => $item4), $choiceList->getChoices());
  181. $this->assertEquals(array(
  182. 'Group1' => array(1 => new ChoiceView($item1, '1', 'Foo'), 2 => new ChoiceView($item2, '2', 'Bar')),
  183. 'Group2' => array(3 => new ChoiceView($item3, '3', 'Baz')),
  184. 4 => new ChoiceView($item4, '4', 'Boo!'),
  185. ), $choiceList->getRemainingViews());
  186. }
  187. public function testGroupByInvalidPropertyPathReturnsFlatChoices()
  188. {
  189. $item1 = new GroupableEntity(1, 'Foo', 'Group1');
  190. $item2 = new GroupableEntity(2, 'Bar', 'Group1');
  191. $this->em->persist($item1);
  192. $this->em->persist($item2);
  193. $choiceList = new EntityChoiceList(
  194. $this->em,
  195. self::GROUPABLE_CLASS,
  196. 'name',
  197. null,
  198. array(
  199. $item1,
  200. $item2,
  201. ),
  202. array(),
  203. 'child.that.does.not.exist'
  204. );
  205. $this->assertEquals(array(
  206. 1 => $item1,
  207. 2 => $item2,
  208. ), $choiceList->getChoices());
  209. }
  210. public function testInitShorthandEntityName()
  211. {
  212. $item1 = new SingleIntIdEntity(1, 'Foo');
  213. $item2 = new SingleIntIdEntity(2, 'Bar');
  214. $this->em->persist($item1);
  215. $this->em->persist($item2);
  216. $choiceList = new EntityChoiceList(
  217. $this->em,
  218. 'SymfonyTestsDoctrine:SingleIntIdEntity'
  219. );
  220. $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2)));
  221. }
  222. /**
  223. * @group legacy
  224. */
  225. public function testLegacyInitShorthandEntityName()
  226. {
  227. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  228. $item1 = new SingleIntIdEntity(1, 'Foo');
  229. $item2 = new SingleIntIdEntity(2, 'Bar');
  230. $this->em->persist($item1);
  231. $this->em->persist($item2);
  232. $choiceList = new EntityChoiceList(
  233. $this->em,
  234. 'SymfonyTestsDoctrine:SingleIntIdEntity'
  235. );
  236. $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2)));
  237. }
  238. }