* @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(); } /** * Based on issue #342 * * @test */ function shouldBeAbleToShiftRootNode() { $repo = $this->em->getRepository(self::CATEGORY); $food = $repo->findOneByTitle('Food'); $acme = new RootCategory; $acme->setTitle('Acme'); $food->setParent($acme); $this->em->persist($acme); $this->em->persist($food); $this->em->flush(); $this->assertNull($acme->getParent()); $this->assertSame($acme, $food->getParent()); $this->assertSame($acme->getId(), $acme->getRoot()); $this->assertSame($acme->getId(), $food->getRoot()); $this->assertSame(1, $acme->getLeft()); $this->assertSame(12, $acme->getRight()); $this->assertSame(2, $food->getLeft()); $this->assertSame(11, $food->getRight()); } /** * @test */ function shouldSupportChildrenHierarchyAsArray() { $repo = $this->em->getRepository(self::CATEGORY); $result = $repo->childrenHierarchy(); $this->assertCount(2, $result); $this->assertTrue(isset($result[0]['__children'][0]['__children'])); $vegies = $repo->findOneByTitle('Vegitables'); $result = $repo->childrenHierarchy($vegies); $this->assertCount(2, $result); $this->assertCount(0, $result[0]['__children']); // Complete Tree $roots = $repo->getRootNodes(); $tree = $repo->childrenHierarchy(); $this->assertEquals(2, count($tree)); // Count roots $this->assertEquals('Food', $tree[0]['title']); $this->assertEquals('Sports', $tree[1]['title']); $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']); $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']); $this->assertEquals('Carrots', $tree[0]['__children'][1]['__children'][0]['title']); $this->assertEquals('Potatoes', $tree[0]['__children'][1]['__children'][1]['title']); // Tree of one specific root, without the root node $roots = $repo->getRootNodes(); $tree = $repo->childrenHierarchy($roots[0]); $this->assertEquals(2, count($tree)); // Count roots $this->assertEquals('Fruits', $tree[0]['title']); $this->assertEquals('Vegitables', $tree[1]['title']); $this->assertEquals('Carrots', $tree[1]['__children'][0]['title']); $this->assertEquals('Potatoes', $tree[1]['__children'][1]['title']); // Tree of one specific root, with the root node $tree = $repo->childrenHierarchy($roots[0], false, array(), true); $this->assertEquals(1, count($tree)); // Count roots $this->assertEquals('Food', $tree[0]['title']); $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']); $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']); $this->assertEquals('Carrots', $tree[0]['__children'][1]['__children'][0]['title']); $this->assertEquals('Potatoes', $tree[0]['__children'][1]['__children'][1]['title']); // Tree of one specific root only with direct children, without the root node $roots = $repo->getRootNodes(); $tree = $repo->childrenHierarchy($roots[0], 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[0], true, array(), true); $this->assertEquals(1, count($tree)); $this->assertEquals('Food', $tree[0]['title']); $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']); $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']); } /** * @test */ function shouldSupportChildrenHierarchyAsHtml() { $repo = $this->em->getRepository(self::CATEGORY); $food = $repo->findOneByTitle('Food'); $decorate = true; $defaultHtmlTree = $repo->childrenHierarchy($food, false, compact('decorate')); $this->assertEquals( '', $defaultHtmlTree ); // custom title $nodeDecorator = function($node) { return ''.$node['title'].''; }; $decoratedHtmlTree = $repo->childrenHierarchy( $food, false, compact('decorate', 'nodeDecorator') ); $this->assertEquals( '', $decoratedHtmlTree ); // cli friendly output $rootOpen = ''; $rootClose = ''; $childOpen = ''; $childClose = ''; $nodeDecorator = function($node) { return str_repeat('-', $node['level']).$node['title']."\n"; }; $decoratedCliTree = $repo->childrenHierarchy( $food, false, compact('decorate', 'nodeDecorator', 'rootOpen', 'rootClose', 'childOpen', 'childClose') ); $this->assertEquals( "-Fruits\n-Vegitables\n--Carrots\n--Potatoes\n", $decoratedCliTree ); $rootOpen = function () {return '