File: EscaperTest.php

package info (click to toggle)
php-twig 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,940 kB
  • sloc: php: 23,320; makefile: 110; sh: 43
file content (91 lines) | stat: -rw-r--r-- 2,986 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
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
83
84
85
86
87
88
89
90
91
<?php

namespace Twig\Tests;

/*
 * This file is part of Twig.
 *
 * (c) Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Extension\EscaperExtension;
use Twig\Loader\ArrayLoader;
use Twig\Runtime\EscaperRuntime;

class EscaperTest extends TestCase
{
    /**
     * @dataProvider provideCustomEscaperCases
     *
     * @group legacy
     */
    #[DataProvider('provideCustomEscaperCases')]
    public function testCustomEscaper($expected, $string, $strategy)
    {
        $twig = new Environment(new ArrayLoader());
        $escaperExt = $twig->getExtension(EscaperExtension::class);
        $escaperExt->setEscaper('foo', 'Twig\Tests\legacy_escaper');
        $this->assertSame($expected, $twig->getRuntime(EscaperRuntime::class)->escape($string, $strategy, 'ISO-8859-1'));
    }

    public static function provideCustomEscaperCases()
    {
        return [
            ['foo**ISO-8859-1**UTF-8', 'foo', 'foo'],
            ['**ISO-8859-1**UTF-8', null, 'foo'],
            ['42**ISO-8859-1**UTF-8', 42, 'foo'],
        ];
    }

    /**
     * @dataProvider provideCustomEscaperCases
     *
     * @group legacy
     */
    #[DataProvider('provideCustomEscaperCases')]
    public function testCustomEscaperWithoutCallingSetEscaperRuntime($expected, $string, $strategy)
    {
        $twig = new Environment(new ArrayLoader());
        $escaperExt = $twig->getExtension(EscaperExtension::class);
        $escaperExt->setEscaper('foo', 'Twig\Tests\legacy_escaper');
        $this->assertSame($expected, $twig->getRuntime(EscaperRuntime::class)->escape($string, $strategy, 'ISO-8859-1'));
    }

    /**
     * @group legacy
     */
    public function testCustomEscapersOnMultipleEnvs()
    {
        $env1 = new Environment(new ArrayLoader());
        $escaperExt1 = $env1->getExtension(EscaperExtension::class);
        $escaperExt1->setEscaper('foo', 'Twig\Tests\legacy_escaper');

        $env2 = new Environment(new ArrayLoader());
        $escaperExt2 = $env2->getExtension(EscaperExtension::class);
        $escaperExt2->setEscaper('foo', 'Twig\Tests\legacy_escaper_again');

        $this->assertSame('foo**ISO-8859-1**UTF-8', $env1->getRuntime(EscaperRuntime::class)->escape('foo', 'foo', 'ISO-8859-1'));
        $this->assertSame('foo**ISO-8859-1**UTF-8**again', $env2->getRuntime(EscaperRuntime::class)->escape('foo', 'foo', 'ISO-8859-1'));
    }

    public function testLastModified()
    {
        $this->assertGreaterThan(1000000000, (new EscaperExtension())->getLastModified());
    }
}

function legacy_escaper(Environment $twig, $string, $charset)
{
    return $string.'**'.$charset.'**'.$twig->getCharset();
}

function legacy_escaper_again(Environment $twig, $string, $charset)
{
    return $string.'**'.$charset.'**'.$twig->getCharset().'**again';
}