Repository navigation
Expand file tree
/
Copy pathMarkdownExtensionTest.php
More file actions
101 lines (86 loc) · 3.22 KB
/
Copy pathMarkdownExtensionTest.php
File metadata and controls
101 lines (86 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace App\Tests\Integration\Twig\Extension;
use App\Twig\Extension\MarkdownExtension;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
#[CoversClass(MarkdownExtension::class)]
final class MarkdownExtensionTest extends KernelTestCase
{
private MarkdownExtension $extension;
protected function setUp(): void
{
self::bootKernel();
/** @var MarkdownExtension $extension */
$extension = self::getContainer()->get(MarkdownExtension::class);
$this->extension = $extension;
}
/**
* @return iterable<string, array{0: string, 1: string}>
*/
public static function provideAddHeadersAnchors(): iterable
{
yield 'h2 gets a slug id' => [
'<h2>Getting started</h2>',
'id="getting-started"',
];
yield 'h3 gets a slug id' => [
'<h3>Run the tests</h3>',
'id="run-the-tests"',
];
yield 'header with only ascii has no trailing dash' => [
'<h2>Symfony is great</h2>',
'id="symfony-is-great"',
];
yield 'trailing emoji adds the GitHub-style dash suffix' => [
'<h2>MicroSymfony 🎶</h2>',
'id="microsymfony-"',
];
}
#[DataProvider('provideAddHeadersAnchors')]
public function testAddHeadersAnchorsAddsIdAttribute(string $html, string $expectedIdAttribute): void
{
$output = $this->extension->addHeadersAnchors($html);
self::assertStringContainsString($expectedIdAttribute, $output);
}
public function testH1AndH4AreNotTouched(): void
{
$html = '<div><h1>Title ⚡</h1><h4>Sub</h4></div>';
$output = $this->extension->addHeadersAnchors($html);
self::assertStringNotContainsString('id=', $output);
}
public function testMultipleHeadersAllGetAnchored(): void
{
$html = '<div><h2>First</h2><p>Some text.</p><h3>Second</h3><h2>Third 🙂</h2></div>';
$output = $this->extension->addHeadersAnchors($html);
self::assertStringContainsString('id="first"', $output);
self::assertStringContainsString('id="second"', $output);
self::assertStringContainsString('id="third-"', $output);
}
public function testInlineContentInHeaderIsPreserved(): void
{
$html = '<h2>Hello <code>world</code></h2>';
$output = $this->extension->addHeadersAnchors($html);
self::assertStringContainsString('id="hello-world"', $output);
self::assertStringContainsString('<code>world</code>', $output);
}
/**
* Empty or blank input must be returned as-is: DOMDocument::loadHTML() throws a
* ValueError on an empty string.
*/
#[DataProvider('provideBlankInput')]
public function testBlankInputIsReturnedUnchanged(string $input): void
{
self::assertSame($input, $this->extension->addHeadersAnchors($input));
}
/**
* @return iterable<string, array{0: string}>
*/
public static function provideBlankInput(): iterable
{
yield 'empty string' => [''];
yield 'spaces only' => [' '];
yield 'newline only' => ["\n"];
}
}