From b688aa31ec9e533aa85593c728316f1ed491cfee Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Tue, 3 Sep 2019 13:59:35 +0200 Subject: [PATCH] [Validator] Add ConstraintValidator::formatValue() tests --- .../Validator/ConstraintValidator.php | 6 +- .../Tests/ConstraintValidatorTest.php | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index 35f41889ea..93cca2ea74 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -85,12 +85,10 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface */ protected function formatValue($value, $format = 0) { - $isDateTime = $value instanceof \DateTimeInterface; - - if (($format & self::PRETTY_DATE) && $isDateTime) { + if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) { if (class_exists('IntlDateFormatter')) { $locale = \Locale::getDefault(); - $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); + $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $value->getTimezone()); // neither the native nor the stub IntlDateFormatter support // DateTimeImmutable as of yet diff --git a/src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php b/src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php new file mode 100644 index 0000000000..96af6f13eb --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php @@ -0,0 +1,65 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +final class ConstraintValidatorTest extends TestCase +{ + /** + * @dataProvider formatValueProvider + */ + public function testFormatValue($expected, $value, $format = 0) + { + $this->assertSame($expected, (new TestFormatValueConstraintValidator())->formatValueProxy($value, $format)); + } + + public function formatValueProvider() + { + $data = [ + ['true', true], + ['false', false], + ['null', null], + ['resource', fopen('php://memory', 'r')], + ['"foo"', 'foo'], + ['array', []], + ['object', $toString = new TestToStringObject()], + ['ccc', $toString, ConstraintValidator::OBJECT_TO_STRING], + ['object', $dateTime = (new \DateTimeImmutable('@0'))->setTimezone(new \DateTimeZone('UTC'))], + [class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 12:00 AM' : '1970-01-01 00:00:00', $dateTime, ConstraintValidator::PRETTY_DATE], + ]; + + return $data; + } +} + +final class TestFormatValueConstraintValidator extends ConstraintValidator +{ + public function validate($value, Constraint $constraint) + { + } + + public function formatValueProxy($value, $format) + { + return $this->formatValue($value, $format); + } +} + +final class TestToStringObject +{ + public function __toString() + { + return 'ccc'; + } +}