SoftDeleteableDocumentTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * @package Gedmo.SoftDeleteable
  23. * @link http://www.gediminasm.org
  24. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  25. */
  26. class SoftDeleteableDocumentTest extends BaseTestCaseMongoODM
  27. {
  28. const ARTICLE_CLASS = 'SoftDeleteable\Fixture\Document\Article';
  29. const COMMENT_CLASS = 'SoftDeleteable\Fixture\Document\Comment';
  30. const PAGE_CLASS = 'SoftDeleteable\Fixture\Document\Page';
  31. const MEGA_PAGE_CLASS = 'SoftDeleteable\Fixture\Document\MegaPage';
  32. const MODULE_CLASS = 'SoftDeleteable\Fixture\Document\Module';
  33. const OTHER_ARTICLE_CLASS = 'SoftDeleteable\Fixture\Document\OtherArticle';
  34. const OTHER_COMMENT_CLASS = 'SoftDeleteable\Fixture\Document\OtherComment';
  35. const USER_CLASS = 'SoftDeleteable\Fixture\Document\User';
  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. public function testPostSoftDeleteEventIsDispatched()
  95. {
  96. $subscriber = $this->getMock(
  97. "Doctrine\Common\EventSubscriber",
  98. array(
  99. "getSubscribedEvents",
  100. "preSoftDelete",
  101. "postSoftDelete"
  102. )
  103. );
  104. $subscriber->expects($this->once())
  105. ->method("getSubscribedEvents")
  106. ->will($this->returnValue(array(SoftDeleteableListener::PRE_SOFT_DELETE, SoftDeleteableListener::POST_SOFT_DELETE)));
  107. $subscriber->expects($this->once())
  108. ->method("preSoftDelete")
  109. ->with($this->anything());
  110. $subscriber->expects($this->once())
  111. ->method("postSoftDelete")
  112. ->with($this->anything());
  113. $this->dm->getEventManager()->addEventSubscriber($subscriber);
  114. $repo = $this->dm->getRepository(self::USER_CLASS);
  115. $newUser = new User();
  116. $username = 'test_user';
  117. $newUser->setUsername($username);
  118. $this->dm->persist($newUser);
  119. $this->dm->flush();
  120. $user = $repo->findOneBy(array('username' => 'test_user'));
  121. $this->assertNull($user->getDeletedAt());
  122. $this->dm->remove($user);
  123. $this->dm->flush();
  124. }
  125. }