AbstractLocalDataTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Functional;
  12. use League\CommonMark\ConverterInterface;
  13. use League\CommonMark\Extension\FrontMatter\Data\SymfonyYamlFrontMatterParser;
  14. use League\CommonMark\Extension\FrontMatter\FrontMatterParser;
  15. use PHPUnit\Framework\TestCase;
  16. use Symfony\Component\Finder\Finder;
  17. use Symfony\Component\Finder\SplFileInfo;
  18. /**
  19. * Tests the parser against locally-stored examples
  20. *
  21. * This is particularly useful for testing minor variations allowed by the spec
  22. * or small regressions not tested by the spec.
  23. */
  24. abstract class AbstractLocalDataTest extends TestCase
  25. {
  26. /**
  27. * @param array<string, mixed> $config
  28. */
  29. abstract protected function createConverter(array $config = []): ConverterInterface;
  30. /**
  31. * @return iterable<array{string, string, array<string, mixed>, string}>
  32. */
  33. abstract public function dataProvider(): iterable;
  34. /**
  35. * @dataProvider dataProvider
  36. *
  37. * @param string $markdown Markdown to parse
  38. * @param string $html Expected result
  39. * @param array<string, mixed> $config Configuration loaded from front matter
  40. * @param string $testName Name of the test
  41. */
  42. public function testWithLocalData(string $markdown, string $html, array $config, string $testName): void
  43. {
  44. $actualResult = (string) $this->createConverter($config)->convert($markdown);
  45. $failureMessage = \sprintf('Unexpected result for "%s" test', $testName);
  46. $failureMessage .= "\n=== markdown ===============\n" . $markdown;
  47. $failureMessage .= "\n=== expected ===============\n" . $html;
  48. $failureMessage .= "\n=== got ====================\n" . $actualResult;
  49. $this->assertEquals($html, $actualResult, $failureMessage);
  50. }
  51. /**
  52. * @return iterable<array{string, string, array<string, mixed>, string}>
  53. */
  54. protected function loadTests(string $dir, string $pattern = '*', string $inputFormat = '.md', string $outputFormat = '.html'): iterable
  55. {
  56. $finder = new Finder();
  57. $finder->files()
  58. ->in($dir)
  59. ->depth('== 0')
  60. ->name($pattern . $inputFormat);
  61. foreach ($finder as $markdownFile) {
  62. \assert($markdownFile instanceof SplFileInfo);
  63. $testName = $markdownFile->getBasename($inputFormat);
  64. $input = $markdownFile->getContents();
  65. $parsed = (new FrontMatterParser(new SymfonyYamlFrontMatterParser()))->parse($input);
  66. $html = \file_get_contents($dir . '/' . $testName . $outputFormat);
  67. yield $testName => [$parsed->getContent(), $html, (array) $parsed->getFrontMatter(), $testName];
  68. }
  69. }
  70. }