123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <?php
- namespace Gedmo\Tree;
- use Doctrine\Common\EventManager;
- use Tool\BaseTestCaseORM;
- use Tree\Fixture\Closure\Category;
- use Tree\Fixture\Closure\CategoryWithoutLevel;
- use Tree\Fixture\Closure\CategoryWithoutLevelClosure;
- /**
- * These are tests for Tree behavior
- *
- * @author Gustavo Adrian <comfortablynumb84@gmail.com>
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class ClosureTreeRepositoryTest extends BaseTestCaseORM
- {
- const CATEGORY = "Tree\\Fixture\\Closure\\Category";
- const CLOSURE = "Tree\\Fixture\\Closure\\CategoryClosure";
- const CATEGORY_WITHOUT_LEVEL = "Tree\\Fixture\\Closure\\CategoryWithoutLevel";
- const CATEGORY_WITHOUT_LEVEL_CLOSURE = "Tree\\Fixture\\Closure\\CategoryWithoutLevelClosure";
- protected $listener;
- protected function setUp()
- {
- parent::setUp();
- $this->listener = new TreeListener;
- $evm = new EventManager;
- $evm->addEventSubscriber($this->listener);
- $this->getMockSqliteEntityManager($evm);
- }
- public function testChildCount()
- {
- $this->populate();
- $repo = $this->em->getRepository(self::CATEGORY);
- $food = $repo->findOneByTitle('Food');
- // Count all
- $count = $repo->childCount();
- $this->assertEquals(15, $count);
- // Count all, but only direct ones
- $count = $repo->childCount(null, true);
- $this->assertEquals(2, $count);
- // Count food children
- $food = $repo->findOneByTitle('Food');
- $count = $repo->childCount($food);
- $this->assertEquals(11, $count);
- // Count food children, but only direct ones
- $food = $repo->findOneByTitle('Food');
- $count = $repo->childCount($food, true);
- $this->assertEquals(3, $count);
- }
- public function testPath()
- {
- $this->populate();
- $repo = $this->em->getRepository(self::CATEGORY);
- $fruits = $repo->findOneByTitle('Fruits');
- $path = $repo->getPath($fruits);
- $this->assertCount(2, $path);
- $this->assertEquals('Food', $path[0]->getTitle());
- $this->assertEquals('Fruits', $path[1]->getTitle());
- $strawberries = $repo->findOneByTitle('Strawberries');
- $path = $repo->getPath($strawberries);
- $this->assertCount(4, $path);
- $this->assertEquals('Food', $path[0]->getTitle());
- $this->assertEquals('Fruits', $path[1]->getTitle());
- $this->assertEquals('Berries', $path[2]->getTitle());
- $this->assertEquals('Strawberries', $path[3]->getTitle());
- }
- public function testChildren()
- {
- $this->populate();
- $repo = $this->em->getRepository(self::CATEGORY);
- $fruits = $repo->findOneByTitle('Fruits');
- // direct children of node, sorted by title ascending order. NOT including the root node
- $children = $repo->children($fruits, true, 'title');
- $this->assertCount(3, $children);
- $this->assertEquals('Berries', $children[0]->getTitle());
- $this->assertEquals('Lemons', $children[1]->getTitle());
- $this->assertEquals('Oranges', $children[2]->getTitle());
- // direct children of node, sorted by title ascending order. including the root node
- $children = $repo->children($fruits, true, 'title', 'asc', true);
- $this->assertCount(4, $children);
- $this->assertEquals('Berries', $children[0]->getTitle());
- $this->assertEquals('Fruits', $children[1]->getTitle());
- $this->assertEquals('Lemons', $children[2]->getTitle());
- $this->assertEquals('Oranges', $children[3]->getTitle());
- // all children of node, NOT including the root
- $children = $repo->children($fruits);
- $this->assertCount(4, $children);
- $this->assertEquals('Oranges', $children[0]->getTitle());
- $this->assertEquals('Lemons', $children[1]->getTitle());
- $this->assertEquals('Berries', $children[2]->getTitle());
- $this->assertEquals('Strawberries', $children[3]->getTitle());
- // all children of node, including the root
- $children = $repo->children($fruits, false, 'title', 'asc', true);
- $this->assertCount(5, $children);
- $this->assertEquals('Berries', $children[0]->getTitle());
- $this->assertEquals('Fruits', $children[1]->getTitle());
- $this->assertEquals('Lemons', $children[2]->getTitle());
- $this->assertEquals('Oranges', $children[3]->getTitle());
- $this->assertEquals('Strawberries', $children[4]->getTitle());
- // direct root nodes
- $children = $repo->children(null, true, 'title');
- $this->assertCount(2, $children);
- $this->assertEquals('Food', $children[0]->getTitle());
- $this->assertEquals('Sports', $children[1]->getTitle());
- // all tree
- $children = $repo->children();
- $this->assertCount(15, $children);
- }
- public function testSingleNodeRemoval()
- {
- $this->populate();
- $repo = $this->em->getRepository(self::CATEGORY);
- $fruits = $repo->findOneByTitle('Fruits');
- $repo->removeFromTree($fruits);
- // ensure in memory node integrity
- $this->em->flush();
- $food = $repo->findOneByTitle('Food');
- $children = $repo->children($food, true);
- $this->assertCount(5, $children);
- $berries = $repo->findOneByTitle('Berries');
- $this->assertEquals(1, $repo->childCount($berries, true));
- $lemons = $repo->findOneByTitle('Lemons');
- $this->assertEquals(0, $repo->childCount($lemons, true));
- $repo->removeFromTree($food);
- $vegitables = $repo->findOneByTitle('Vegitables');
- $this->assertEquals(2, $repo->childCount($vegitables, true));
- $this->assertNull($vegitables->getParent());
- $repo->removeFromTree($lemons);
- $this->assertCount(5, $repo->children(null, true));
- }
- public function testBuildTreeWithLevelProperty()
- {
- $this->populate();
- $this->buildTreeTests(self::CATEGORY);
- }
- public function testBuildTreeWithoutLevelProperty()
- {
- $this->populate(self::CATEGORY_WITHOUT_LEVEL);
- $this->buildTreeTests(self::CATEGORY_WITHOUT_LEVEL);
- }
- public function testHavingLevelPropertyAvoidsSubqueryInSelectInGetNodesHierarchy()
- {
- $this->populate();
- $repo = $this->em->getRepository(self::CATEGORY);
- $roots = $repo->getRootNodes();
- $meta = $this->em->getClassMetadata(self::CATEGORY);
- $config = $this->listener->getConfiguration($this->em, $meta->name);
- $qb = $repo->getNodesHierarchyQueryBuilder($roots[0], false, $config);
- $this->assertFalse(strpos($qb->getQuery()->getDql(), '(SELECT MAX('));
- }
- public function testNotHavingLevelPropertyUsesASubqueryInSelectInGetNodesHierarchy()
- {
- $this->populate(self::CATEGORY_WITHOUT_LEVEL);
- $repo = $this->em->getRepository(self::CATEGORY_WITHOUT_LEVEL);
- $roots = $repo->getRootNodes();
- $meta = $this->em->getClassMetadata(self::CATEGORY_WITHOUT_LEVEL);
- $config = $this->listener->getConfiguration($this->em, $meta->name);
- $qb = $repo->getNodesHierarchyQueryBuilder($roots[0], false, $config);
- $this->assertTrue(((bool) strpos($qb->getQuery()->getDql(), '(SELECT MAX(')));
- }
- public function test_changeChildrenIndex()
- {
- $this->populate(self::CATEGORY);
- $childrenIndex = 'myChildren';
- $repo = $this->em->getRepository(self::CATEGORY);
- $repo->setChildrenIndex($childrenIndex);
- $tree = $repo->childrenHierarchy();
- $this->assertInternalType('array', $tree[0][$childrenIndex]);
- }
- // Utility Methods
- protected function buildTreeTests($class)
- {
- $repo = $this->em->getRepository($class);
- $sortOption = array('childSort' => array('field' => 'title', 'dir' => 'asc'));
- $testClosure = function(ClosureTreeRepositoryTest $phpUnit, array $tree, $includeNode = false, $whichTree = 'both', $includeNewNode = false) {
- if ($whichTree === 'both' || $whichTree === 'first') {
- $boringFood = $includeNewNode ? ($includeNode ? $tree[0]['__children'][0] : $tree[0]) : null;
- $fruitsIndex = $includeNewNode ? 1 : 0;
- $milkIndex = $includeNewNode ? 2 : 1;
- $fruits = $includeNode ? $tree[0]['__children'][$fruitsIndex] : $tree[$fruitsIndex];
- $milk = $includeNode ? $tree[0]['__children'][$milkIndex] : $tree[$milkIndex];
- $vegitables = $includeNewNode ? $boringFood['__children'][0] : ($includeNode ? $tree[0]['__children'][2] : $tree[2]);
- if ($includeNode) {
- $phpUnit->assertEquals('Food', $tree[0]['title']);
- }
- $phpUnit->assertEquals('Fruits', $fruits['title']);
- $phpUnit->assertEquals('Berries', $fruits['__children'][0]['title']);
- $phpUnit->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
- $phpUnit->assertEquals('Milk', $milk['title']);
- $phpUnit->assertEquals('Cheese', $milk['__children'][0]['title']);
- $phpUnit->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
- if ($boringFood) {
- $phpUnit->assertEquals('Boring Food', $boringFood['title']);
- }
- $phpUnit->assertEquals('Vegitables', $vegitables['title']);
- $phpUnit->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
- $phpUnit->assertEquals('Carrots', $vegitables['__children'][1]['title']);
- }
- if ($whichTree === 'both' || $whichTree === 'second') {
- $root = $whichTree === 'both' ? $tree[1] : $tree[0];
- $soccer = $includeNode ? $root['__children'][0] : $root;
- if ($includeNode) {
- $phpUnit->assertEquals('Sports', $root['title']);
- }
- $phpUnit->assertEquals('Soccer', $soccer['title']);
- $phpUnit->assertEquals('Indoor Soccer', $soccer['__children'][0]['title']);
- }
- };
- // All trees
- $tree = $repo->childrenHierarchy(null, false, $sortOption);
- $testClosure($this, $tree, true, 'both');
- $roots = $repo->getRootNodes();
- // First root tree, including root node
- $tree = $repo->childrenHierarchy(
- $roots[0],
- false,
- $sortOption,
- true
- );
- $testClosure($this, $tree, true, 'first');
- // First root tree, not including root node
- $tree = $repo->childrenHierarchy(
- $roots[0],
- false,
- $sortOption
- );
- $testClosure($this, $tree, false, 'first');
- // Second root tree, including root node
- $tree = $repo->childrenHierarchy(
- $roots[1],
- false,
- $sortOption,
- true
- );
- $testClosure($this, $tree, true, 'second');
- // Second root tree, not including root node
- $tree = $repo->childrenHierarchy(
- $roots[1],
- false,
- $sortOption
- );
- $testClosure($this, $tree, false, 'second');
- $food = $repo->findOneByTitle('Food');
- $vegitables = $repo->findOneByTitle('Vegitables');
- $boringFood = new $class();
- $boringFood->setTitle('Boring Food');
- $boringFood->setParent($food);
- $vegitables->setParent($boringFood);
- $this->em->persist($boringFood);
- $this->em->flush();
- // First root tree, after inserting a new node in the middle. This includes the root node
- $tree = $repo->childrenHierarchy(
- $roots[0],
- false,
- $sortOption,
- true
- );
- $testClosure($this, $tree, true, 'first', true);
- // First root tree, after inserting a new node in the middle. This not includes the root node
- $tree = $repo->childrenHierarchy(
- $roots[0],
- false,
- $sortOption
- );
- $testClosure($this, $tree, false, 'first', true);
- // Second root tree, after inserting a new node in the middle. This includes the root node
- $tree = $repo->childrenHierarchy(
- $roots[1],
- false,
- $sortOption,
- true
- );
- $testClosure($this, $tree, true, 'second', true);
- // Second root tree, after inserting a new node in the middle. This not includes the root node
- $tree = $repo->childrenHierarchy(
- $roots[1],
- false,
- $sortOption
- );
- $testClosure($this, $tree, false, 'second', false);
- // Test a subtree, including node
- $node = $repo->findOneByTitle('Fruits');
- $tree = $repo->childrenHierarchy(
- $node,
- false,
- $sortOption,
- true
- );
- $this->assertEquals('Fruits', $tree[0]['title']);
- $this->assertEquals('Berries', $tree[0]['__children'][0]['title']);
- $this->assertEquals('Strawberries', $tree[0]['__children'][0]['__children'][0]['title']);
- $node = $repo->findOneByTitle('Fruits');
- $tree = $repo->childrenHierarchy(
- $node,
- false,
- $sortOption
- );
- $this->assertEquals('Berries', $tree[0]['title']);
- $this->assertEquals('Strawberries', $tree[0]['__children'][0]['title']);
- // First Tree Direct Nodes, including root node
- $tree = $repo->childrenHierarchy(
- $roots[0],
- true,
- $sortOption,
- true
- );
- $food = $tree[0];
- $this->assertEquals('Food', $food['title']);
- $this->assertEquals(3, count($food['__children']));
- $this->assertEquals('Boring Food', $food['__children'][0]['title']);
- $this->assertEquals('Fruits', $food['__children'][1]['title']);
- $this->assertEquals('Milk', $food['__children'][2]['title']);
- // First Tree Direct Nodes, not including root node
- $tree = $repo->childrenHierarchy(
- $roots[0],
- true,
- $sortOption
- );
- $this->assertEquals(3, count($tree));
- $this->assertEquals('Boring Food', $tree[0]['title']);
- $this->assertEquals('Fruits', $tree[1]['title']);
- $this->assertEquals('Milk', $tree[2]['title']);
- // Helper Closures
- $getTree = function($includeNode) use ($repo, $roots, $sortOption) {
- return $repo->childrenHierarchy(
- $roots[0],
- true,
- array_merge($sortOption, array('decorate' => true)),
- $includeNode
- );
- };
- $getTreeHtml = function($includeNode) {
- $baseHtml = '<li>Boring Food<ul><li>Vegitables<ul><li>Cabbages</li><li>Carrots</li></ul></li></ul></li><li>Fruits<ul><li>Berries<ul><li>Strawberries</li></ul></li><li>Lemons</li><li>Oranges</li></ul></li><li>Milk<ul><li>Cheese<ul><li>Mould cheese</li></ul></li></ul></li></ul>';
- return $includeNode ? '<ul><li>Food<ul>'.$baseHtml.'</li></ul>' : '<ul>'.$baseHtml;
- };
- // First Tree - Including Root Node - Html test
- $this->assertEquals($getTreeHtml(true), $getTree(true));
- // First Tree - Not including Root Node - Html test
- $this->assertEquals($getTreeHtml(false), $getTree(false));
- }
- protected function getUsedEntityFixtures()
- {
- return array(
- self::CATEGORY,
- self::CLOSURE,
- self::CATEGORY_WITHOUT_LEVEL,
- self::CATEGORY_WITHOUT_LEVEL_CLOSURE
- );
- }
- private function populate($class = self::CATEGORY)
- {
- $food = new $class;
- $food->setTitle("Food");
- $this->em->persist($food);
- $vegitables = new $class;
- $vegitables->setTitle('Vegitables');
- $vegitables->setParent($food);
- $this->em->persist($vegitables);
- $fruits = new $class;
- $fruits->setTitle('Fruits');
- $fruits->setParent($food);
- $this->em->persist($fruits);
- $oranges = new $class;
- $oranges->setTitle('Oranges');
- $oranges->setParent($fruits);
- $this->em->persist($oranges);
- $lemons = new $class;
- $lemons->setTitle('Lemons');
- $lemons->setParent($fruits);
- $this->em->persist($lemons);
- $berries = new $class;
- $berries->setTitle('Berries');
- $berries->setParent($fruits);
- $this->em->persist($berries);
- $strawberries = new $class;
- $strawberries->setTitle('Strawberries');
- $strawberries->setParent($berries);
- $this->em->persist($strawberries);
- $cabbages = new $class;
- $cabbages->setTitle('Cabbages');
- $cabbages->setParent($vegitables);
- $this->em->persist($cabbages);
- $carrots = new $class;
- $carrots->setTitle('Carrots');
- $carrots->setParent($vegitables);
- $this->em->persist($carrots);
- $milk = new $class;
- $milk->setTitle('Milk');
- $milk->setParent($food);
- $this->em->persist($milk);
- $cheese = new $class;
- $cheese->setTitle('Cheese');
- $cheese->setParent($milk);
- $this->em->persist($cheese);
- $mouldCheese = new $class;
- $mouldCheese->setTitle('Mould cheese');
- $mouldCheese->setParent($cheese);
- $this->em->persist($mouldCheese);
- $sports = new $class;
- $sports->setTitle('Sports');
- $this->em->persist($sports);
- $soccer = new $class;
- $soccer->setTitle('Soccer');
- $soccer->setParent($sports);
- $this->em->persist($soccer);
- $indoorSoccer = new $class;
- $indoorSoccer->setTitle('Indoor Soccer');
- $indoorSoccer->setParent($soccer);
- $this->em->persist($indoorSoccer);
- $this->em->flush();
- $this->em->clear();
- }
- }
|