simplify the tests
This commit is contained in:
parent
2af156d6fe
commit
cfc6fc8527
@ -20,30 +20,14 @@ use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
|||||||
|
|
||||||
class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
|
class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage
|
|
||||||
*/
|
|
||||||
protected $expressionLanguage;
|
|
||||||
|
|
||||||
protected function createValidator()
|
protected function createValidator()
|
||||||
{
|
{
|
||||||
return new ExpressionLanguageSyntaxValidator($this->expressionLanguage);
|
return new ExpressionLanguageSyntaxValidator(new ExpressionLanguage());
|
||||||
}
|
|
||||||
|
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
$this->expressionLanguage = $this->createExpressionLanguage();
|
|
||||||
|
|
||||||
parent::setUp();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExpressionValid(): void
|
public function testExpressionValid(): void
|
||||||
{
|
{
|
||||||
$this->expressionLanguage->expects($this->once())
|
$this->validator->validate('1 + 1', new ExpressionLanguageSyntax([
|
||||||
->method('lint')
|
|
||||||
->with($this->value, []);
|
|
||||||
|
|
||||||
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
'allowedVariables' => [],
|
'allowedVariables' => [],
|
||||||
]));
|
]));
|
||||||
@ -53,37 +37,34 @@ class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
public function testExpressionWithoutNames(): void
|
public function testExpressionWithoutNames(): void
|
||||||
{
|
{
|
||||||
$this->expressionLanguage->expects($this->once())
|
$this->validator->validate('1 + 1', new ExpressionLanguageSyntax([
|
||||||
->method('lint')
|
|
||||||
->with($this->value, null);
|
|
||||||
|
|
||||||
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->assertNoViolation();
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testExpressionWithAllowedVariableName(): void
|
||||||
|
{
|
||||||
|
$this->validator->validate('a + 1', new ExpressionLanguageSyntax([
|
||||||
|
'message' => 'myMessage',
|
||||||
|
'allowedVariables' => ['a'],
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
public function testExpressionIsNotValid(): void
|
public function testExpressionIsNotValid(): void
|
||||||
{
|
{
|
||||||
$this->expressionLanguage->expects($this->once())
|
$this->validator->validate('a + 1', new ExpressionLanguageSyntax([
|
||||||
->method('lint')
|
|
||||||
->with($this->value, [])
|
|
||||||
->willThrowException(new SyntaxError('Test exception', 42));
|
|
||||||
|
|
||||||
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
'allowedVariables' => [],
|
'allowedVariables' => [],
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->buildViolation('myMessage')
|
$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)
|
->setCode(ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR)
|
||||||
->assertRaised();
|
->assertRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createExpressionLanguage(): MockObject
|
|
||||||
{
|
|
||||||
return $this->getMockBuilder('\Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user