Browse Source

[tests] updated tests for tree mapping in yaml

Gediminas Morkevicius 14 năm trước cách đây
mục cha
commit
e4d3b73c45

+ 4 - 0
tests/Gedmo/Mapping/Driver/Yaml/Mapping.Fixture.Yaml.BaseCategory.dcm.yml

@@ -10,6 +10,10 @@ Mapping\Fixture\Yaml\BaseCategory:
       type: integer
       gedmo:
         - treeRight
+    root:
+      type: integer
+      gedmo:
+        - treeRoot
     lvl:
       type: integer
       gedmo:

+ 2 - 0
tests/Gedmo/Mapping/Driver/Yaml/Mapping.Fixture.Yaml.Category.dcm.yml

@@ -11,6 +11,8 @@ Mapping\Fixture\Yaml\Category:
     translation:
       entity: Translatable\Fixture\CategoryTranslation
       locale: localeField
+    tree:
+      type: nested
   fields:
     title:
       type: string

+ 34 - 0
tests/Gedmo/Mapping/Driver/Yaml/Mapping.Fixture.Yaml.ClosureCategory.dcm.yml

@@ -0,0 +1,34 @@
+---
+Mapping\Fixture\Yaml\ClosureCategory:
+  type: entity
+  table: closure_categories
+  id:
+    id:
+      type: integer
+      generator:
+        strategy: AUTO
+  gedmo:
+    tree:
+      type: closure
+      closure: Mapping\Fixture\Yaml\BaseCategory
+  fields:
+    title:
+      type: string
+      length: 64
+    childCount:
+      type: integer
+      gedmo:
+        - treeChildCount
+  manyToOne:
+    parent:
+      targetEntity: Mapping\Fixture\Yaml\ClosureCategory
+      inversedBy: children
+      gedmo:
+        - treeParent
+  oneToMany:
+    children:
+      targetEntity: Mapping\Fixture\Yaml\ClosureCategory
+      mappedBy: parent
+  indexes:
+    search_idx:
+      columns: title

+ 7 - 0
tests/Gedmo/Mapping/Fixture/Yaml/BaseCategory.php

@@ -27,6 +27,13 @@ class BaseCategory
      * @Column(name="lvl", type="integer")
      */
     private $lvl;
+    
+    /**
+     * @var integer $root
+     *
+     * @Column(name="root", type="integer")
+     */
+    private $root;
 
     /**
      * @var datetime $created

+ 86 - 0
tests/Gedmo/Mapping/Fixture/Yaml/ClosureCategory.php

@@ -0,0 +1,86 @@
+<?php
+
+namespace Mapping\Fixture\Yaml;
+
+class ClosureCategory
+{
+    private $id;
+
+    private $title;
+    
+    private $children;
+
+    private $parent;
+    
+    private $childCount;
+
+    /**
+     * Get id
+     *
+     * @return integer $id
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * Set title
+     *
+     * @param string $title
+     */
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    /**
+     * Get title
+     *
+     * @return string $title
+     */
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    /**
+     * Add children
+     *
+     * @param Entity\Category $children
+     */
+    public function addChildren(Category $children)
+    {
+        $this->children[] = $children;
+    }
+
+    /**
+     * Get children
+     *
+     * @return Doctrine\Common\Collections\Collection $children
+     */
+    public function getChildren()
+    {
+        return $this->children;
+    }
+
+    /**
+     * Set parent
+     *
+     * @param Entity\Category $parent
+     */
+    public function setParent($parent)
+    {
+        $this->parent = $parent;
+    }
+
+    /**
+     * Get parent
+     *
+     * @return Entity\Category $parent
+     */
+    public function getParent()
+    {
+        return $this->parent;
+    }
+}

+ 0 - 2
tests/Gedmo/Mapping/MappingTest.php

@@ -25,8 +25,6 @@ class MappingTest extends \PHPUnit_Framework_TestCase
     public function setUp()
     {        
         $config = new \Doctrine\ORM\Configuration();
-        //$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
-        //$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
         $config->setProxyDir(__DIR__ . '/Proxy');
         $config->setProxyNamespace('Gedmo\Mapping\Proxy');
         $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());

+ 22 - 7
tests/Gedmo/Mapping/TreeMappingTest.php

@@ -19,6 +19,7 @@ use Doctrine\Common\Util\Debug,
 class TreeMappingTest extends \PHPUnit_Framework_TestCase
 {
     const TEST_YAML_ENTITY_CLASS = 'Mapping\Fixture\Yaml\Category';
+    const YAML_CLOSURE_CATEGORY = 'Mapping\Fixture\Yaml\ClosureCategory';
     private $em;
 
     public function setUp()
@@ -45,15 +46,9 @@ class TreeMappingTest extends \PHPUnit_Framework_TestCase
         $evm = new \Doctrine\Common\EventManager();
         $evm->addEventSubscriber(new TreeListener());
         $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
-
-        $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
-        $schemaTool->dropSchema(array());
-        $schemaTool->createSchema(array(
-            $this->em->getClassMetadata(self::TEST_YAML_ENTITY_CLASS)
-        ));
     }
     
-    public function testYamlMapping()
+    public function testYamlNestedMapping()
     {
         $meta = $this->em->getClassMetadata(self::TEST_YAML_ENTITY_CLASS);
         $cacheId = ExtensionMetadataFactory::getCacheId(
@@ -69,5 +64,25 @@ class TreeMappingTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('parent', $config['parent']);
         $this->assertArrayHasKey('level', $config);
         $this->assertEquals('lvl', $config['level']);
+        $this->assertArrayHasKey('root', $config);
+        $this->assertEquals('root', $config['root']);
+        $this->assertArrayHasKey('strategy', $config);
+        $this->assertEquals('nested', $config['strategy']);
+    }
+    
+    public function testYamlClosureMapping()
+    {
+        $meta = $this->em->getClassMetadata(self::YAML_CLOSURE_CATEGORY);
+        $cacheId = ExtensionMetadataFactory::getCacheId(self::YAML_CLOSURE_CATEGORY, 'Gedmo\Tree');
+        $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId);
+        
+        $this->assertArrayHasKey('parent', $config);
+        $this->assertEquals('parent', $config['parent']);
+        $this->assertArrayHasKey('strategy', $config);
+        $this->assertEquals('closure', $config['strategy']);
+        $this->assertArrayHasKey('closure', $config);
+        $this->assertEquals('Mapping\\Fixture\\Yaml\\BaseCategory', $config['closure']);
+        $this->assertArrayHasKey('childCount', $config);
+        $this->assertEquals('childCount', $config['childCount']);
     }
 }