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
53 changes: 27 additions & 26 deletions tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public function testReturnTrueAndMatchXmlAfterAppendToMultipleTreeWhenTargetIsIn

self::assertNotNull(
$node,
'Node with ID \'9\' must exist before attempting to append to a node in another tree.',
'Node with ID \'9\' must exist before attempting to \'appendTo()\' a node in another tree.',
);

$node->name = 'Updated node 2';
Expand All @@ -632,7 +632,7 @@ public function testReturnTrueAndMatchXmlAfterAppendToMultipleTreeWhenTargetIsIn

self::assertNotNull(
$childOfNode,
'Target node with ID \'53\' must exist before attempting to append to it.',
'Target node with ID \'53\' must exist before attempting to \'appendTo()\' it.',
);

self::assertTrue(
Expand Down Expand Up @@ -719,7 +719,7 @@ public function testReturnTrueAndMatchXmlAfterInsertBeforeUpForTreeAndMultipleTr

self::assertNotNull(
$node,
'Node with ID \'9\' must exist before calling insertBefore() on another node.',
'Node with ID \'9\' must exist before calling \'insertBefore()\' on another node.',
);

$node->name = 'Updated node 2';
Expand All @@ -728,7 +728,7 @@ public function testReturnTrueAndMatchXmlAfterInsertBeforeUpForTreeAndMultipleTr

self::assertNotNull(
$childOfNode,
'Target node with ID \'2\' must exist before calling insertBefore() on it.',
'Target node with ID \'2\' must exist before calling \'insertBefore()\' on it.',
);
self::assertTrue(
$node->insertBefore($childOfNode),
Expand All @@ -739,7 +739,7 @@ public function testReturnTrueAndMatchXmlAfterInsertBeforeUpForTreeAndMultipleTr

self::assertNotNull(
$node,
'Node with ID \'31\' must exist before calling insertBefore() on another node.',
'Node with ID \'31\' must exist before calling \'insertBefore()\' on another node.',
);

$node->name = 'Updated node 2';
Expand All @@ -748,19 +748,19 @@ public function testReturnTrueAndMatchXmlAfterInsertBeforeUpForTreeAndMultipleTr

self::assertNotNull(
$childOfNode,
'Target node with ID \'24\' must exist before calling insertBefore() on it.',
'Target node with ID \'24\' must exist before calling \'insertBefore()\' on it.',
);
self::assertTrue(
$node->insertBefore($childOfNode),
'insertBefore() should return true when moving node \'31\' before node \'24\' in MultipleTree.',
'\'insertBefore()\' should return \'true\' when moving node \'31\' before node \'24\' in \'MultipleTree\'.',
);

$simpleXML = $this->loadFixtureXML('test-insert-before-exists-up.xml');

self::assertEquals(
$this->buildFlatXMLDataSet($this->getDataSet()),
$simpleXML->asXML(),
'Resulting dataset after insertBefore() must match the expected XML structure.',
'Resulting dataset after \'insertBefore()\' must match the expected XML structure.',
);
}

Expand All @@ -772,7 +772,7 @@ public function testReturnTrueAndMatchXmlAfterInsertBeforeDownForTreeAndMultiple

self::assertNotNull(
$node,
'Node with ID \'9\' should exist before calling insertBefore() on another node.',
'Node with ID \'9\' should exist before calling \'insertBefore()\' on another node.',
);

$node->name = 'Updated node 2';
Expand All @@ -781,18 +781,18 @@ public function testReturnTrueAndMatchXmlAfterInsertBeforeDownForTreeAndMultiple

self::assertNotNull(
$childOfNode,
'Target node with ID \'16\' should exist before calling insertBefore() on it.',
'Target node with ID \'16\' should exist before calling \'insertBefore()\' on it.',
);
self::assertTrue(
$node->insertBefore($childOfNode),
'insertBefore() should return true when moving node \'9\' before node \'16\' in Tree.',
'\'insertBefore()\' should return \'true\' when moving node \'9\' before node \'16\' in \'Tree\'.',
);

$node = MultipleTree::findOne(31);

self::assertNotNull(
$node,
'Node with ID \'31\' should exist before calling insertBefore() on another node.',
'Node with ID \'31\' should exist before calling \'insertBefore()\' on another node.',
);

$node->name = 'Updated node 2';
Expand All @@ -801,19 +801,19 @@ public function testReturnTrueAndMatchXmlAfterInsertBeforeDownForTreeAndMultiple

self::assertNotNull(
$childOfNode,
'Target node with ID \'38\' should exist before calling insertBefore() on it.',
'Target node with ID \'38\' should exist before calling \'insertBefore()\' on it.',
);
self::assertTrue(
$node->insertBefore($childOfNode),
'insertBefore() should return true when moving node \'31\' before node \'38\' in MultipleTree.',
'\'insertBefore()\' should return \'true\' when moving node \'31\' before node \'38\' in \'MultipleTree\'.',
);

$simpleXML = $this->loadFixtureXML('test-insert-before-exists-down.xml');

self::assertEquals(
$this->buildFlatXMLDataSet($this->getDataSet()),
$simpleXML->asXML(),
'Resulting dataset after insertBefore() must match the expected XML structure.',
'Resulting dataset after \'insertBefore()\' must match the expected XML structure.',
);
}

Expand Down 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