123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- namespace Gedmo\Tree;
- use Doctrine\Common\EventManager;
- use Tool\BaseTestCaseORM;
- use Doctrine\Common\Util\Debug;
- use Tree\Fixture\RootCategory;
- /**
- * These are tests for Tree behavior
- *
- * @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 NestedTreeRootRepositoryTest extends BaseTestCaseORM
- {
- const CATEGORY = "Tree\\Fixture\\RootCategory";
- protected function setUp()
- {
- parent::setUp();
- $evm = new EventManager;
- $evm->addEventSubscriber(new TreeListener);
- $this->getMockSqliteEntityManager($evm);
- $this->populate();
- }
- public function testRootRemoval()
- {
- $repo = $this->em->getRepository(self::CATEGORY);
- $this->populateMore();
- $food = $repo->findOneByTitle('Food');
- $repo->removeFromTree($food);
- $this->em->clear();
- $food = $repo->findOneByTitle('Food');
- $this->assertTrue(is_null($food));
- $node = $repo->findOneByTitle('Fruits');
- $this->assertEquals(1, $node->getLeft());
- $this->assertEquals(2, $node->getRight());
- $this->assertEquals(3, $node->getRoot());
- $this->assertTrue(is_null($node->getParent()));
- $node = $repo->findOneByTitle('Vegitables');
- $this->assertEquals(1, $node->getLeft());
- $this->assertEquals(10, $node->getRight());
- $this->assertEquals(4, $node->getRoot());
- $this->assertTrue(is_null($node->getParent()));
- }
- public function testRepository()
- {
- $repo = $this->em->getRepository(self::CATEGORY);
- $carrots = $repo->findOneByTitle('Carrots');
- $path = $repo->getPath($carrots);
- $this->assertEquals(3, count($path));
- $this->assertEquals('Food', $path[0]->getTitle());
- $this->assertEquals('Vegitables', $path[1]->getTitle());
- $this->assertEquals('Carrots', $path[2]->getTitle());
- $vegies = $repo->findOneByTitle('Vegitables');
- $childCount = $repo->childCount($vegies);
- $this->assertEquals(2, $childCount);
- $food = $repo->findOneByTitle('Food');
- $childCount = $repo->childCount($food, true);
- $this->assertEquals(2, $childCount);
- $childCount = $repo->childCount($food);
- $this->assertEquals(4, $childCount);
- $childCount = $repo->childCount();
- $this->assertEquals(6, $childCount);
- }
- public function testAdvancedRepositoryFunctions()
- {
- $this->populateMore();
- $repo = $this->em->getRepository(self::CATEGORY);
- // verification
- $this->assertTrue($repo->verify());
- $dql = 'UPDATE ' . self::CATEGORY . ' node';
- $dql .= ' SET node.lft = 1';
- $dql .= ' WHERE node.id = 4';
- $this->em->createQuery($dql)->getSingleScalarResult();
- $this->em->clear(); // must clear cached entities
- $errors = $repo->verify();
- $this->assertEquals(2, count($errors));
- $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
- $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
- $dql = 'UPDATE ' . self::CATEGORY . ' node';
- $dql .= ' SET node.lft = 4';
- $dql .= ' WHERE node.id = 4';
- $this->em->createQuery($dql)->getSingleScalarResult();
- //@todo implement
- //$this->em->clear();
- //$repo->recover();
- //$this->em->clear();
- //$this->assertTrue($repo->verify());
- $this->em->clear();
- $onions = $repo->findOneByTitle('Onions');
- $this->assertEquals(11, $onions->getLeft());
- $this->assertEquals(12, $onions->getRight());
- // move up
- $repo->moveUp($onions);
- $this->assertEquals(9, $onions->getLeft());
- $this->assertEquals(10, $onions->getRight());
- $repo->moveUp($onions, true);
- $this->assertEquals(5, $onions->getLeft());
- $this->assertEquals(6, $onions->getRight());
- // move down
- $repo->moveDown($onions, 2);
- $this->assertEquals(9, $onions->getLeft());
- $this->assertEquals(10, $onions->getRight());
- // reorder
- $node = $repo->findOneByTitle('Food');
- $repo->reorder($node, 'title', 'ASC', false);
- $node = $repo->findOneByTitle('Cabbages');
- $this->assertEquals(5, $node->getLeft());
- $this->assertEquals(6, $node->getRight());
- $node = $repo->findOneByTitle('Carrots');
- $this->assertEquals(7, $node->getLeft());
- $this->assertEquals(8, $node->getRight());
- $node = $repo->findOneByTitle('Onions');
- $this->assertEquals(9, $node->getLeft());
- $this->assertEquals(10, $node->getRight());
- $node = $repo->findOneByTitle('Potatoes');
- $this->assertEquals(11, $node->getLeft());
- $this->assertEquals(12, $node->getRight());
- // leafs
- $leafs = $repo->getLeafs($node);
- $this->assertEquals(5, count($leafs));
- $this->assertEquals('Fruits', $leafs[0]->getTitle());
- $this->assertEquals('Cabbages', $leafs[1]->getTitle());
- $this->assertEquals('Carrots', $leafs[2]->getTitle());
- $this->assertEquals('Onions', $leafs[3]->getTitle());
- $this->assertEquals('Potatoes', $leafs[4]->getTitle());
- // remove
- $node = $repo->findOneByTitle('Fruits');
- $id = $node->getId();
- $repo->removeFromTree($node);
- $this->assertTrue(is_null($repo->find($id)));
- $node = $repo->findOneByTitle('Vegitables');
- $id = $node->getId();
- $repo->removeFromTree($node);
- $this->assertTrue(is_null($repo->find($id)));
- $this->em->clear();
- $node = $repo->findOneByTitle('Cabbages');
- $this->assertEquals(1, $node->getRoot());
- $this->assertEquals(1, $node->getParent()->getId());
- }
- public function testRemoveFromTreeLeaf()
- {
- $this->populateMore();
- $repo = $this->em->getRepository(self::CATEGORY);
- $onions = $repo->findOneByTitle('Onions');
- $id = $onions->getId();
- $repo->removeFromTree($onions);
- $this->assertTrue(is_null($repo->find($id)));
- $this->em->clear();
- $vegies = $repo->findOneByTitle('Vegitables');
- $this->assertTrue($repo->verify());
- }
- protected function getUsedEntityFixtures()
- {
- return array(
- self::CATEGORY
- );
- }
- private function populateMore()
- {
- $vegies = $this->em->getRepository(self::CATEGORY)
- ->findOneByTitle('Vegitables');
- $cabbages = new RootCategory();
- $cabbages->setParent($vegies);
- $cabbages->setTitle('Cabbages');
- $onions = new RootCategory();
- $onions->setParent($vegies);
- $onions->setTitle('Onions');
- $this->em->persist($cabbages);
- $this->em->persist($onions);
- $this->em->flush();
- }
- private function populate()
- {
- $root = new RootCategory();
- $root->setTitle("Food");
- $root2 = new RootCategory();
- $root2->setTitle("Sports");
- $child = new RootCategory();
- $child->setTitle("Fruits");
- $child->setParent($root);
- $child2 = new RootCategory();
- $child2->setTitle("Vegitables");
- $child2->setParent($root);
- $childsChild = new RootCategory();
- $childsChild->setTitle("Carrots");
- $childsChild->setParent($child2);
- $potatoes = new RootCategory();
- $potatoes->setTitle("Potatoes");
- $potatoes->setParent($child2);
- $this->em->persist($root);
- $this->em->persist($root2);
- $this->em->persist($child);
- $this->em->persist($child2);
- $this->em->persist($childsChild);
- $this->em->persist($potatoes);
- $this->em->flush();
- }
- }
|