Skip to content

Commit

Permalink
moved some ClosureTable testing methods to EntityTestCase class
Browse files Browse the repository at this point in the history
  • Loading branch information
franzose committed Feb 3, 2015
1 parent ce8b630 commit c8ba936
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/Franzose/ClosureTable/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ public function removeChild($position = null, $forceDelete = false)
if ($this->exists)
{
$action = ($forceDelete === true ? 'forceDelete' : 'delete');

$this->children($position)->$action();
}

Expand Down
87 changes: 0 additions & 87 deletions tests/ClosureTableTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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());
Expand Down
118 changes: 112 additions & 6 deletions tests/EntityTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,26 +678,43 @@ 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()
{
$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()
{
$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()
Expand Down Expand Up @@ -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());
}
}

0 comments on commit c8ba936

Please sign in to comment.