ParsedownTest.php (3724B)
1 <?php 2 3 use PHPUnit\Framework\TestCase; 4 5 class ParsedownTest extends TestCase 6 { 7 final function __construct($name = null, array $data = array(), $dataName = '') 8 { 9 $this->dirs = $this->initDirs(); 10 $this->Parsedown = $this->initParsedown(); 11 12 parent::__construct($name, $data, $dataName); 13 } 14 15 private $dirs, $Parsedown; 16 17 /** 18 * @return array 19 */ 20 protected function initDirs() 21 { 22 $dirs []= dirname(__FILE__).'/data/'; 23 24 return $dirs; 25 } 26 27 /** 28 * @return Parsedown 29 */ 30 protected function initParsedown() 31 { 32 $Parsedown = new Parsedown(); 33 34 return $Parsedown; 35 } 36 37 /** 38 * @dataProvider data 39 * @param $test 40 * @param $dir 41 */ 42 function test_($test, $dir) 43 { 44 $markdown = file_get_contents($dir . $test . '.md'); 45 46 $expectedMarkup = file_get_contents($dir . $test . '.html'); 47 48 $expectedMarkup = str_replace("\r\n", "\n", $expectedMarkup); 49 $expectedMarkup = str_replace("\r", "\n", $expectedMarkup); 50 51 $actualMarkup = $this->Parsedown->text($markdown); 52 53 $this->assertEquals($expectedMarkup, $actualMarkup); 54 } 55 56 function data() 57 { 58 $data = array(); 59 60 foreach ($this->dirs as $dir) 61 { 62 $Folder = new DirectoryIterator($dir); 63 64 foreach ($Folder as $File) 65 { 66 /** @var $File DirectoryIterator */ 67 68 if ( ! $File->isFile()) 69 { 70 continue; 71 } 72 73 $filename = $File->getFilename(); 74 75 $extension = pathinfo($filename, PATHINFO_EXTENSION); 76 77 if ($extension !== 'md') 78 { 79 continue; 80 } 81 82 $basename = $File->getBasename('.md'); 83 84 if (file_exists($dir . $basename . '.html')) 85 { 86 $data []= array($basename, $dir); 87 } 88 } 89 } 90 91 return $data; 92 } 93 94 public function test_no_markup() 95 { 96 $markdownWithHtml = <<<MARKDOWN_WITH_MARKUP 97 <div>_content_</div> 98 99 sparse: 100 101 <div> 102 <div class="inner"> 103 _content_ 104 </div> 105 </div> 106 107 paragraph 108 109 <style type="text/css"> 110 p { 111 color: red; 112 } 113 </style> 114 115 comment 116 117 <!-- html comment --> 118 MARKDOWN_WITH_MARKUP; 119 120 $expectedHtml = <<<EXPECTED_HTML 121 <p><div><em>content</em></div></p> 122 <p>sparse:</p> 123 <p><div> 124 <div class="inner"> 125 <em>content</em> 126 </div> 127 </div></p> 128 <p>paragraph</p> 129 <p><style type="text/css"> 130 p { 131 color: red; 132 } 133 </style></p> 134 <p>comment</p> 135 <p><!-- html comment --></p> 136 EXPECTED_HTML; 137 $parsedownWithNoMarkup = new Parsedown(); 138 $parsedownWithNoMarkup->setMarkupEscaped(true); 139 $this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml)); 140 } 141 142 public function testLateStaticBinding() 143 { 144 include __DIR__ . '/TestParsedown.php'; 145 146 $parsedown = Parsedown::instance(); 147 $this->assertInstanceOf('Parsedown', $parsedown); 148 149 // After instance is already called on Parsedown 150 // subsequent calls with the same arguments return the same instance 151 $sameParsedown = TestParsedown::instance(); 152 $this->assertInstanceOf('Parsedown', $sameParsedown); 153 $this->assertSame($parsedown, $sameParsedown); 154 155 $testParsedown = TestParsedown::instance('test late static binding'); 156 $this->assertInstanceOf('TestParsedown', $testParsedown); 157 158 $sameInstanceAgain = TestParsedown::instance('test late static binding'); 159 $this->assertSame($testParsedown, $sameInstanceAgain); 160 } 161 }