Skip to content

Commit afb8d78

Browse files
committed
[Validator] Add ConstraintValidator::formatValue() tests
1 parent c729d31 commit afb8d78

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines 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)