diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index e65cb4906a..ff741ffb01 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -84,7 +84,7 @@ class ConstraintViolation implements ConstraintViolationInterface $class .= '.'; } - if ('' !== $code = (string) $this->code) { + if (null !== $this->code && '' !== $code = $this->code) { $code = ' (code '.$code.')'; } diff --git a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php index 3b9690b8d0..58a6a24954 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php @@ -108,20 +108,4 @@ EOF; $this->assertSame($expected, (string) $violation); } - - public function testNonStringCode() - { - $violation = new ConstraintViolation( - '42 cannot be used here', - 'this is the message template', - [], - ['some_value' => 42], - 'some_value', - null, - null, - 42 - ); - - self::assertSame('42', $violation->getCode()); - } } diff --git a/src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php b/src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php deleted file mode 100644 index c82f2a1d9f..0000000000 --- a/src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Tests\Violation; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Translation\IdentityTranslator; -use Symfony\Component\Validator\ConstraintViolationList; -use Symfony\Component\Validator\Tests\Fixtures\ConstraintA; -use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; - -class ConstraintViolationBuilderTest extends TestCase -{ - public function testNonStringCode() - { - $constraintViolationList = new ConstraintViolationList(); - (new ConstraintViolationBuilder($constraintViolationList, new ConstraintA(), 'invalid message', [], null, 'foo', 'baz', new IdentityTranslator())) - ->setCode(42) - ->addViolation(); - - self::assertSame('42', $constraintViolationList->get(0)->getCode()); - } -}