SoftDeleteableDocumentTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Gedmo\SoftDeleteable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Doctrine\Common\Util\Debug,
  6. SoftDeleteable\Fixture\Document\Article,
  7. SoftDeleteable\Fixture\Document\Comment,
  8. SoftDeleteable\Fixture\Document\User,
  9. SoftDeleteable\Fixture\Document\Page,
  10. SoftDeleteable\Fixture\Document\MegaPage,
  11. SoftDeleteable\Fixture\Document\Module,
  12. SoftDeleteable\Fixture\Document\OtherArticle,
  13. SoftDeleteable\Fixture\Document\OtherComment,
  14. SoftDeleteable\Fixture\Document\Child,
  15. Gedmo\SoftDeleteable\SoftDeleteableListener;
  16. /**
  17. * These are tests for SoftDeleteable behavior
  18. *
  19. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  20. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  21. * @author Patrik Votoček <patrik@votocek.cz>
  22. * @link http://www.gediminasm.org
  23. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  24. */
  25. class SoftDeleteableDocumentTest extends BaseTestCaseMongoODM
  26. {
  27. const ARTICLE_CLASS = 'SoftDeleteable\Fixture\Document\Article';
  28. const COMMENT_CLASS = 'SoftDeleteable\Fixture\Document\Comment';
  29. const PAGE_CLASS = 'SoftDeleteable\Fixture\Document\Page';
  30. const MEGA_PAGE_CLASS = 'SoftDeleteable\Fixture\Document\MegaPage';
  31. const MODULE_CLASS = 'SoftDeleteable\Fixture\Document\Module';
  32. const OTHER_ARTICLE_CLASS = 'SoftDeleteable\Fixture\Document\OtherArticle';
  33. const OTHER_COMMENT_CLASS = 'SoftDeleteable\Fixture\Document\OtherComment';
  34. const USER_CLASS = 'SoftDeleteable\Fixture\Document\User';
  35. const USER__TIME_AWARE_CLASS = 'SoftDeleteable\Fixture\Document\UserTimeAware';
  36. const MAPPED_SUPERCLASS_CHILD_CLASS = 'SoftDeleteable\Fixture\Document\Child';
  37. const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable';
  38. private $softDeleteableListener;
  39. protected function setUp()
  40. {
  41. parent::setUp();
  42. $evm = new EventManager();
  43. $this->softDeleteableListener = new SoftDeleteableListener();
  44. $evm->addEventSubscriber($this->softDeleteableListener);
  45. $config = $this->getMockAnnotatedConfig();
  46. $config->addFilter(self::SOFT_DELETEABLE_FILTER_NAME, 'Gedmo\SoftDeleteable\Filter\ODM\SoftDeleteableFilter');
  47. $this->dm = $this->getMockDocumentManager($evm, $config);
  48. $this->dm->getFilterCollection()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  49. }
  50. /**
  51. * @test
  52. */
  53. public function shouldSoftlyDeleteIfColumnNameDifferFromPropertyName()
  54. {
  55. $repo = $this->dm->getRepository(self::USER_CLASS);
  56. $newUser = new User();
  57. $username = 'test_user';
  58. $newUser->setUsername($username);
  59. $this->dm->persist($newUser);
  60. $this->dm->flush();
  61. $user = $repo->findOneBy(array('username' => $username));
  62. $this->assertNull($user->getDeletedAt());
  63. $this->dm->remove($user);
  64. $this->dm->flush();
  65. $user = $repo->findOneBy(array('username' => $username));
  66. $this->assertNull($user);
  67. }
  68. /**
  69. * Tests the filter by enabling and disabling it between
  70. * some user persists actions.
  71. *
  72. * @test
  73. */
  74. public function testSoftDeleteableFilter()
  75. {
  76. $filter = $this->dm->getFilterCollection()->getFilter(self::SOFT_DELETEABLE_FILTER_NAME);
  77. $filter->disableForDocument(self::USER_CLASS);
  78. $repo = $this->dm->getRepository(self::USER_CLASS);
  79. $newUser = new User();
  80. $username = 'test_user';
  81. $newUser->setUsername($username);
  82. $this->dm->persist($newUser);
  83. $this->dm->flush();
  84. $user = $repo->findOneBy(array('username' => $username));
  85. $this->assertNull($user->getDeletedAt());
  86. $this->dm->remove($user);
  87. $this->dm->flush();
  88. $user = $repo->findOneBy(array('username' => $username));
  89. $this->assertNotNull($user->getDeletedAt());
  90. $filter->enableForDocument(self::USER_CLASS);
  91. $user = $repo->findOneBy(array('username' => $username));
  92. $this->assertNull($user);
  93. }
  94. /**
  95. * Tests the filter with time aware option by enabling and disabling it between
  96. * some user persists actions.
  97. *
  98. * @TODO: not supported in ODM yet
  99. * test
  100. */
  101. public function shouldSupportSoftDeleteableFilterTimeAware()
  102. {
  103. $filter = $this->dm->getFilterCollection()->getFilter(self::SOFT_DELETEABLE_FILTER_NAME);
  104. $filter->disableForDocument(self::USER__TIME_AWARE_CLASS);
  105. $repo = $this->dm->getRepository(self::USER__TIME_AWARE_CLASS);
  106. //Find entity with deletedAt date in future
  107. $newUser = new User();
  108. $username = 'test_user';
  109. $newUser->setUsername($username);
  110. $newUser->setDeletedAt(new \DateTime('tomorrow'));
  111. $this->dm->persist($newUser);
  112. $this->dm->flush();
  113. $user = $repo->findOneBy(array('username' => $username));
  114. $this->dm->remove($user);
  115. $this->dm->flush();
  116. //Don't find entity with deletedAt date in past
  117. $newUser = new User();
  118. $username = 'test_user';
  119. $newUser->setUsername($username);
  120. $newUser->setDeletedAt(new \DateTime('yesterday'));
  121. $this->dm->persist($newUser);
  122. $this->dm->flush();
  123. $user = $repo->findOneBy(array('username' => $username));
  124. $this->assertNull($user);
  125. $this->dm->remove($user);
  126. $this->dm->flush();
  127. }
  128. public function testPostSoftDeleteEventIsDispatched()
  129. {
  130. $subscriber = $this->getMock(
  131. "Doctrine\Common\EventSubscriber",
  132. array(
  133. "getSubscribedEvents",
  134. "preSoftDelete",
  135. "postSoftDelete"
  136. )
  137. );
  138. $subscriber->expects($this->once())
  139. ->method("getSubscribedEvents")
  140. ->will($this->returnValue(array(SoftDeleteableListener::PRE_SOFT_DELETE, SoftDeleteableListener::POST_SOFT_DELETE)));
  141. $subscriber->expects($this->once())
  142. ->method("preSoftDelete")
  143. ->with($this->anything());
  144. $subscriber->expects($this->once())
  145. ->method("postSoftDelete")
  146. ->with($this->anything());
  147. $this->dm->getEventManager()->addEventSubscriber($subscriber);
  148. $repo = $this->dm->getRepository(self::USER_CLASS);
  149. $newUser = new User();
  150. $username = 'test_user';
  151. $newUser->setUsername($username);
  152. $this->dm->persist($newUser);
  153. $this->dm->flush();
  154. $user = $repo->findOneBy(array('username' => 'test_user'));
  155. $this->assertNull($user->getDeletedAt());
  156. $this->dm->remove($user);
  157. $this->dm->flush();
  158. }
  159. }