20
20
21
21
class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
22
22
{
23
- /**
24
- * @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage
25
- */
26
- protected $ expressionLanguage ;
27
-
28
23
protected function createValidator ()
29
24
{
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 ());
38
26
}
39
27
40
28
public function testExpressionValid (): void
41
29
{
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 ([
47
31
'message ' => 'myMessage ' ,
48
32
'allowedVariables ' => [],
49
33
]));
@@ -53,37 +37,34 @@ public function testExpressionValid(): void
53
37
54
38
public function testExpressionWithoutNames (): void
55
39
{
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
+ ]) );
59
43
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 ([
61
50
'message ' => 'myMessage ' ,
51
+ 'allowedVariables ' => ['a ' ],
62
52
]));
63
53
64
54
$ this ->assertNoViolation ();
65
55
}
66
56
67
57
public function testExpressionIsNotValid (): void
68
58
{
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 ([
75
60
'message ' => 'myMessage ' ,
76
61
'allowedVariables ' => [],
77
62
]));
78
63
79
64
$ 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 ' )
81
67
->setCode (ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR )
82
68
->assertRaised ();
83
69
}
84
-
85
- protected function createExpressionLanguage (): MockObject
86
- {
87
- return $ this ->getMockBuilder ('\Symfony\Component\ExpressionLanguage\ExpressionLanguage ' )->getMock ();
88
- }
89
70
}
0 commit comments