Browse Source

[Tree] Materialized Path (ODM): Minor fixes and added some tests.

comfortablynumb 13 years ago
parent
commit
4875c9b20d

+ 1 - 1
lib/Gedmo/Tree/Document/MongoDB/Repository/AbstractTreeRepository.php

@@ -84,7 +84,7 @@ abstract class AbstractTreeRepository extends DocumentRepository implements Repo
      */
      */
     public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
     public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
     {
     {
-        return $this->repoUtils->childrenHierarchy($node, $direct, $options);
+        return $this->repoUtils->childrenHierarchy($node, $direct, $options, $includeNode);
     }
     }
 
 
     /**
     /**

+ 10 - 7
lib/Gedmo/Tree/Document/MongoDB/Repository/MaterializedPathRepository.php

@@ -91,6 +91,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
         $separator = preg_quote($config['path_separator']);
         $separator = preg_quote($config['path_separator']);
         $qb = $this->dm->createQueryBuilder()
         $qb = $this->dm->createQueryBuilder()
             ->find($meta->name);
             ->find($meta->name);
+        $regex = false;
 
 
         if (is_object($node) && $node instanceof $meta->name) {
         if (is_object($node) && $node instanceof $meta->name) {
             $node = new MongoDocumentWrapper($node, $this->dm);
             $node = new MongoDocumentWrapper($node, $this->dm);
@@ -106,12 +107,14 @@ class MaterializedPathRepository extends AbstractTreeRepository
                 $regex = sprintf('/^%s(.+)'.($includeNode ? '?' : '').'/',
                 $regex = sprintf('/^%s(.+)'.($includeNode ? '?' : '').'/',
                      $nodePath);
                      $nodePath);
             }
             }
-
-            $qb->field($config['path'])->equals(new \MongoRegex($regex));
         } else if ($direct) {
         } else if ($direct) {
-            $qb->field($config['path'])->equals(new \MongoRegex(sprintf('/^([^%s]+)'.($includeNode ? '?' : '').'%s$/',
+            $regex = sprintf('/^([^%s]+)'.($includeNode ? '?' : '').'%s$/',
                 $separator,
                 $separator,
-                $separator)));
+                $separator);
+        }
+
+        if ($regex) {
+            $qb->field($config['path'])->equals(new \MongoRegex($regex));
         }
         }
 
 
         $qb->sort(is_null($sortByField) ? $config['path'] : $sortByField, $direction === 'asc' ? 'asc' : 'desc');
         $qb->sort(is_null($sortByField) ? $config['path'] : $sortByField, $direction === 'asc' ? 'asc' : 'desc');
@@ -149,7 +152,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
             $sortBy = array_merge($sortBy, $options['childSort']);
             $sortBy = array_merge($sortBy, $options['childSort']);
         }
         }
 
 
-        return $this->getChildrenQueryBuilder($node, $direct, $sortBy['field'], $sortBy['dir'], true);
+        return $this->getChildrenQueryBuilder($node, $direct, $sortBy['field'], $sortBy['dir'], $includeNode);
     }
     }
 
 
     /**
     /**
@@ -157,7 +160,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
      */
      */
     public function getNodesHierarchyQuery($node = null, $direct, array $config, array $options = array(), $includeNode = false)
     public function getNodesHierarchyQuery($node = null, $direct, array $config, array $options = array(), $includeNode = false)
     {
     {
-        return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options)->getQuery();
+        return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options, $includeNode)->getQuery();
     }
     }
 
 
     /**
     /**
@@ -165,7 +168,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
      */
      */
     public function getNodesHierarchy($node = null, $direct, array $config, array $options = array(), $includeNode = false)
     public function getNodesHierarchy($node = null, $direct, array $config, array $options = array(), $includeNode = false)
     {
     {
-        $query = $this->getNodesHierarchyQuery($node, $direct, $config, $options);
+        $query = $this->getNodesHierarchyQuery($node, $direct, $config, $options, $includeNode);
         $query->setHydrate(false);
         $query->setHydrate(false);
 
 
         return $query->toArray();
         return $query->toArray();

+ 40 - 1
tests/Gedmo/Tree/MaterializedPathODMMongoDBRepositoryTest.php

@@ -160,11 +160,50 @@ class MaterializedPathODMMongoDBRepositoryTest extends BaseTestCaseMongoODM
 
 
         // Tree of one specific root
         // Tree of one specific root
         $roots = $repo->getRootNodes();
         $roots = $repo->getRootNodes();
-        $tree = $repo->childrenHierarchy($roots->getNext());
+        $drinks = $roots->getNext();
+        $food = $roots->getNext();
+        $tree = $repo->childrenHierarchy();
 
 
         $this->assertEquals('Drinks', $tree[0]['title']);
         $this->assertEquals('Drinks', $tree[0]['title']);
         $this->assertEquals('Whisky', $tree[0]['__children'][0]['title']);
         $this->assertEquals('Whisky', $tree[0]['__children'][0]['title']);
         $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['__children'][0]['title']);
         $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['__children'][0]['title']);
+
+        // Tree of one specific root, with the root node
+        $tree = $repo->childrenHierarchy($drinks, false, array(), true);
+
+        $this->assertEquals('Drinks', $tree[0]['title']);
+        $this->assertEquals('Whisky', $tree[0]['__children'][0]['title']);
+        $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['__children'][0]['title']);
+
+        // Tree of one specific root only with direct children, without the root node
+        $roots = $repo->getRootNodes();
+        $tree = $repo->childrenHierarchy($food, true);
+
+        $this->assertEquals(2, count($tree));
+        $this->assertEquals('Fruits', $tree[0]['title']);
+        $this->assertEquals('Vegitables', $tree[1]['title']);
+
+        // Tree of one specific root only with direct children, with the root node
+        $tree = $repo->childrenHierarchy($food, true, array(), true);
+
+        $this->assertEquals(1, count($tree));
+        $this->assertEquals(2, count($tree[0]['__children']));
+        $this->assertEquals('Food', $tree[0]['title']);
+        $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']);
+        $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']);
+
+        // HTML Tree of one specific root, without the root node
+        $roots = $repo->getRootNodes();
+        $tree = $repo->childrenHierarchy($drinks, false, array('decorate' => true), false);
+
+        $this->assertEquals('<ul><li>Whisky<ul><li>Best Whisky</li></ul></li></ul>', $tree);
+
+
+        // HTML Tree of one specific root, with the root node
+        $roots = $repo->getRootNodes();
+        $tree = $repo->childrenHierarchy($drinks, false, array('decorate' => true), true);
+
+        $this->assertEquals('<ul><li>Drinks<ul><li>Whisky<ul><li>Best Whisky</li></ul></li></ul></li></ul>', $tree);
     }
     }
 
 
     protected function getUsedEntityFixtures()
     protected function getUsedEntityFixtures()

+ 30 - 0
tests/Gedmo/Tree/MaterializedPathORMRepositoryTest.php

@@ -173,6 +173,36 @@ class MaterializedPathORMRepositoryTest extends BaseTestCaseORM
         $this->assertEquals('Drinks', $tree[0]['title']);
         $this->assertEquals('Drinks', $tree[0]['title']);
         $this->assertEquals('Whisky', $tree[0]['__children'][0]['title']);
         $this->assertEquals('Whisky', $tree[0]['__children'][0]['title']);
         $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['__children'][0]['title']);
         $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['__children'][0]['title']);
+
+        // Tree of one specific root only with direct children, without the root node
+        $roots = $repo->getRootNodes();
+        $tree = $repo->childrenHierarchy($roots[1], true);
+
+        $this->assertEquals(2, count($tree));
+        $this->assertEquals('Fruits', $tree[0]['title']);
+        $this->assertEquals('Vegitables', $tree[1]['title']);
+
+        // Tree of one specific root only with direct children, with the root node
+        $tree = $repo->childrenHierarchy($roots[1], true, array(), true);
+
+        $this->assertEquals(1, count($tree));
+        $this->assertEquals(2, count($tree[0]['__children']));
+        $this->assertEquals('Food', $tree[0]['title']);
+        $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']);
+        $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']);
+
+        // HTML Tree of one specific root, without the root node
+        $roots = $repo->getRootNodes();
+        $tree = $repo->childrenHierarchy($roots[0], false, array('decorate' => true), false);
+
+        $this->assertEquals('<ul><li>Whisky<ul><li>Best Whisky</li></ul></li></ul>', $tree);
+
+
+        // HTML Tree of one specific root, with the root node
+        $roots = $repo->getRootNodes();
+        $tree = $repo->childrenHierarchy($roots[0], false, array('decorate' => true), true);
+
+        $this->assertEquals('<ul><li>Drinks<ul><li>Whisky<ul><li>Best Whisky</li></ul></li></ul></li></ul>', $tree);
     }
     }
 
 
     protected function getUsedEntityFixtures()
     protected function getUsedEntityFixtures()