LocalDataTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This is part of the webuni/commonmark-table-extension package.
7
 *
8
 * (c) Martin Hasoň <[email protected]>
9
 * (c) Webuni s.r.o. <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Webuni\CommonMark\TableExtension\Tests\Functional;
16
17
use League\CommonMark\DocParser;
18
use League\CommonMark\ElementRendererInterface;
19
use League\CommonMark\Environment;
20
use League\CommonMark\HtmlRenderer;
21
use PHPUnit\Framework\TestCase;
22
use Webuni\CommonMark\TableExtension\TableExtension;
23
24
/**
25
 * @internal
26
 * @coversNothing
27
 */
28
class LocalDataTest extends TestCase
29
{
30
    private $environment;
31
    private $parser;
32
33
    protected function setUp(): void
34
    {
35
        $this->environment = Environment::createCommonMarkEnvironment();
36
        $this->environment->addExtension(new TableExtension());
37
38
        $this->parser = new DocParser($this->environment);
39
    }
40
41
    /**
42
     * @dataProvider dataProvider
43
     */
44
    public function testRenderer(string $markdown, string $html, string $testName): void
45
    {
46
        $renderer = new HtmlRenderer($this->environment);
47
        $this->assertCommonMark($renderer, $markdown, $html, $testName);
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function dataProvider()
54
    {
55
        $ret = [];
56
        foreach (glob(__DIR__.'/data/*.md') as $markdownFile) {
57
            $testName = basename($markdownFile, '.md');
58
            $markdown = file_get_contents($markdownFile);
59
            $html = file_get_contents(__DIR__.'/data/'.$testName.'.html');
60
61
            $ret[] = [$markdown, $html, $testName];
62
        }
63
64
        return $ret;
65
    }
66
67
    protected function assertCommonMark(ElementRendererInterface $renderer, $markdown, $html, $testName): void
68
    {
69
        $documentAST = $this->parser->parse($markdown);
70
        $actualResult = $renderer->renderBlock($documentAST);
71
72
        $failureMessage = sprintf('Unexpected result for "%s" test', $testName);
73
        $failureMessage .= "\n=== markdown ===============\n".$markdown;
74
        $failureMessage .= "\n=== expected ===============\n".$html;
75
        $failureMessage .= "\n=== got ====================\n".$actualResult;
76
77
        $this->assertEquals($html, $actualResult, $failureMessage);
78
    }
79
}
80