2014-09-06 00:12:33 +04:00
|
|
|
<?php
|
2018-04-17 16:44:38 +03:00
|
|
|
|
|
|
|
namespace Erusev\Parsedown\Tests;
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2019-01-25 23:54:25 +03:00
|
|
|
use Erusev\Parsedown\Components\Inlines\Url;
|
|
|
|
use Erusev\Parsedown\Configurables\InlineTypes;
|
2019-01-23 00:35:13 +03:00
|
|
|
use Erusev\Parsedown\Configurables\StrictMode;
|
2019-01-20 05:52:46 +03:00
|
|
|
use Erusev\Parsedown\Parsedown;
|
2019-01-23 00:35:13 +03:00
|
|
|
use Erusev\Parsedown\State;
|
2018-12-04 20:08:14 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
2014-09-06 00:28:07 +04:00
|
|
|
/**
|
2016-09-05 05:51:28 +03:00
|
|
|
* Test Parsedown against the CommonMark spec
|
2014-09-06 00:28:07 +04:00
|
|
|
*
|
2014-09-14 01:02:11 +04:00
|
|
|
* @link http://commonmark.org/ CommonMark
|
2014-09-06 00:28:07 +04:00
|
|
|
*/
|
2018-12-04 20:08:14 +03:00
|
|
|
class CommonMarkTestStrict extends TestCase
|
2014-09-06 00:12:33 +04:00
|
|
|
{
|
2016-10-13 23:16:46 +03:00
|
|
|
const SPEC_URL = 'https://raw.githubusercontent.com/jgm/CommonMark/master/spec.txt';
|
2019-02-03 04:03:45 +03:00
|
|
|
const SPEC_LOCAL_CACHE = 'spec_cache.txt';
|
|
|
|
const SPEC_CACHE_SECONDS = 300;
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2019-02-11 01:38:34 +03:00
|
|
|
/** @var Parsedown */
|
|
|
|
protected $Parsedown;
|
2016-09-05 05:51:28 +03:00
|
|
|
|
2019-02-11 01:38:34 +03:00
|
|
|
/**
|
|
|
|
* @param string|null $name
|
|
|
|
* @param array $data
|
|
|
|
* @param string $dataName
|
|
|
|
*/
|
|
|
|
public function __construct($name = null, array $data = [], $dataName = '')
|
2016-09-05 05:51:28 +03:00
|
|
|
{
|
2019-02-11 01:38:34 +03:00
|
|
|
$this->Parsedown = new Parsedown(new State([
|
2019-01-25 23:54:25 +03:00
|
|
|
StrictMode::enabled(),
|
|
|
|
InlineTypes::initial()->removing([Url::class]),
|
2019-01-23 00:35:13 +03:00
|
|
|
]));
|
2019-02-11 01:38:34 +03:00
|
|
|
|
|
|
|
parent::__construct($name, $data, $dataName);
|
2016-09-05 05:51:28 +03:00
|
|
|
}
|
|
|
|
|
2014-11-29 22:34:46 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider data
|
2019-02-11 01:38:34 +03:00
|
|
|
* @param int $_
|
|
|
|
* @param string $__
|
|
|
|
* @param string $markdown
|
|
|
|
* @param string $expectedHtml
|
|
|
|
* @return void
|
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
2014-11-29 22:34:46 +03:00
|
|
|
*/
|
2019-02-11 01:38:34 +03:00
|
|
|
public function testExample($_, $__, $markdown, $expectedHtml)
|
2014-11-29 22:34:46 +03:00
|
|
|
{
|
2020-01-19 18:26:48 +03:00
|
|
|
$actualHtml = $this->Parsedown->toHtml($markdown);
|
2014-11-29 22:34:46 +03:00
|
|
|
$this->assertEquals($expectedHtml, $actualHtml);
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:38:34 +03:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError
|
|
|
|
*/
|
|
|
|
public function getSpec()
|
2019-02-03 03:57:23 +03:00
|
|
|
{
|
2019-02-03 04:03:45 +03:00
|
|
|
$specPath = __DIR__ .'/'.self::SPEC_LOCAL_CACHE;
|
|
|
|
|
2019-02-03 03:57:23 +03:00
|
|
|
if (
|
2019-02-03 04:03:45 +03:00
|
|
|
\is_file($specPath)
|
|
|
|
&& \time() - \filemtime($specPath) < self::SPEC_CACHE_SECONDS
|
2019-02-03 03:57:23 +03:00
|
|
|
) {
|
2019-02-03 04:03:45 +03:00
|
|
|
$spec = \file_get_contents($specPath);
|
2019-02-03 03:57:23 +03:00
|
|
|
} else {
|
|
|
|
$spec = \file_get_contents(self::SPEC_URL);
|
2019-02-03 04:03:45 +03:00
|
|
|
\file_put_contents($specPath, $spec);
|
2019-02-03 03:57:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-11 01:38:34 +03:00
|
|
|
if ($spec === false) {
|
|
|
|
$this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL);
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:57:23 +03:00
|
|
|
return $spec;
|
|
|
|
}
|
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
/**
|
2019-02-11 01:38:34 +03:00
|
|
|
* @return array<int, array{id: int, section: string, markdown: string, expectedHtml: string}>
|
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError
|
2016-09-05 05:51:28 +03:00
|
|
|
*/
|
|
|
|
public function data()
|
2014-09-06 00:12:33 +04:00
|
|
|
{
|
2019-02-11 01:38:34 +03:00
|
|
|
$spec = $this->getSpec();
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$spec = \str_replace("\r\n", "\n", $spec);
|
2019-02-11 01:38:34 +03:00
|
|
|
/** @var string */
|
2018-12-04 19:24:25 +03:00
|
|
|
$spec = \strstr($spec, '<!-- END TESTS -->', true);
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$matches = [];
|
|
|
|
\preg_match_all('/^`{32} example\n((?s).*?)\n\.\n(?:|((?s).*?)\n)`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, \PREG_SET_ORDER);
|
2014-11-29 23:58:42 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$data = [];
|
2016-10-12 03:01:40 +03:00
|
|
|
$currentId = 0;
|
2016-09-05 05:51:28 +03:00
|
|
|
$currentSection = '';
|
2019-02-11 01:38:34 +03:00
|
|
|
/** @var array{0: string, 1: string, 2?: string, 3?: string} $match */
|
2016-09-05 05:51:28 +03:00
|
|
|
foreach ($matches as $match) {
|
|
|
|
if (isset($match[3])) {
|
|
|
|
$currentSection = $match[3];
|
|
|
|
} else {
|
2016-10-13 23:16:46 +03:00
|
|
|
$currentId++;
|
2018-12-04 19:24:25 +03:00
|
|
|
$markdown = \str_replace('→', "\t", $match[1]);
|
|
|
|
$expectedHtml = isset($match[2]) ? \str_replace('→', "\t", $match[2]) : '';
|
2016-10-13 23:16:46 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$data[$currentId] = [
|
2016-10-13 23:16:46 +03:00
|
|
|
'id' => $currentId,
|
2016-09-05 05:51:28 +03:00
|
|
|
'section' => $currentSection,
|
2016-10-13 23:16:46 +03:00
|
|
|
'markdown' => $markdown,
|
|
|
|
'expectedHtml' => $expectedHtml
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2016-09-05 05:51:28 +03:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 23:58:42 +03:00
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
return $data;
|
2014-11-29 23:58:42 +03:00
|
|
|
}
|
2014-09-06 00:12:33 +04:00
|
|
|
}
|