2
0

PersistAndFlushTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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->testUpdate();
  35. $this->testUpdateJoinColumn();
  36. $this->testDelete();
  37. }
  38. private function testUpdate(): void
  39. {
  40. $userRepository = $this->em->getRepository(UserTest::class);
  41. $user = $userRepository->findBy()->first()->orderBy('id')->toObject();
  42. $this->assertInstanceOf(ProxyInterface::class, $user);
  43. $this->assertInstanceOf(UserTest::class, $user);
  44. /**
  45. * @var ProxyInterface|UserTest $user
  46. */
  47. $user->setActive(false);
  48. $user->setLastname('TOTO');
  49. $this->assertStrictEquals(2, count($user->__getPropertiesModified()));
  50. $this->em->persist($user);
  51. $this->em->flush();
  52. $user = null;
  53. $this->em->clear();
  54. $user = $userRepository->findBy()->first()->orderBy('id')->toObject();
  55. $this->assertInstanceOf(ProxyInterface::class, $user);
  56. $this->assertInstanceOf(UserTest::class, $user);
  57. /**
  58. * @var ProxyInterface|UserTest $user
  59. */
  60. $this->assertStrictEquals(0, count($user->__getPropertiesModified()));
  61. $this->assertFalse($user->isActive());
  62. $this->assertStrictEquals('TOTO', $user->getLastname());
  63. }
  64. private function testUpdateJoinColumn()
  65. {
  66. $userRepository = $this->em->getRepository(UserTest::class);
  67. $postRepository = $this->em->getRepository(PostTest::class);
  68. $post = $postRepository->findBy()->first()
  69. ->where(Expr::isNotNull('user'))
  70. ->with(UserTest::class)
  71. ->toObject();
  72. $this->assertInstanceOf(ProxyInterface::class, $post);
  73. $this->assertInstanceOf(PostTest::class, $post);
  74. $this->assertInstanceOf(UserTest::class, $post->getUser());
  75. $this->assertStrictEquals(1, $post->getUser()->getId());
  76. $user2 = $userRepository->find(2)
  77. ->with(PostTest::class)
  78. ->toObject();
  79. $this->assertStrictEquals(2, count($user2->getPosts()->toArray()));
  80. foreach ($user2->getPosts()->toArray() as $postItem) {
  81. $this->assertInstanceOf(ProxyInterface::class, $postItem);
  82. $this->assertInstanceOf(PostTest::class, $postItem);
  83. }
  84. $post->setUser($user2);
  85. $this->em->persist($post);
  86. $this->em->flush();
  87. $user2 = $userRepository->find(2)
  88. ->with(PostTest::class)
  89. ->toObject();
  90. $this->assertStrictEquals(3, count($user2->getPosts()->toArray()));
  91. $user1 = $userRepository->find(1)->with(PostTest::class)->toObject();
  92. $this->assertStrictEquals(1, count($user1->getPosts()->toArray()));
  93. }
  94. private function testDelete()
  95. {
  96. $user = $this->em->getRepository(UserTest::class)->find(1)->toObject();
  97. $this->assertInstanceOf(ProxyInterface::class, $user);
  98. $this->assertInstanceOf(UserTest::class, $user);
  99. $posts = $user->getPosts();
  100. $this->em->remove($user);
  101. $this->em->flush();
  102. $this->assertFalse($user->__isInitialized());
  103. /**
  104. * @var PostTest|ProxyInterface $post
  105. */
  106. $post = $this->em->getRepository(PostTest::class)
  107. ->findBy()
  108. ->first()
  109. ->where(Expr::equal('user', $user->getId()))
  110. ->with(UserTest::class)
  111. ->toObject();
  112. $this->assertNull($post->getUser());
  113. $user = $this->em->getRepository(UserTest::class)->find(1)->toObject();
  114. $this->assertNull($user);
  115. $ids = [];
  116. foreach ($posts as $postToDelete) {
  117. $ids[] = $postToDelete->getId();
  118. $this->em->remove($postToDelete);
  119. $this->em->flush();
  120. $this->assertFalse($postToDelete->__isInitialized());
  121. }
  122. $this->assertStrictEquals($posts->count(), count($ids));
  123. foreach ($ids as $idPost) {
  124. $postToDelete = $this->em->getRepository(PostTest::class)->find($idPost)->toObject();
  125. $this->assertNull($postToDelete);
  126. }
  127. $this->assertFalse($post->__isInitialized());
  128. }
  129. }