File: CommonMarkTest.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 (123 lines) | stat: -rw-r--r-- 4,266 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

namespace Erusev\Parsedown\Tests;

use PHPUnit\Framework\Attributes\RequiresPhpunit;

/**
 * Test Parsedown against cached expect-to-pass CommonMark spec examples
 *
 * This test suite runs tests the same way as `test/CommonMarkTestWeak.php`,
 * but uses a cached set of CommonMark spec examples in `test/commonmark/`.
 * It is executed along with Parsedown's default test suite and runs various
 * CommonMark spec examples, which are expected to pass. If they don't pass,
 * the Parsedown build fails. The intention of this test suite is to make sure,
 * that previously passed CommonMark spec examples don't fail due to unwanted
 * side-effects of code changes.
 *
 * You can re-create the `test/commonmark/` directory by executing the PHPUnit
 * group `update`. The test suite will then run `test/CommonMarkTestWeak.php`
 * and create files with the Markdown source and the resulting HTML markup of
 * all passed tests. The command to execute looks like the following:
 *
 *     $ phpunit --group update
 *
 * @link http://commonmark.org/ CommonMark
 */
class CommonMarkTest extends CommonMarkTestStrict
{
    /**
     * @return array<int, array{id: int, section: string, markdown: string, expectedHtml: string}>
     * @throws \PHPUnit\Framework\AssertionFailedError
     */
    public function data()
    {
        $data = [];

        $dir = static::getDataDir();
        $files = @\scandir($dir);
        if (!empty($files)) {
            foreach ($files as $file) {
                if (($file === '.') || ($file === '..')) {
                    continue;
                }

                if (\substr($file, -3) === '.md') {
                    $testName = \substr($file, 0, -3);
                    if (\file_exists($dir . $testName . '.html')) {
                        \preg_match('/^(\d+)-(.*)$/', $testName, $matches);
                        $id = isset($matches[1]) ? \intval($matches[1]) : 0;
                        $section = isset($matches[2]) ? \preg_replace('/_+/', ' ', $matches[2]) : '';

                        $markdown = \file_get_contents($dir . $testName . '.md');
                        $expectedHtml = \file_get_contents($dir . $testName . '.html');

                        $data[$id] = [
                            'id' => $id,
                            'section' => $section,
                            'markdown' => $markdown,
                            'expectedHtml' => $expectedHtml
                        ];
                    }
                }
            }
        } else {
            $this->fail('The CommonMark cache folder ' . $dir . ' is empty or not readable.');
        }

        return $data;
    }

    /**
     * @group update
     * @dataProvider dataUpdate
     * @param int $id
     * @param string $section
     * @param string $markdown
     * @param string $expectedHtml
     * @return void
     * @throws \PHPUnit\Framework\AssertionFailedError
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
     */
    #[RequiresPhpunit('< 11')]
    public function testUpdateDatabase($id, $section, $markdown, $expectedHtml)
    {
        parent::testExample($id, $section, $markdown, $expectedHtml);

        // you can only get here when the test passes
        $dir = static::getDataDir(true);
        $basename = \strval($id) . '-' . \preg_replace('/[^\w.-]/', '_', $section);
        \file_put_contents($dir . $basename . '.md', $markdown);
        \file_put_contents($dir . $basename . '.html', $expectedHtml);
    }

    /**
     * @return array<int, array{id: int, section: string, markdown: string, expectedHtml: string}>
     * @throws \PHPUnit\Framework\AssertionFailedError
     */
    public function dataUpdate()
    {
        return parent::data();
    }

    /**
     * @param bool $mkdir
     * @return string
     * @throws \PHPUnit\Framework\AssertionFailedError
     */
    public static function getDataDir($mkdir = false)
    {
        $dir = __DIR__ . '/commonmark/';

        if ($mkdir) {
            if (!\file_exists($dir)) {
                @\mkdir($dir);
            }
            if (!\is_dir($dir)) {
                static::fail('Unable to create CommonMark cache folder ' . $dir . '.');
            }
        }

        return $dir;
    }
}