Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb27353

Browse files
committed
feature #16161 [Validator] Add expressionLanguage to ExpressionValidator 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
2 parents 43026f9 + 4ad1e20 commit cb27353

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/Symfony/Component/Validator/Constraints/ExpressionValidator.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,10 @@ class ExpressionValidator extends ConstraintValidator
3737
*/
3838
private $expressionLanguage;
3939

40-
/**
41-
* @param PropertyAccessorInterface|null $propertyAccessor Optional as of Symfony 2.5
42-
*
43-
* @throws UnexpectedTypeException If the property accessor is invalid
44-
*/
45-
public function __construct($propertyAccessor = null)
40+
public function __construct(PropertyAccessorInterface $propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
4641
{
47-
if (null !== $propertyAccessor && !$propertyAccessor instanceof PropertyAccessorInterface) {
48-
throw new UnexpectedTypeException($propertyAccessor, 'null or \Symfony\Component\PropertyAccess\PropertyAccessorInterface');
49-
}
50-
5142
$this->propertyAccessor = $propertyAccessor;
43+
$this->expressionLanguage = $expressionLanguage;
5244
}
5345

5446
/**

src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,28 @@ public function testFailingExpressionAtPropertyLevelWithoutRoot()
217217
->setCode(Expression::EXPRESSION_FAILED_ERROR)
218218
->assertRaised();
219219
}
220+
221+
public function testExpressionLanguageUsage()
222+
{
223+
$constraint = new Expression(array(
224+
'expression' => 'false',
225+
));
226+
227+
$expressionLanguage = $this->getMock('Symfony\Component\ExpressionLanguage\ExpressionLanguage');
228+
229+
$used = false;
230+
231+
$expressionLanguage->method('evaluate')
232+
->will($this->returnCallback(function () use (&$used) {
233+
$used = true;
234+
235+
return true;
236+
}));
237+
238+
$validator = new ExpressionValidator(null, $expressionLanguage);
239+
$validator->initialize($this->createContext());
240+
$validator->validate(null, $constraint);
241+
242+
$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
243+
}
220244
}

0 commit comments

Comments
 (0)