diff --git a/tests/AnchorUtilitiesTest.php b/tests/AnchorUtilitiesTest.php new file mode 100644 index 0000000..da5a754 --- /dev/null +++ b/tests/AnchorUtilitiesTest.php @@ -0,0 +1,149 @@ + $expectedStart + * @param list $expectedClose + * @param list $expectedText + */ + #[DataProvider('provideComputeSamples')] + public function testCompute(string $html, array $expectedStart, array $expectedClose, array $expectedText): void + { + $result = AnchorUtilities::compute($html); + + $this->assertSame([$expectedStart, $expectedClose, $expectedText], $result); + } + + /** + * @return array, 2: list, 3: list}> + */ + public static function provideComputeSamples(): array + { + return [ + 'plain text' => [ + 'Just some plain text.', + [], + [], + [ + ['text' => 'Just some plain text.', 'depth' => 0], + ], + ], + 'single anchor' => [ + 'Text link text.', + [1], + [1], + [ + ['text' => 'Text ', 'depth' => 0], + ['text' => 'link', 'depth' => 1], + ['text' => ' text.', 'depth' => 0], + ], + ], + 'nested anchors' => [ + '
link1 link2 link1 again
', + [1, 2], + [2, 1], + [ + ['text' => 'link1 ', 'depth' => 1], + ['text' => 'link2', 'depth' => 2], + ['text' => ' link1 again', 'depth' => 1], + ], + ], + 'sibling anchors' => [ + 'first and second', + [1, 1], + [1, 1], + [ + ['text' => 'first', 'depth' => 1], + ['text' => ' and ', 'depth' => 0], + ['text' => 'second', 'depth' => 1], + ], + ], + 'anchor with attributes and uppercase' => [ + 'caps', + [1], + [1], + [ + ['text' => 'caps', 'depth' => 1], + ], + ], + 'self-closing anchor' => [ + 'Before After', + [1], + [1], + [ + ['text' => 'Before ', 'depth' => 0], + ['text' => ' After', 'depth' => 0], + ], + ], + 'unclosed anchor' => [ + 'Text link', + [1], + [], + [ + ['text' => 'Text ', 'depth' => 0], + ['text' => 'link', 'depth' => 1], + ], + ], + 'extra closing anchor' => [ + 'Text link', + [], + [], + [ + ['text' => 'Text ', 'depth' => 0], + ['text' => ' link', 'depth' => 0], + ], + ], + 'mixed tags' => [ + '

Title

A link here.

', + [1], + [1], + [ + ['text' => 'Title', 'depth' => 0], + ['text' => 'A ', 'depth' => 0], + ['text' => 'link', 'depth' => 1], + ['text' => ' here.', 'depth' => 0], + ], + ], + 'anchor tag starting with spaces' => [ + '< a href="x">space', + [1], + [1], + [ + ['text' => 'space', 'depth' => 1], + ], + ], + 'non-anchor tag closing with />' => [ + '
text', + [], + [], + [ + ['text' => 'text', 'depth' => 0], + ], + ], + 'empty token simulation' => [ + '', + [1], + [1], + [], + ], + 'tag-like text but not tags' => [ + 'a < b and c > d', + [], + [], + [ + ['text' => 'a ', 'depth' => 0], + ['text' => ' d', 'depth' => 0], + ], + ], + ]; + } +}