1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00
parsedown/tests/ParsedownTest.php

139 lines
3.1 KiB
PHP
Raw Normal View History

2014-09-26 03:04:25 +04:00
<?php
2018-04-17 16:44:38 +03:00
namespace Erusev\Parsedown\Tests;
2014-09-26 03:04:25 +04:00
use Erusev\Parsedown\Configurables\SafeMode;
use Erusev\Parsedown\Configurables\StrictMode;
2018-04-17 16:44:38 +03:00
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\State;
2018-12-04 19:24:25 +03:00
use PHPUnit\Framework\TestCase;
class ParsedownTest extends TestCase
2014-09-26 03:04:25 +04:00
{
2018-12-04 19:24:25 +03:00
final public function __construct($name = null, array $data = [], $dataName = '')
2014-09-26 03:04:25 +04:00
{
$this->dirs = $this->initDirs();
parent::__construct($name, $data, $dataName);
}
private $dirs;
protected $Parsedown;
2014-09-26 03:04:25 +04:00
/**
* @return array
*/
protected function initDirs()
{
2018-12-04 19:24:25 +03:00
$dirs []= \dirname(__FILE__).'/data/';
2014-09-26 03:04:25 +04:00
return $dirs;
}
/**
* @dataProvider data
* @param $test
* @param $dir
*/
2018-12-04 19:24:25 +03:00
public function test_($test, $dir)
2014-09-26 03:04:25 +04:00
{
2018-12-04 19:24:25 +03:00
$markdown = \file_get_contents($dir . $test . '.md');
2014-09-26 03:04:25 +04:00
2018-12-04 19:24:25 +03:00
$expectedMarkup = \file_get_contents($dir . $test . '.html');
2014-09-26 03:04:25 +04:00
2018-12-04 19:24:25 +03:00
$expectedMarkup = \str_replace("\r\n", "\n", $expectedMarkup);
$expectedMarkup = \str_replace("\r", "\n", $expectedMarkup);
2014-09-26 03:04:25 +04:00
$Parsedown = new Parsedown(new State([
new SafeMode(\substr($test, 0, 3) === 'xss'),
new StrictMode(\substr($test, 0, 6) === 'strict'),
]));
$actualMarkup = $Parsedown->text($markdown);
$this->assertEquals($expectedMarkup, $actualMarkup);
}
2018-12-04 19:24:25 +03:00
public function data()
2014-09-26 03:04:25 +04:00
{
2018-12-04 19:24:25 +03:00
$data = [];
2014-09-26 03:04:25 +04:00
2018-12-04 19:24:25 +03:00
foreach ($this->dirs as $dir) {
2018-04-17 16:44:38 +03:00
$Folder = new \DirectoryIterator($dir);
2014-09-26 03:04:25 +04:00
2018-12-04 19:24:25 +03:00
foreach ($Folder as $File) {
2014-09-26 03:04:25 +04:00
/** @var $File DirectoryIterator */
2018-12-04 19:24:25 +03:00
if (! $File->isFile()) {
2014-09-26 03:04:25 +04:00
continue;
}
$filename = $File->getFilename();
2018-12-04 19:24:25 +03:00
$extension = \pathinfo($filename, \PATHINFO_EXTENSION);
2014-09-26 03:04:25 +04:00
2018-12-04 19:24:25 +03:00
if ($extension !== 'md') {
2014-09-26 03:04:25 +04:00
continue;
}
$basename = $File->getBasename('.md');
2018-12-04 19:24:25 +03:00
if (\file_exists($dir . $basename . '.html')) {
$data []= [$basename, $dir];
2014-09-26 03:04:25 +04:00
}
}
}
return $data;
}
// public function test_no_markup()
// {
// $markdownWithHtml = <<<MARKDOWN_WITH_MARKUP
// <div>_content_</div>
// sparse:
// <div>
// <div class="inner">
// _content_
// </div>
// </div>
// paragraph
// <style type="text/css">
// p {
// color: red;
// }
// </style>
// comment
// <!-- html comment -->
// MARKDOWN_WITH_MARKUP;
// $expectedHtml = <<<EXPECTED_HTML
// <p>&lt;div&gt;<em>content</em>&lt;/div&gt;</p>
// <p>sparse:</p>
// <p>&lt;div&gt;
// &lt;div class="inner"&gt;
// <em>content</em>
// &lt;/div&gt;
// &lt;/div&gt;</p>
// <p>paragraph</p>
// <p>&lt;style type="text/css"&gt;
// p {
// color: red;
// }
// &lt;/style&gt;</p>
// <p>comment</p>
// <p>&lt;!-- html comment --&gt;</p>
// EXPECTED_HTML;
// $parsedownWithNoMarkup = new TestParsedown();
// $parsedownWithNoMarkup->setMarkupEscaped(true);
// $this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml));
// }
2014-09-26 03:04:25 +04:00
}