|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 16 | +use Symfony\Component\ExpressionLanguage\SyntaxError; |
| 17 | +use Symfony\Component\Validator\Constraints\ExpressionLanguageSyntax; |
| 18 | +use Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator; |
| 19 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 20 | + |
| 21 | +class ExpressionLanguageSyntaxTest extends ConstraintValidatorTestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage |
| 25 | + */ |
| 26 | + protected $expressionLanguage; |
| 27 | + |
| 28 | + protected function createValidator() |
| 29 | + { |
| 30 | + return new ExpressionLanguageSyntaxValidator($this->expressionLanguage); |
| 31 | + } |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->expressionLanguage = $this->createExpressionLanguage(); |
| 36 | + |
| 37 | + parent::setUp(); |
| 38 | + } |
| 39 | + |
| 40 | + public function testExpressionValid(): void |
| 41 | + { |
| 42 | + $this->expressionLanguage->expects($this->once()) |
| 43 | + ->method('lint') |
| 44 | + ->with($this->value, []); |
| 45 | + |
| 46 | + $this->validator->validate($this->value, new ExpressionLanguageSyntax([ |
| 47 | + 'message' => 'myMessage', |
| 48 | + ])); |
| 49 | + |
| 50 | + $this->assertNoViolation(); |
| 51 | + } |
| 52 | + |
| 53 | + public function testExpressionWithoutNames(): void |
| 54 | + { |
| 55 | + $this->expressionLanguage->expects($this->once()) |
| 56 | + ->method('lint') |
| 57 | + ->with($this->value, null); |
| 58 | + |
| 59 | + $this->validator->validate($this->value, new ExpressionLanguageSyntax([ |
| 60 | + 'message' => 'myMessage', |
| 61 | + 'validateNames' => false, |
| 62 | + ])); |
| 63 | + |
| 64 | + $this->assertNoViolation(); |
| 65 | + } |
| 66 | + |
| 67 | + public function testExpressionIsNotValid(): void |
| 68 | + { |
| 69 | + $this->expressionLanguage->expects($this->once()) |
| 70 | + ->method('lint') |
| 71 | + ->with($this->value, []) |
| 72 | + ->willThrowException(new SyntaxError('Test exception', 42)); |
| 73 | + |
| 74 | + $this->validator->validate($this->value, new ExpressionLanguageSyntax([ |
| 75 | + 'message' => 'myMessage', |
| 76 | + ])); |
| 77 | + |
| 78 | + $this->buildViolation('myMessage') |
| 79 | + ->setParameter('{{ syntax_error }}', '"Test exception around position 42."') |
| 80 | + ->setCode(ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR) |
| 81 | + ->assertRaised(); |
| 82 | + } |
| 83 | + |
| 84 | + protected function createExpressionLanguage(): MockObject |
| 85 | + { |
| 86 | + return $this->getMockBuilder('\Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock(); |
| 87 | + } |
| 88 | +} |
0 commit comments