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
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ enabled:

disabled:
- function_declaration
- new_with_parentheses
5 changes: 3 additions & 2 deletions src/NestedSetsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -1055,10 +1055,11 @@ protected function beforeInsertNode(int|null $value, int $depth): void
}

$nodeDepthValue = $this->node?->getAttribute($this->depthAttribute) ?? 0;

$this->getOwner()->setAttribute($this->depthAttribute, $nodeDepthValue + $depth);

if ($this->treeAttribute !== false) {
$this->getOwner()->setAttribute($this->treeAttribute, $this->node?->getAttribute($this->treeAttribute));
if ($this->treeAttribute !== false && $this->node !== null) {
$this->getOwner()->setAttribute($this->treeAttribute, $this->node->getAttribute($this->treeAttribute));
}

$this->shiftLeftRightAttribute($value ?? 0, 2);
Expand Down
99 changes: 99 additions & 0 deletions tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1620,4 +1620,103 @@ public function makeRoot(): bool

$node->makeRoot();
}

public function testSetNodeToNullAndCallBeforeInsertNodeSetsLftRgtAndDepth(): void
{
$this->createDatabase();

$behavior = new class extends NestedSetsBehavior {
public function callBeforeInsertNode(int|null $value, int $depth): void
{
$this->beforeInsertNode($value, $depth);
}

public function setNodeToNull(): void
{
$this->node = null;
}

public function getNodeDepth(): int|null
{
return $this->node !== null ? $this->node->getAttribute($this->depthAttribute) : null;
}
};

$newNode = new Tree(['name' => 'Test Node']);

$newNode->attachBehavior('testBehavior', $behavior);
$behavior->setNodeToNull();
$behavior->callBeforeInsertNode(5, 1);

self::assertEquals(
5,
$newNode->lft,
'\'beforeInsertNode\' should set \'lft\' attribute to \'5\' on the new node.',
);
self::assertEquals(
6,
$newNode->rgt,
'\'beforeInsertNode\' should set \'rgt\' attribute to \'6\' on the new node.',
);

$actualDepth = $newNode->getAttribute('depth');

self::assertEquals(
1,
$actualDepth,
'\'beforeInsertNode\' method should set \'depth\' attribute to \'1\' on the new node.',
);
}

public function testAppendChildNodeToRootCreatesValidTreeStructure(): void
{
$this->createDatabase();

$root = new Tree(['name' => 'Root']);

$root->makeRoot();

self::assertEquals(
1,
$root->lft,
'Root node left value should be \'1\' after \'makeRoot\'.',
);
self::assertEquals(
2,
$root->rgt,
'Root node right value should be \'2\' after \'makeRoot\'.',
);
self::assertEquals(
0,
$root->depth,
'Root node depth should be \'0\' after \'makeRoot\'.',
);

$child = new Tree(['name' => 'Child']);

try {
$result = $child->appendTo($root);

self::assertTrue(
$result,
'\'appendTo\' should return \'true\' when successfully appending a child node.',
);

$root->refresh();
$child->refresh();

self::assertGreaterThan(
$child->lft,
$child->rgt,
'Child node right value should be greater than its left value after \'appendTo\'.',
);
self::assertEquals(
1,
$child->depth,
'Child node depth should be \'1\' after being appended to the root node.',
);
} catch (Exception $e) {
self::fail('Real insertion failed: ' . $e->getMessage());
}
}
}