bug #9865 [Validator] Fixes message value for objects (jongotlin)

This PR was submitted for the 2.2 branch but it was merged into the 2.3 branch instead (closes #9865).

Discussion
----------

[Validator] Fixes message value for objects

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

Commits
-------

7b69e9b [Validator] Fixes message value for objects
This commit is contained in:
Fabien Potencier 2013-12-27 22:53:59 +01:00
commit c9dd348947
2 changed files with 15 additions and 6 deletions

View File

@ -27,6 +27,12 @@ class NullValidator extends ConstraintValidator
public function validate($value, Constraint $constraint)
{
if (null !== $value) {
if (is_object($value)) {
$value = get_class($value);
} elseif (is_array($value)) {
$value = 'Array';
}
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}
}

View File

@ -43,7 +43,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value)
public function testInvalidValues($value, $readableValue)
{
$constraint = new Null(array(
'message' => 'myMessage'
@ -52,7 +52,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $value,
'{{ value }}' => $readableValue,
));
$this->validator->validate($value, $constraint);
@ -61,10 +61,13 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase
public function getInvalidValues()
{
return array(
array(0),
array(false),
array(true),
array(''),
array(0, 0),
array(false, false),
array(true, true),
array('', ''),
array('foo bar', 'foo bar'),
array(new \DateTime(), 'DateTime'),
array(array(), 'Array'),
);
}
}