File: TwigValidatorTest.php

package info (click to toggle)
symfony 7.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,424 kB
  • sloc: php: 1,510,651; xml: 7,039; javascript: 979; sh: 586; makefile: 242; pascal: 70
file content (121 lines) | stat: -rw-r--r-- 4,186 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bridge\Twig\Tests\Validator\Constraints;

use Symfony\Bridge\Twig\Validator\Constraints\Twig;
use Symfony\Bridge\Twig\Validator\Constraints\TwigValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Twig\DeprecatedCallableInfo;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\TwigFilter;

/**
 * @author Mokhtar Tlili <tlili.mokhtar@gmail.com>
 */
class TwigValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): TwigValidator
    {
        $environment = new Environment(new ArrayLoader());
        $environment->addFilter(new TwigFilter('humanize_filter', fn ($v) => $v));
        if (class_exists(DeprecatedCallableInfo::class)) {
            $options = ['deprecation_info' => new DeprecatedCallableInfo('foo/bar', '1.1')];
        } else {
            $options = ['deprecated' => true];
        }

        $environment->addFilter(new TwigFilter('deprecated_filter', fn ($v) => $v, $options));

        return new TwigValidator($environment);
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getValidValues')]
    public function testTwigIsValid($value)
    {
        $this->validator->validate($value, new Twig());

        $this->assertNoViolation();
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getInvalidValues')]
    public function testInvalidValues($value, $message, $line)
    {
        $constraint = new Twig('myMessageTest');

        $this->validator->validate($value, $constraint);

        $this->buildViolation('myMessageTest')
            ->setParameter('{{ error }}', $message)
            ->setParameter('{{ line }}', $line)
            ->setCode(Twig::INVALID_TWIG_ERROR)
            ->assertRaised();
    }

    /**
     * When deprecations are skipped by the validator, the testsuite reporter will catch them so we need to mark the test as legacy.
     */
    #[\PHPUnit\Framework\Attributes\Group('legacy')]
    public function testTwigWithSkipDeprecation()
    {
        $constraint = new Twig(skipDeprecations: true);

        $this->validator->validate('{{ name|deprecated_filter }}', $constraint);

        $this->assertNoViolation();
    }

    public function testTwigWithoutSkipDeprecation()
    {
        $constraint = new Twig(skipDeprecations: false);

        $this->validator->validate('{{ name|deprecated_filter }}', $constraint);

        $line = 1;
        $error = 'Twig Filter "deprecated_filter" is deprecated in  at line 1 at line 1.';
        if (class_exists(DeprecatedCallableInfo::class)) {
            $line = 0;
            $error = 'Since foo/bar 1.1: Twig Filter "deprecated_filter" is deprecated.';
        }
        $this->buildViolation($constraint->message)
            ->setParameter('{{ error }}', $error)
            ->setParameter('{{ line }}', $line)
            ->setCode(Twig::INVALID_TWIG_ERROR)
            ->assertRaised();
    }

    public static function getValidValues()
    {
        return [
            ['Hello {{ name }}'],
            ['{% if condition %}Yes{% else %}No{% endif %}'],
            ['{# Comment #}'],
            ['Hello {{ "world"|upper }}'],
            ['{% for i in 1..3 %}Item {{ i }}{% endfor %}'],
            ['{{ name|humanize_filter }}'],
        ];
    }

    public static function getInvalidValues()
    {
        return [
            // Invalid syntax example (missing end tag)
            ['{% if condition %}Oops', 'Unexpected end of template at line 1.', 1],
            // Another syntax error example (unclosed variable)
            ['Hello {{ name', 'Unexpected token "end of template" ("end of print statement" expected) at line 1.', 1],
            // Unknown filter error
            ['Hello {{ name|unknown_filter }}', 'Unknown "unknown_filter" filter at line 1.', 1],
            // Invalid variable syntax
            ['Hello {{ .name }}', 'Unexpected token "operator" of value "." at line 1.', 1],
        ];
    }
}