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..d24645f 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -1719,4 +1719,21 @@ public function testAppendChildNodeToRootCreatesValidTreeStructure(): void self::fail('Real insertion failed: ' . $e->getMessage()); } } + + 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); + } }