3 * @copyright Copyright (c) 2014 Carsten Brandt
4 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5 * @link https://github.com/cebe/markdown#readme
8 namespace cebe\markdown\tests;
10 use cebe\markdown\Parser;
13 * Base class for all Test cases.
15 * @author Carsten Brandt <mail@cebe.cc>
17 abstract class BaseMarkdownTest extends \PHPUnit_Framework_TestCase
19 protected $outputFileExtension = '.html';
21 abstract public function getDataPaths();
26 abstract public function createMarkdown();
29 * @dataProvider dataFiles
31 public function testParse($path, $file)
33 list($markdown, $html) = $this->getTestData($path, $file);
34 // Different OS line endings should not affect test
35 $html = str_replace(["\r\n", "\n\r", "\r"], "\n", $html);
37 $m = $this->createMarkdown();
38 $this->assertEquals($html, $m->parse($markdown));
41 public function testUtf8()
43 $this->assertSame("<p>абвгдеёжзийклмнопрстуфхцчшщъыьэюя</p>\n", $this->createMarkdown()->parse('абвгдеёжзийклмнопрстуфхцчшщъыьэюя'));
44 $this->assertSame("<p>there is a charater, 配</p>\n", $this->createMarkdown()->parse('there is a charater, 配'));
45 $this->assertSame("<p>Arabic Latter \"م (M)\"</p>\n", $this->createMarkdown()->parse('Arabic Latter "م (M)"'));
46 $this->assertSame("<p>電腦</p>\n", $this->createMarkdown()->parse('電腦'));
48 $this->assertSame('абвгдеёжзийклмнопрстуфхцчшщъыьэюя', $this->createMarkdown()->parseParagraph('абвгдеёжзийклмнопрстуфхцчшщъыьэюя'));
49 $this->assertSame('there is a charater, 配', $this->createMarkdown()->parseParagraph('there is a charater, 配'));
50 $this->assertSame('Arabic Latter "م (M)"', $this->createMarkdown()->parseParagraph('Arabic Latter "م (M)"'));
51 $this->assertSame('電腦', $this->createMarkdown()->parseParagraph('電腦'));
54 public function testInvalidUtf8()
56 $m = $this->createMarkdown();
57 $this->assertEquals("<p><code>�</code></p>\n", $m->parse("`\x80`"));
58 $this->assertEquals('<code>�</code>', $m->parseParagraph("`\x80`"));
61 public function pregData()
63 // http://en.wikipedia.org/wiki/Newline#Representations
66 ["a\n\rb", "a\nb"], // Acorn BBC and RISC OS spooled text output :)
70 ["a\n\nb", "a\n\nb", "a</p>\n<p>b"],
71 ["a\r\rb", "a\n\nb", "a</p>\n<p>b"],
72 ["a\n\r\n\rb", "a\n\nb", "a</p>\n<p>b"], // Acorn BBC and RISC OS spooled text output :)
73 ["a\r\n\r\nb", "a\n\nb", "a</p>\n<p>b"],
78 * @dataProvider pregData
80 public function testPregReplaceR($input, $exptected, $pexpect = null)
82 $this->assertSame($exptected, $this->createMarkdown()->parseParagraph($input));
83 $this->assertSame($pexpect === null ? "<p>$exptected</p>\n" : "<p>$pexpect</p>\n", $this->createMarkdown()->parse($input));
86 public function getTestData($path, $file)
89 file_get_contents($this->getDataPaths()[$path] . '/' . $file . '.md'),
90 file_get_contents($this->getDataPaths()[$path] . '/' . $file . $this->outputFileExtension),
94 public function dataFiles()
97 foreach ($this->getDataPaths() as $name => $src) {
98 $handle = opendir($src);
99 if ($handle === false) {
100 throw new \Exception('Unable to open directory: ' . $src);
102 while (($file = readdir($handle)) !== false) {
103 if ($file === '.' || $file === '..') {
107 if (substr($file, -3, 3) === '.md' && file_exists($src . '/' . substr($file, 0, -3) . $this->outputFileExtension)) {
108 $files[] = [$name, substr($file, 0, -3)];