From 44a06ef9fb15e4c4617850d546a659adc0ec2671 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Tue, 13 Dec 2022 12:29:14 -0700 Subject: [PATCH] complete test coverage --- src/Containers/ContainerGroup.php | 19 ++-- src/Html5/Tags/ScriptTag.php | 2 +- tests/Containers/ContainerGroupTest.php | 33 +++++++ tests/Containers/GroupedContainerTest.php | 110 ++++++++++++++++++++++ 4 files changed, 154 insertions(+), 10 deletions(-) create mode 100644 tests/Containers/GroupedContainerTest.php diff --git a/src/Containers/ContainerGroup.php b/src/Containers/ContainerGroup.php index bcb6d3e..64c6c87 100644 --- a/src/Containers/ContainerGroup.php +++ b/src/Containers/ContainerGroup.php @@ -29,15 +29,16 @@ class ContainerGroup implements ContainerInterface, NodeInterface protected $limit = 0; /** + * @param int $limit * @return ContainerGroup */ - public static function catchAll(): ContainerGroup + public static function catchAll(int $limit = 0): ContainerGroup { return new ContainerGroup( function () { return true; }, - 0 + $limit ); } @@ -78,32 +79,32 @@ class ContainerGroup implements ContainerInterface, NodeInterface public function addChild(NodeInterface|Stringable|string $child, bool $prepend = false, bool $skip_sanitize = false): static { - if ($this->willAccept($child)) { - $this->makeRoomForChild($prepend); + if ($this->willAccept($child, false)) { $this->doAddChild($child, $prepend, $skip_sanitize); + $this->enforceChildLimit($prepend); } return $this; } public function addChildAfter(NodeInterface|Stringable|string $new_child, NodeInterface|Stringable|string $after_child, bool $skip_sanitize = false): static { - if ($this->willAccept($new_child)) { - $this->makeRoomForChild(false); + if ($this->willAccept($new_child, false)) { $this->doAddChildAfter($new_child, $after_child, $skip_sanitize); + $this->enforceChildLimit(false); } return $this; } public function addChildBefore(NodeInterface|Stringable|string $new_child, NodeInterface|Stringable|string $before_child, bool $skip_sanitize = false): static { - if ($this->willAccept($new_child)) { - $this->makeRoomForChild(true); + if ($this->willAccept($new_child, false)) { $this->doAddChildBefore($new_child, $before_child, $skip_sanitize); + $this->enforceChildLimit(true); } return $this; } - protected function makeRoomForChild(bool $remove_from_end): void + protected function enforceChildLimit(bool $remove_from_end): void { if ($this->limit > 0) { while (count($this->children) > $this->limit) { diff --git a/src/Html5/Tags/ScriptTag.php b/src/Html5/Tags/ScriptTag.php index b53c22f..1e71fd2 100644 --- a/src/Html5/Tags/ScriptTag.php +++ b/src/Html5/Tags/ScriptTag.php @@ -9,7 +9,7 @@ use ByJoby\HTML\Tags\AbstractContentTag; class ScriptTag extends AbstractContentTag implements MetadataContent, PhrasingContent, DisplayNone { - const TAG = 'noscript'; + const TAG = 'script'; public function setAsync(bool $async): static { diff --git a/tests/Containers/ContainerGroupTest.php b/tests/Containers/ContainerGroupTest.php index 023131b..abbf2b7 100644 --- a/tests/Containers/ContainerGroupTest.php +++ b/tests/Containers/ContainerGroupTest.php @@ -2,6 +2,10 @@ namespace ByJoby\HTML\Containers; +use ByJoby\HTML\Html5\Tags\LinkTag; +use ByJoby\HTML\Html5\Tags\ScriptTag; +use ByJoby\HTML\Nodes\Comment; +use ByJoby\HTML\Nodes\Text; use PHPUnit\Framework\TestCase; class ContainerGroupTest extends TestCase @@ -19,4 +23,33 @@ class ContainerGroupTest extends TestCase }); $this->assertFalse($group->willAccept('Foo')); } + + public function testOfTag() + { + $group = ContainerGroup::ofTag('script'); + $this->assertTrue($group->willAccept(new ScriptTag)); + $this->assertFalse($group->willAccept(new LinkTag)); + $this->assertFalse($group->willAccept(new Comment('comment'))); + $this->assertFalse($group->willAccept(new Text('comment'))); + } + + public function testLimits() + { + $group = ContainerGroup::catchAll(2); + $group->addChild('a'); + $group->addChild('b'); + $this->assertEquals('a' . PHP_EOL . 'b', $group->__toString()); + // appending a child drops the first one + $group->addChild('c'); + $this->assertEquals('b' . PHP_EOL . 'c', $group->__toString()); + // prepending a child drops the last one + $group->addChild('d', true); + $this->assertEquals('d' . PHP_EOL . 'b', $group->__toString()); + // adding a child before another drops the last child + $group->addChildBefore('e', 'b'); + $this->assertEquals('d' . PHP_EOL . 'e', $group->__toString()); + // adding a child after another drops the first child + $group->addChildAfter('f', 'e'); + $this->assertEquals('e' . PHP_EOL . 'f', $group->__toString()); + } } diff --git a/tests/Containers/GroupedContainerTest.php b/tests/Containers/GroupedContainerTest.php new file mode 100644 index 0000000..c0cdfbd --- /dev/null +++ b/tests/Containers/GroupedContainerTest.php @@ -0,0 +1,110 @@ +assertFalse($container->willAccept($script)); + $this->assertFalse($container->willAccept($style)); + $this->assertFalse($container->willAccept($link)); + $this->assertFalse($container->willAccept($base)); + // also ensure none of these accept non-tag node + $this->assertFalse($container->willAccept(new Comment('test comment'))); + // add group to hold style tags + $container->addGroup($styleGroup = ContainerGroup::ofTag('style')); + $this->assertFalse($container->willAccept($script)); + $this->assertTrue($container->willAccept($style)); + $this->assertFalse($container->willAccept($link)); + $this->assertFalse($container->willAccept($base)); + // add group to hold link tags by class + $container->addGroupBefore($linkGroup = ContainerGroup::ofClass(LinkTag::class), $styleGroup); + $this->assertFalse($container->willAccept($script)); + $this->assertTrue($container->willAccept($style)); + $this->assertTrue($container->willAccept($link)); + $this->assertFalse($container->willAccept($base)); + // add group to hold script tags by class + $container->addGroupAfter($scriptGroup = ContainerGroup::ofClass(ScriptTag::class), $styleGroup); + $this->assertTrue($container->willAccept($script)); + $this->assertTrue($container->willAccept($style)); + $this->assertTrue($container->willAccept($link)); + $this->assertFalse($container->willAccept($base)); + // add catch-all group + $container->addGroup($catchAll = ContainerGroup::catchAll()); + $this->assertTrue($container->willAccept($script)); + $this->assertTrue($container->willAccept($style)); + $this->assertTrue($container->willAccept($link)); + $this->assertTrue($container->willAccept($base)); + // add script tag + $container->addChild($script); + $this->assertEquals($script->__toString(), $container->__toString()); + // add style tag (it's before the style tag) + $container->addChild($style); + $this->assertEquals( + implode(PHP_EOL, [ + $style->__toString(), + $script->__toString(), + ]), + $container->__toString() + ); + // add link tag (it's after the style tag) + $container->addChild($link); + $this->assertEquals( + implode(PHP_EOL, [ + $link->__toString(), + $style->__toString(), + $script->__toString(), + ]), + $container->__toString() + ); + // add base (it's at the beginning) + $container->addChild($base); + $this->assertEquals( + implode(PHP_EOL, [ + $link->__toString(), + $style->__toString(), + $script->__toString(), + $base->__toString(), + ]), + $container->__toString() + ); + } + + public function testContains() + { + $container = new GroupedContainer(); + $this->assertFalse($container->contains('a')); + $container->addGroup(ContainerGroup::catchAll()); + $container->addChild('a'); + $this->assertTrue($container->contains('a')); + $container->addChild($script = new ScriptTag); + $this->assertTrue($container->contains($script)); + } + + public function testAddAndRemoveChild() + { + $container = new GroupedContainer(); + $container->addGroup(ContainerGroup::catchAll()); + $container->addChild('a'); + $container->addChildBefore('b', 'a'); + $container->addChildAfter('c', 'b'); + $this->assertEquals(implode(PHP_EOL, ['b', 'c', 'a']), $container->__toString()); + // remove child c + $container->removeChild('c'); + $this->assertEquals(implode(PHP_EOL, ['b', 'a']), $container->__toString()); + } +}