table = TableRegistry::get('NumberTrees'); $this->table->addBehavior('Tree'); } public function tearDown() { parent::tearDown(); TableRegistry::clear(); } /** * Tests the find('path') method * * @return void */ public function testFindPath() { $nodes = $this->table->find('path', ['for' => 9]); $this->assertEquals([1, 6, 9], $nodes->extract('id')->toArray()); $nodes = $this->table->find('path', ['for' => 10]); $this->assertEquals([1, 6, 10], $nodes->extract('id')->toArray()); $nodes = $this->table->find('path', ['for' => 5]); $this->assertEquals([1, 2, 5], $nodes->extract('id')->toArray()); $nodes = $this->table->find('path', ['for' => 1]); $this->assertEquals([1], $nodes->extract('id')->toArray()); // find path with scope $table = TableRegistry::get('MenuLinkTrees'); $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]); $nodes = $table->find('path', ['for' => 5]); $this->assertEquals([1, 3, 4, 5], $nodes->extract('id')->toArray()); } /** * Tests the childCount() method * * @return void */ public function testChildCount() { // direct children for the root node $countDirect = $this->table->childCount(1, true); $this->assertEquals(2, $countDirect); // counts all the children of root $count = $this->table->childCount(1, false); $this->assertEquals(9, $count); // counts direct children $count = $this->table->childCount(2, false); $this->assertEquals(3, $count); // count children for a middle-node $count = $this->table->childCount(6, false); $this->assertEquals(4, $count); // count leaf children $count = $this->table->childCount(10, false); $this->assertEquals(0, $count); // test scoping $table = TableRegistry::get('MenuLinkTrees'); $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]); $count = $table->childCount(3, false); $this->assertEquals(2, $count); } /** * Tests the childCount() plus callable scoping * * @return void */ public function testCallableScoping() { $table = TableRegistry::get('MenuLinkTrees'); $table->addBehavior('Tree', [ 'scope' => function ($query) { return $query->where(['menu' => 'main-menu']); } ]); $count = $table->childCount(1, false); $this->assertEquals(4, $count); } /** * Tests the children() method * * @return void */ public function testChildren() { $table = TableRegistry::get('MenuLinkTrees'); $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]); // root $nodeIds = []; foreach ($table->children(1) as $node) { $nodeIds[] = $node->id; } $this->assertEquals([2, 3, 4, 5], $nodeIds); // unexisting node $this->assertEquals(false, $table->children(500)); // leaf $nodeIds = []; foreach ($table->children(5) as $node) { $nodeIds[] = $node->id; } $this->assertEquals(0, count($nodeIds)); } /** * Tests the moveUp() method * * @return void */ public function testMoveUp() { $table = TableRegistry::get('MenuLinkTrees'); $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]); // top level, wont move $this->assertEquals(false, $this->table->moveUp(1, 10)); // edge cases $this->assertEquals(false, $this->table->moveUp(1, 0)); $this->assertEquals(false, $this->table->moveUp(1, -10)); // move inner node $nodeIds = []; $result = $table->moveUp(3, 1); foreach ($table->children(1) as $node) { $nodeIds[] = $node->id; } $this->assertEquals([3, 4, 5, 2], $nodeIds); $this->assertEquals(true, $result); } }