From e533a10b1c26c3d6a03af9cdb95d7f0a3c81da5c Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Mon, 30 Jun 2025 12:09:47 -0400 Subject: [PATCH 1/3] test: Add method to verify children order in tree traversal with disordered `XML` data. --- tests/NestedSetsBehaviorTest.php | 49 ++++++++++++++++++++++++++++ tests/support/data/test-disorder.xml | 17 ++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/support/data/test-disorder.xml diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index ac6adbd..4ec1699 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -2271,4 +2271,53 @@ public function testProtectedMoveNodeAsRootRemainsAccessibleToSubclasses(): void '\'moveNodeAsRoot\' method should remain protected to allow subclass customization.', ); } + + public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void + { + $this->createDatabase(); + + $command = $this->getDb()->createCommand(); + $xml = $this->loadFixtureXML('test-disorder.xml'); + + $children = $xml->children(); + + self::assertNotNull( + $children, + 'XML children should not be \'null\'.', + ); + + 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(); + } + } + + $root = Tree::findOne(1); + + self::assertNotNull( + $root, + 'Root node with ID \'1\' should exist in the database.', + ); + + $childrenList = $root->children()->all(); + $expectedOrder = ['Child B', 'Child C', 'Child A']; + + for ($i = 0; $i < 3; $i++) { + if (isset($childrenList[$i]) === true) { + self::assertEquals( + $expectedOrder[$i], + $childrenList[$i]->getAttribute('name'), + "Child at index {$i} should be {$expectedOrder[$i]} in correct \'lft\' order.", + ); + } + } + } } diff --git a/tests/support/data/test-disorder.xml b/tests/support/data/test-disorder.xml new file mode 100644 index 0000000..fc6846f --- /dev/null +++ b/tests/support/data/test-disorder.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + From 4655cb0da30e46fe4e709a1f1d54ee8d26f9d345 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Mon, 30 Jun 2025 12:16:50 -0400 Subject: [PATCH 2/3] test: Add assertion to verify the count of children in the list after tree traversal. --- tests/NestedSetsBehaviorTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index 4ec1699..d8e7a1f 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -2310,6 +2310,12 @@ public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void $childrenList = $root->children()->all(); $expectedOrder = ['Child B', 'Child C', 'Child A']; + self::assertCount( + 3, + $childrenList, + 'Children list should contain exactly 3 elements.', + ); + for ($i = 0; $i < 3; $i++) { if (isset($childrenList[$i]) === true) { self::assertEquals( From 0937cb7106ed06d8900b2c3ba151b60465aeb4db Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Mon, 30 Jun 2025 12:28:05 -0400 Subject: [PATCH 3/3] test: Refactor assertion loop for verifying children order in tree traversal. --- tests/NestedSetsBehaviorTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index d8e7a1f..59bef74 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -2316,12 +2316,12 @@ public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void 'Children list should contain exactly 3 elements.', ); - for ($i = 0; $i < 3; $i++) { - if (isset($childrenList[$i]) === true) { + foreach ($childrenList as $index => $child) { + if (isset($expectedOrder[$index])) { self::assertEquals( - $expectedOrder[$i], - $childrenList[$i]->getAttribute('name'), - "Child at index {$i} should be {$expectedOrder[$i]} in correct \'lft\' order.", + $expectedOrder[$index], + $child->getAttribute('name'), + "Child at index {$index} should be {$expectedOrder[$index]} in correct \'lft\' order.", ); } }