parsedown/tests/CommonMarkTestStrict.php

102 lines
2.7 KiB
PHP
Raw Normal View History

<?php
2018-04-17 15:44:38 +02:00
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;
2018-12-04 18:08:14 +01:00
use PHPUnit\Framework\TestCase;
/**
2016-09-05 04:51:28 +02:00
* Test Parsedown against the CommonMark spec
*
2014-09-13 23:02:11 +02:00
* @link http://commonmark.org/ CommonMark
*/
2018-12-04 18:08:14 +01:00
class CommonMarkTestStrict extends TestCase
{
const SPEC_URL = 'https://raw.githubusercontent.com/jgm/CommonMark/master/spec.txt';
2019-02-03 02:03:45 +01:00
const SPEC_LOCAL_CACHE = 'spec_cache.txt';
const SPEC_CACHE_SECONDS = 300;
2016-09-05 04:51:28 +02:00
protected $parsedown;
protected function setUp()
{
$this->parsedown = new Parsedown(new State([
StrictMode::enabled(),
InlineTypes::initial()->removing([Url::class]),
]));
2016-09-05 04:51:28 +02:00
}
2014-11-29 20:34:46 +01:00
/**
* @dataProvider data
* @param $id
2014-11-29 22:53:38 +01:00
* @param $section
2014-11-29 20:34:46 +01:00
* @param $markdown
* @param $expectedHtml
*/
public function testExample($id, $section, $markdown, $expectedHtml)
2014-11-29 20:34:46 +01:00
{
2016-09-05 04:51:28 +02:00
$actualHtml = $this->parsedown->text($markdown);
2014-11-29 20:34:46 +01:00
$this->assertEquals($expectedHtml, $actualHtml);
}
2019-02-03 01:57:23 +01:00
public static function getSpec()
{
2019-02-03 02:03:45 +01:00
$specPath = __DIR__ .'/'.self::SPEC_LOCAL_CACHE;
2019-02-03 01:57:23 +01:00
if (
2019-02-03 02:03:45 +01:00
\is_file($specPath)
&& \time() - \filemtime($specPath) < self::SPEC_CACHE_SECONDS
2019-02-03 01:57:23 +01:00
) {
2019-02-03 02:03:45 +01:00
$spec = \file_get_contents($specPath);
2019-02-03 01:57:23 +01:00
} else {
$spec = \file_get_contents(self::SPEC_URL);
2019-02-03 02:03:45 +01:00
\file_put_contents($specPath, $spec);
2019-02-03 01:57:23 +01:00
}
return $spec;
}
2016-09-05 04:51:28 +02:00
/**
* @return array
*/
public function data()
{
2019-02-03 01:57:23 +01:00
$spec = self::getSpec();
2016-09-05 04:51:28 +02:00
if ($spec === false) {
$this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL);
}
2018-12-04 17:24:25 +01:00
$spec = \str_replace("\r\n", "\n", $spec);
$spec = \strstr($spec, '<!-- END TESTS -->', true);
2018-12-04 17:24:25 +01:00
$matches = [];
\preg_match_all('/^`{32} example\n((?s).*?)\n\.\n(?:|((?s).*?)\n)`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, \PREG_SET_ORDER);
2018-12-04 17:24:25 +01:00
$data = [];
$currentId = 0;
2016-09-05 04:51:28 +02:00
$currentSection = '';
foreach ($matches as $match) {
if (isset($match[3])) {
$currentSection = $match[3];
} else {
$currentId++;
2018-12-04 17:24:25 +01:00
$markdown = \str_replace('→', "\t", $match[1]);
$expectedHtml = isset($match[2]) ? \str_replace('→', "\t", $match[2]) : '';
2018-12-04 17:24:25 +01:00
$data[$currentId] = [
'id' => $currentId,
2016-09-05 04:51:28 +02:00
'section' => $currentSection,
'markdown' => $markdown,
'expectedHtml' => $expectedHtml
2018-12-04 17:24:25 +01:00
];
2016-09-05 04:51:28 +02:00
}
}
2016-09-05 04:51:28 +02:00
return $data;
}
}