Skip to content

Commit 53437bd

Browse files
Merge branch '3.4' into 4.0
* 3.4: SCA with Php Inspections (EA Extended) Add test case for #25264 Fixed the null value exception case. Remove rc/beta suffix from composer.json files Throw an exception is expression language is not installed Fail as early and noisily as possible [Console][DI] Fail gracefully [FrameworkBundle] Fix visibility of a test helper [link] clear the cache after linking [DI] Trigger deprecation when setting a to-be-private synthetic service [link] Prevent warnings when running link with 2.7 [Validator] ExpressionValidator should use OBJECT_TO_STRING to allow value in message do not eagerly filter comment lines [WebProfilerBundle], [TwigBundle] Fix Profiler breaking XHTML pages (Content-Type: application/xhtml+xml)
2 parents ea6c4e8 + 1a46cfa commit 53437bd

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

Constraints/ExpressionValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function validate($value, Constraint $constraint)
4545

4646
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
4747
$this->context->buildViolation($constraint->message)
48-
->setParameter('{{ value }}', $this->formatValue($value))
48+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
4949
->setCode(Expression::EXPRESSION_FAILED_ERROR)
5050
->addViolation();
5151
}

Constraints/ValidValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function validate($value, Constraint $constraint)
2626
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
2727
}
2828

29+
if (null === $value) {
30+
return;
31+
}
32+
2933
$this->context
3034
->getValidator()
3135
->inContext($this->context)

Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\ExpressionValidator;
1616
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
1717
use Symfony\Component\Validator\Tests\Fixtures\Entity;
18+
use Symfony\Component\Validator\Tests\Fixtures\ToString;
1819

1920
class ExpressionValidatorTest extends ConstraintValidatorTestCase
2021
{
@@ -87,6 +88,40 @@ public function testFailingExpressionAtObjectLevel()
8788
->assertRaised();
8889
}
8990

91+
public function testSucceedingExpressionAtObjectLevelWithToString()
92+
{
93+
$constraint = new Expression('this.data == 1');
94+
95+
$object = new ToString();
96+
$object->data = '1';
97+
98+
$this->setObject($object);
99+
100+
$this->validator->validate($object, $constraint);
101+
102+
$this->assertNoViolation();
103+
}
104+
105+
public function testFailingExpressionAtObjectLevelWithToString()
106+
{
107+
$constraint = new Expression(array(
108+
'expression' => 'this.data == 1',
109+
'message' => 'myMessage',
110+
));
111+
112+
$object = new ToString();
113+
$object->data = '2';
114+
115+
$this->setObject($object);
116+
117+
$this->validator->validate($object, $constraint);
118+
119+
$this->buildViolation('myMessage')
120+
->setParameter('{{ value }}', 'toString')
121+
->setCode(Expression::EXPRESSION_FAILED_ERROR)
122+
->assertRaised();
123+
}
124+
90125
public function testSucceedingExpressionAtPropertyLevel()
91126
{
92127
$constraint = new Expression('value == this.data');

Tests/Constraints/ValidValidatorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public function testPropertyPathsArePassedToNestedContexts()
2020
$this->assertSame('fooBar.fooBarBaz.foo', $violations->get(0)->getPropertyPath());
2121
}
2222

23+
public function testNullValues()
24+
{
25+
$validatorBuilder = new ValidatorBuilder();
26+
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
27+
28+
$foo = new Foo();
29+
$foo->fooBar = null;
30+
$violations = $validator->validate($foo, null, array('nested'));
31+
32+
$this->assertCount(0, $violations);
33+
}
34+
2335
protected function createValidator()
2436
{
2537
return new ValidValidator();

Tests/Fixtures/ToString.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
class ToString
15+
{
16+
public $data;
17+
18+
public function __toString()
19+
{
20+
return 'toString';
21+
}
22+
}

0 commit comments

Comments
 (0)