Просмотр исходного кода

add test for mapped superclass softdeleteable. refs #498

David Buchmann 12 лет назад
Родитель
Сommit
d3e57fda05

+ 29 - 0
tests/Gedmo/SoftDeleteable/Fixture/Entity/Child.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace SoftDeleteable\Fixture\Entity;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @ORM\Entity
+ */
+class Child extends MappedSuperclass
+{
+    /**
+     * @ORM\Column(name="title", type="string")
+     */
+    private $title;
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+}

+ 40 - 0
tests/Gedmo/SoftDeleteable/Fixture/Entity/MappedSuperclass.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace SoftDeleteable\Fixture\Entity;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\MappedSuperclass
+ * @Gedmo\SoftDeleteable(fieldName="deletedAt")
+ */
+class MappedSuperclass
+{
+    /**
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="IDENTITY")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
+     */
+    private $deletedAt;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setDeletedAt($deletedAt)
+    {
+        $this->deletedAt = $deletedAt;
+    }
+
+    public function getDeletedAt()
+    {
+        return $this->deletedAt;
+    }
+}

+ 30 - 7
tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

@@ -13,6 +13,7 @@ use Doctrine\Common\Util\Debug,
     SoftDeleteable\Fixture\Entity\Module,
     SoftDeleteable\Fixture\Entity\OtherArticle,
     SoftDeleteable\Fixture\Entity\OtherComment,
+    SoftDeleteable\Fixture\Entity\Child,
     Gedmo\SoftDeleteable\SoftDeleteableListener;
 
 /**
@@ -35,6 +36,7 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
     const OTHER_ARTICLE_CLASS = 'SoftDeleteable\Fixture\Entity\OtherArticle';
     const OTHER_COMMENT_CLASS = 'SoftDeleteable\Fixture\Entity\OtherComment';
     const USER_CLASS = 'SoftDeleteable\Fixture\Entity\User';
+    const MAPPED_SUPERCLASS_CHILD_CLASS = 'SoftDeleteable\Fixture\Entity\Child';
     const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable';
 
     private $softDeleteableListener;
@@ -94,7 +96,7 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
 
         $this->em->persist($art0);
         $this->em->flush();
-        
+
         $art = $repo->findOneBy(array($field => $value));
 
         $this->assertNull($art->getDeletedAt());
@@ -137,7 +139,7 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
             \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
             'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
         );
-        
+
         $query->execute();
 
         $art = $repo->findOneBy(array($field => $value));
@@ -146,9 +148,9 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
         // 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();
-        
+
         $art = $repo->findOneBy(array($field => $value));
-        
+
         $this->assertTrue(is_object($art));
         $this->assertTrue(is_object($art->getDeletedAt()));
         $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
@@ -156,7 +158,7 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
 
         // 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');
@@ -168,7 +170,7 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
         $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);
@@ -237,6 +239,27 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
 
     }
 
+    /**
+     * Make sure that soft delete also works when configured on a mapped superclass
+     */
+    public function testMappedSuperclass()
+    {
+        $child = new Child();
+        $child->setTitle('test title');
+
+        $this->em->persist($child);
+        $this->em->flush();
+
+        $this->em->remove($child);
+        $this->em->clear();
+
+        $repo = $this->em->getRepository(self::MAPPED_SUPERCLASS_CHILD_CLASS);
+        $this->assertNull($repo->findById($child->getId()));
+
+        $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
+        $this->assertNotNull($repo->findById($child->getId()));
+    }
+
     public function testSoftDeleteableFilter()
     {
         $filter = $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
@@ -307,7 +330,7 @@ class SoftDeleteableEntityTest extends BaseTestCaseORM
 
         $this->em->persist($art0);
         $this->em->flush();
-        
+
         $art = $repo->findOneBy(array($field => $value));
 
         $this->assertNull($art->getDeletedAt());