Prechádzať zdrojové kódy

[Sortable] Xml mapping driver. fix #122

Lukas Botsch 14 rokov pred
rodič
commit
f338808182

+ 33 - 7
lib/Gedmo/Sortable/Mapping/Driver/Xml.php

@@ -67,18 +67,44 @@ class Xml extends BaseXml
                         throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
                     }
                     $config['position'] = $field;
-                } elseif (isset($mapping->{'sortable-group'})) {
-                    if (!isset($config['groups'])) {
-                        $config['groups'] = array();
-                    }
-                    $config['groups'][] = $field;
                 }
             }
+            $this->readSortableGroups($xml->field, $config, 'name');
+        }
+        
+        
+        // Search for sortable-groups in association mappings
+        if (isset($xml->{'many-to-one'})) {
+            $this->readSortableGroups($xml->{'many-to-one'}, $config);
+        }
+        
+        // Search for sortable-groups in association mappings
+        if (isset($xml->{'many-to-many'})) {
+            $this->readSortableGroups($xml->{'many-to-many'}, $config);
+        }
+    }
+
+    private function readSortableGroups($mapping, array &$config, $fieldAttr='field')
+    {
+        foreach ($mapping as $map) {
+            $mappingDoctrine = $map;
+            /**
+             * @var \SimpleXmlElement $mapping
+             */
+            $map = $map->children(self::GEDMO_NAMESPACE_URI);
+            
+            $field = $this->_getAttribute($mappingDoctrine, $fieldAttr);
+            if (isset($map->{'sortable-group'})) {
+                if (!isset($config['groups'])) {
+                    $config['groups'] = array();
+                }
+                $config['groups'][] = $field;
+            }
         }
     }
 
     /**
-     * Checks if $field type is valid as Sluggable field
+     * Checks if $field type is valid as Sortable Position field
      *
      * @param ClassMetadata $meta
      * @param string $field
@@ -89,4 +115,4 @@ class Xml extends BaseXml
         $mapping = $meta->getFieldMapping($field);
         return $mapping && in_array($mapping['type'], $this->validTypes);
     }
-}
+}

+ 37 - 0
tests/Gedmo/Mapping/Driver/Xml/Mapping.Fixture.Xml.Sortable.dcm.xml

@@ -0,0 +1,37 @@
+<?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\Sortable" table="sortables">
+        <id name="id" type="integer" column="id">
+            <generator strategy="AUTO"/>
+        </id>
+
+        <field name="title" type="string" length="128">
+            <gedmo:sluggable position="0"/>
+        </field>
+        <field name="position" type="integer">
+            <gedmo:sortable-position/>
+        </field>
+        <field name="grouping" type="string" length="128">
+            <gedmo:sortable-group/>
+        </field>
+        <many-to-one field="sortable_group" target-entity="Mapping\Fixture\SortableGroup" orphan-removal="true">
+            <join-columns>
+                <join-column name="sortable_group_id" referenced-column-name="id" nullable="false"/>
+            </join-columns>
+            <gedmo:sortable-group/>
+        </many-to-one>
+        <many-to-many field="sortable_groups" target-entity="Mapping\Fixture\SortableGroup">
+            <join-table name="sortable_sortable_groups">
+                <join-columns>
+                    <join-column name="sortable_id" referenced-column-name="id" />
+                </join-columns>
+                <inverse-join-columns>
+                    <join-column name="group_id" referenced-column-name="id" />
+                </inverse-join-columns>
+            </join-table>
+            <gedmo:sortable-group/>
+        </many-to-many>
+    </entity>
+</doctrine-mapping>

+ 24 - 0
tests/Gedmo/Mapping/Fixture/SortableGroup.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Mapping\Fixture;
+
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Table(name="test_sortable_groups")
+ * @ORM\Entity
+ */
+class SortableGroup
+{
+    /**
+     * @ORM\Column(type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     */
+    private $id;
+    
+    /**
+     * @ORM\Column(length=64)
+     */
+    private $name;
+}

+ 18 - 0
tests/Gedmo/Mapping/Fixture/Xml/Sortable.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Mapping\Fixture\Xml;
+
+class Sortable
+{
+    private $id;
+
+    private $title;
+
+    private $position;
+
+    private $grouping;
+    
+    private $sortable_group;
+    
+    private $sortable_groups;
+}

+ 69 - 0
tests/Gedmo/Mapping/Xml/SortableMappingTest.php

@@ -0,0 +1,69 @@
+<?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\Sortable\SortableListener;
+use Tool\BaseTestCaseOM;
+
+/**
+ * These are mapping extension tests
+ *
+ * @author Lukas Botsch <lukas.botsch@gmail.com>
+ * @package Gedmo.Mapping
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class SortableMappingTest extends BaseTestCaseOM
+{
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
+    private $em;
+
+    /**
+     * @var Gedmo\Sortable\SortableListener
+     */
+    private $sortable;
+
+    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->sortable = new SortableListener;
+        $this->evm = new EventManager;
+        $this->evm->addEventSubscriber($this->sortable);
+
+        $this->em = $this->getMockSqliteEntityManager(array(
+            'Mapping\Fixture\Xml\Sortable',
+            'Mapping\Fixture\SortableGroup'
+        ), $chain);
+    }
+
+    public function testSluggableMetadata()
+    {
+        $meta = $this->em->getClassMetadata('Mapping\Fixture\Xml\Sortable');
+        $config = $this->sortable->getConfiguration($this->em, $meta->name);
+
+        $this->assertArrayHasKey('position', $config);
+        $this->assertEquals('position', $config['position']);
+        $this->assertArrayHasKey('groups', $config);
+        $this->assertEquals(3, count($config['groups']));
+        $this->assertEquals('grouping', $config['groups'][0]);
+        $this->assertEquals('sortable_group', $config['groups'][1]);
+        $this->assertEquals('sortable_groups', $config['groups'][2]);
+    }
+}

+ 1 - 1
tests/Gedmo/Sortable/SortableTest.php

@@ -276,4 +276,4 @@ class SortableTest extends BaseTestCaseORM
         $this->em->clear();
         $this->nodeId = $node->getId();
     }
-}
+}

+ 4 - 1
tests/phpunit.xml.dist

@@ -7,6 +7,9 @@
         <testsuite name="Sluggable Extension">
             <directory suffix=".php">./Gedmo/Sluggable/</directory>
         </testsuite>
+        <testsuite name="Sortable Extension">
+            <directory suffix=".php">./Gedmo/Sortable/</directory>
+        </testsuite>
         <testsuite name="Tree Extension">
             <directory suffix=".php">./Gedmo/Tree/</directory>
         </testsuite>
@@ -23,4 +26,4 @@
             <directory suffix=".php">./Gedmo/Wrapper/</directory>
         </testsuite>
     </testsuites>
-</phpunit>
+</phpunit>