From 734b4fc3d75dc34a1489dc287a640ee3f4c8c7d2 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Wed, 12 Oct 2016 03:06:11 +0200 Subject: [PATCH] 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 --- phpunit.xml.dist | 6 +++ tests/CommonMarkTest.php | 110 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tests/CommonMarkTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1edb0a6..c1207fb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,6 +3,12 @@ tests/ParsedownTest.php + tests/CommonMarkTest.php + + + update + + diff --git a/tests/CommonMarkTest.php b/tests/CommonMarkTest.php new file mode 100644 index 0000000..67a41f2 --- /dev/null +++ b/tests/CommonMarkTest.php @@ -0,0 +1,110 @@ + $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 $id + * @param $section + * @param $markdown + * @param $expectedHtml + */ + 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 = $id . '-' . \preg_replace('/[^\w-.]/', '_', $section); + \file_put_contents($dir . $basename . '.md', $markdown); + \file_put_contents($dir . $basename . '.html', $expectedHtml); + } + + /** + * @return array + */ + public function dataUpdate() + { + return parent::data(); + } + + 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; + } +}