Skip to content

Commit

Permalink
Testing the implementation of find('path')
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 29, 2014
1 parent 094321f commit cb5e233
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -68,8 +68,8 @@ public function findPath($query, $options) {

return $this->_scope($query)
->where([
"$left <=" => $entity->get($left),
"$right >=" => $entity->get($right)
"$left <=" => $node->get($left),
"$right >=" => $node->get($right)
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/CategoryFixture.php
Expand Up @@ -43,7 +43,7 @@ class CategoryFixture extends TestFixture {
*
* @var array
*/
public $records = array(
public $_records = array(
array('parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
array('parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
array('parent_id' => 1, 'name' => 'Category 1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
Expand Down
79 changes: 79 additions & 0 deletions tests/Fixture/NumberTreeFixture.php
Expand Up @@ -41,4 +41,83 @@ class NumberTreeFixture extends TestFixture {
'rght' => ['type' => 'integer', 'null' => false],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
);

/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => '1',
'name' => 'electronics',
'parent_id' => null,
'lft' => '1',
'rght' => '20'
),
array(
'id' => '2',
'name' => 'televisions',
'parent_id' => '1',
'lft' => '2',
'rght' => '9'
),
array(
'id' => '3',
'name' => 'tube',
'parent_id' => '2',
'lft' => '3',
'rght' => '4'
),
array(
'id' => '4',
'name' => 'lcd',
'parent_id' => '2',
'lft' => '5',
'rght' => '6'
),
array(
'id' => '5',
'name' => 'plasma',
'parent_id' => '2',
'lft' => '7',
'rght' => '8'
),
array(
'id' => '6',
'name' => 'portable',
'parent_id' => '1',
'lft' => '10',
'rght' => '19'
),
array(
'id' => '7',
'name' => 'mp3',
'parent_id' => '6',
'lft' => '11',
'rght' => '14'
),
array(
'id' => '8',
'name' => 'flash',
'parent_id' => '7',
'lft' => '12',
'rght' => '13'
),
array(
'id' => '9',
'name' => 'cd',
'parent_id' => '6',
'lft' => '15',
'rght' => '16'
),
array(
'id' => '10',
'name' => 'radios',
'parent_id' => '6',
'lft' => '17',
'rght' => '18'
)
);

}
64 changes: 64 additions & 0 deletions tests/TestCase/Model/Behavior/TreeBehaviorTest.php
@@ -0,0 +1,64 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Behavior;

use Cake\Collection\Collection;
use Cake\Event\Event;
use Cake\Model\Behavior\TranslateBehavior;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
* Translate behavior test case
*/
class TreeBehaviorTest extends TestCase {

/**
* fixtures
*
* @var array
*/
public $fixtures = [
'core.number_tree'
];

public function setUp() {
$this->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());
}

}

0 comments on commit cb5e233

Please sign in to comment.