just-a-blog

A bad blogging software that converts Markdown into PHP for some reason.
Log | Files | Refs | README | LICENSE

CommonMarkTest.php (2277B)


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