From c12b8e79319a87d63059e55235b156e96c1f4bf5 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 29 Jun 2025 16:02:40 -0400 Subject: [PATCH 1/2] refactor: Validate `non-null` value for `right` attribute in `beforeInsertNode()` method of `NestedSetsBehavior` class. --- src/NestedSetsBehavior.php | 10 +++--- tests/NestedSetsBehaviorTest.php | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/NestedSetsBehavior.php b/src/NestedSetsBehavior.php index c2347b4..4c63ce0 100644 --- a/src/NestedSetsBehavior.php +++ b/src/NestedSetsBehavior.php @@ -1049,11 +1049,13 @@ protected function beforeInsertNode(int|null $value, int $depth): void throw new Exception('Can not create a node when the target node is root.'); } - if ($value !== null) { - $this->getOwner()->setAttribute($this->leftAttribute, $value); - $this->getOwner()->setAttribute($this->rightAttribute, $value + 1); + if ($value === null) { + throw new Exception('Value cannot be \'null\' in \'beforeInsertNode()\' method.'); } + $this->getOwner()->setAttribute($this->leftAttribute, $value); + $this->getOwner()->setAttribute($this->rightAttribute, $value + 1); + $nodeDepthValue = $this->node?->getAttribute($this->depthAttribute) ?? 0; $this->getOwner()->setAttribute($this->depthAttribute, $nodeDepthValue + $depth); @@ -1062,7 +1064,7 @@ protected function beforeInsertNode(int|null $value, int $depth): void $this->getOwner()->setAttribute($this->treeAttribute, $this->node->getAttribute($this->treeAttribute)); } - $this->shiftLeftRightAttribute($value ?? 0, 2); + $this->shiftLeftRightAttribute($value, 2); } /** diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index 4553b45..bd48d9f 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -1719,4 +1719,65 @@ public function testAppendChildNodeToRootCreatesValidTreeStructure(): void self::fail('Real insertion failed: ' . $e->getMessage()); } } + + 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(); + + $parentNode = new Tree(['name' => 'Parent Node']); + + $parentNode->makeRoot(); + $parentNode->setAttribute('rgt', null); + + $childNode = new Tree(['name' => 'Child Node']); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Value cannot be \'null\' in \'beforeInsertNode()\' method.'); + + $childNode->appendTo($parentNode); + } } From 04190e5c722081c93427503d75001dd3bdc7c288 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 29 Jun 2025 16:05:00 -0400 Subject: [PATCH 2/2] refactor: Remove redundant test for `left` and `right` attributes after appending child to root in `NestedSetsBehaviorTest` class. --- tests/NestedSetsBehaviorTest.php | 44 -------------------------------- 1 file changed, 44 deletions(-) diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index bd48d9f..d24645f 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -1720,50 +1720,6 @@ 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();