ParsedownTest.php 3.6 KB

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