8000 [Validator] Add expressionLanguage to ExpressionValidator constructor by enumag · Pull Request #16161 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add expressionLanguage to ExpressionValidator constructor #16161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,10 @@ class ExpressionValidator extends ConstraintValidator
*/
private $expressionLanguage;

/**
* @param PropertyAccessorInterface|null $propertyAccessor Optional as of Symfony 2.5
*
* @throws UnexpectedTypeException If the property accessor is invalid
*/
public function __construct($propertyAccessor = null)
public function __construct(PropertyAccessorInterface $propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
{
if (null !== $propertyAccessor && !$propertyAccessor instanceof PropertyAccessorInterface) {
throw new UnexpectedTypeException($propertyAccessor, 'null or \Symfony\Component\PropertyAccess\PropertyAccessorInterface');
}

$this->propertyAccessor = $propertyAccessor;
$this->expressionLanguage = $expressionLanguage;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,28 @@ public function testFailingExpressionAtPropertyLevelWithoutRoot()
->setCode(Expression::EXPRESSION_FAILED_ERROR)
->assertRaised();
}

public function testExpressionLanguageUsage()
{
$constraint = new Expression(array(
'expression' => 'false',
));

$expressionLanguage = $this->getMock('Symfony\Component\ExpressionLanguage\ExpressionLanguage');

$used = false;

$expressionLanguage->method('evaluate')
->will($this->returnCallback(function () use (&$used) {
$used = true;

return true;
}));

$validator = new ExpressionValidator(null, $expressionLanguage);
$validator->initialize($this->createContext());
$validator->validate(null, $constraint);

$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
}
}
0