Skip to content

Commit 81fbc60

Browse files
committed
feat: Add tests to include validation scenarios for insertAfter method.
1 parent 3e0efc1 commit 81fbc60

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

tests/NestedSetsBehaviorTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,4 +1830,56 @@ public function testAppendToWithRunValidationParameterUsingStrictValidation(): v
18301830
'Node should exist in database after appending to target node with validation disabled.',
18311831
);
18321832
}
1833+
1834+
public function testInsertAfterWithRunValidationParameterUsingStrictValidation(): void
1835+
{
1836+
$this->generateFixtureTree();
1837+
1838+
$targetNode = Tree::findOne(9);
1839+
1840+
self::assertNotNull(
1841+
$targetNode,
1842+
'Target node with ID \'9\' should exist before calling \'insertAfter\'.',
1843+
);
1844+
self::assertFalse(
1845+
$targetNode->isRoot(),
1846+
'Target node with ID \'9\' should not be root for \'insertAfter\' operation.',
1847+
);
1848+
1849+
$invalidNode = new TreeWithStrictValidation(['name' => 'x']);
1850+
1851+
$result1 = $invalidNode->insertAfter($targetNode);
1852+
$hasError1 = $invalidNode->hasErrors();
1853+
1854+
self::assertFalse(
1855+
$result1,
1856+
'\'insertAfter()\' should return \'false\' when \'runValidation=true\' and data fails validation.',
1857+
);
1858+
self::assertTrue(
1859+
$hasError1,
1860+
'Node should have validation errors when \'runValidation=true\' and data is invalid.',
1861+
);
1862+
1863+
$invalidNode2 = new TreeWithStrictValidation(['name' => 'x']);
1864+
1865+
$result2 = $invalidNode2->insertAfter($targetNode, false);
1866+
$hasError2 = $invalidNode2->hasErrors();
1867+
1868+
self::assertTrue(
1869+
$result2,
1870+
'\'insertAfter()\' should return \'true\' when \'runValidation=false\', even with invalid data ' .
1871+
'that would fail validation.',
1872+
);
1873+
self::assertFalse(
1874+
$hasError2,
1875+
'Node should not have validation errors when \'runValidation=false\' because validation was skipped.',
1876+
);
1877+
1878+
$persistedNode = TreeWithStrictValidation::findOne($invalidNode2->id);
1879+
1880+
self::assertNotNull(
1881+
$persistedNode,
1882+
'Node should exist in database after inserting after target node with validation disabled.',
1883+
);
1884+
}
18331885
}

0 commit comments

Comments
 (0)