1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
<?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;
use Symfony\Component\Validator\Tests\Fixtures\TestEnum;
class ConstraintValidatorTest extends TestCase
{
/**
* @dataProvider formatValueProvider
*/
public function testFormatValue($expected, $value, $format = 0)
{
\Locale::setDefault('en');
$this->assertSame($expected, (new TestFormatValueConstraintValidator())->formatValueProxy($value, $format));
}
/**
* @dataProvider formatValueStartsProvider
*/
public function testFormatValueStarts($expected, $value, $format = 0)
{
\Locale::setDefault('en');
$this->assertStringStartsWith($expected, (new TestFormatValueConstraintValidator())->formatValueProxy($value, $format));
}
/**
* @dataProvider formatValueEndsProvider
*/
public function testFormatValueEnds($expected, $value, $format = 0)
{
\Locale::setDefault('en');
$this->assertStringEndsWith($expected, (new TestFormatValueConstraintValidator())->formatValueProxy($value, $format));
}
public static function formatValueProvider()
{
$defaultTimezone = date_default_timezone_get();
date_default_timezone_set('Europe/Moscow'); // GMT+3
$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('1971-02-02T08:00:00UTC')],
];
date_default_timezone_set($defaultTimezone);
return $data;
}
public static function formatValueStartsProvider()
{
$defaultTimezone = date_default_timezone_get();
date_default_timezone_set('Europe/Moscow'); // GMT+3
$data = [
[class_exists(\IntlDateFormatter::class) ? 'Oct 4, 2019, 11:02' : '2019-10-04 11:02:03', new \DateTimeImmutable('2019-10-04T11:02:03+09:00'), ConstraintValidator::PRETTY_DATE],
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 6:00' : '1970-01-01 06:00:00', new \DateTimeImmutable('1970-01-01T06:00:00Z'), ConstraintValidator::PRETTY_DATE],
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 3:00' : '1970-01-01 15:00:00', (new \DateTimeImmutable('1970-01-01T23:00:00'))->setTimezone(new \DateTimeZone('America/New_York')), ConstraintValidator::PRETTY_DATE],
];
date_default_timezone_set($defaultTimezone);
return $data;
}
public static function formatValueEndsProvider()
{
$defaultTimezone = date_default_timezone_get();
date_default_timezone_set('Europe/Moscow'); // GMT+3
$data = [
[class_exists(\IntlDateFormatter::class) ? 'AM' : '2019-10-04 11:02:03', new \DateTimeImmutable('2019-10-04T11:02:03+09:00'), ConstraintValidator::PRETTY_DATE],
[class_exists(\IntlDateFormatter::class) ? 'AM' : '1970-01-01 06:00:00', new \DateTimeImmutable('1970-01-01T06:00:00Z'), ConstraintValidator::PRETTY_DATE],
[class_exists(\IntlDateFormatter::class) ? 'PM' : '1970-01-01 15:00:00', (new \DateTimeImmutable('1970-01-01T23:00:00'))->setTimezone(new \DateTimeZone('America/New_York')), ConstraintValidator::PRETTY_DATE],
];
if (\PHP_VERSION_ID >= 80100) {
$data[] = ['FirstCase', TestEnum::FirstCase];
}
date_default_timezone_set($defaultTimezone);
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';
}
}
|