LocalDataTest::assertCommonMark()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This is part of the webuni/commonmark-attributes-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\AttributesExtension\Tests\Functional;
16
17
use League\CommonMark\DocParser;
18
use League\CommonMark\ElementRendererInterface;
19
use League\CommonMark\Environment;
20
use League\CommonMark\Ext\Table\TableExtension;
21
use League\CommonMark\HtmlRenderer;
22
use PHPUnit\Framework\TestCase;
23
use Webuni\CommonMark\AttributesExtension\AttributesExtension;
24
use Webuni\CommonMark\TableExtension\TableExtension as TableExtensionOld;
25
26
/**
27
 * @internal
28
 * @coversNothing
29
 */
30
class LocalDataTest extends TestCase
31
{
32
    protected $environment;
33
34
    protected function setUp(): void
35
    {
36
        $this->environment = Environment::createCommonMarkEnvironment();
37
        $this->environment->addExtension(new AttributesExtension());
38
        $this->environment->addExtension(class_exists(TableExtension::class) ? new TableExtension() : new TableExtensionOld());
39
40
        $this->parser = new DocParser($this->environment);
0 ignored issues
show
Bug introduced by
The property parser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
    }
42
43
    /**
44
     * @dataProvider dataProvider
45
     */
46
    public function testRenderer(string $markdown, string $html, string $testName): void
47
    {
48
        $renderer = new HtmlRenderer($this->environment);
49
        $this->assertCommonMark($renderer, $markdown, $html, $testName);
50
    }
51
52
    public function dataProvider(): array
53
    {
54
        $ret = [];
55
        foreach (glob(__DIR__.'/data/*.md') as $markdownFile) {
56
            $testName = basename($markdownFile, '.md');
57
            $markdown = file_get_contents($markdownFile);
58
            $html = file_get_contents(__DIR__.'/data/'.$testName.'.html');
59
60
            $ret[] = [$markdown, $html, $testName];
61
        }
62
63
        return $ret;
64
    }
65
66
    protected function assertCommonMark(ElementRendererInterface $renderer, $markdown, $html, $testName): void
67
    {
68
        $documentAST = $this->parser->parse($markdown);
69
        $actualResult = $renderer->renderBlock($documentAST);
70
71
        $failureMessage = sprintf('Unexpected result for "%s" test', $testName);
72
        $failureMessage .= "\n=== markdown ===============\n".$markdown;
73
        $failureMessage .= "\n=== expected ===============\n".$html;
74
        $failureMessage .= "\n=== got ====================\n".$actualResult;
75
76
        $this->assertEquals($html, $actualResult, $failureMessage);
77
    }
78
}
79