Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and add more unit tests for the sidebar table of contents generator #1342

Merged
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection HtmlUnknownAnchorTarget */

declare(strict_types=1);

namespace Hyde\Framework\Testing\Feature\Actions;
Expand Down Expand Up @@ -30,11 +32,172 @@ public function testCanGenerateTableOfContents()

public function testReturnStringContainsExpectedContent()
{
$markdown = "# Level 1\n## Level 2\n### Level 3\n";
$result = (new GeneratesTableOfContents($markdown))->execute();
$markdown = <<<'MARKDOWN'
# Level 1
## Level 2
### Level 3
MARKDOWN;

$this->assertSameIgnoringIndentation(<<<'HTML'
<ul class="table-of-contents">
<li>
<a href="#level-2">Level 2</a>
<ul>
<li>
<a href="#level-3">Level 3</a>
</li>
</ul>
</li>
</ul>
HTML, (new GeneratesTableOfContents($markdown))->execute()
);
}

public function testNonHeadingMarkdownIsRemoved()
{
$expected = <<<'MARKDOWN'
# Level 1
## Level 2
### Level 3
MARKDOWN;

$actual = <<<'MARKDOWN'
# Level 1
Foo bar
## Level 2
Bar baz
### Level 3
Baz foo
MARKDOWN;

$this->assertSame(
(new GeneratesTableOfContents($expected))->execute(),
(new GeneratesTableOfContents($actual))->execute()
);
}

public function testWithNoLevelOneHeading()
{
$markdown = <<<'MARKDOWN'
## Level 2
### Level 3
MARKDOWN;

$this->assertSameIgnoringIndentation(<<<'HTML'
<ul class="table-of-contents">
<li>
<a href="#level-2">Level 2</a>
<ul>
<li>
<a href="#level-3">Level 3</a>
</li>
</ul>
</li>
</ul>
HTML, (new GeneratesTableOfContents($markdown))->execute()
);
}

public function testWithMultipleNestedHeadings()
{
$markdown = <<<'MARKDOWN'
# Level 1
## Level 2
### Level 3
#### Level 4
##### Level 5
###### Level 6

## Level 2B
### Level 3B
### Level 3C
## Level 2C
### Level 3D
MARKDOWN;

$this->assertSameIgnoringIndentation(<<<'HTML'
<ul class="table-of-contents">
<li>
<a href="#level-2">Level 2</a>
<ul>
<li>
<a href="#level-3">Level 3</a>
<ul>
<li>
<a href="#level-4">Level 4</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#level-2b">Level 2B</a>
<ul>
<li>
<a href="#level-3b">Level 3B</a>
</li>
<li>
<a href="#level-3c">Level 3C</a>
</li>
</ul>
</li>
<li>
<a href="#level-2c">Level 2C</a>
<ul>
<li>
<a href="#level-3d">Level 3D</a>
</li>
</ul>
</li>
</ul>
HTML, (new GeneratesTableOfContents($markdown))->execute()
);
}

public function testWithMultipleLevelOneHeadings()
{
$markdown = <<<'MARKDOWN'
# Level 1
## Level 2
### Level 3
# Level 1B
## Level 2B
### Level 3B
MARKDOWN;

$this->assertEquals('<ul class="table-of-contents"><li><a href="#level-2">Level 2</a><ul><li><a href="#level-3">Level 3</a></li></ul></li></ul>',
str_replace("\n", '', $result)
$this->assertSameIgnoringIndentation(<<<'HTML'
<ul class="table-of-contents">
<li>
<a href="#level-2">Level 2</a>
<ul>
<li>
<a href="#level-3">Level 3</a>
</li>
</ul>
</li>
<li>
<a href="#level-2b">Level 2B</a>
<ul>
<li>
<a href="#level-3b">Level 3B</a>
</li>
</ul>
</li>
</ul>
HTML, (new GeneratesTableOfContents($markdown))->execute()
);
}

protected function assertSameIgnoringIndentation(string $expected, string $actual): void
{
$this->assertSame(
$this->removeIndentation(trim($expected)),
$this->removeIndentation(trim($actual))
);
}

protected function removeIndentation(string $actual): string
{
return implode("\n", array_map('trim', explode("\n", $actual)));
}
}