Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/NestedSetsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}