SoftDeleteableEntityTest.php 6.2 KB

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