|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 | + |
| 12 | +namespace Symfony\Bridge\Twig\Tests\Validator\Constraints; |
| 13 | + |
| 14 | +use Symfony\Bridge\Twig\Validator\Constraints\Twig; |
| 15 | +use Symfony\Bridge\Twig\Validator\Constraints\TwigValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | +use Twig\Environment; |
| 18 | +use Twig\Loader\ArrayLoader; |
| 19 | +use Twig\TwigFilter; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Mokhtar Tlili <tlili.mokhtar@gmail.com> |
| 23 | + */ |
| 24 | +class TwigValidatorTest extends ConstraintValidatorTestCase |
| 25 | +{ |
| 26 | + protected function createValidator(): TwigValidator |
| 27 | + { |
| 28 | + $environment = new Environment(new ArrayLoader()); |
| 29 | + $environment->addFilter(new TwigFilter('humanize_filter', fn ($v) => $v)); |
| 30 | + |
| 31 | + return new TwigValidator($environment); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @dataProvider getValidValues |
| 36 | + */ |
| 37 | + public function testTwigIsValid($value) |
| 38 | + { |
| 39 | + $this->validator->validate($value, new Twig()); |
| 40 | + |
| 41 | + $this->assertNoViolation(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @dataProvider getInvalidValues |
| 46 | + */ |
| 47 | + public function testInvalidValues($value, $message, $line) |
| 48 | + { |
| 49 | + $constraint = new Twig('myMessageTest'); |
| 50 | + |
| 51 | + $this->validator->validate($value, $constraint); |
| 52 | + |
| 53 | + $this->buildViolation('myMessageTest') |
| 54 | + ->setParameter('{{ error }}', $message) |
| 55 | + ->setParameter('{{ line }}', $line) |
| 56 | + ->setCode(Twig::INVALID_TWIG_ERROR) |
| 57 | + ->assertRaised(); |
| 58 | + } |
| 59 | + |
| 60 | + public static function getValidValues() |
| 61 | + { |
| 62 | + return [ |
| 63 | + ['Hello {{ name }}'], |
| 64 | + ['{% if condition %}Yes{% else %}No{% endif %}'], |
| 65 | + ['{# Comment #}'], |
| 66 | + ['Hello {{ "world" | upper }}'], |
| 67 | + ['{% for i in 1..3 %}Item {{ i }}{% endfor %}'], |
| 68 | + ['{{ name|humanize_filter }}'], |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + public static function getInvalidValues() |
| 73 | + { |
| 74 | + return [ |
| 75 | + // Invalid syntax example (missing end tag) |
| 76 | + ['{% if condition %}Oops', 'Unexpected end of template at line 1.', 1], |
| 77 | + // Another syntax error example (unclosed variable) |
| 78 | + ['Hello {{ name', 'Unexpected token "end of template" ("end of print statement" expected) at line 1.', 1], |
| 79 | + // Unknown filter error |
| 80 | + ['Hello {{ name | unknown_filter }}', 'Unknown "unknown_filter" filter at line 1.', 1], |
| 81 | + // Invalid variable syntax |
| 82 | + ['Hello {{ .name }}', 'Unexpected token "punctuation" of value "." at line 1.', 1], |
| 83 | + ]; |
| 84 | + } |
| 85 | +} |
0 commit comments