Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 12 additions & 11 deletions tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ public function testAppendChildNodeToRootCreatesValidTreeStructure(): void
self::assertEquals(
1,
$child->depth,
'Child node depth should be \'1\' after being appended to the root node.',
'Child node depth should be \'1\' after being \'appendTo\' the root node.',
);
} catch (Exception $e) {
self::fail('Real insertion failed: ' . $e->getMessage());
Expand Down Expand Up @@ -1753,12 +1753,12 @@ public function testReturnShiftedLeftRightAttributesWhenChildAppendedToRoot(): v
self::assertEquals(
2,
$child->lft,
'Child node left value should be \'2\' after being appended to the root node.',
'Child node left value should be \'2\' after being \'appendTo\' to the root node.',
);
self::assertEquals(
3,
$child->rgt,
'Child node right value should be \'3\' after being appended to the root node.',
'Child node right value should be \'3\' after being \'appendTo\' the root node.',
);
self::assertNotEquals(
0,
Expand Down Expand Up @@ -1979,26 +1979,26 @@ public function testMakeRootWithRunValidationParameterUsingStrictValidation(): v

self::assertNotNull(
$persistedNode,
'Node should exist in database after makeRoot with validation disabled.',
'Node should exist in database after \'makeRoot\' with validation disabled.',
);
self::assertTrue(
$persistedNode->isRoot(),
'Node should be a root node after makeRoot operation.',
'Node should be a root node after \'makeRoot\' operation.',
);
self::assertEquals(
1,
$persistedNode->lft,
'Root node should have left value of 1.',
'Root node should have left value of \'1\'.',
);
self::assertEquals(
2,
$persistedNode->rgt,
'Root node should have right value of 2.',
'Root node should have right value of \'2\'.',
);
self::assertEquals(
0,
$persistedNode->depth,
'Root node should have depth of 0.',
'Root node should have depth of \'0\'.',
);
}

Expand All @@ -2021,7 +2021,7 @@ public function testPrependToWithRunValidationParameterUsingStrictValidation():

self::assertFalse(
$resultWithValidation,
"\'prependTo()\' with \'runValidation=true\' should return \'false\' when validation fails.",
'\'prependTo()\' with \'runValidation=true\' should return \'false\' when validation fails.',
);
self::assertTrue(
$hasError1,
Expand Down Expand Up @@ -2310,13 +2310,14 @@ public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void
self::assertInstanceOf(
Tree::class,
$child,
"Child at index {$index} should be an instance of \'Tree\'.",
"Child at index {$index} should be an instance of 'Tree'.",
);

if (isset($expectedOrder[$index])) {
self::assertEquals(
$expectedOrder[$index],
$child->getAttribute('name'),
"Child at index {$index} should be {$expectedOrder[$index]} in correct \'lft\' order.",
"Child at index {$index} should be {$expectedOrder[$index]} in correct 'lft' order.",
);
}
}
Expand Down
71 changes: 69 additions & 2 deletions tests/NestedSetsQueryBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,80 @@ public function testRootsMethodRequiresOrderByForCorrectTreeTraversal(): void
self::assertInstanceOf(
MultipleTree::class,
$root,
"Root at index {$index} should be an instance of \'MultipleTree\'.",
"Root at index {$index} should be an instance of 'MultipleTree'.",
);

if (isset($expectedOrder[$index])) {
self::assertEquals(
$expectedOrder[$index],
$root->getAttribute('name'),
"Root at index {$index} should be {$expectedOrder[$index]} in correct \'tree\' order.",
"Root at index {$index} should be {$expectedOrder[$index]} in correct 'tree' order.",
);
}
}
}

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

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

$root->makeRoot();

$leaf1 = new MultipleTree(['name' => 'Leaf A']);

$leaf1->appendTo($root);

$leaf2 = new MultipleTree(['name' => 'Leaf B']);

$leaf2->appendTo($root);

$initialLeaves = MultipleTree::find()->leaves()->all();

self::assertCount(
2,
$initialLeaves,
'Should have exactly \'2\' initial leaf nodes.',
);

$command = $this->getDb()->createCommand();

$command->update('multiple_tree', ['lft' => 3, 'rgt' => 4], ['name' => 'Leaf B'])->execute();
$command->update('multiple_tree', ['lft' => 5, 'rgt' => 6], ['name' => 'Leaf A'])->execute();
$command->update('multiple_tree', ['lft' => 1, 'rgt' => 7], ['name' => 'Root'])->execute();

$leaves = MultipleTree::find()->leaves()->all();

/** @phpstan-var array<array{name: string, lft: int}> */
$expectedLeaves = [
['name' => 'Leaf B', 'lft' => 3],
['name' => 'Leaf A', 'lft' => 5],
];

self::assertCount(
2,
$leaves,
'Should return exactly \'2\' leaf nodes.',
);

foreach ($leaves as $index => $leaf) {
self::assertInstanceOf(
MultipleTree::class,
$leaf,
"Leaf at index {$index} should be an instance of 'MultipleTree'.",
);

if (isset($expectedLeaves[$index])) {
self::assertEquals(
$expectedLeaves[$index]['name'],
$leaf->getAttribute('name'),
"Leaf at index {$index} should be {$expectedLeaves[$index]['name']} in correct order.",
);
self::assertEquals(
$expectedLeaves[$index]['lft'],
$leaf->getAttribute('lft'),
"Leaf at index {$index} should have left value {$expectedLeaves[$index]['lft']}.",
);
}
}
Expand Down