From 491457dddfc6c49070b3db8cfbd02cf521286bb0 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Mon, 30 Jun 2025 16:21:22 -0400 Subject: [PATCH 1/2] test: Add verification for correct order of root nodes in `MultipleTree` traversal. --- tests/NestedSetsBehaviorTest.php | 48 ++++++++++++--------------- tests/NestedSetsQueryBehaviorTest.php | 47 ++++++++++++++++++++++++++ tests/support/data/test-disorder.xml | 17 ---------- 3 files changed, 68 insertions(+), 44 deletions(-) delete mode 100644 tests/support/data/test-disorder.xml diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index 59bef74..f0aba27 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -2276,47 +2276,41 @@ public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void { $this->createDatabase(); - $command = $this->getDb()->createCommand(); - $xml = $this->loadFixtureXML('test-disorder.xml'); + $root = new Tree(['name' => 'Root']); - $children = $xml->children(); + $root->makeRoot(); - self::assertNotNull( - $children, - 'XML children should not be \'null\'.', - ); + $childB = new Tree(['name' => 'Child B']); + $childC = new Tree(['name' => 'Child C']); + $childA = new Tree(['name' => 'Child A']); - foreach ($children as $element => $treeElement) { - if ($element === 'tree') { - $command->insert( - 'tree', - [ - 'name' => (string) $treeElement['name'], - 'lft' => (int) $treeElement['lft'], - 'rgt' => (int) $treeElement['rgt'], - 'depth' => (int) $treeElement['depth'], - ], - )->execute(); - } - } + $childB->appendTo($root); + $childC->appendTo($root); + $childA->appendTo($root); - $root = Tree::findOne(1); + $command = $this->getDb()->createCommand(); - self::assertNotNull( - $root, - 'Root node with ID \'1\' should exist in the database.', - ); + $command->update('tree', ['lft' => 4, 'rgt' => 5], ['name' => 'Child B'])->execute(); + $command->update('tree', ['lft' => 6, 'rgt' => 7], ['name' => 'Child C'])->execute(); + $command->update('tree', ['lft' => 2, 'rgt' => 3], ['name' => 'Child A'])->execute(); + $command->update('tree', ['rgt' => 8], ['name' => 'Root'])->execute(); $childrenList = $root->children()->all(); - $expectedOrder = ['Child B', 'Child C', 'Child A']; + + $expectedOrder = ['Child A', 'Child B', 'Child C']; self::assertCount( 3, $childrenList, - 'Children list should contain exactly 3 elements.', + 'Children list should contain exactly \'3\' elements.', ); foreach ($childrenList as $index => $child) { + self::assertInstanceOf( + Tree::class, + $child, + "Child at index {$index} should be an instance of \'Tree\'.", + ); if (isset($expectedOrder[$index])) { self::assertEquals( $expectedOrder[$index], diff --git a/tests/NestedSetsQueryBehaviorTest.php b/tests/NestedSetsQueryBehaviorTest.php index abc9d84..81ce6df 100644 --- a/tests/NestedSetsQueryBehaviorTest.php +++ b/tests/NestedSetsQueryBehaviorTest.php @@ -70,4 +70,51 @@ public function testThrowLogicExceptionWhenBehaviorIsDetachedFromOwner(): void $behavior->leaves(); } + + public function testRootsMethodRequiresOrderByForCorrectTreeTraversal(): void + { + $this->createDatabase(); + + $rootD = new MultipleTree(['name' => 'Root D']); + $rootD->makeRoot(); + + $rootB = new MultipleTree(['name' => 'Root B']); + $rootB->makeRoot(); + + $rootA = new MultipleTree(['name' => 'Root A']); + $rootA->makeRoot(); + + $rootC = new MultipleTree(['name' => 'Root C']); + $rootC->makeRoot(); + + $rootsList = MultipleTree::find()->roots()->all(); + $expectedOrder = ['Root D', 'Root B', 'Root A', 'Root C']; + + self::assertCount( + 4, + $rootsList, + 'Roots list should contain exactly \'4\' elements.', + ); + + foreach ($rootsList as $index => $root) { + self::assertInstanceOf( + MultipleTree::class, + $root, + "Root at index {$index} should be an instance of \'MultipleTree\'.", + ); + self::assertArrayHasKey( + $index, + $expectedOrder, + "Expected order array should have key at index {$index}.", + ); + + if (isset($expectedOrder[$index])) { + self::assertEquals( + $expectedOrder[$index], + $root->getAttribute('name'), + "Root at index {$index} should be {$expectedOrder[$index]} in correct \'id\' order.", + ); + } + } + } } diff --git a/tests/support/data/test-disorder.xml b/tests/support/data/test-disorder.xml deleted file mode 100644 index fc6846f..0000000 --- a/tests/support/data/test-disorder.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - From af253320b79c607554e15333ec56ade01b5b3fa1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Mon, 30 Jun 2025 16:37:41 -0400 Subject: [PATCH 2/2] test: Refresh root node after updates to ensure correct children order in `NestedSetsBehavior`. --- tests/NestedSetsBehaviorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index f0aba27..fd7f55f 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -2295,6 +2295,7 @@ public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void $command->update('tree', ['lft' => 2, 'rgt' => 3], ['name' => 'Child A'])->execute(); $command->update('tree', ['rgt' => 8], ['name' => 'Root'])->execute(); + $root->refresh(); $childrenList = $root->children()->all(); $expectedOrder = ['Child A', 'Child B', 'Child C'];