From a8497db242b80f821b3e97503cc6e940214e3a67 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Mon, 30 Jun 2025 06:24:03 -0400 Subject: [PATCH] feat: Add tests to include validation scenarios for `makeRoot` method. --- tests/NestedSetsBehaviorTest.php | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index cf5576e..f2a9872 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -1935,4 +1935,64 @@ public function testInsertBeforeWithRunValidationParameterUsingStrictValidation( 'Node should exist in database after inserting before target node with validation disabled.', ); } + + public function testMakeRootWithRunValidationParameterUsingStrictValidation(): void + { + $this->createDatabase(); + + $invalidNode = new TreeWithStrictValidation(['name' => 'x']); + + $result1 = $invalidNode->makeRoot(); + $hasError1 = $invalidNode->hasErrors(); + + self::assertFalse( + $result1, + '\'makeRoot()\' should return \'false\' when \'runValidation=true\' and data fails validation.', + ); + self::assertTrue( + $hasError1, + 'Node should have validation errors when \'runValidation=true\' and data is invalid.', + ); + + $invalidNode2 = new TreeWithStrictValidation(['name' => 'x']); + + $result2 = $invalidNode2->makeRoot(false); + $hasError2 = $invalidNode2->hasErrors(); + + self::assertTrue( + $result2, + '\'makeRoot()\' should return \'true\' when \'runValidation=false\', even with invalid data ' . + 'that would fail validation.', + ); + self::assertFalse( + $hasError2, + 'Node should not have validation errors when \'runValidation=false\' because validation was skipped.', + ); + + $persistedNode = TreeWithStrictValidation::findOne($invalidNode2->id); + + self::assertNotNull( + $persistedNode, + 'Node should exist in database after makeRoot with validation disabled.', + ); + self::assertTrue( + $persistedNode->isRoot(), + 'Node should be a root node after makeRoot operation.', + ); + self::assertEquals( + 1, + $persistedNode->lft, + 'Root node should have left value of 1.', + ); + self::assertEquals( + 2, + $persistedNode->rgt, + 'Root node should have right value of 2.', + ); + self::assertEquals( + 0, + $persistedNode->depth, + 'Root node should have depth of 0.', + ); + } }