File: Html5EntityDecoderTest.php

package info (click to toggle)
php-league-commonmark 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,264 kB
  • sloc: php: 20,396; xml: 1,988; ruby: 45; makefile: 21; javascript: 15
file content (44 lines) | stat: -rw-r--r-- 1,710 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

namespace League\CommonMark\Tests\Unit\Util;

use League\CommonMark\Util\Html5EntityDecoder;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class Html5EntityDecoderTest extends TestCase
{
    public function testEntityToChar(): void
    {
        $this->assertEquals('©', Html5EntityDecoder::decode('&copy;'));
        $this->assertEquals('&copy', Html5EntityDecoder::decode('&copy'));
        $this->assertEquals('&MadeUpEntity;', Html5EntityDecoder::decode('&MadeUpEntity;'));
        $this->assertEquals('#', Html5EntityDecoder::decode('&#35;'));
        $this->assertEquals('Æ', Html5EntityDecoder::decode('&AElig;'));
        $this->assertEquals('Ď', Html5EntityDecoder::decode('&Dcaron;'));
    }

    #[DataProvider('htmlEntityDataProvider')]
    public function testAllHtml5EntityReferences(string $entity, string $decoded): void
    {
        $this->assertEquals($decoded, \html_entity_decode($entity, ENT_QUOTES | ENT_HTML5, 'UTF-8'), \sprintf('Failed parsing the "%s" entity', $entity));
    }

    /**
     * @return iterable<array<mixed>>
     */
    public static function htmlEntityDataProvider(): iterable
    {
        // Test data from https://html.spec.whatwg.org/multipage/entities.json
        $data = \json_decode(\file_get_contents(__DIR__ . '/entities.json'), true);
        foreach ($data as $entity => $info) {
            // Per the spec, we only care about entities that have a trailing semi-colon.
            // See https://spec.commonmark.org/0.29/#entity-references
            if (\substr($entity, -1, 1) === ';') {
                yield [$entity, $info['characters']];
            }
        }
    }
}