PersistAndFlushTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM;
  3. use PhpDevCommunity\PaperORM\EntityManager;
  4. use PhpDevCommunity\PaperORM\Expression\Expr;
  5. use PhpDevCommunity\PaperORM\Proxy\ProxyInterface;
  6. use PhpDevCommunity\UniTester\TestCase;
  7. use Test\PhpDevCommunity\PaperORM\Entity\PostTest;
  8. use Test\PhpDevCommunity\PaperORM\Entity\UserTest;
  9. use Test\PhpDevCommunity\PaperORM\Helper\DataBaseHelperTest;
  10. class PersistAndFlushTest extends TestCase
  11. {
  12. private EntityManager $em;
  13. protected function setUp(): void
  14. {
  15. $this->em = new EntityManager([
  16. 'driver' => 'sqlite',
  17. 'user' => null,
  18. 'password' => null,
  19. 'memory' => true,
  20. 'debug' => true
  21. ]);
  22. $this->setUpDatabaseSchema();
  23. }
  24. protected function setUpDatabaseSchema(): void
  25. {
  26. DataBaseHelperTest::init($this->em);
  27. }
  28. protected function tearDown(): void
  29. {
  30. $this->em->getConnection()->close();
  31. }
  32. protected function execute(): void
  33. {
  34. $this->testInsert();
  35. $this->testUpdate();
  36. $this->testUpdateJoinColumn();
  37. $this->testDelete();
  38. }
  39. private function testInsert(): void
  40. {
  41. $user = new UserTest();
  42. $this->assertNull($user->getId());
  43. $user->setFirstname('John');
  44. $user->setLastname('Doe');
  45. $user->setPassword('secret');
  46. $user->setEmail('Xq5qI@example.com');
  47. $user->setActive(true);
  48. $this->em->persist($user);
  49. $this->em->flush();
  50. $this->assertNotNull($user->getId());
  51. $this->em->clear();
  52. }
  53. private function testUpdate(): void
  54. {
  55. $userRepository = $this->em->getRepository(UserTest::class);
  56. $user = $userRepository->findBy()->first()->orderBy('id')->toObject();
  57. $this->assertInstanceOf(ProxyInterface::class, $user);
  58. $this->assertInstanceOf(UserTest::class, $user);
  59. /**
  60. * @var ProxyInterface|UserTest $user
  61. */
  62. $user->setActive(false);
  63. $user->setLastname('TOTO');
  64. $this->assertStrictEquals(2, count($user->__getPropertiesModified()));
  65. $this->em->persist($user);
  66. $this->em->flush();
  67. $user = null;
  68. $this->em->clear();
  69. $user = $userRepository->findBy()->first()->orderBy('id')->toObject();
  70. $this->assertInstanceOf(ProxyInterface::class, $user);
  71. $this->assertInstanceOf(UserTest::class, $user);
  72. /**
  73. * @var ProxyInterface|UserTest $user
  74. */
  75. $this->assertStrictEquals(0, count($user->__getPropertiesModified()));
  76. $this->assertFalse($user->isActive());
  77. $this->assertStrictEquals('TOTO', $user->getLastname());
  78. }
  79. private function testUpdateJoinColumn()
  80. {
  81. $userRepository = $this->em->getRepository(UserTest::class);
  82. $postRepository = $this->em->getRepository(PostTest::class);
  83. $post = $postRepository->findBy()->first()
  84. ->where(Expr::isNotNull('user'))
  85. ->with(UserTest::class)
  86. ->toObject();
  87. $this->assertInstanceOf(ProxyInterface::class, $post);
  88. $this->assertInstanceOf(PostTest::class, $post);
  89. $this->assertInstanceOf(UserTest::class, $post->getUser());
  90. $this->assertStrictEquals(1, $post->getUser()->getId());
  91. $user2 = $userRepository->find(2)
  92. ->with(PostTest::class)
  93. ->toObject();
  94. $this->assertStrictEquals(2, count($user2->getPosts()->toArray()));
  95. foreach ($user2->getPosts()->toArray() as $postItem) {
  96. $this->assertInstanceOf(ProxyInterface::class, $postItem);
  97. $this->assertInstanceOf(PostTest::class, $postItem);
  98. }
  99. $post->setUser($user2);
  100. $this->em->persist($post);
  101. $this->em->flush();
  102. $user2 = $userRepository->find(2)
  103. ->with(PostTest::class)
  104. ->toObject();
  105. $this->assertStrictEquals(3, count($user2->getPosts()->toArray()));
  106. $user1 = $userRepository->find(1)->with(PostTest::class)->toObject();
  107. $this->assertStrictEquals(1, count($user1->getPosts()->toArray()));
  108. }
  109. private function testDelete()
  110. {
  111. $user = $this->em->getRepository(UserTest::class)->find(1)->toObject();
  112. $this->assertInstanceOf(ProxyInterface::class, $user);
  113. $this->assertInstanceOf(UserTest::class, $user);
  114. $posts = $user->getPosts();
  115. $this->em->remove($user);
  116. $this->em->flush();
  117. $this->assertFalse($user->__isInitialized());
  118. /**
  119. * @var PostTest|ProxyInterface $post
  120. */
  121. $post = $this->em->getRepository(PostTest::class)
  122. ->findBy()
  123. ->first()
  124. ->where(Expr::equal('user', $user->getId()))
  125. ->with(UserTest::class)
  126. ->toObject();
  127. $this->assertNull($post->getUser());
  128. $user = $this->em->getRepository(UserTest::class)->find(1)->toObject();
  129. $this->assertNull($user);
  130. $ids = [];
  131. foreach ($posts as $postToDelete) {
  132. $ids[] = $postToDelete->getId();
  133. $this->em->remove($postToDelete);
  134. $this->em->flush();
  135. $this->assertFalse($postToDelete->__isInitialized());
  136. }
  137. $this->assertStrictEquals($posts->count(), count($ids));
  138. foreach ($ids as $idPost) {
  139. $postToDelete = $this->em->getRepository(PostTest::class)->find($idPost)->toObject();
  140. $this->assertNull($postToDelete);
  141. }
  142. $this->assertFalse($post->__isInitialized());
  143. }
  144. }