File: CommonMarkTestStrict.php

package info (click to toggle)
php-parsedown 2.0.0~beta-1-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,036 kB
  • sloc: php: 4,260; xml: 38; makefile: 14
file content (107 lines) | stat: -rw-r--r-- 3,205 bytes parent folder | download
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
102
103
104
105
106
107
<?php

namespace Erusev\Parsedown\Tests;

use Erusev\Parsedown\Components\Inlines\Url;
use Erusev\Parsedown\Configurables\InlineTypes;
use Erusev\Parsedown\Configurables\StrictMode;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\State;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;

/**
 * Test Parsedown against the CommonMark spec
 *
 * @link http://commonmark.org/ CommonMark
 */
class CommonMarkTestStrict extends TestCase
{
    const SPEC_URL = 'https://raw.githubusercontent.com/jgm/CommonMark/master/spec.txt';
    const SPEC_LOCAL_CACHE = 'spec_cache.txt';
    const SPEC_CACHE_SECONDS = 300;

    /** @var Parsedown */
    protected $Parsedown;

    /**
     * @dataProvider data
     * @param int $_
     * @param string $__
     * @param string $markdown
     * @param string $expectedHtml
     * @return void
     * @throws \PHPUnit\Framework\AssertionFailedError
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
     */
    #[RequiresPhpunit('< 11')]
    public function testExample($_, $__, $markdown, $expectedHtml)
    {
        $actualHtml = $this->Parsedown->toHtml($markdown);
        $this->assertEquals($expectedHtml, $actualHtml);
    }

    /**
     * @return string
     * @throws \PHPUnit\Framework\AssertionFailedError
     */
    public function getSpec()
    {
        $specPath = __DIR__ .'/'.self::SPEC_LOCAL_CACHE;

        if (
            \is_file($specPath)
            && \time() - \filemtime($specPath) < self::SPEC_CACHE_SECONDS
        ) {
            $spec = \file_get_contents($specPath);
        } else {
            $spec = \file_get_contents(self::SPEC_URL);
            \file_put_contents($specPath, $spec);
        }

        if ($spec === false) {
            $this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL);
        }

        return $spec;
    }

    /**
     * @return array<int, array{id: int, section: string, markdown: string, expectedHtml: string}>
     * @throws \PHPUnit\Framework\AssertionFailedError
     */
    public function data()
    {
        $spec = $this->getSpec();

        $spec = \str_replace("\r\n", "\n", $spec);
        /** @var string */
        $spec = \strstr($spec, '<!-- END TESTS -->', true);

        $matches = [];
        \preg_match_all('/^`{32} example\n((?s).*?)\n\.\n(?:|((?s).*?)\n)`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, \PREG_SET_ORDER);

        $data = [];
        $currentId = 0;
        $currentSection = '';
        /** @var array{0: string, 1: string, 2?: string, 3?: string} $match */
        foreach ($matches as $match) {
            if (isset($match[3])) {
                $currentSection = $match[3];
            } else {
                $currentId++;
                $markdown = \str_replace('→', "\t", $match[1]);
                $expectedHtml = isset($match[2]) ? \str_replace('→', "\t", $match[2]) : '';

                $data[$currentId] = [
                    'id' => $currentId,
                    'section' => $currentSection,
                    'markdown' => $markdown,
                    'expectedHtml' => $expectedHtml
                ];
            }
        }

        return $data;
    }
}