From b5c705ad04ec20d7468addcf33a633b8e51c5c34 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 29 Jun 2025 16:13:22 -0400 Subject: [PATCH 1/3] refactor: Update save logic in `NestedSetsBehavior` class to refresh node after saving. Add test for `left` and `right` attribute adjustment when appending child to root. --- src/NestedSetsBehavior.php | 5 +++- tests/NestedSetsBehaviorTest.php | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/NestedSetsBehavior.php b/src/NestedSetsBehavior.php index 4c63ce0..caca4f8 100644 --- a/src/NestedSetsBehavior.php +++ b/src/NestedSetsBehavior.php @@ -296,7 +296,10 @@ public function appendTo(ActiveRecord $node, bool $runValidation = true, array|n $this->operation = self::OPERATION_APPEND_TO; $this->node = $node; - return $this->getOwner()->save($runValidation, $attributes); + $result = $this->getOwner()->save($runValidation, $attributes); + $node->refresh(); + + return $result; } /** diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index d24645f..bd48d9f 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -1720,6 +1720,50 @@ public function testAppendChildNodeToRootCreatesValidTreeStructure(): void } } + public function testReturnShiftedLeftRightAttributesWhenChildAppendedToRoot(): void + { + $this->createDatabase(); + + $root = new Tree(['name' => 'Root']); + + $root->makeRoot(); + + $child = new Tree(['name' => 'Child']); + + $child->appendTo($root); + + self::assertEquals( + 1, + $root->lft, + 'Root node left value should be \'1\' after \'makeRoot\' and appending a child.', + ); + self::assertEquals( + 4, + $root->rgt, + 'Root node right value should be \'4\' after \'makeRoot\' and appending a child.', + ); + self::assertEquals( + 2, + $child->lft, + 'Child node left value should be \'2\' after being appended to the root node.', + ); + self::assertEquals( + 3, + $child->rgt, + 'Child node right value should be \'3\' after being appended to the root node.', + ); + self::assertNotEquals( + 0, + $child->lft, + 'Child node left value should not be \'0\' after \'appendTo\' operation.', + ); + self::assertNotEquals( + 1, + $child->rgt, + 'Child node right value should not be \'1\' after \'appendTo\' operation.', + ); + } + public function testThrowExceptionWhenAppendToParentWithNullRightValue(): void { $this->createDatabase(); From eb8cfad6284a6b86c31fd46693cdcbeba2d108cd Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 29 Jun 2025 16:21:15 -0400 Subject: [PATCH 2/3] refactor: Update save logic in `NestedSetsBehavior` class to refresh node only on successful save. --- src/NestedSetsBehavior.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/NestedSetsBehavior.php b/src/NestedSetsBehavior.php index caca4f8..3ae9c10 100644 --- a/src/NestedSetsBehavior.php +++ b/src/NestedSetsBehavior.php @@ -297,7 +297,10 @@ public function appendTo(ActiveRecord $node, bool $runValidation = true, array|n $this->node = $node; $result = $this->getOwner()->save($runValidation, $attributes); - $node->refresh(); + + if ($result === true) { + $node->refresh(); + } return $result; } From b64d4d3e5a2f2fca7d4c74d11599936e14a4031f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 29 Jun 2025 16:25:15 -0400 Subject: [PATCH 3/3] refactor: Refresh nodes after appending child to ensure correct state in tests. --- tests/NestedSetsBehaviorTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index bd48d9f..e00b854 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -1727,10 +1727,12 @@ public function testReturnShiftedLeftRightAttributesWhenChildAppendedToRoot(): v $root = new Tree(['name' => 'Root']); $root->makeRoot(); + $root->refresh(); $child = new Tree(['name' => 'Child']); $child->appendTo($root); + $child->refresh(); self::assertEquals( 1,