diff --git a/src/Franzose/ClosureTable/Models/Entity.php b/src/Franzose/ClosureTable/Models/Entity.php index 173b5a6..3d3e0fa 100644 --- a/src/Franzose/ClosureTable/Models/Entity.php +++ b/src/Franzose/ClosureTable/Models/Entity.php @@ -751,7 +751,7 @@ public function removeChild($position = null, $forceDelete = false) if ($this->exists) { $action = ($forceDelete === true ? 'forceDelete' : 'delete'); - + $this->children($position)->$action(); } diff --git a/tests/ClosureTableTestCase.php b/tests/ClosureTableTestCase.php index 9e6527e..90e76d2 100644 --- a/tests/ClosureTableTestCase.php +++ b/tests/ClosureTableTestCase.php @@ -51,40 +51,6 @@ public function insertNodeProvider() ]; } - public function testInsertNode() - { - $this->ctable->insertNode(16, 16); - $item = ClosureTable::where($this->descendantColumn, '=', 16)->first(); - - $this->assertNotNull($item); - $this->assertEquals(16, $item->{$this->ancestorColumn}); - $this->assertEquals(16, $item->{$this->descendantColumn}); - $this->assertEquals(0, $item->{$this->depthColumn}); - } - - public function testInsertedNodeDepth() - { - $this->ctable->insertNode(20, 20); - $this->ctable->insertNode(13, 20); - - $item = ClosureTable::where($this->descendantColumn, '=', 20) - ->where($this->ancestorColumn, '=', 13)->first(); - - $this->assertNotNull($item); - $this->assertEquals(1, $item->{$this->depthColumn}); - } - - public function testValidNumberOfRowsInsertedByInsertNode() - { - $this->ctable->insertNode(1, 17); - - $ancestorRows = ClosureTable::where($this->descendantColumn, '=', 1)->count(); - $descendantRows = ClosureTable::where($this->descendantColumn, '=', 17)->count(); - - $this->assertEquals(1, $ancestorRows); - $this->assertEquals(2, $descendantRows); - } - /** * @expectedException \InvalidArgumentException */ @@ -93,59 +59,6 @@ public function testMoveNodeToValidatesItsArgument() $this->ctable->moveNodeTo('wrong'); } - public function testMoveNodeToAnotherAncestor() - { - $item = ClosureTable::find(1); - $item->moveNodeTo(2); - - $ancestors = ClosureTable::where($this->descendantColumn, '=', 2)->count(); - $descendants = ClosureTable::where($this->descendantColumn, '=', 1)->count(); - - $this->assertEquals(1, $ancestors); - $this->assertEquals(2, $descendants); - } - - public function testMoveNodeToDeepNesting() - { - $item = ClosureTable::find(1); - $item->moveNodeTo(2); - - $item = ClosureTable::find(2); - $item->moveNodeTo(3); - - $item = ClosureTable::find(3); - $item->moveNodeTo(4); - - $item = ClosureTable::find(4); - $item->moveNodeTo(5); - - $descendantRows = ClosureTable::where($this->descendantColumn, '=', 1)->count(); - $ancestorRows = ClosureTable::where($this->descendantColumn, '=', 2)->count(); - - $this->assertEquals(4, $ancestorRows); - $this->assertEquals(5, $descendantRows); - } - - public function testMoveNodeToBecomeRoot() - { - $item = ClosureTable::find(1); - $item->moveNodeTo(2); - - $item = ClosureTable::find(2); - $item->moveNodeTo(3); - - $item = ClosureTable::find(3); - $item->moveNodeTo(4); - - $item = ClosureTable::find(4); - $item->moveNodeTo(5); - - $item = ClosureTable::find(1); - $item->moveNodeTo(); - - $this->assertEquals(1, $item->where($this->descendantColumn, '=', 1)->count()); - } - public function testAncestorQualifiedKeyName() { $this->assertEquals($this->ctable->getTable().'.'.$this->ancestorColumn, $this->ctable->getQualifiedAncestorColumn()); diff --git a/tests/EntityTestCase.php b/tests/EntityTestCase.php index e459014..56e4362 100644 --- a/tests/EntityTestCase.php +++ b/tests/EntityTestCase.php @@ -678,8 +678,8 @@ public function testDeleteSubtree() $entity = Entity::find(9); $entity->deleteSubtree(); - $this->assertCount(1, Entity::whereBetween('id', [9, 15])->get()); - $this->assertCount(8, Entity::whereBetween('id', [1, 8])->get()); + $this->assertEquals(1, Entity::whereBetween('id', [9, 15])->count()); + $this->assertEquals(8, Entity::whereBetween('id', [1, 8])->count()); } public function testDeleteSubtreeWithAncestor() @@ -687,8 +687,8 @@ public function testDeleteSubtreeWithAncestor() $entity = Entity::find(9); $entity->deleteSubtree(true); - $this->assertCount(0, Entity::whereBetween('id', [9, 15])->get()); - $this->assertCount(8, Entity::whereBetween('id', [1, 8])->get()); + $this->assertEquals(0, Entity::whereBetween('id', [9, 15])->count()); + $this->assertEquals(8, Entity::whereBetween('id', [1, 8])->count()); } public function testForceDeleteSubtree() @@ -696,8 +696,25 @@ public function testForceDeleteSubtree() $entity = Entity::find(9); $entity->deleteSubtree(false, true); - $this->assertCount(1, Entity::whereBetween('id', [9, 15])->get()); - $this->assertCount(1, ClosureTable::whereBetween('ancestor', [9, 15])->get()); + $this->assertEquals(1, Entity::whereBetween('id', [9, 15])->count()); + $this->assertEquals(1, ClosureTable::whereBetween('ancestor', [9, 15])->count()); + } + + public function testForceDeleteDeepSubtree() + { + Entity::find(9)->moveTo(0, 8); + Entity::find(8)->moveTo(0, 7); + Entity::find(7)->moveTo(0, 6); + Entity::find(6)->moveTo(0, 5); + Entity::find(5)->moveTo(0, 4); + Entity::find(4)->moveTo(0, 3); + Entity::find(3)->moveTo(0, 2); + Entity::find(2)->moveTo(0, 1); + + Entity::find(1)->deleteSubtree(false, true); + + $this->assertEquals(1, Entity::whereBetween('id', [1, 9])->count()); + $this->assertEquals(1, ClosureTable::whereBetween('ancestor', [1, 9])->count()); } public function testCreateFromArray() @@ -804,4 +821,93 @@ public function testCreateFromArrayBug81() $this->assertEquals(0, $child2->countChildren()); $this->assertEquals(19, $child2->getKey()); } + + public function testInsertNode() + { + $entity = Entity::create(['title' => 'abcde']); + $closure = ClosureTable::whereDescendant($entity->getKey())->first(); + + $this->assertNotNull($closure); + $this->assertEquals($entity->getKey(), $closure->ancestor); + $this->assertEquals(0, $closure->depth); + } + + public function testInsertedNodeDepth() + { + $entity = Entity::create(['title' => 'abcde']); + $child = Entity::create(['title' => 'abcde']); + $child->moveTo(0, $entity); + + $closure = ClosureTable::whereDescendant($child->getKey()) + ->whereAncestor($entity->getKey())->first(); + + $this->assertNotNull($closure); + $this->assertEquals(1, $closure->depth); + } + + public function testValidNumberOfRowsInsertedByInsertNode() + { + $ancestor = Entity::create(['title' => 'abcde']); + $descendant = Entity::create(['title' => 'abcde']); + $descendant->moveTo(0, $ancestor); + + $ancestorRows = ClosureTable::whereDescendant($ancestor->getKey())->count(); + $descendantRows = ClosureTable::whereDescendant($descendant->getKey())->count(); + + $this->assertEquals(1, $ancestorRows); + $this->assertEquals(2, $descendantRows); + } + + public function testMoveNodeToAnotherAncestor() + { + $descendant = Entity::find(1); + $descendant->moveTo(0, 2); + + $ancestors = ClosureTable::whereDescendant(2)->count(); + $descendants = ClosureTable::whereDescendant(1)->count(); + + $this->assertEquals(1, $ancestors); + $this->assertEquals(2, $descendants); + } + + public function testMoveNodeToDeepNesting() + { + $item = Entity::find(1); + $item->moveTo(0, 2); + + $item = Entity::find(2); + $item->moveTo(0, 3); + + $item = Entity::find(3); + $item->moveTo(0, 4); + + $item = Entity::find(4); + $item->moveTo(0, 5); + + $descendantRows = ClosureTable::whereDescendant(1)->count(); + $ancestorRows = ClosureTable::whereDescendant(2)->count(); + + $this->assertEquals(4, $ancestorRows); + $this->assertEquals(5, $descendantRows); + } + + public function testMoveNodeToBecomeRoot() + { + $item = Entity::find(1); + $item->moveTo(0, 2); + + $item = Entity::find(2); + $item->moveTo(0, 3); + + $item = Entity::find(3); + $item->moveTo(0, 4); + + $item = Entity::find(4); + $item->moveTo(0, 5); + + $item = Entity::find(1); + $item->moveTo(0); + + $this->assertEquals(1, ClosureTable::whereDescendant(1)->count()); + } } \ No newline at end of file