SymfonyYamlFrontMatterParserTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Unit\Extension\FrontMatter\Data;
  12. use League\CommonMark\Extension\FrontMatter\Data\SymfonyYamlFrontMatterParser;
  13. use League\CommonMark\Extension\FrontMatter\Exception\InvalidFrontMatterException;
  14. use PHPUnit\Framework\TestCase;
  15. use PackageVersions\Versions as InstalledComposerPackages;
  16. final class SymfonyYamlFrontMatterParserTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider provideValidYamlExamples
  20. *
  21. * @param mixed $expected
  22. */
  23. public function testParseWithValidYaml(string $input, $expected): void
  24. {
  25. $dataParser = new SymfonyYamlFrontMatterParser();
  26. $this->assertSame($expected, $dataParser->parse($input));
  27. }
  28. /**
  29. * @return iterable<mixed>
  30. */
  31. public function provideValidYamlExamples(): iterable
  32. {
  33. yield ['Hello, World!', 'Hello, World!'];
  34. yield ["- 1\n- 2\n- 3", [1, 2, 3]];
  35. yield ["foo: bar\nbaz: 42", ['foo' => 'bar', 'baz' => 42]];
  36. }
  37. /**
  38. * @dataProvider provideInvalidYamlExamples
  39. */
  40. public function testParseWithInvalidYaml(string $input): void
  41. {
  42. $this->expectException(InvalidFrontMatterException::class);
  43. $dataParser = new SymfonyYamlFrontMatterParser();
  44. $dataParser->parse($input);
  45. }
  46. /**
  47. * @return iterable<string>
  48. */
  49. public function provideInvalidYamlExamples(): iterable
  50. {
  51. yield ["this:\n is:invalid\n yaml: data"];
  52. if (! self::yamlLibrarySupportsObjectUnserialization()) {
  53. yield ['foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}'];
  54. }
  55. }
  56. private static function yamlLibrarySupportsObjectUnserialization(): bool
  57. {
  58. $fullVersion = InstalledComposerPackages::getVersion('symfony/yaml');
  59. \preg_match('/^v?([^@]+)/', $fullVersion, $matches);
  60. return \version_compare($matches[1], '4.1.0', '<');
  61. }
  62. }