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
49 changes: 22 additions & 27 deletions tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2276,47 +2276,42 @@ 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();

$root->refresh();
$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],
Expand Down
47 changes: 47 additions & 0 deletions tests/NestedSetsQueryBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
);
}
}
}
}
17 changes: 0 additions & 17 deletions tests/support/data/test-disorder.xml

This file was deleted.