Skip to content

Commit

Permalink
feature #16161 [Validator] Add expressionLanguage to ExpressionValida…
Browse files Browse the repository at this point in the history
…tor constructor (enumag)

This PR was merged into the 2.8 branch.

Discussion
----------

[Validator] Add expressionLanguage to ExpressionValidator constructor

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

4ad1e20 [Validator] Add expressionLanguage to ExpressionValidator constructor
  • Loading branch information
fabpot committed Oct 19, 2015
2 parents 43026f9 + 4ad1e20 commit cb27353
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
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
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 comments on commit cb27353

Please sign in to comment.