Html5EntityDecoderTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. namespace League\CommonMark\Tests\Unit\Util;
  4. use League\CommonMark\Util\Html5EntityDecoder;
  5. use PHPUnit\Framework\TestCase;
  6. final class Html5EntityDecoderTest extends TestCase
  7. {
  8. public function testEntityToChar(): void
  9. {
  10. $this->assertEquals('©', Html5EntityDecoder::decode('&copy;'));
  11. $this->assertEquals('&copy', Html5EntityDecoder::decode('&copy'));
  12. $this->assertEquals('&MadeUpEntity;', Html5EntityDecoder::decode('&MadeUpEntity;'));
  13. $this->assertEquals('#', Html5EntityDecoder::decode('&#35;'));
  14. $this->assertEquals('Æ', Html5EntityDecoder::decode('&AElig;'));
  15. $this->assertEquals('Ď', Html5EntityDecoder::decode('&Dcaron;'));
  16. }
  17. /**
  18. * @dataProvider htmlEntityDataProvider
  19. */
  20. public function testAllHtml5EntityReferences(string $entity, string $decoded): void
  21. {
  22. $this->assertEquals($decoded, \html_entity_decode($entity, ENT_QUOTES | ENT_HTML5, 'UTF-8'), \sprintf('Failed parsing the "%s" entity', $entity));
  23. }
  24. /**
  25. * @return iterable<array<mixed>>
  26. */
  27. public function htmlEntityDataProvider(): iterable
  28. {
  29. // Test data from https://html.spec.whatwg.org/multipage/entities.json
  30. $data = \json_decode(\file_get_contents(__DIR__ . '/entities.json'), true);
  31. foreach ($data as $entity => $info) {
  32. // Per the spec, we only care about entities that have a trailing semi-colon.
  33. // See https://spec.commonmark.org/0.29/#entity-references
  34. if (\substr($entity, -1, 1) === ';') {
  35. yield [$entity, $info['characters']];
  36. }
  37. }
  38. }
  39. }