Completed
Push — master ( c4e70a...2158db )
by Martin
17:53
created

LocalDataTest::testHtmlRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-table-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webuni\CommonMark\TableExtension\tests\functional;
13
14
use League\CommonMark\DocParser;
15
use League\CommonMark\ElementRendererInterface;
16
use League\CommonMark\Environment;
17
use League\CommonMark\HtmlRenderer;
18
use Webuni\CommonMark\TableExtension\TableExtension;
19
use Webuni\CommonMark\TwigRenderer\CommonMarkTwigExtension;
20
use Webuni\CommonMark\TwigRenderer\TwigRenderer;
21
22
class LocalDataTest extends \PHPUnit_Framework_TestCase
23
{
24
    /* @var Environment */
25
    private $environment;
26
    private $parser;
27
28
    protected function setUp()
29
    {
30
        $this->environment = Environment::createCommonMarkEnvironment();
31
        $this->environment->addExtension(new TableExtension());
32
33
        $this->parser = new DocParser($this->environment);
34
    }
35
36
    /**
37
     * @dataProvider dataProvider
38
     */
39
    public function testHtmlRenderer($markdown, $html, $testName)
40
    {
41
        $renderer = new HtmlRenderer($this->environment);
42
        $this->assertCommonMark($renderer, $markdown, $html, $testName);
43
    }
44
45
    /**
46
     * @dataProvider dataProvider
47
     */
48
    public function testTwigRenderer($markdown, $html, $testName)
49
    {
50
        $loader = CommonMarkTwigExtension::createTwigLoader();
51
52
        $ref = new \ReflectionClass('Webuni\CommonMark\TableExtension\TableExtension');
53
        $loader->addPath(dirname($ref->getFileName()).'/Resources');
54
        $loader->addPath(__DIR__);
55
56
        $twig = new \Twig_Environment($loader, [
57
            'strict_variables' => true,
58
        ]);
59
        $twig->addExtension(new CommonMarkTwigExtension());
60
61
62
        $this->environment->mergeConfig(['renderer' => ['twig_template' => 'template.html.twig']]);
63
        $renderer = new TwigRenderer($this->environment, $twig);
64
65
        $this->assertCommonMark($renderer, $markdown, $html, $testName);
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function dataProvider()
72
    {
73
        $ret = [];
74
        foreach (glob(__DIR__.'/data/*.md') as $markdownFile) {
75
            $testName = basename($markdownFile, '.md');
76
            $markdown = file_get_contents($markdownFile);
77
            $html = file_get_contents(__DIR__.'/data/'.$testName.'.html');
78
79
            $ret[] = [$markdown, $html, $testName];
80
        }
81
82
        return $ret;
83
    }
84
85
    private function assertCommonMark(ElementRendererInterface $renderer, $markdown, $html, $testName)
86
    {
87
        $documentAST = $this->parser->parse($markdown);
88
        $actualResult = $renderer->renderBlock($documentAST);
89
90
        $failureMessage = sprintf('Unexpected result for "%s" test', $testName);
91
        $failureMessage .= "\n=== markdown ===============\n".$markdown;
92
        $failureMessage .= "\n=== expected ===============\n".$html;
93
        $failureMessage .= "\n=== got ====================\n".$actualResult;
94
95
        $this->assertEquals($html, $actualResult, $failureMessage);
96
    }
97
}
98