浏览代码

[Sortable] Yaml mapping driver.

Lukas Botsch 14 年之前
父节点
当前提交
6ad0bd2254

+ 22 - 6
lib/Gedmo/Sortable/Mapping/Driver/Yaml.php

@@ -65,14 +65,30 @@ class Yaml extends File implements Driver
                             throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
                         }
                         $config['position'] = $field;
-                    } elseif (in_array('sortableGroup', $fieldMapping['gedmo'])) {
-                        if (!isset($config['groups'])) {
-                            $config['groups'] = array();
-                        }
-                        $config['groups'][] = $field;
                     }
                 }
             }
+            $this->readSortableGroups($mapping['fields'], $config);
+        }
+        if (isset($mapping['manyToOne'])) {
+            $this->readSortableGroups($mapping['manyToOne'], $config);
+        }
+        if (isset($mapping['manyToMany'])) {
+            $this->readSortableGroups($mapping['manyToMany'], $config);
+        }
+    }
+    
+    private function readSortableGroups($mapping, array &$config)
+    {
+        foreach ($mapping as $field => $fieldMapping) {
+            if (isset($fieldMapping['gedmo'])) {
+                if (in_array('sortableGroup', $fieldMapping['gedmo'])) {
+                    if (!isset($config['groups'])) {
+                        $config['groups'] = array();
+                    }
+                    $config['groups'][] = $field;
+                }
+            }
         }
     }
 
@@ -96,4 +112,4 @@ class Yaml extends File implements Driver
         $mapping = $meta->getFieldMapping($field);
         return $mapping && in_array($mapping['type'], $this->validTypes);
     }
-}
+}

+ 33 - 0
tests/Gedmo/Mapping/Driver/Yaml/Mapping.Fixture.Yaml.Sortable.dcm.yml

@@ -0,0 +1,33 @@
+---
+Mapping\Fixture\Yaml\Sortable:
+  type: entity
+  table: sortables
+  id:
+    id:
+      type: integer
+      generator:
+        strategy: AUTO
+  fields:
+    title:
+      type: string
+      length: 32
+    position:
+      type: integer
+      gedmo:
+        - sortablePosition
+    grouping:
+      type: string
+      length: 128
+      gedmo:
+        - sortableGroup
+  manyToOne:
+    sortable_group:
+      targetEntity: Mapping\Fixture\SortableGroup
+      gedmo:
+        - sortableGroup
+  manyToMany:
+    sortable_groups:
+      targetEntity: Mapping\Fixture\SortableGroup
+      gedmo:
+        - sortableGroup
+

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

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

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

@@ -0,0 +1,69 @@
+<?php
+
+namespace Gedmo\Sluggable;
+
+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\Sortable\SortableListener;
+use Tool\BaseTestCaseOM;
+
+/**
+ * These are mapping tests for sortable extension
+ *
+ * @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);
+
+        $yamlDriver = new YamlDriver(__DIR__.'/Driver/Yaml');
+
+        $chain = new DriverChain;
+        $chain->addDriver($yamlDriver, 'Mapping\Fixture\Yaml');
+        $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\Yaml\Sortable',
+            'Mapping\Fixture\SortableGroup'
+        ), $chain);
+    }
+
+    public function testYamlMapping()
+    {
+        $meta = $this->em->getClassMetadata('Mapping\Fixture\Yaml\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/Mapping/Xml/SortableMappingTest.php

@@ -32,7 +32,7 @@ class SortableMappingTest extends BaseTestCaseOM
 
     public function setUp()
     {
-        //parent::setUp();
+        parent::setUp();
         
         $reader = new AnnotationReader();
         $annotationDriver = new AnnotationDriver($reader);