/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2602Test.php

https://github.com/adrienbrault/doctrine2 · PHP · 333 lines · 167 code · 55 blank · 111 comment · 1 complexity · 2400ce5a78e9d065206ecbd87790d7ad MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Tests\ORM\Functional\Ticket;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Annotation as ORM;
  6. use Doctrine\ORM\Event\LifecycleEventArgs;
  7. use Doctrine\ORM\Events;
  8. use Doctrine\Tests\OrmFunctionalTestCase;
  9. /**
  10. * @group performance
  11. * @group DDC-2602
  12. */
  13. class DDC2602Test extends OrmFunctionalTestCase
  14. {
  15. protected function setUp() : void
  16. {
  17. parent::setUp();
  18. $this->schemaTool->createSchema(
  19. [
  20. $this->em->getClassMetadata(DDC2602User::class),
  21. $this->em->getClassMetadata(DDC2602Biography::class),
  22. $this->em->getClassMetadata(DDC2602BiographyField::class),
  23. $this->em->getClassMetadata(DDC2602BiographyFieldChoice::class),
  24. ]
  25. );
  26. $this->loadFixture();
  27. }
  28. protected function tearDown() : void
  29. {
  30. parent::tearDown();
  31. $this->schemaTool->dropSchema(
  32. [
  33. $this->em->getClassMetadata(DDC2602User::class),
  34. $this->em->getClassMetadata(DDC2602Biography::class),
  35. $this->em->getClassMetadata(DDC2602BiographyField::class),
  36. $this->em->getClassMetadata(DDC2602BiographyFieldChoice::class),
  37. ]
  38. );
  39. }
  40. public function testPostLoadListenerShouldBeAbleToRunQueries() : void
  41. {
  42. $eventManager = $this->em->getEventManager();
  43. $eventManager->addEventListener([Events::postLoad], new DDC2602PostLoadListener());
  44. $result = $this->em
  45. ->createQuery('SELECT u, b FROM Doctrine\Tests\ORM\Performance\DDC2602User u JOIN u.biography b')
  46. ->getResult();
  47. self::assertCount(2, $result);
  48. self::assertCount(2, $result[0]->biography->fieldList);
  49. self::assertCount(1, $result[1]->biography->fieldList);
  50. }
  51. private function loadFixture() : void
  52. {
  53. $user1 = new DDC2602User();
  54. $user2 = new DDC2602User();
  55. $biography1 = new DDC2602Biography();
  56. $biography2 = new DDC2602Biography();
  57. $biographyField1 = new DDC2602BiographyField();
  58. $biographyField2 = new DDC2602BiographyField();
  59. $biographyFieldChoice1 = new DDC2602BiographyFieldChoice();
  60. $biographyFieldChoice2 = new DDC2602BiographyFieldChoice();
  61. $biographyFieldChoice3 = new DDC2602BiographyFieldChoice();
  62. $biographyFieldChoice4 = new DDC2602BiographyFieldChoice();
  63. $biographyFieldChoice5 = new DDC2602BiographyFieldChoice();
  64. $biographyFieldChoice6 = new DDC2602BiographyFieldChoice();
  65. $user1->name = 'Gblanco';
  66. $user1->biography = $biography1;
  67. $user2->name = 'Beberlei';
  68. $user2->biography = $biography2;
  69. $biography1->user = $user1;
  70. $biography1->content = '[{"field": 1, "choiceList": [1,3]}, {"field": 2, "choiceList": [5]}]';
  71. $biography2->user = $user2;
  72. $biography2->content = '[{"field": 1, "choiceList": [1,2,3,4]}]';
  73. $biographyField1->alias = 'question_1';
  74. $biographyField1->label = 'Question 1';
  75. $biographyField1->choiceList->add($biographyFieldChoice1);
  76. $biographyField1->choiceList->add($biographyFieldChoice2);
  77. $biographyField1->choiceList->add($biographyFieldChoice3);
  78. $biographyField1->choiceList->add($biographyFieldChoice4);
  79. $biographyField2->alias = 'question_2';
  80. $biographyField2->label = 'Question 2';
  81. $biographyField2->choiceList->add($biographyFieldChoice5);
  82. $biographyField2->choiceList->add($biographyFieldChoice6);
  83. $biographyFieldChoice1->field = $biographyField1;
  84. $biographyFieldChoice1->label = 'Answer 1.1';
  85. $biographyFieldChoice2->field = $biographyField1;
  86. $biographyFieldChoice2->label = 'Answer 1.2';
  87. $biographyFieldChoice3->field = $biographyField1;
  88. $biographyFieldChoice3->label = 'Answer 1.3';
  89. $biographyFieldChoice4->field = $biographyField1;
  90. $biographyFieldChoice4->label = 'Answer 1.4';
  91. $biographyFieldChoice5->field = $biographyField2;
  92. $biographyFieldChoice5->label = 'Answer 2.1';
  93. $biographyFieldChoice6->field = $biographyField2;
  94. $biographyFieldChoice6->label = 'Answer 2.2';
  95. $this->em->persist($user1);
  96. $this->em->persist($user2);
  97. $this->em->persist($biographyField1);
  98. $this->em->persist($biographyField2);
  99. $this->em->flush();
  100. $this->em->clear();
  101. }
  102. }
  103. class DDC2602PostLoadListener
  104. {
  105. public function postLoad(LifecycleEventArgs $event) : void
  106. {
  107. $entity = $event->getEntity();
  108. if ( ! ($entity instanceof DDC2602Biography)) {
  109. return;
  110. }
  111. $entityManager = $event->getEntityManager();
  112. $query = $entityManager->createQuery('
  113. SELECT f, fc
  114. FROM Doctrine\Tests\ORM\Functional\Ticket\DDC2602BiographyField f INDEX BY f.id
  115. JOIN f.choiceList fc INDEX BY fc.id
  116. ');
  117. $result = $query->getResult();
  118. $content = json_decode($entity->content);
  119. $fieldList = new ArrayCollection();
  120. foreach ($content as $selection) {
  121. $field = $result[$selection->field];
  122. $choiceList = $selection->choiceList;
  123. $fieldSelection = new DDC2602FieldSelection();
  124. $fieldSelection->field = $field;
  125. $fieldSelection->choiceList = $field->choiceList->filter(function ($choice) use ($choiceList) {
  126. return in_array($choice->id, $choiceList, true);
  127. });
  128. $fieldList->add($fieldSelection);
  129. }
  130. $entity->fieldList = $fieldList;
  131. }
  132. }
  133. /**
  134. * @ORM\Entity
  135. */
  136. class DDC2602User
  137. {
  138. /**
  139. * @ORM\Id @ORM\GeneratedValue
  140. * @ORM\Column(type="integer")
  141. *
  142. * @var integer
  143. */
  144. public $id;
  145. /**
  146. * @ORM\Column(type="string", length=15)
  147. *
  148. * @var string
  149. */
  150. public $name;
  151. /**
  152. * @ORM\OneToOne(
  153. * targetEntity=DDC2602Biography::class,
  154. * inversedBy="user",
  155. * cascade={"persist", "refresh", "remove"}
  156. * )
  157. * @ORM\JoinColumn(nullable=false)
  158. *
  159. * @var DDC2602Biography
  160. */
  161. public $biography;
  162. }
  163. /**
  164. * @ORM\Entity
  165. */
  166. class DDC2602Biography
  167. {
  168. /**
  169. * @ORM\Id @ORM\GeneratedValue
  170. * @ORM\Column(type="integer")
  171. *
  172. * @var integer
  173. */
  174. public $id;
  175. /**
  176. * @ORM\OneToOne(
  177. * targetEntity=DDC2602User::class,
  178. * mappedBy="biography",
  179. * cascade={"persist", "refresh"}
  180. * )
  181. *
  182. * @var DDC2602User
  183. */
  184. public $user;
  185. /**
  186. * @ORM\Column(type="text", nullable=true)
  187. *
  188. * @var string
  189. */
  190. public $content;
  191. /**
  192. * @var array
  193. */
  194. public $fieldList = [];
  195. }
  196. /**
  197. * @ORM\Entity
  198. */
  199. class DDC2602BiographyField
  200. {
  201. /**
  202. * @ORM\Id @ORM\GeneratedValue
  203. * @ORM\Column(type="integer")
  204. *
  205. * @var integer
  206. */
  207. public $id;
  208. /**
  209. * @ORM\Column(type="string", unique=true, length=100)
  210. */
  211. public $alias;
  212. /**
  213. * @ORM\Column(type="string", length=100)
  214. */
  215. public $label;
  216. /**
  217. * @ORM\OneToMany(
  218. * targetEntity=DDC2602BiographyFieldChoice::class,
  219. * mappedBy="field",
  220. * cascade={"persist", "refresh"}
  221. * )
  222. *
  223. * @var \Doctrine\Common\Collections\ArrayCollection
  224. */
  225. public $choiceList;
  226. /**
  227. * Constructor.
  228. */
  229. public function __construct()
  230. {
  231. $this->choiceList = new ArrayCollection();
  232. }
  233. }
  234. /**
  235. * @ORM\Entity
  236. */
  237. class DDC2602BiographyFieldChoice
  238. {
  239. /**
  240. * @ORM\Id @ORM\GeneratedValue
  241. * @ORM\Column(type="integer")
  242. *
  243. * @var integer
  244. */
  245. public $id;
  246. /**
  247. * @ORM\Column(type="string", unique=true, length=100)
  248. */
  249. public $label;
  250. /**
  251. * @ORM\ManyToOne(
  252. * targetEntity=DDC2602BiographyField::class,
  253. * inversedBy="choiceList"
  254. * )
  255. * @ORM\JoinColumn(onDelete="CASCADE")
  256. *
  257. * @var DDC2602BiographyField
  258. */
  259. public $field;
  260. }
  261. class DDC2602FieldSelection
  262. {
  263. /**
  264. * @var DDC2602BiographyField
  265. */
  266. public $field;
  267. /**
  268. * @var \Doctrine\Common\Collections\ArrayCollection
  269. */
  270. public $choiceList;
  271. /**
  272. * Constructor.
  273. */
  274. public function __construct()
  275. {
  276. $this->choiceList = new ArrayCollection();
  277. }
  278. }