Browse Source

[SoftDeleteable] Added mapping tests

comfortablynumb 13 years ago
parent
commit
fabe5bb842

+ 2 - 2
lib/Gedmo/SoftDeleteable/Mapping/Driver/Xml.php

@@ -35,8 +35,8 @@ class Xml extends BaseXml
         $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
 
         if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
-            if (isset($xmlDoctrine->soft_deleteable)) {
-                $field = $this->_getAttribute($xmlDoctrine, 'field-name');
+            if (isset($xml->{'soft-deleteable'})) {
+                $field = $this->_getAttribute($xml->{'soft-deleteable'}, 'field-name');
 
                 if (!$field) {
                     throw new InvalidMappingException('Field name for SoftDeleteable class is mandatory.');

+ 5 - 0
schemas/orm/doctrine-extensions-mapping-2-2.xsd

@@ -26,6 +26,7 @@ people to push their own additional attributes/elements into the same field elem
   <xs:element name="tree" type="gedmo:tree"/>
   <xs:element name="tree-closure" type="gedmo:tree-closure"/>
   <xs:element name="loggable" type="gedmo:loggable"/>
+  <xs:element name="soft-deleteable" type="gedmo:soft-deleteable"/>
   <!-- field -->
   <xs:element name="slug" type="gedmo:slug"/>
   <xs:element name="translatable" type="gedmo:emptyType"/>
@@ -67,6 +68,10 @@ people to push their own additional attributes/elements into the same field elem
     <xs:attribute name="separator" type="xs:string" use="optional" />
     <xs:attribute name="style" type="gedmo:slug-style" use="optional" />
   </xs:complexType>
+
+  <xs:complexType name="soft-deleteable">
+    <xs:attribute name="field-name" type="xs:string" use="required" />
+  </xs:complexType>
   
   <xs:complexType name="handler">
     <xs:sequence>

+ 14 - 0
tests/Gedmo/Mapping/Driver/Xml/Mapping.Fixture.Xml.SoftDeleteable.dcm.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
+                  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
+    <entity name="Mapping\Fixture\Xml\SoftDeleteable" table="soft_deleteables">
+        <id name="id" type="integer" column="id">
+            <generator strategy="AUTO"/>
+        </id>
+
+        <field name="deletedAt" type="datetime" nullable="true"/>
+
+        <gedmo:soft-deleteable field-name="deletedAt"/>
+    </entity>
+</doctrine-mapping>

+ 16 - 0
tests/Gedmo/Mapping/Driver/Yaml/Mapping.Fixture.Yaml.SoftDeleteable.dcm.yml

@@ -0,0 +1,16 @@
+---
+Mapping\Fixture\Yaml\SoftDeleteable:
+  type: entity
+  table: soft_deleteables
+  gedmo:
+    soft_deleteable:
+      field_name: deletedAt
+  id:
+    id:
+      type: integer
+      generator:
+        strategy: AUTO
+  fields:
+    deletedAt:
+      type: datetime
+      nullable: true

+ 55 - 0
tests/Gedmo/Mapping/Fixture/SoftDeleteable.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace Mapping\Fixture;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ * @Gedmo\SoftDeleteable(fieldName="deletedAt")
+ */
+class SoftDeleteable
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
+     */
+    private $deletedAt;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function setCode($code)
+    {
+        $this->code = $code;
+    }
+
+    public function getCode()
+    {
+        return $this->code;
+    }
+
+    public function getSlug()
+    {
+        return $this->slug;
+    }
+}

+ 10 - 0
tests/Gedmo/Mapping/Fixture/Xml/SoftDeleteable.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace Mapping\Fixture\Xml;
+
+class SoftDeleteable
+{
+    private $id;
+
+    private $deletedAt;
+}

+ 10 - 0
tests/Gedmo/Mapping/Fixture/Yaml/SoftDeleteable.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace Mapping\Fixture\Yaml;
+
+class SoftDeleteable
+{
+    private $id;
+
+    private $deletedAt;
+}

+ 67 - 0
tests/Gedmo/Mapping/SoftDeleteableMappingTest.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace Gedmo\Mapping;
+
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
+use Doctrine\Common\EventManager;
+use Doctrine\ORM\Mapping\Driver\DriverChain;
+use Doctrine\ORM\Mapping\Driver\YamlDriver;
+use Gedmo\SoftDeleteable\SoftDeleteableListener;
+use Tool\BaseTestCaseOM;
+
+/**
+ * These are mapping tests for SoftDeleteable extension
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Mapping
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class SoftDeleteableMappingTest extends BaseTestCaseOM
+{
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
+    private $em;
+
+    /**
+     * @var Gedmo\SoftDeleteable\SoftDeleteableListener
+     */
+    private $softDeleteable;
+
+    public function setUp()
+    {
+        parent::setUp();
+        
+        $reader = new AnnotationReader();
+        $annotationDriver = new AnnotationDriver($reader);
+
+        $yamlDriver = new YamlDriver(__DIR__.'/Driver/Yaml');
+
+        $chain = new DriverChain;
+        $chain->addDriver($yamlDriver, 'Mapping\Fixture\Yaml');
+        $chain->addDriver($annotationDriver, 'Mapping\Fixture');
+
+        $this->softDeleteable = new SoftDeleteableListener();
+        $this->evm = new EventManager;
+        $this->evm->addEventSubscriber($this->softDeleteable);
+
+        $this->em = $this->getMockSqliteEntityManager(array(
+            'Mapping\Fixture\Yaml\SoftDeleteable',
+            'Mapping\Fixture\SoftDeleteable'
+        ), $chain);
+    }
+
+    public function testYamlMapping()
+    {
+        $meta = $this->em->getClassMetadata('Mapping\Fixture\Yaml\SoftDeleteable');
+        $config = $this->softDeleteable->getConfiguration($this->em, $meta->name);
+        
+        $this->assertArrayHasKey('softDeleteable', $config);
+        $this->assertTrue($config['softDeleteable']);
+        $this->assertArrayHasKey('fieldName', $config);
+        $this->assertEquals('deletedAt', $config['fieldName']);
+    }
+}

+ 67 - 0
tests/Gedmo/Mapping/Xml/SoftDeleteableMappingTest.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace Gedmo\Mapping\Xml;
+
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
+use Doctrine\Common\EventManager;
+use Doctrine\ORM\Mapping\Driver\DriverChain;
+use Doctrine\ORM\Mapping\Driver\XmlDriver;
+use Gedmo\SoftDeleteable\SoftDeleteableListener;
+use Tool\BaseTestCaseOM;
+
+/**
+ * These are mapping tests for SoftDeleteable extension
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Mapping
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class SoftDeleteableMappingTest extends BaseTestCaseOM
+{
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
+    private $em;
+
+    /**
+     * @var Gedmo\SoftDeleteable\SoftDeleteableListener
+     */
+    private $softDeleteable;
+
+    public function setUp()
+    {
+        parent::setUp();
+        
+        $reader = new AnnotationReader();
+        $annotationDriver = new AnnotationDriver($reader);
+
+        $xmlDriver = new XmlDriver(__DIR__.'/../Driver/Xml');
+
+        $chain = new DriverChain;
+        $chain->addDriver($xmlDriver, 'Mapping\Fixture\Xml');
+        $chain->addDriver($annotationDriver, 'Mapping\Fixture');
+
+        $this->softDeleteable = new SoftDeleteableListener;
+        $this->evm = new EventManager;
+        $this->evm->addEventSubscriber($this->softDeleteable);
+
+        $this->em = $this->getMockSqliteEntityManager(array(
+            'Mapping\Fixture\Xml\SoftDeleteable',
+            'Mapping\Fixture\SoftDeleteable'
+        ), $chain);
+    }
+
+    public function testMetadata()
+    {
+        $meta = $this->em->getClassMetadata('Mapping\Fixture\Xml\SoftDeleteable');
+        $config = $this->softDeleteable->getConfiguration($this->em, $meta->name);
+
+        $this->assertArrayHasKey('softDeleteable', $config);
+        $this->assertTrue($config['softDeleteable']);
+        $this->assertArrayHasKey('fieldName', $config);
+        $this->assertEquals('deletedAt', $config['fieldName']);
+    }
+}