SoftDeleteableEntityTest.php 4.5 KB

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