From 3ac07135f3c8bc3037456175a1548172c53c42ac Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Wed, 8 Feb 2017 15:23:14 +1000 Subject: [PATCH 1/2] Test moveNodeBelow is reentrant --- tests/Functional/DbalNestedSetTest.php | 55 +++++++++++++++----------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/tests/Functional/DbalNestedSetTest.php b/tests/Functional/DbalNestedSetTest.php index a39b2f1c..19e1cb06 100644 --- a/tests/Functional/DbalNestedSetTest.php +++ b/tests/Functional/DbalNestedSetTest.php @@ -320,30 +320,11 @@ public function testMoveSubTreeBelow() { $node = $this->nestedSet->getNode(7, 1); $this->nestedSet->moveSubTreeBelow($parent, $node); + $this->assertNodeMovedBelow(); - // Check node is in new position. $node = $this->nestedSet->getNode(7, 1); - $this->assertEquals(2, $node->getLeft()); - $this->assertEquals(7, $node->getRight()); - $this->assertEquals(1, $node->getDepth()); - - // Check children are in new position. - $node = $this->nestedSet->getNode(10, 1); - $this->assertEquals(3, $node->getLeft()); - $this->assertEquals(4, $node->getRight()); - $this->assertEquals(2, $node->getDepth()); - - $node = $this->nestedSet->getNode(11, 1); - $this->assertEquals(5, $node->getLeft()); - $this->assertEquals(6, $node->getRight()); - $this->assertEquals(2, $node->getDepth()); - - // Check old parent is updated. - $node = $this->nestedSet->getNode(3, 1); - $this->assertEquals(16, $node->getLeft()); - $this->assertEquals(21, $node->getRight()); - $this->assertEquals(1, $node->getDepth()); - + $this->nestedSet->moveSubTreeBelow($parent, $node); + $this->assertNodeMovedBelow(); } /** @@ -552,7 +533,35 @@ public function printTree($tree) { $node->getDepth(), ]); } - echo $table->getTable(); + echo PHP_EOL . $table->getTable(); + } + + /** + * Assert node correctly moved. + */ + protected function assertNodeMovedBelow() { + // Check node is in new position. + $node = $this->nestedSet->getNode(7, 1); + $this->assertEquals(2, $node->getLeft()); + $this->assertEquals(7, $node->getRight()); + $this->assertEquals(1, $node->getDepth()); + + // Check children are in new position. + $node = $this->nestedSet->getNode(10, 1); + $this->assertEquals(3, $node->getLeft()); + $this->assertEquals(4, $node->getRight()); + $this->assertEquals(2, $node->getDepth()); + + $node = $this->nestedSet->getNode(11, 1); + $this->assertEquals(5, $node->getLeft()); + $this->assertEquals(6, $node->getRight()); + $this->assertEquals(2, $node->getDepth()); + + // Check old parent is updated. + $node = $this->nestedSet->getNode(3, 1); + $this->assertEquals(16, $node->getLeft()); + $this->assertEquals(21, $node->getRight()); + $this->assertEquals(1, $node->getDepth()); } } From c8654bbcd8b07cbaf56b81a9182ffbf01ca765db Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Wed, 8 Feb 2017 15:26:54 +1000 Subject: [PATCH 2/2] Test for moving before being reentrant --- tests/Functional/DbalNestedSetTest.php | 54 +++++++++++++++----------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/tests/Functional/DbalNestedSetTest.php b/tests/Functional/DbalNestedSetTest.php index 19e1cb06..f547d8a8 100644 --- a/tests/Functional/DbalNestedSetTest.php +++ b/tests/Functional/DbalNestedSetTest.php @@ -336,30 +336,12 @@ public function testMoveSubTreeBefore() { $node = $this->nestedSet->getNode(7, 1); $this->nestedSet->moveSubTreeBefore($target, $node); + $this->assertNodeMovedBefore(); - // Check node is in new position. $node = $this->nestedSet->getNode(7, 1); - $this->assertEquals(3, $node->getLeft()); - $this->assertEquals(8, $node->getRight()); - $this->assertEquals(2, $node->getDepth()); - - // Check children are in new position. - $node = $this->nestedSet->getNode(10, 1); - $this->assertEquals(4, $node->getLeft()); - $this->assertEquals(5, $node->getRight()); - $this->assertEquals(3, $node->getDepth()); - - $node = $this->nestedSet->getNode(11, 1); - $this->assertEquals(6, $node->getLeft()); - $this->assertEquals(7, $node->getRight()); - $this->assertEquals(3, $node->getDepth()); - - // Check old parent is updated. - $node = $this->nestedSet->getNode(3, 1); - $this->assertEquals(16, $node->getLeft()); - $this->assertEquals(21, $node->getRight()); - $this->assertEquals(1, $node->getDepth()); + $this->nestedSet->moveSubTreeBefore($target, $node); + $this->assertNodeMovedBefore(); } /** @@ -537,7 +519,7 @@ public function printTree($tree) { } /** - * Assert node correctly moved. + * Assert node correctly moved below. */ protected function assertNodeMovedBelow() { // Check node is in new position. @@ -564,4 +546,32 @@ protected function assertNodeMovedBelow() { $this->assertEquals(1, $node->getDepth()); } + /** + * Assert node correctly moved before. + */ + protected function assertNodeMovedBefore() { + // Check node is in new position. + $node = $this->nestedSet->getNode(7, 1); + $this->assertEquals(3, $node->getLeft()); + $this->assertEquals(8, $node->getRight()); + $this->assertEquals(2, $node->getDepth()); + + // Check children are in new position. + $node = $this->nestedSet->getNode(10, 1); + $this->assertEquals(4, $node->getLeft()); + $this->assertEquals(5, $node->getRight()); + $this->assertEquals(3, $node->getDepth()); + + $node = $this->nestedSet->getNode(11, 1); + $this->assertEquals(6, $node->getLeft()); + $this->assertEquals(7, $node->getRight()); + $this->assertEquals(3, $node->getDepth()); + + // Check old parent is updated. + $node = $this->nestedSet->getNode(3, 1); + $this->assertEquals(16, $node->getLeft()); + $this->assertEquals(21, $node->getRight()); + $this->assertEquals(1, $node->getDepth()); + } + }