Skip to content

Commit

Permalink
minor #33434 [Validator] Add ConstraintValidator::formatValue() tests…
Browse files Browse the repository at this point in the history
… (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 #33401 tests can be built on top of this.

Commits
-------

b688aa3 [Validator] Add ConstraintValidator::formatValue() tests
  • Loading branch information
fabpot committed Sep 3, 2019
2 parents 38514cb + b688aa3 commit 33c02ae
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Symfony/Component/Validator/ConstraintValidator.php
Expand Up @@ -85,12 +85,10 @@ protected function formatTypeOf($value)
*/
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
Expand Down
65 changes: 65 additions & 0 deletions src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php
@@ -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';
}
}

0 comments on commit 33c02ae

Please sign in to comment.