瀏覽代碼

[SoftDeleteable] Added tests

comfortablynumb 13 年之前
父節點
當前提交
f9a468c2be

+ 10 - 4
lib/Gedmo/SoftDeleteable/Query/TreeWalker/SoftDeleteableWalker.php

@@ -8,6 +8,7 @@ use Doctrine\ORM\Query\AST\DeleteClause;
 use Doctrine\ORM\Query\AST\UpdateClause;
 use Doctrine\ORM\Query\AST\UpdateItem;
 use Doctrine\ORM\Query\Exec\SingleTableDeleteUpdateExecutor;
+use Doctrine\ORM\Query\Exec\MultiTableDeleteExecutor;
 use Doctrine\ORM\Query\AST\PathExpression;
 use Gedmo\SoftDeleteable\SoftDeleteableListener;
 
@@ -51,11 +52,16 @@ class SoftDeleteableWalker extends SqlWalker
      */
     public function getExecutor($AST)
     {
-        if (!$AST instanceof DeleteStatement) {
-            throw new \Gedmo\Exception\UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
+        switch (true) {
+            case ($AST instanceof DeleteStatement):
+                $primaryClass = $this->getEntityManager()->getClassMetadata($AST->deleteClause->abstractSchemaName);
+
+                return ($primaryClass->isInheritanceTypeJoined())
+                    ? new MultiTableDeleteExecutor($AST, $this)
+                    : new SingleTableDeleteUpdateExecutor($AST, $this);
+            default:
+                throw new \Gedmo\Exception\UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
         }
-        
-        return parent::getExecutor($AST);
     }
 
     /**

+ 13 - 0
tests/Gedmo/SoftDeleteable/Fixture/Entity/MegaPage.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace SoftDeleteable\Fixture\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @ORM\Entity
+ */
+class MegaPage extends Page
+{
+}

+ 70 - 0
tests/Gedmo/SoftDeleteable/Fixture/Entity/Module.php

@@ -0,0 +1,70 @@
+<?php
+
+namespace SoftDeleteable\Fixture\Entity;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ * @Gedmo\SoftDeleteable(fieldName="deletedAt")
+ */
+class Module
+{
+    /**
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="IDENTITY")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="title", type="string")
+     */
+    private $title;
+
+    /**
+     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
+     */
+    private $deletedAt;
+
+    /**
+     * @ORM\ManyToOne(targetEntity="Page", inversedBy="modules")
+     */
+    private $page;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setPage(Page $page)
+    {
+        $this->page = $page;
+    }
+
+    public function getPage()
+    {
+        return $this->page;
+    }
+
+    public function setDeletedAt($deletedAt)
+    {
+        $this->deletedAt = $deletedAt;
+    }
+
+    public function getDeletedAt()
+    {
+        return $this->deletedAt;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+}

+ 75 - 0
tests/Gedmo/SoftDeleteable/Fixture/Entity/Page.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace SoftDeleteable\Fixture\Entity;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @ORM\Entity
+ * @ORM\InheritanceType("JOINED")
+ * @ORM\DiscriminatorColumn(name="discr", type="string")
+ * @ORM\DiscriminatorMap({"page" = "Page", "mega_page" = "MegaPage"})
+ * @Gedmo\SoftDeleteable(fieldName="deletedAt")
+ */
+class Page
+{
+    /**
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="IDENTITY")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="title", type="string")
+     */
+    private $title;
+
+    /**
+     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
+     */
+    private $deletedAt;
+
+    /**
+     * @ORM\OneToMany(targetEntity="Module", mappedBy="page", cascade={"persist", "remove"})
+     */
+    private $modules;
+
+
+    public function __construct()
+    {
+        $this->modules = new ArrayCollection();
+    }
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function setDeletedAt($deletedAt)
+    {
+        $this->deletedAt = $deletedAt;
+    }
+
+    public function getDeletedAt()
+    {
+        return $this->deletedAt;
+    }
+
+    public function addModule(Module $module)
+    {
+        $this->module[] = $module;
+    }
+}

+ 49 - 1
tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

@@ -7,6 +7,9 @@ use Doctrine\Common\EventManager;
 use Doctrine\Common\Util\Debug,
     SoftDeleteable\Fixture\Entity\Article,
     SoftDeleteable\Fixture\Entity\Comment,
+    SoftDeleteable\Fixture\Entity\Page,
+    SoftDeleteable\Fixture\Entity\MegaPage,
+    SoftDeleteable\Fixture\Entity\Module,
     Gedmo\SoftDeleteable\SoftDeleteableListener;
 
 /**
@@ -22,6 +25,9 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
 {
     const ARTICLE_CLASS = 'SoftDeleteable\Fixture\Entity\Article';
     const COMMENT_CLASS = 'SoftDeleteable\Fixture\Entity\Comment';
+    const PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\Page';
+    const MEGA_PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\MegaPage';
+    const MODULE_CLASS = 'SoftDeleteable\Fixture\Entity\Module';
     const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable';
 
     private $softDeleteableListener;
@@ -55,7 +61,6 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
         $art0->addComment($comment);
 
         $this->em->persist($art0);
-
         $this->em->flush();
         
         $art = $repo->findOneBy(array($field => $value));
@@ -114,12 +119,55 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
         $this->assertTrue(is_object($art));
         $this->assertTrue(is_object($art->getDeletedAt()));
         $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
+
+        /*
+        // Inheritance tree DELETE DQL
+        $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
+        
+        $megaPageRepo = $this->em->getRepository(self::MEGA_PAGE_CLASS);
+        $module = new Module();
+        $module->setTitle('Module 1');
+        $page = new MegaPage();
+        $page->setTitle('Page 1');
+        $page->addModule($module);
+        $module->setPage($page);
+
+        $this->em->persist($page);
+        $this->em->persist($module);
+        $this->em->flush();
+        
+        $dql = sprintf('DELETE FROM %s p',
+            self::PAGE_CLASS);
+        $query = $this->em->createQuery($dql);
+        $query->setHint(
+            \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
+            'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
+        );
+        
+        $query->execute();
+
+        $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
+        $this->assertNull($p);
+
+        // Now we deactivate the filter so we test if the entity appears in the result
+        $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
+        $this->em->clear();
+
+        $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
+
+        $this->assertTrue(is_object($p));
+        $this->assertTrue(is_object($p->getDeletedAt()));
+        $this->assertTrue($p->getDeletedAt() instanceof \DateTime);
+        */
     }
 
     protected function getUsedEntityFixtures()
     {
         return array(
             self::ARTICLE_CLASS,
+            self::PAGE_CLASS,
+            self::MEGA_PAGE_CLASS,
+            self::MODULE_CLASS,
             self::COMMENT_CLASS
         );
     }