* @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']); } /** * @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 '