* @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(); } /** * @test */ function shouldSupportChildrenHierarchyAsArray() { $repo = $this->em->getRepository(self::CATEGORY); $result = $repo->childrenHierarchy(); $this->assertEquals(2, count($result)); $this->assertTrue(isset($result[0]['__children'][0]['__children'])); $vegies = $repo->findOneByTitle('Vegitables'); $result = $repo->childrenHierarchy($vegies); $this->assertEquals(2, count($result)); $this->assertEquals(0, count($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'].PHP_EOL; }; $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 '