PhpMarkdownTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use Michelf\Markdown;
  4. use Michelf\MarkdownExtra;
  5. class PhpMarkdownTest extends TestCase
  6. {
  7. /**
  8. * Returns all php-markdown.mdtest tests
  9. * @return array
  10. */
  11. public function dataProviderForPhpMarkdown() {
  12. $dir = TEST_RESOURCES_ROOT . '/php-markdown.mdtest';
  13. return MarkdownTestHelper::getInputOutputPaths($dir);
  14. }
  15. /**
  16. * Runs php-markdown.mdtest against Markdown::defaultTransform
  17. *
  18. * @dataProvider dataProviderForPhpMarkdown
  19. *
  20. * @param string $inputPath Input markdown path
  21. * @param string $htmlPath File path of expected transformed output (X)HTML
  22. *
  23. * @param bool $xhtml True if XHTML. Otherwise, HTML is assumed.
  24. *
  25. * @return void
  26. */
  27. public function testTransformingOfPhpMarkdown($inputPath, $htmlPath, $xhtml = false) {
  28. $inputMarkdown = file_get_contents($inputPath);
  29. $expectedHtml = file_get_contents($htmlPath);
  30. $result = Markdown::defaultTransform($inputMarkdown);
  31. MarkdownTestHelper::assertSameNormalized(
  32. $expectedHtml,
  33. $result,
  34. "Markdown in $inputPath converts exactly to expected $htmlPath",
  35. $xhtml
  36. );
  37. }
  38. /**
  39. * Returns all php-markdown.mdtest tests EXCEPT Emphasis test.
  40. * @return array
  41. */
  42. public function dataProviderForPhpMarkdownExceptEmphasis()
  43. {
  44. $dir = TEST_RESOURCES_ROOT . '/php-markdown.mdtest';
  45. $allTests = MarkdownTestHelper::getInputOutputPaths($dir);
  46. foreach ($allTests as $index => $test) {
  47. // Because MarkdownExtra treats underscore emphasis differently, this one test has to be excluded
  48. if (preg_match('~/Emphasis\.text$~', $test[0])) {
  49. unset($allTests[$index]);
  50. }
  51. }
  52. return array_values($allTests);
  53. }
  54. /**
  55. * Runs php-markdown.mdtest against MarkdownExtra::defaultTransform
  56. *
  57. * @dataProvider dataProviderForPhpMarkdownExceptEmphasis
  58. *
  59. * @param $inputPath
  60. * @param $htmlPath
  61. * @param bool $xhtml
  62. */
  63. public function testPhpMarkdownMdTestWithMarkdownExtra($inputPath, $htmlPath, $xhtml = false)
  64. {
  65. $inputMarkdown = file_get_contents($inputPath);
  66. $expectedHtml = file_get_contents($htmlPath);
  67. $result = MarkdownExtra::defaultTransform($inputMarkdown);
  68. MarkdownTestHelper::assertSameNormalized(
  69. $expectedHtml,
  70. $result,
  71. "Markdown in $inputPath converts exactly to expected $htmlPath",
  72. $xhtml
  73. );
  74. }
  75. /**
  76. * @return array
  77. */
  78. public function dataProviderForMarkdownExtra() {
  79. $dir = TEST_RESOURCES_ROOT . '/php-markdown-extra.mdtest';
  80. return MarkdownTestHelper::getInputOutputPaths($dir);
  81. }
  82. /**
  83. * @dataProvider dataProviderForMarkdownExtra
  84. *
  85. * @param string $inputPath Input markdown path
  86. * @param string $htmlPath File path of expected transformed output (X)HTML
  87. *
  88. * @param bool $xhtml True if XHTML. Otherwise, HTML is assumed.
  89. *
  90. * @return void
  91. */
  92. public function testTransformingOfMarkdownExtra($inputPath, $htmlPath, $xhtml = false) {
  93. $inputMarkdown = file_get_contents($inputPath);
  94. $expectedHtml = file_get_contents($htmlPath);
  95. $result = MarkdownExtra::defaultTransform($inputMarkdown);
  96. MarkdownTestHelper::assertSameNormalized(
  97. $expectedHtml,
  98. $result,
  99. "Markdown in $inputPath converts exactly to expected $htmlPath",
  100. $xhtml
  101. );
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function dataProviderForRegularMarkdown()
  107. {
  108. $dir = TEST_RESOURCES_ROOT . '/markdown.mdtest';
  109. return MarkdownTestHelper::getInputOutputPaths($dir);
  110. }
  111. /**
  112. * @dataProvider dataProviderForRegularMarkdown
  113. *
  114. * @param string $inputPath Input markdown path
  115. * @param string $htmlPath File path of expected transformed output (X)HTML
  116. *
  117. * @param bool $xhtml True if XHTML. Otherwise, HTML is assumed.
  118. *
  119. * @return void
  120. */
  121. public function testTransformingOfRegularMarkdown($inputPath, $htmlPath, $xhtml = false)
  122. {
  123. $inputMarkdown = file_get_contents($inputPath);
  124. $expectedHtml = file_get_contents($htmlPath);
  125. $result = Markdown::defaultTransform($inputMarkdown);
  126. MarkdownTestHelper::assertSameNormalized(
  127. $expectedHtml,
  128. $result,
  129. "Markdown in $inputPath converts exactly to expected $htmlPath",
  130. $xhtml
  131. );
  132. }
  133. /**
  134. * Runs markdown.mdtest against MarkdownExtra::defaultTransform
  135. *
  136. * @dataProvider dataProviderForRegularMarkdown
  137. *
  138. * @param $inputPath
  139. * @param $htmlPath
  140. * @param bool $xhtml
  141. */
  142. public function testMarkdownMdTestWithMarkdownExtra($inputPath, $htmlPath, $xhtml = false)
  143. {
  144. $inputMarkdown = file_get_contents($inputPath);
  145. $expectedHtml = file_get_contents($htmlPath);
  146. $result = MarkdownExtra::defaultTransform($inputMarkdown);
  147. MarkdownTestHelper::assertSameNormalized(
  148. $expectedHtml,
  149. $result,
  150. "Markdown in $inputPath converts exactly to expected $htmlPath",
  151. $xhtml
  152. );
  153. }
  154. }