public function testDeleteRootNode_WithDepth() { $this->useTables(array(self::CATEGORY_WITH_DEPTH_TREE, self::CATEGORY_DEPTH_TREE)); // Get the entity manager $entityManager = $this->getEntityManager(); // Initialize the entities $ios = new CategoryWithDepthTree(); $ios->setName('iOS'); // Initialize the entities $iphone = new CategoryWithDepthTree(); $iphone->setName('iPhone'); $iphone->setParent($ios); // Persist the entity $entityManager->persist($ios); $entityManager->persist($iphone); $entityManager->flush(); // Now delete the root node $entityManager->remove($ios); $entityManager->flush(); // Get the entity repository $nodeRepository = $entityManager->getRepository(self::CATEGORY_WITH_DEPTH_TREE); $treeRepository = $entityManager->getRepository(self::CATEGORY_DEPTH_TREE); // Make sure all nodes were deleted $this->assertEquals(0, $nodeRepository->findNodeCount()); // Make sure all tree rows were deleted $treeExists = $treeRepository->createQueryBuilder('t')->select('COUNT(t.ancestor)')->getQuery()->getSingleScalarResult(); $this->assertEquals(0, $treeExists); }
public function testFindParentHierarchy() { $this->useTables(array(self::CATEGORY_WITH_DEPTH_TREE, self::CATEGORY_DEPTH_TREE)); // Get the entity manager $entityManager = $this->getEntityManager(); // Initialize the entities $ios = new CategoryWithDepthTree(); $ios->setName('iOS'); $iphone = new CategoryWithDepthTree(); $iphone->setName('iPhone'); $iphone->setParent($ios); $ipad = new CategoryWithDepthTree(); $ipad->setName('iPad'); $ipad->setParent($ios); $ipadMini = new CategoryWithDepthTree(); $ipadMini->setName('iPad Mini'); $ipadMini->setParent($ipad); $android = new CategoryWithDepthTree(); $android->setName('Android'); // Persist the entity $entityManager->persist($ios); $entityManager->persist($iphone); $entityManager->persist($ipad); $entityManager->persist($ipadMini); $entityManager->persist($android); $entityManager->flush(); // Get the entity repository $nodeRepository = $entityManager->getRepository(self::CATEGORY_WITH_DEPTH_TREE); // Make sure there is the correct number of root nodes for the iPad Mini $parentHierarchy = $nodeRepository->getParentHierarchyQueryBuilder($ipadMini->getId(), true)->select('node.id')->orderBy('tree.depth', 'DESC')->getQuery()->getScalarResult(); $ids = array_map('current', $parentHierarchy); $this->assertEquals(array($ios->getId(), $ipad->getId(), $ipadMini->getId()), $ids); // Make sure there is the correct number of root nodes for the iPhone $parentHierarchy = $nodeRepository->getParentHierarchyQueryBuilder($iphone->getId(), true)->select('node.id')->orderBy('tree.depth', 'DESC')->getQuery()->getScalarResult(); $ids = array_map('current', $parentHierarchy); // Make sure there is the correct number of root nodes $this->assertEquals(array($ios->getId(), $iphone->getId()), $ids); }