1<?php 2/** 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 6 */ 7 8namespace cebe\markdown\tests; 9 10use cebe\markdown\Parser; 11 12/** 13 * Base class for all Test cases. 14 * 15 * @author Carsten Brandt <mail@cebe.cc> 16 */ 17abstract class BaseMarkdownTest extends \PHPUnit_Framework_TestCase 18{ 19 protected $outputFileExtension = '.html'; 20 21 abstract public function getDataPaths(); 22 23 /** 24 * @return Parser 25 */ 26 abstract public function createMarkdown(); 27 28 /** 29 * @dataProvider dataFiles 30 */ 31 public function testParse($path, $file) 32 { 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); 36 37 $m = $this->createMarkdown(); 38 $this->assertEquals($html, $m->parse($markdown)); 39 } 40 41 public function testUtf8() 42 { 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('電腦')); 47 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('電腦')); 52 } 53 54 public function testInvalidUtf8() 55 { 56 $m = $this->createMarkdown(); 57 $this->assertEquals("<p><code>�</code></p>\n", $m->parse("`\x80`")); 58 $this->assertEquals('<code>�</code>', $m->parseParagraph("`\x80`")); 59 } 60 61 public function pregData() 62 { 63 // http://en.wikipedia.org/wiki/Newline#Representations 64 return [ 65 ["a\r\nb", "a\nb"], 66 ["a\n\rb", "a\nb"], // Acorn BBC and RISC OS spooled text output :) 67 ["a\nb", "a\nb"], 68 ["a\rb", "a\nb"], 69 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"], 74 ]; 75 } 76 77 /** 78 * @dataProvider pregData 79 */ 80 public function testPregReplaceR($input, $exptected, $pexpect = null) 81 { 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)); 84 } 85 86 public function getTestData($path, $file) 87 { 88 return [ 89 file_get_contents($this->getDataPaths()[$path] . '/' . $file . '.md'), 90 file_get_contents($this->getDataPaths()[$path] . '/' . $file . $this->outputFileExtension), 91 ]; 92 } 93 94 public function dataFiles() 95 { 96 $files = []; 97 foreach ($this->getDataPaths() as $name => $src) { 98 $handle = opendir($src); 99 if ($handle === false) { 100 throw new \Exception('Unable to open directory: ' . $src); 101 } 102 while (($file = readdir($handle)) !== false) { 103 if ($file === '.' || $file === '..') { 104 continue; 105 } 106 107 if (substr($file, -3, 3) === '.md' && file_exists($src . '/' . substr($file, 0, -3) . $this->outputFileExtension)) { 108 $files[] = [$name, substr($file, 0, -3)]; 109 } 110 } 111 closedir($handle); 112 } 113 return $files; 114 } 115} 116