Skip to content

Commit

Permalink
Merge branch 'makechild-helpers' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepat committed Jun 4, 2014
2 parents b18af1f + 6f765f8 commit 8f6117a
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ Baum provides several methods for moving nodes around:
* `makeSiblingOf($otherNode)`: Alias for `makeNextSiblingOf`.
* `makePreviousSiblingOf($otherNode)`: Alias for `moveToLeftOf`.
* `makeChildOf($otherNode)`: Make the node a child of ...
* `makeFirstChildOf($otherNode)`: Make the node the first child of ...
* `makeLastChildOf($otherNode)`: Alias for `makeChildOf`.
* `makeRoot()`: Make current node a root node.

For example:
Expand Down
21 changes: 21 additions & 0 deletions src/Baum/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,27 @@ public function makeChildOf($node) {
return $this->moveTo($node, 'child');
}

/**
* Make the node the first child of ...
*
* @return \Baum\Node
*/
public function makeFirstChildOf($node) {
if ( $node->children()->count() == 0 )
return $this->makeChildOf($node);

return $this->moveToLeftOf($node->children()->first());
}

/**
* Make the node the last child of ...
*
* @return \Baum\Node
*/
public function makeLastChildOf($node) {
return $this->makeChildOf($node);
}

/**
* Make current node a root node.
*
Expand Down
136 changes: 136 additions & 0 deletions tests/suite/Category/CategoryMovementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,142 @@ public function testMakeChildOfSwappingRootsWithSubtrees() {
$this->assertEquals(9, $this->categories('Child 2.1')->getRight());
}

public function testMakeFirstChildOf() {
$this->categories('Child 1')->makeFirstChildOf($this->categories('Child 3'));

$this->assertEquals($this->categories('Child 3'), $this->categories('Child 1')->parent()->first());

$this->assertTrue(Category::isValid());
}

public function testMakeFirstChildOfAppendsAtTheBeginning() {
$newChild = Category::create(array('name' => 'Child 4'));

$newChild->makeFirstChildOf($this->categories('Root 1'));

$lastChild = $this->categories('Root 1')->children()->get()->first();
$this->assertEquals($newChild, $lastChild);

$this->assertTrue(Category::isValid());
}

public function testMakeFirstChildOfMovesWithSubtree() {
$this->categories('Child 2')->makeFirstChildOf($this->categories('Child 1'));

$this->assertTrue(Category::isValid());

$this->assertEquals($this->categories('Child 1')->getKey(), $this->categories('Child 2')->getParentId());

$this->assertEquals(3, $this->categories('Child 2')->getLeft());
$this->assertEquals(6, $this->categories('Child 2')->getRight());

$this->assertEquals(2, $this->categories('Child 1')->getLeft());
$this->assertEquals(7, $this->categories('Child 1')->getRight());
}

public function testMakeFirstChildOfSwappingRoots() {
$newRoot = Category::create(array('name' => 'Root 3'));

$this->assertEquals(13, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());

$this->categories('Root 2')->makeFirstChildOf($newRoot);

$this->assertTrue(Category::isValid());

$this->assertEquals($newRoot->getKey(), $this->categories('Root 2')->getParentId());

$this->assertEquals(12, $this->categories('Root 2')->getLeft());
$this->assertEquals(13, $this->categories('Root 2')->getRight());

$this->assertEquals(11, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());
}

public function testMakeFirstChildOfSwappingRootsWithSubtrees() {
$newRoot = Category::create(array('name' => 'Root 3'));

$this->categories('Root 1')->makeFirstChildOf($newRoot);

$this->assertTrue(Category::isValid());

$this->assertEquals($newRoot->getKey(), $this->categories('Root 1')->getParentId());

$this->assertEquals(4, $this->categories('Root 1')->getLeft());
$this->assertEquals(13, $this->categories('Root 1')->getRight());

$this->assertEquals(8, $this->categories('Child 2.1')->getLeft());
$this->assertEquals(9, $this->categories('Child 2.1')->getRight());
}

public function testMakeLastChildOf() {
$this->categories('Child 1')->makeLastChildOf($this->categories('Child 3'));

$this->assertEquals($this->categories('Child 3'), $this->categories('Child 1')->parent()->first());

$this->assertTrue(Category::isValid());
}

public function testMakeLastChildOfAppendsAtTheEnd() {
$newChild = Category::create(array('name' => 'Child 4'));

$newChild->makeLastChildOf($this->categories('Root 1'));

$lastChild = $this->categories('Root 1')->children()->get()->last();
$this->assertEquals($newChild, $lastChild);

$this->assertTrue(Category::isValid());
}

public function testMakeLastChildOfMovesWithSubtree() {
$this->categories('Child 2')->makeLastChildOf($this->categories('Child 1'));

$this->assertTrue(Category::isValid());

$this->assertEquals($this->categories('Child 1')->getKey(), $this->categories('Child 2')->getParentId());

$this->assertEquals(3, $this->categories('Child 2')->getLeft());
$this->assertEquals(6, $this->categories('Child 2')->getRight());

$this->assertEquals(2, $this->categories('Child 1')->getLeft());
$this->assertEquals(7, $this->categories('Child 1')->getRight());
}

public function testMakeLastChildOfSwappingRoots() {
$newRoot = Category::create(array('name' => 'Root 3'));

$this->assertEquals(13, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());

$this->categories('Root 2')->makeLastChildOf($newRoot);

$this->assertTrue(Category::isValid());

$this->assertEquals($newRoot->getKey(), $this->categories('Root 2')->getParentId());

$this->assertEquals(12, $this->categories('Root 2')->getLeft());
$this->assertEquals(13, $this->categories('Root 2')->getRight());

$this->assertEquals(11, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());
}

public function testMakeLastChildOfSwappingRootsWithSubtrees() {
$newRoot = Category::create(array('name' => 'Root 3'));

$this->categories('Root 1')->makeLastChildOf($newRoot);

$this->assertTrue(Category::isValid());

$this->assertEquals($newRoot->getKey(), $this->categories('Root 1')->getParentId());

$this->assertEquals(4, $this->categories('Root 1')->getLeft());
$this->assertEquals(13, $this->categories('Root 1')->getRight());

$this->assertEquals(8, $this->categories('Child 2.1')->getLeft());
$this->assertEquals(9, $this->categories('Child 2.1')->getRight());
}

/**
* @expectedException Baum\MoveNotPossibleException
*/
Expand Down
136 changes: 136 additions & 0 deletions tests/suite/Cluster/ClusterMovementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,142 @@ public function testMakeChildOfSwappingRootsWithSubtrees() {
$this->assertEquals(9, $this->clusters('Child 2.1')->getRight());
}

public function testMakeFirstChildOf() {
$this->clusters('Child 1')->makeFirstChildOf($this->clusters('Child 3'));

$this->assertEquals($this->clusters('Child 3'), $this->clusters('Child 1')->parent()->first());

$this->assertTrue(Cluster::isValid());
}

public function testMakeFirstChildOfAppendsAtTheBeginning() {
$newChild = Cluster::create(array('name' => 'Child 4'));

$newChild->makeFirstChildOf($this->clusters('Root 1'));

$lastChild = $this->clusters('Root 1')->children()->get()->first();
$this->assertEquals($newChild, $lastChild);

$this->assertTrue(Cluster::isValid());
}

public function testMakeFirstChildOfMovesWithSubtree() {
$this->clusters('Child 2')->makeFirstChildOf($this->clusters('Child 1'));

$this->assertTrue(Cluster::isValid());

$this->assertEquals($this->clusters('Child 1')->getKey(), $this->clusters('Child 2')->getParentId());

$this->assertEquals(3, $this->clusters('Child 2')->getLeft());
$this->assertEquals(6, $this->clusters('Child 2')->getRight());

$this->assertEquals(2, $this->clusters('Child 1')->getLeft());
$this->assertEquals(7, $this->clusters('Child 1')->getRight());
}

public function testMakeFirstChildOfSwappingRoots() {
$newRoot = Cluster::create(array('name' => 'Root 3'));

$this->assertEquals(13, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());

$this->clusters('Root 2')->makeFirstChildOf($newRoot);

$this->assertTrue(Cluster::isValid());

$this->assertEquals($newRoot->getKey(), $this->clusters('Root 2')->getParentId());

$this->assertEquals(12, $this->clusters('Root 2')->getLeft());
$this->assertEquals(13, $this->clusters('Root 2')->getRight());

$this->assertEquals(11, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());
}

public function testMakeFirstChildOfSwappingRootsWithSubtrees() {
$newRoot = Cluster::create(array('name' => 'Root 3'));

$this->clusters('Root 1')->makeFirstChildOf($newRoot);

$this->assertTrue(Cluster::isValid());

$this->assertEquals($newRoot->getKey(), $this->clusters('Root 1')->getParentId());

$this->assertEquals(4, $this->clusters('Root 1')->getLeft());
$this->assertEquals(13, $this->clusters('Root 1')->getRight());

$this->assertEquals(8, $this->clusters('Child 2.1')->getLeft());
$this->assertEquals(9, $this->clusters('Child 2.1')->getRight());
}

public function testMakeLastChildOf() {
$this->clusters('Child 1')->makeLastChildOf($this->clusters('Child 3'));

$this->assertEquals($this->clusters('Child 3'), $this->clusters('Child 1')->parent()->first());

$this->assertTrue(Cluster::isValid());
}

public function testMakeLastChildOfAppendsAtTheEnd() {
$newChild = Cluster::create(array('name' => 'Child 4'));

$newChild->makeLastChildOf($this->clusters('Root 1'));

$lastChild = $this->clusters('Root 1')->children()->get()->last();
$this->assertEquals($newChild, $lastChild);

$this->assertTrue(Cluster::isValid());
}

public function testMakeLastChildOfMovesWithSubtree() {
$this->clusters('Child 2')->makeLastChildOf($this->clusters('Child 1'));

$this->assertTrue(Cluster::isValid());

$this->assertEquals($this->clusters('Child 1')->getKey(), $this->clusters('Child 2')->getParentId());

$this->assertEquals(3, $this->clusters('Child 2')->getLeft());
$this->assertEquals(6, $this->clusters('Child 2')->getRight());

$this->assertEquals(2, $this->clusters('Child 1')->getLeft());
$this->assertEquals(7, $this->clusters('Child 1')->getRight());
}

public function testMakeLastChildOfSwappingRoots() {
$newRoot = Cluster::create(array('name' => 'Root 3'));

$this->assertEquals(13, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());

$this->clusters('Root 2')->makeLastChildOf($newRoot);

$this->assertTrue(Cluster::isValid());

$this->assertEquals($newRoot->getKey(), $this->clusters('Root 2')->getParentId());

$this->assertEquals(12, $this->clusters('Root 2')->getLeft());
$this->assertEquals(13, $this->clusters('Root 2')->getRight());

$this->assertEquals(11, $newRoot->getLeft());
$this->assertEquals(14, $newRoot->getRight());
}

public function testMakeLastChildOfSwappingRootsWithSubtrees() {
$newRoot = Cluster::create(array('name' => 'Root 3'));

$this->clusters('Root 1')->makeLastChildOf($newRoot);

$this->assertTrue(Cluster::isValid());

$this->assertEquals($newRoot->getKey(), $this->clusters('Root 1')->getParentId());

$this->assertEquals(4, $this->clusters('Root 1')->getLeft());
$this->assertEquals(13, $this->clusters('Root 1')->getRight());

$this->assertEquals(8, $this->clusters('Child 2.1')->getLeft());
$this->assertEquals(9, $this->clusters('Child 2.1')->getRight());
}

/**
* @expectedException Baum\MoveNotPossibleException
*/
Expand Down

0 comments on commit 8f6117a

Please sign in to comment.