SoftDeleteableEntityTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Gedmo\SoftDeleteable;
  3. use Tool\BaseTestCaseORM;
  4. use Doctrine\Common\EventManager;
  5. use Doctrine\Common\Util\Debug,
  6. SoftDeleteable\Fixture\Entity\Article,
  7. SoftDeleteable\Fixture\Entity\Comment,
  8. SoftDeleteable\Fixture\Entity\User,
  9. SoftDeleteable\Fixture\Entity\Page,
  10. SoftDeleteable\Fixture\Entity\MegaPage,
  11. SoftDeleteable\Fixture\Entity\Module,
  12. Gedmo\SoftDeleteable\SoftDeleteableListener;
  13. /**
  14. * These are tests for SoftDeleteable behavior
  15. *
  16. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  17. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  18. * @package Gedmo.SoftDeleteable
  19. * @link http://www.gediminasm.org
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. class SoftDeleteableEntityTest extends BaseTestCaseORM
  23. {
  24. const ARTICLE_CLASS = 'SoftDeleteable\Fixture\Entity\Article';
  25. const COMMENT_CLASS = 'SoftDeleteable\Fixture\Entity\Comment';
  26. const PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\Page';
  27. const MEGA_PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\MegaPage';
  28. const MODULE_CLASS = 'SoftDeleteable\Fixture\Entity\Module';
  29. const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable';
  30. const USER_CLASS = 'SoftDeleteable\Fixture\Entity\User';
  31. private $softDeleteableListener;
  32. protected function setUp()
  33. {
  34. parent::setUp();
  35. $evm = new EventManager;
  36. $this->softDeleteableListener = new SoftDeleteableListener();
  37. $evm->addEventSubscriber($this->softDeleteableListener);
  38. $config = $this->getMockAnnotatedConfig();
  39. $config->addFilter(self::SOFT_DELETEABLE_FILTER_NAME, 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
  40. $this->em = $this->getMockSqliteEntityManager($evm, $config);
  41. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  42. }
  43. /**
  44. * @test
  45. */
  46. public function shouldSoftlyDeleteIfColumnNameDifferFromPropertyName()
  47. {
  48. $repo = $this->em->getRepository(self::USER_CLASS);
  49. $newUser = new User();
  50. $username = 'test_user';
  51. $newUser->setUsername($username);
  52. $this->em->persist($newUser);
  53. $this->em->flush();
  54. $user = $repo->findOneBy(array('username' => $username));
  55. $this->assertNull($user->getDeletedAt());
  56. $this->em->remove($user);
  57. $this->em->flush();
  58. $user = $repo->findOneBy(array('username' => $username));
  59. $this->assertNull($user);
  60. }
  61. public function testSoftDeleteable()
  62. {
  63. $repo = $this->em->getRepository(self::ARTICLE_CLASS);
  64. $commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
  65. $comment = new Comment();
  66. $commentField = 'comment';
  67. $commentValue = 'Comment 1';
  68. $comment->setComment($commentValue);
  69. $art0 = new Article();
  70. $field = 'title';
  71. $value = 'Title 1';
  72. $art0->setTitle($value);
  73. $art0->addComment($comment);
  74. $this->em->persist($art0);
  75. $this->em->flush();
  76. $art = $repo->findOneBy(array($field => $value));
  77. $this->assertNull($art->getDeletedAt());
  78. $this->assertNull($comment->getDeletedAt());
  79. $this->em->remove($art);
  80. $this->em->flush();
  81. $art = $repo->findOneBy(array($field => $value));
  82. $this->assertNull($art);
  83. $comment = $commentRepo->findOneBy(array($commentField => $commentValue));
  84. $this->assertNull($comment);
  85. // Now we deactivate the filter so we test if the entity appears in the result
  86. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  87. $this->em->clear();
  88. $art = $repo->findOneBy(array($field => $value));
  89. $this->assertTrue(is_object($art));
  90. $this->assertTrue(is_object($art->getDeletedAt()));
  91. $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
  92. $comment = $commentRepo->findOneBy(array($commentField => $commentValue));
  93. $this->assertTrue(is_object($comment));
  94. $this->assertTrue(is_object($comment->getDeletedAt()));
  95. $this->assertTrue($comment->getDeletedAt() instanceof \DateTime);
  96. $this->em->createQuery('UPDATE '.self::ARTICLE_CLASS.' a SET a.deletedAt = NULL')->execute();
  97. $this->em->refresh($art);
  98. $this->em->refresh($comment);
  99. // Now we try with a DQL Delete query
  100. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  101. $dql = sprintf('DELETE FROM %s a WHERE a.%s = :%s',
  102. self::ARTICLE_CLASS, $field, $field);
  103. $query = $this->em->createQuery($dql);
  104. $query->setParameter($field, $value);
  105. $query->setHint(
  106. \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  107. 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
  108. );
  109. $query->execute();
  110. $art = $repo->findOneBy(array($field => $value));
  111. $this->assertNull($art);
  112. // Now we deactivate the filter so we test if the entity appears in the result
  113. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  114. $this->em->clear();
  115. $art = $repo->findOneBy(array($field => $value));
  116. $this->assertTrue(is_object($art));
  117. $this->assertTrue(is_object($art->getDeletedAt()));
  118. $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
  119. // Inheritance tree DELETE DQL
  120. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  121. $megaPageRepo = $this->em->getRepository(self::MEGA_PAGE_CLASS);
  122. $module = new Module();
  123. $module->setTitle('Module 1');
  124. $page = new MegaPage();
  125. $page->setTitle('Page 1');
  126. $page->addModule($module);
  127. $module->setPage($page);
  128. $this->em->persist($page);
  129. $this->em->persist($module);
  130. $this->em->flush();
  131. $dql = sprintf('DELETE FROM %s p',
  132. self::PAGE_CLASS);
  133. $query = $this->em->createQuery($dql);
  134. $query->setHint(
  135. \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  136. 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
  137. );
  138. $query->execute();
  139. $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
  140. $this->assertNull($p);
  141. // Now we deactivate the filter so we test if the entity appears in the result
  142. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  143. $this->em->clear();
  144. $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
  145. $this->assertTrue(is_object($p));
  146. $this->assertTrue(is_object($p->getDeletedAt()));
  147. $this->assertTrue($p->getDeletedAt() instanceof \DateTime);
  148. }
  149. protected function getUsedEntityFixtures()
  150. {
  151. return array(
  152. self::ARTICLE_CLASS,
  153. self::PAGE_CLASS,
  154. self::MEGA_PAGE_CLASS,
  155. self::MODULE_CLASS,
  156. self::COMMENT_CLASS,
  157. self::USER_CLASS
  158. );
  159. }
  160. private function populate()
  161. {
  162. }
  163. }