Browse Source

[Tree - Closure Table] Added ClosureTreeRepository and unit tests

Gustavo Adrian 14 years ago
parent
commit
b11fee1705

+ 0 - 0
lib/Gedmo/Tree/AbstractTreeListener.php


+ 0 - 0
lib/Gedmo/Tree/AbstractTreeRepository.php


+ 28 - 13
lib/Gedmo/Tree/Entity/Repository/ClosureTreeRepository.php

@@ -25,23 +25,19 @@ class ClosureTreeRepository extends AbstractTreeRepository
     /**
      * Counts the children of given TreeNode
      * 
-     * @param object $node - if null counts all records in tree
+     * @param object $node - The node from which we'll count its children
      * @param boolean $direct - true to count only direct children
      * @return integer
      */ 
-    public function childCount( $node = null, $direct = false )
+    public function childCount( $node, $direct = false )
     {
-        $meta 			= $this->getClassMetadata();
-		$qb 			= $this->_em->createQueryBuilder();
-		$config 		= $this->listener->getConfiguration( $this->_em, $meta->name );
-		$closureMeta	= $this->_em->getClassMetadata( $config[ 'closure' ] );
-		$table			= $closureMeta->getTableName();
-		$nodeID 		= $meta->getSingleIdentifierFieldName();
-		$id				= $meta->getReflectionProperty( $nodeID )->getValue( $node );
-		
+		$meta 	= $this->getClassMetadata();
+		$id		= $this->getIdFromEntity( $node );
+		$qb 	= $this->getQueryBuilder();
 		$qb->select( 'COUNT( c.id )' )
-			->from( $table, 'c' )
-			->where( 'c.ancestor = :node_id' );
+			->from( $meta->rootEntityName, 'c' )
+			->where( 'c.ancestor = :node_id' )
+			->andWhere( 'c.ancestor != c.descendant' );
 		
 		if ( $direct === true )
 		{
@@ -53,11 +49,30 @@ class ClosureTreeRepository extends AbstractTreeRepository
         return $qb->getQuery()->getSingleScalarResult();
     }
 	
+	
+	protected function getQueryBuilder()
+	{
+		$qb = $this->_em->createQueryBuilder();
+		
+		return $qb;
+	}
+	
+	protected function getIdFromEntity( $node )
+	{
+		$meta 		= $this->_em->getClassMetadata( get_class( $node ) );
+		$nodeID 	= $meta->getSingleIdentifierFieldName();
+		$refProp	= $meta->getReflectionProperty( $nodeID );
+		$id 		= $refProp->getValue( $node );
+		
+		return $id;
+	}
+	
 	/**
      * {@inheritdoc}
      */
     protected function validates()
     {
-        return $this->listener->getStrategy($this->_em, $this->getClassMetadata()->name)->getName() === Strategy::CLOSURE;
+        // Temporarily solution to validation problem with this class
+		return true;
     }
 }

+ 14 - 4
tests/Gedmo/Tree/ClosureTreeRepositoryTest.php

@@ -68,13 +68,23 @@ class ClosureTreeRepositoryTest extends \PHPUnit_Framework_TestCase
     
     public function test_childCount_returnsNumberOfChilds()
     {        
-        $repo 		= $this->em->getRepository( self::TEST_ENTITY_CLASS );
-        $food 		= $repo->findOneByTitle( 'Food' );
-        die( var_dump( get_class( $repo ) ) );
-        $childCount = $repo->childCount( $food );
+        $repo 			= $this->em->getRepository( self::TEST_ENTITY_CLASS );
+        $food 			= $repo->findOneByTitle( 'Food' );
+		$closureRepo	= $this->em->getRepository( self::TEST_CLOSURE_CLASS );
+        $childCount 	= $closureRepo->childCount( $food );
 		
 		$this->assertEquals( $childCount, 4 );
     }
+	
+	public function test_childCount_returnsNumberOfDirectChilds()
+    {        
+        $repo 			= $this->em->getRepository( self::TEST_ENTITY_CLASS );
+        $food 			= $repo->findOneByTitle( 'Food' );
+		$closureRepo	= $this->em->getRepository( self::TEST_CLOSURE_CLASS );
+        $childCount 	= $closureRepo->childCount( $food, true );
+		
+		$this->assertEquals( $childCount, 2 );
+    }
     
     
     

+ 1 - 1
tests/Gedmo/Tree/Fixture/Closure/CategoryClosure.php

@@ -5,7 +5,7 @@ namespace Tree\Fixture\Closure;
 use Gedmo\Tree\Entity\AbstractClosure;
 
 /**
- * @Entity
+ * @Entity(repositoryClass="Gedmo\Tree\Entity\Repository\ClosureTreeRepository")
  */
 class CategoryClosure extends AbstractClosure
 {