CommonMarkTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Test Parsedown against the CommonMark spec.
  4. *
  5. * Some code based on the original JavaScript test runner by jgm.
  6. *
  7. * @link http://commonmark.org/ CommonMark
  8. * @link http://git.io/8WtRvQ JavaScript test runner
  9. */
  10. class CommonMarkTest extends \PHPUnit\Framework\TestCase
  11. {
  12. const SPEC_URL = 'https://raw.githubusercontent.com/jgm/stmd/master/spec.txt';
  13. /**
  14. * @dataProvider data
  15. * @param $section
  16. * @param $markdown
  17. * @param $expectedHtml
  18. */
  19. function test_($section, $markdown, $expectedHtml)
  20. {
  21. $Parsedown = new Parsedown();
  22. $Parsedown->setUrlsLinked(false);
  23. $actualHtml = $Parsedown->text($markdown);
  24. $actualHtml = $this->normalizeMarkup($actualHtml);
  25. $this->assertEquals($expectedHtml, $actualHtml);
  26. }
  27. function data()
  28. {
  29. $spec = file_get_contents(self::SPEC_URL);
  30. $spec = strstr($spec, '<!-- END TESTS -->', true);
  31. $tests = array();
  32. $currentSection = '';
  33. preg_replace_callback(
  34. '/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/m',
  35. function($matches) use ( & $tests, & $currentSection, & $testCount) {
  36. if (isset($matches[3]) and $matches[3]) {
  37. $currentSection = $matches[3];
  38. } else {
  39. $testCount++;
  40. $markdown = $matches[1];
  41. $markdown = preg_replace('/→/', "\t", $markdown);
  42. $expectedHtml = $matches[2];
  43. $expectedHtml = $this->normalizeMarkup($expectedHtml);
  44. $tests []= array(
  45. $currentSection, # section
  46. $markdown, # markdown
  47. $expectedHtml, # html
  48. );
  49. }
  50. },
  51. $spec
  52. );
  53. return $tests;
  54. }
  55. private function normalizeMarkup($markup)
  56. {
  57. $markup = preg_replace("/\n+/", "\n", $markup);
  58. $markup = preg_replace('/^\s+/m', '', $markup);
  59. $markup = preg_replace('/^((?:<[\w]+>)+)\n/m', '$1', $markup);
  60. $markup = preg_replace('/\n((?:<\/[\w]+>)+)$/m', '$1', $markup);
  61. $markup = trim($markup);
  62. return $markup;
  63. }
  64. }