Skip to content

Commit 09448e6

Browse files
committed
simplify the tests
1 parent 7a012b9 commit 09448e6

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php

+15-34
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,14 @@
2020

2121
class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
2222
{
23-
/**
24-
* @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage
25-
*/
26-
protected $expressionLanguage;
27-
2823
protected function createValidator()
2924
{
30-
return new ExpressionLanguageSyntaxValidator($this->expressionLanguage);
31-
}
32-
33-
protected function setUp(): void
34-
{
35-
$this->expressionLanguage = $this->createExpressionLanguage();
36-
37-
parent::setUp();
25+
return new ExpressionLanguageSyntaxValidator(new ExpressionLanguage());
3826
}
3927

4028
public function testExpressionValid(): void
4129
{
42-
$this->expressionLanguage->expects($this->once())
43-
->method('lint')
44-
->with($this->value, []);
45-
46-
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
30+
$this->validator->validate('1 + 1', new ExpressionLanguageSyntax([
4731
'message' => 'myMessage',
4832
'allowedVariables' => [],
4933
]));
@@ -53,37 +37,34 @@ public function testExpressionValid(): void
5337

5438
public function testExpressionWithoutNames(): void
5539
{
56-
$this->expressionLanguage->expects($this->once())
57-
->method('lint')
58-
->with($this->value, null);
40+
$this->validator->validate('1 + 1', new ExpressionLanguageSyntax([
41+
'message' => 'myMessage',
42+
]));
5943

60-
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
44+
$this->assertNoViolation();
45+
}
46+
47+
public function testExpressionWithAllowedVariableName(): void
48+
{
49+
$this->validator->validate('a + 1', new ExpressionLanguageSyntax([
6150
'message' => 'myMessage',
51+
'allowedVariables' => ['a'],
6252
]));
6353

6454
$this->assertNoViolation();
6555
}
6656

6757
public function testExpressionIsNotValid(): void
6858
{
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([
59+
$this->validator->validate('a + 1', new ExpressionLanguageSyntax([
7560
'message' => 'myMessage',
7661
'allowedVariables' => [],
7762
]));
7863

7964
$this->buildViolation('myMessage')
80-
->setParameter('{{ syntax_error }}', '"Test exception around position 42."')
65+
->setParameter('{{ syntax_error }}', '"Variable "a" is not valid around position 1 for expression `a + 1`."')
66+
->setInvalidValue('a + 1')
8167
->setCode(ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR)
8268
->assertRaised();
8369
}
84-
85-
protected function createExpressionLanguage(): MockObject
86-
{
87-
return $this->getMockBuilder('\Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
88-
}
8970
}

0 commit comments

Comments
 (0)