From cfc6fc8527f2f81529f0c85cbe8aa2329e74c461 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 31 May 2020 09:20:08 +0200 Subject: [PATCH] simplify the tests --- .../ExpressionLanguageSyntaxValidatorTest.php | 51 ++++++------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php index 7db835b333..19cbb1b94b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php @@ -20,30 +20,14 @@ use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase { - /** - * @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage - */ - protected $expressionLanguage; - protected function createValidator() { - return new ExpressionLanguageSyntaxValidator($this->expressionLanguage); - } - - protected function setUp(): void - { - $this->expressionLanguage = $this->createExpressionLanguage(); - - parent::setUp(); + return new ExpressionLanguageSyntaxValidator(new ExpressionLanguage()); } public function testExpressionValid(): void { - $this->expressionLanguage->expects($this->once()) - ->method('lint') - ->with($this->value, []); - - $this->validator->validate($this->value, new ExpressionLanguageSyntax([ + $this->validator->validate('1 + 1', new ExpressionLanguageSyntax([ 'message' => 'myMessage', 'allowedVariables' => [], ])); @@ -53,37 +37,34 @@ class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase public function testExpressionWithoutNames(): void { - $this->expressionLanguage->expects($this->once()) - ->method('lint') - ->with($this->value, null); - - $this->validator->validate($this->value, new ExpressionLanguageSyntax([ + $this->validator->validate('1 + 1', new ExpressionLanguageSyntax([ 'message' => 'myMessage', ])); $this->assertNoViolation(); } + public function testExpressionWithAllowedVariableName(): void + { + $this->validator->validate('a + 1', new ExpressionLanguageSyntax([ + 'message' => 'myMessage', + 'allowedVariables' => ['a'], + ])); + + $this->assertNoViolation(); + } + public function testExpressionIsNotValid(): void { - $this->expressionLanguage->expects($this->once()) - ->method('lint') - ->with($this->value, []) - ->willThrowException(new SyntaxError('Test exception', 42)); - - $this->validator->validate($this->value, new ExpressionLanguageSyntax([ + $this->validator->validate('a + 1', new ExpressionLanguageSyntax([ 'message' => 'myMessage', 'allowedVariables' => [], ])); $this->buildViolation('myMessage') - ->setParameter('{{ syntax_error }}', '"Test exception around position 42."') + ->setParameter('{{ syntax_error }}', '"Variable "a" is not valid around position 1 for expression `a + 1`."') + ->setInvalidValue('a + 1') ->setCode(ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR) ->assertRaised(); } - - protected function createExpressionLanguage(): MockObject - { - return $this->getMockBuilder('\Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock(); - } }