|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace yii2\extensions\nestedsets\tests\mysql; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Group; |
| 8 | +use yii2\extensions\nestedsets\tests\support\model\MultipleTree; |
| 9 | +use yii2\extensions\nestedsets\tests\TestCase; |
| 10 | + |
| 11 | +#[Group('mutation')] |
| 12 | +final class MutationTest extends TestCase |
| 13 | +{ |
| 14 | + protected string $driverName = 'mysql'; |
| 15 | + protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4'; |
| 16 | + protected string $password = 'root'; |
| 17 | + protected string $username = 'root'; |
| 18 | + |
| 19 | + public function testLeavesMethodRequiresLeftAttributeOrderingForConsistentResults(): void |
| 20 | + { |
| 21 | + $this->createDatabase(); |
| 22 | + |
| 23 | + $root = new MultipleTree(['name' => 'Root']); |
| 24 | + |
| 25 | + $root->makeRoot(); |
| 26 | + |
| 27 | + $leaf1 = new MultipleTree(['name' => 'Leaf A']); |
| 28 | + |
| 29 | + $leaf1->appendTo($root); |
| 30 | + |
| 31 | + $leaf2 = new MultipleTree(['name' => 'Leaf B']); |
| 32 | + |
| 33 | + $leaf2->appendTo($root); |
| 34 | + |
| 35 | + $initialLeaves = MultipleTree::find()->leaves()->all(); |
| 36 | + |
| 37 | + self::assertCount( |
| 38 | + 2, |
| 39 | + $initialLeaves, |
| 40 | + "Should have exactly '2' initial leaf nodes.", |
| 41 | + ); |
| 42 | + |
| 43 | + $command = $this->getDb()->createCommand(); |
| 44 | + |
| 45 | + $command->update('multiple_tree', ['lft' => 3, 'rgt' => 4], ['name' => 'Leaf B'])->execute(); |
| 46 | + $command->update('multiple_tree', ['lft' => 5, 'rgt' => 6], ['name' => 'Leaf A'])->execute(); |
| 47 | + $command->update('multiple_tree', ['lft' => 1, 'rgt' => 7], ['name' => 'Root'])->execute(); |
| 48 | + |
| 49 | + $leaves = MultipleTree::find()->leaves()->all(); |
| 50 | + |
| 51 | + /** @phpstan-var array<array{name: string, lft: int}> */ |
| 52 | + $expectedLeaves = [ |
| 53 | + ['name' => 'Leaf B', 'lft' => 3], |
| 54 | + ['name' => 'Leaf A', 'lft' => 5], |
| 55 | + ]; |
| 56 | + |
| 57 | + self::assertCount( |
| 58 | + 2, |
| 59 | + $leaves, |
| 60 | + "Should return exactly '2' leaf nodes.", |
| 61 | + ); |
| 62 | + |
| 63 | + foreach ($leaves as $index => $leaf) { |
| 64 | + self::assertInstanceOf( |
| 65 | + MultipleTree::class, |
| 66 | + $leaf, |
| 67 | + "Leaf at index {$index} should be an instance of 'MultipleTree'.", |
| 68 | + ); |
| 69 | + |
| 70 | + if (isset($expectedLeaves[$index])) { |
| 71 | + self::assertEquals( |
| 72 | + $expectedLeaves[$index]['name'], |
| 73 | + $leaf->getAttribute('name'), |
| 74 | + "Leaf at index {$index} should be {$expectedLeaves[$index]['name']} in correct order.", |
| 75 | + ); |
| 76 | + self::assertEquals( |
| 77 | + $expectedLeaves[$index]['lft'], |
| 78 | + $leaf->getAttribute('lft'), |
| 79 | + "Leaf at index {$index} should have left value {$expectedLeaves[$index]['lft']}.", |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments