Ver código fonte

[Tree] Materialized Path (MongoDB): Added some methods to the repository and unit tests.

comfortablynumb 13 anos atrás
pai
commit
1863d5cd2c

+ 96 - 14
lib/Gedmo/Tree/Document/MongoDB/Repository/MaterializedPathRepository.php

@@ -4,7 +4,8 @@ namespace Gedmo\Tree\Document\MongoDB\Repository;
 
 use Gedmo\Exception\InvalidArgumentException,
     Gedmo\Tree\Strategy,
-    Gedmo\Tree\Strategy\ODM\MongoDB\MaterializedPath;
+    Gedmo\Tree\Strategy\ODM\MongoDB\MaterializedPath,
+    Gedmo\Tool\Wrapper\MongoDocumentWrapper;
 
 /**
  * The MaterializedPathRepository has some useful functions
@@ -20,43 +21,124 @@ use Gedmo\Exception\InvalidArgumentException,
  */
 class MaterializedPathRepository extends AbstractTreeRepository
 {
+    /**
+     * Get tree query builder
+     *
+     * @return Doctrine\ODM\MongoDB\QueryBuilder
+     */
+    public function getTreeQueryBuilder()
+    {
+        return $this->getChildrenQueryBuilder();
+    }
+
+    /**
+     * Get tree query
+     *
+     * @return Doctrine\ODM\MongoDB\Query\Query
+     */
+    public function getTreeQuery()
+    {
+        return $this->getTreeQueryBuilder()->getQuery();
+    }
+
+    /**
+     * Get tree
+     *
+     * @return Doctrine\ODM\MongoDB\Cursor
+     */
+    public function getTree()
+    {
+        return $this->getTreeQuery()->execute();
+    }
+
     /**
      * Get all root nodes query builder
      *
      * @return Doctrine\ODM\MongoDB\QueryBuilder
      */
-    public function getRootNodesQueryBuilder()
+    public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc')
+    {
+        return $this->getChildrenQueryBuilder(null, true, $sortByField, $direction);
+    }
+
+    /**
+     * Get all root nodes query
+     *
+     * @return Doctrine\ODM\MongoDB\Query\Query
+     */
+    public function getRootNodesQuery($sortByField = null, $direction = 'asc')
+    {
+        return $this->getRootNodesQueryBuilder($sortByField, $direction)->getQuery();
+    }
+
+    /**
+     * Get all root nodes
+     *
+     * @return Doctrine\ODM\MongoDB\Cursor
+     */
+    public function getRootNodes($sortByField = null, $direction = 'asc')
+    {
+        return $this->getRootNodesQuery($sortByField, $direction)->execute();
+    }
+
+    /**
+     * Get children from node
+     *
+     * @return Doctrine\ODM\MongoDB\QueryBuilder
+     */
+    public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     {
         $meta = $this->getClassMetadata();
         $config = $this->listener->getConfiguration($this->dm, $meta->name);
         $separator = preg_quote($config['path_separator']);
-        
-        return $this->dm->createQueryBuilder()
-            ->find($meta->name)
-            ->field($config['path'])->equals(new \MongoRegex(sprintf('/^[^%s]+%s{1}$/u',
+        $qb = $this->dm->createQueryBuilder()
+            ->find($meta->name);
+
+        if (is_object($node) && $node instanceof $meta->name) {
+            $node = new MongoDocumentWrapper($node, $this->dm);
+            $nodePath = preg_quote($node->getPropertyValue($config['path']));
+
+            if ($direct) {
+                $regex = sprintf('/^%s[^%s]+%s$/',
+                     $nodePath,
+                     $separator,
+                     $separator);
+                
+            } else {
+                $regex = sprintf('/^%s.+/',
+                     $nodePath);
+            }
+
+            $qb->field($config['path'])->equals(new \MongoRegex($regex));
+        } else if ($direct) {
+            $qb->field($config['path'])->equals(new \MongoRegex(sprintf('/^[^%s]+%s$/',
                 $separator,
-                $separator)))
-            ->sort($config['path'], 'asc');
+                $separator)));
+        }
+
+        $qb->sort(is_null($sortByField) ? $config['path'] : $sortByField, $direction === 'asc' ? 'asc' : 'desc');
+
+        return $qb;
     }
 
     /**
-     * Get all root nodes query
+     * Get children query
      *
      * @return Doctrine\ODM\MongoDB\Query\Query
      */
-    public function getRootNodesQuery()
+    public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     {
-        return $this->getRootNodesQueryBuilder()->getQuery();
+        return $this->getChildrenQueryBuilder($node, $direct, $sortByField, $direction)->getQuery();
     }
 
     /**
-     * Get all root nodes
+     * Get children
      *
      * @return Doctrine\ODM\MongoDB\Cursor
      */
-    public function getRootNodes()
+    public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     {
-        return $this->getRootNodesQuery()->execute();
+        return $this->getChildrenQuery($node, $direct, $sortByField, $direction)->execute();
     }
 
     /**

+ 144 - 0
tests/Gedmo/Tree/MaterializedPathODMMongoDBRepositoryTest.php

@@ -0,0 +1,144 @@
+<?php
+
+namespace Gedmo\Tree;
+
+use Doctrine\Common\EventManager;
+use Tool\BaseTestCaseMongoODM;
+use Tree\Fixture\RootCategory;
+
+/**
+ * These are tests for Tree behavior
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Tree
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class MaterializedPathODMMongoDBRepositoryTest extends BaseTestCaseMongoODM
+{
+    const CATEGORY = "Tree\\Fixture\\Document\\Category";
+
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $evm = new EventManager;
+        $evm->addEventSubscriber(new TreeListener);
+
+        $this->getMockDocumentManager($evm);
+        $this->populate();
+    }
+
+    /**
+     * @test
+     */
+    function getRootNodes()
+    {
+        $repo = $this->dm->getRepository(self::CATEGORY);
+        $result = $repo->getRootNodes('title');
+        
+        $this->assertEquals(2, $result->count());
+        $this->assertEquals('Food', $result->getNext()->getTitle());
+        $this->assertEquals('Sports', $result->getNext()->getTitle());
+    }
+
+    /**
+     * @test
+     */
+    function getChildren()
+    {
+        $repo = $this->dm->getRepository(self::CATEGORY);
+        $root = $repo->findOneByTitle('Food');
+
+        // Get all children from the root
+        $result = $repo->getChildren($root, false, 'title');
+
+        $this->assertEquals(4, count($result));
+        $this->assertEquals('Carrots', $result->getNext()->getTitle());
+        $this->assertEquals('Fruits', $result->getNext()->getTitle());
+        $this->assertEquals('Potatoes', $result->getNext()->getTitle());
+        $this->assertEquals('Vegitables', $result->getNext()->getTitle());
+
+        // Get direct children from the root
+        $result = $repo->getChildren($root, true, 'title');
+
+        $this->assertEquals(2, $result->count());
+        $this->assertEquals('Fruits', $result->getNext()->getTitle());
+        $this->assertEquals('Vegitables', $result->getNext()->getTitle());
+
+        // Get ALL nodes
+        $result = $repo->getChildren(null, false, 'title');
+
+        $this->assertEquals(6, $result->count());
+        $this->assertEquals('Carrots', $result->getNext()->getTitle());
+        $this->assertEquals('Food', $result->getNext()->getTitle());
+        $this->assertEquals('Fruits', $result->getNext()->getTitle());
+        $this->assertEquals('Potatoes', $result->getNext()->getTitle());
+        $this->assertEquals('Sports', $result->getNext()->getTitle());
+        $this->assertEquals('Vegitables', $result->getNext()->getTitle());
+    }
+
+    /**
+     * @test
+     */
+    function getTree()
+    {
+        $repo = $this->dm->getRepository(self::CATEGORY);
+        $tree = $repo->getTree();
+
+        $this->assertEquals(6, $tree->count());
+        $this->assertEquals('Food', $tree->getNext()->getTitle());
+        $this->assertEquals('Fruits', $tree->getNext()->getTitle());
+        $this->assertEquals('Vegitables', $tree->getNext()->getTitle());
+        $this->assertEquals('Carrots', $tree->getNext()->getTitle());
+        $this->assertEquals('Potatoes', $tree->getNext()->getTitle());
+        $this->assertEquals('Sports', $tree->getNext()->getTitle());
+    }
+
+    protected function getUsedEntityFixtures()
+    {
+        return array(
+            self::CATEGORY
+        );
+    }
+
+    public function createCategory()
+    {
+        $class = self::CATEGORY;
+        return new $class;
+    }
+
+    private function populate()
+    {
+        $root = $this->createCategory();
+        $root->setTitle("Food");
+
+        $root2 = $this->createCategory();
+        $root2->setTitle("Sports");
+
+        $child = $this->createCategory();
+        $child->setTitle("Fruits");
+        $child->setParent($root);
+
+        $child2 = $this->createCategory();
+        $child2->setTitle("Vegitables");
+        $child2->setParent($root);
+
+        $childsChild = $this->createCategory();
+        $childsChild->setTitle("Carrots");
+        $childsChild->setParent($child2);
+
+        $potatoes = $this->createCategory();
+        $potatoes->setTitle("Potatoes");
+        $potatoes->setParent($child2);
+
+        $this->dm->persist($root);
+        $this->dm->persist($root2);
+        $this->dm->persist($child);
+        $this->dm->persist($child2);
+        $this->dm->persist($childsChild);
+        $this->dm->persist($potatoes);
+        $this->dm->flush();
+    }
+}

+ 0 - 91
tests/Gedmo/Tree/MaterializedPathRepositoryTest.php

@@ -1,91 +0,0 @@
-<?php
-
-namespace Gedmo\Tree;
-
-use Doctrine\Common\EventManager;
-use Tool\BaseTestCaseMongoODM;
-use Tree\Fixture\RootCategory;
-
-/**
- * These are tests for Tree behavior
- *
- * @author Gustavo Falco <comfortablynumb84@gmail.com>
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @package Gedmo.Tree
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
-class MaterializedPathRepositoryTest extends BaseTestCaseMongoODM
-{
-    const CATEGORY = "Tree\\Fixture\\Document\\Category";
-
-    protected function setUp()
-    {
-        parent::setUp();
-
-        $evm = new EventManager;
-        $evm->addEventSubscriber(new TreeListener);
-
-        $this->getMockDocumentManager($evm);
-        $this->populate();
-    }
-
-    /**
-     * @test
-     */
-    function getRootNodes()
-    {
-        $repo = $this->dm->getRepository(self::CATEGORY);
-        $result = $repo->getRootNodesQueryBuilder()->sort('title', 'asc')->getQuery()->execute();
-        
-        $this->assertEquals(2, count($result));
-        $this->assertEquals('Food', $result->getNext()->getTitle());
-        $this->assertEquals('Sports', $result->getNext()->getTitle());
-    }
-
-    protected function getUsedEntityFixtures()
-    {
-        return array(
-            self::CATEGORY
-        );
-    }
-
-    public function createCategory()
-    {
-        $class = self::CATEGORY;
-        return new $class;
-    }
-
-    private function populate()
-    {
-        $root = $this->createCategory();
-        $root->setTitle("Food");
-
-        $root2 = $this->createCategory();
-        $root2->setTitle("Sports");
-
-        $child = $this->createCategory();
-        $child->setTitle("Fruits");
-        $child->setParent($root);
-
-        $child2 = $this->createCategory();
-        $child2->setTitle("Vegitables");
-        $child2->setParent($root);
-
-        $childsChild = $this->createCategory();
-        $childsChild->setTitle("Carrots");
-        $childsChild->setParent($child2);
-
-        $potatoes = $this->createCategory();
-        $potatoes->setTitle("Potatoes");
-        $potatoes->setParent($child2);
-
-        $this->dm->persist($root);
-        $this->dm->persist($root2);
-        $this->dm->persist($child);
-        $this->dm->persist($child2);
-        $this->dm->persist($childsChild);
-        $this->dm->persist($potatoes);
-        $this->dm->flush();
-    }
-}