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