diff --git a/README.md b/README.md index ed12187..75d9a91 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ In Tree you can find a basic but flexible tree data structure for php together with and an handful Builder class, that enables you to build tree in a fluent way. ## Changelog + - 0.2.3 Node::getAncestors now does not return the current node ([Issue 4](https://github.com/nicmart/Tree/issues/4)) - 0.2.2 Fixed a bug in the builder ([Issue 3](https://github.com/nicmart/Tree/issues/3)) - 0.2.1 root() and isRoot() methods - 0.2.0 Dropped php 5.3 support. Node implemented as a trait. diff --git a/src/Tree/Node/NodeTrait.php b/src/Tree/Node/NodeTrait.php index 12b8a15..31ca3e6 100644 --- a/src/Tree/Node/NodeTrait.php +++ b/src/Tree/Node/NodeTrait.php @@ -143,7 +143,7 @@ public function getParent() */ public function getAncestors() { - $parents = [$this]; + $parents = []; $node = $this; while ($parent = $node->getParent()) { array_unshift($parents, $parent); diff --git a/tests/Tree/Node/NodeTest.php b/tests/Tree/Node/NodeTest.php index 683cff9..15910d2 100644 --- a/tests/Tree/Node/NodeTest.php +++ b/tests/Tree/Node/NodeTest.php @@ -140,6 +140,16 @@ public function testRemoveAllChildren() $this->assertEmpty($root->getChildren()); } + public function testGetAncestors() + { + $root = new Node('r'); + $root->addChild($a = new Node('a')); + $a->addChild($b = new Node('b')); + $b->addChild($c = new Node('c')); + + $this->assertEquals([$root, $a, $b], $c->getAncestors()); + } + public function testIsLeaf() { $root = new Node;