AbstractSpecTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
  9. * - (c) John MacFarlane
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace League\CommonMark\Tests\Functional;
  15. use League\CommonMark\CommonMarkConverter;
  16. use League\CommonMark\MarkdownConverter;
  17. use League\CommonMark\Util\SpecReader;
  18. use PHPUnit\Framework\TestCase;
  19. abstract class AbstractSpecTest extends TestCase
  20. {
  21. protected MarkdownConverter $converter;
  22. protected function setUp(): void
  23. {
  24. $this->converter = new CommonMarkConverter();
  25. }
  26. /**
  27. * @dataProvider dataProvider
  28. *
  29. * @param string $markdown Markdown to parse
  30. * @param string $html Expected result
  31. */
  32. public function testSpecExample(string $markdown, string $html): void
  33. {
  34. $actualResult = (string) $this->converter->convert($markdown);
  35. $failureMessage = 'Unexpected result:';
  36. $failureMessage .= "\n=== markdown ===============\n" . $this->showSpaces($markdown);
  37. $failureMessage .= "\n=== expected ===============\n" . $this->showSpaces($html);
  38. $failureMessage .= "\n=== got ====================\n" . $this->showSpaces($actualResult);
  39. $this->assertEquals($html, $actualResult, $failureMessage);
  40. }
  41. public function dataProvider(): \Generator
  42. {
  43. yield from $this->loadSpecExamples();
  44. }
  45. protected function loadSpecExamples(): \Generator
  46. {
  47. yield from SpecReader::readFile($this->getFileName());
  48. }
  49. private function showSpaces(string $str): string
  50. {
  51. return \strtr($str, ["\t" => '→', ' ' => '␣']);
  52. }
  53. abstract protected function getFileName(): string;
  54. }