[Validator] Add ConstraintValidator::formatValue() tests
This commit is contained in:
parent
33e466577f
commit
b688aa31ec
@ -85,12 +85,10 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
*/
|
*/
|
||||||
protected function formatValue($value, $format = 0)
|
protected function formatValue($value, $format = 0)
|
||||||
{
|
{
|
||||||
$isDateTime = $value instanceof \DateTimeInterface;
|
if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
|
||||||
|
|
||||||
if (($format & self::PRETTY_DATE) && $isDateTime) {
|
|
||||||
if (class_exists('IntlDateFormatter')) {
|
if (class_exists('IntlDateFormatter')) {
|
||||||
$locale = \Locale::getDefault();
|
$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
|
// neither the native nor the stub IntlDateFormatter support
|
||||||
// DateTimeImmutable as of yet
|
// DateTimeImmutable as of yet
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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';
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user