From 72a464e449502060a7b6f4acfb6d53bea01142a9 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 8 Mar 2021 16:43:01 +0100 Subject: [PATCH] Fix `ConstraintViolation#getMessageTemplate()` to always return `string` `ConstraintViolation#getMessageTemplate()`'s inherited signature states that `string` is to be returned by it at all times, yet the implementation returns `null` when no message template had been provided at instantiation. This patch obviates it, returning an empty string when the message template is `null`. Ref: https://github.com/symfony/symfony/pull/40415#issuecomment-792839512 --- .../Component/Validator/ConstraintViolation.php | 2 +- .../Validator/Tests/ConstraintViolationTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index 2004745fe238d..e9aa1bd9f4400 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -100,7 +100,7 @@ public function __toString() */ public function getMessageTemplate() { - return $this->messageTemplate; + return (string) $this->messageTemplate; } /** diff --git a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php index a7734635aacff..dbac96a8aff4d 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php @@ -171,4 +171,19 @@ public function testRetrievedPropertyPathIsAStringEvenIfNotSet() ))->getPropertyPath() ); } + + public function testRetrievedMessageTemplateIsAStringEvenIfNotSet() + { + self::assertSame( + '', + (new ConstraintViolation( + 'irrelevant', + null, + [], + 'irrelevant', + 'irrelevant', + null + ))->getMessageTemplate() + ); + } }