Skip to content

Commit

Permalink
Using an array primaryKey value in TreeBehaviorTests to confirm that …
Browse files Browse the repository at this point in the history
…it is not a problem
  • Loading branch information
lorenzo committed Jul 12, 2014
1 parent 4492cdc commit 84a83fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
5 changes: 5 additions & 0 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -756,6 +756,11 @@ protected function _ensureFields($entity) {
}
}

/**
* Returns a single string value representing the primary key of the attached table
*
* @return string
*/
protected function _getPrimaryKey() {
if (!$this->_primaryKey) {
$this->_primaryKey = (array)$this->_table->primaryKey();
Expand Down
58 changes: 20 additions & 38 deletions tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -39,6 +39,7 @@ class TreeBehaviorTest extends TestCase {
public function setUp() {
parent::setUp();
$this->table = TableRegistry::get('NumberTrees');
$this->table->primaryKey(['id']);
$this->table->addBehavior('Tree');
}

Expand Down Expand Up @@ -385,8 +386,7 @@ public function testMoveDownNoTreeColumns() {
* @return void
*/
public function testRecover() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$table->updateAll(['lft' => null, 'rght' => null], []);
$table->recover();
Expand Down Expand Up @@ -437,8 +437,7 @@ public function testRecoverScoped() {
* @return void
*/
public function testAddOrphan() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = new Entity(
['name' => 'New Orphan', 'parent_id' => null],
['markNew' => true]
Expand All @@ -459,8 +458,7 @@ public function testAddOrphan() {
* @return void
*/
public function testAddMiddle() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = new Entity(
['name' => 'laptops', 'parent_id' => 1],
['markNew' => true]
Expand All @@ -481,8 +479,7 @@ public function testAddMiddle() {
* @return void
*/
public function testAddLeaf() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = new Entity(
['name' => 'laptops', 'parent_id' => 2],
['markNew' => true]
Expand All @@ -503,8 +500,7 @@ public function testAddLeaf() {
* @return void
*/
public function testReParentSubTreeRight() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(2);
$entity->parent_id = 6;
$this->assertSame($entity, $table->save($entity));
Expand All @@ -522,8 +518,7 @@ public function testReParentSubTreeRight() {
* @return void
*/
public function testReParentSubTreeLeft() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(6);
$entity->parent_id = 2;
$this->assertSame($entity, $table->save($entity));
Expand All @@ -542,8 +537,7 @@ public function testReParentSubTreeLeft() {
* @return void
*/
public function testReParentLeafLeft() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(10);
$entity->parent_id = 2;
$this->assertSame($entity, $table->save($entity));
Expand All @@ -562,8 +556,7 @@ public function testReParentLeafLeft() {
* @return void
*/
public function testReParentLeafRight() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(5);
$entity->parent_id = 6;
$this->assertSame($entity, $table->save($entity));
Expand All @@ -581,8 +574,7 @@ public function testReParentLeafRight() {
* @return void
*/
public function testReParentNoTreeColumns() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(6);
$entity->unsetProperty('lft');
$entity->unsetProperty('rght');
Expand All @@ -603,8 +595,7 @@ public function testReParentNoTreeColumns() {
* @return void
*/
public function testRootingSubTree() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(2);
$entity->parent_id = null;
$this->assertSame($entity, $table->save($entity));
Expand All @@ -622,8 +613,7 @@ public function testRootingSubTree() {
* @return void
*/
public function testRootingNoTreeColumns() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(2);
$entity->unsetProperty('lft');
$entity->unsetProperty('rght');
Expand All @@ -645,8 +635,7 @@ public function testRootingNoTreeColumns() {
* @return void
*/
public function testReparentCycle() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(2);
$entity->parent_id = 5;
$table->save($entity);
Expand All @@ -658,8 +647,7 @@ public function testReparentCycle() {
* @return void
*/
public function testDeleteLeaf() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(4);
$this->assertTrue($table->delete($entity));
$result = $table->find()->order('lft')->hydrate(false)->toArray();
Expand All @@ -674,8 +662,7 @@ public function testDeleteLeaf() {
* @return void
*/
public function testDeleteSubTree() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(6);
$this->assertTrue($table->delete($entity));
$result = $table->find()->order('lft')->hydrate(false)->toArray();
Expand All @@ -690,8 +677,7 @@ public function testDeleteSubTree() {
* @return void
*/
public function testDeleteRoot() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(1);
$this->assertTrue($table->delete($entity));
$result = $table->find()->order('lft')->hydrate(false)->toArray();
Expand All @@ -706,8 +692,7 @@ public function testDeleteRoot() {
* @return void
*/
public function testDeleteRootNoTreeColumns() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(1);
$entity->unsetProperty('lft');
$entity->unsetProperty('rght');
Expand All @@ -724,8 +709,7 @@ public function testDeleteRootNoTreeColumns() {
* @return void
*/
public function testRemoveFromLeafFromTree() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(10);
$this->assertSame($entity, $table->removeFromTree($entity));
$this->assertEquals(21, $entity->lft);
Expand All @@ -742,8 +726,7 @@ public function testRemoveFromLeafFromTree() {
* @return void
*/
public function testRemoveMiddleNodeFromTree() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(6);
$this->assertSame($entity, $table->removeFromTree($entity));
$result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
Expand All @@ -761,8 +744,7 @@ public function testRemoveMiddleNodeFromTree() {
* @return void
*/
public function testRemoveRootFromTree() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$table = $this->table;
$entity = $table->get(1);
$this->assertSame($entity, $table->removeFromTree($entity));
$result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
Expand Down

0 comments on commit 84a83fa

Please sign in to comment.