Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b688aa3

Browse filesBrowse files
fancywebfabpot
authored andcommitted
[Validator] Add ConstraintValidator::formatValue() tests
1 parent 33e4665 commit b688aa3
Copy full SHA for b688aa3

File tree

2 files changed

+67
-4
lines changed
Filter options

2 files changed

+67
-4
lines changed

‎src/Symfony/Component/Validator/ConstraintValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ConstraintValidator.php
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ protected function formatTypeOf($value)
8585
*/
8686
protected function formatValue($value, $format = 0)
8787
{
88-
$isDateTime = $value instanceof \DateTimeInterface;
89-
90-
if (($format & self::PRETTY_DATE) && $isDateTime) {
88+
if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
9189
if (class_exists('IntlDateFormatter')) {
9290
$locale = \Locale::getDefault();
93-
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
91+
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $value->getTimezone());
9492

9593
// neither the native nor the stub IntlDateFormatter support
9694
// DateTimeImmutable as of yet
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\ConstraintValidator;
17+
18+
final class ConstraintValidatorTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider formatValueProvider
22+
*/
23+
public function testFormatValue($expected, $value, $format = 0)
24+
{
25+
$this->assertSame($expected, (new TestFormatValueConstraintValidator())->formatValueProxy($value, $format));
26+
}
27+
28+
public function formatValueProvider()
29+
{
30+
$data = [
31+
['true', true],
32+
['false', false],
33+
['null', null],
34+
['resource', fopen('php://memory', 'r')],
35+
['"foo"', 'foo'],
36+
['array', []],
37+
['object', $toString = new TestToStringObject()],
38+
['ccc', $toString, ConstraintValidator::OBJECT_TO_STRING],
39+
['object', $dateTime = (new \DateTimeImmutable('@0'))->setTimezone(new \DateTimeZone('UTC'))],
40+
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 12:00 AM' : '1970-01-01 00:00:00', $dateTime, ConstraintValidator::PRETTY_DATE],
41+
];
42+
43+
return $data;
44+
}
45+
}
46+
47+
final class TestFormatValueConstraintValidator extends ConstraintValidator
48+
{
49+
public function validate($value, Constraint $constraint)
50+
{
51+
}
52+
53+
public function formatValueProxy($value, $format)
54+
{
55+
return $this->formatValue($value, $format);
56+
}
57+
}
58+
59+
final class TestToStringObject
60+
{
61+
public function __toString()
62+
{
63+
return 'ccc';
64+
}
65+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.