minor #33434 [Validator] Add ConstraintValidator::formatValue() tests (fancyweb)

This PR was squashed before being merged into the 3.4 branch (closes #33434).

Discussion
----------

[Validator] Add ConstraintValidator::formatValue() tests

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

So https://github.com/symfony/symfony/pull/33401 tests can be built on top of this.

Commits
-------

b688aa31ec [Validator] Add ConstraintValidator::formatValue() tests
This commit is contained in:
Fabien Potencier 2019-09-03 18:24:41 +02:00
commit 33c02aedec
2 changed files with 67 additions and 4 deletions

View File

@ -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

View File

@ -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';
}
}