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($countDirect, 2); // counts all the children of root $count = $this->table->childCount(1, false); $this->assertEquals($count, 9); // counts direct children $count = $this->table->childCount(2, false); $this->assertEquals($count, 3); // count children for a middle-node $count = $this->table->childCount(6, false); $this->assertEquals($count, 4); // count leaf children $count = $this->table->childCount(10, false); $this->assertEquals($count, 0); // test scoping $table = TableRegistry::get('MenuLinkTrees'); $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]); $count = $table->childCount(3, false); $this->assertEquals($count, 2); } /** * Tests the childCount() plus callable scoping * * @return void */ public function testCallableScoping() { $table = TableRegistry::get('MenuLinkTrees'); $table->addBehavior('Tree', [ 'scope' => function ($query) { return $query->where(['url LIKE' => '/what%']); } ]); $count = $table->childCount(2, false); $this->assertEquals($count, 2); } }