Skip to content

Commit 4fb63ec

Browse files
Merge branch '3.4' into 4.3
* 3.4: [Validator] Add ConstraintValidator::formatValue() tests [Validator] Sync string to date behavior and throw a better exception Check phpunit configuration for listeners
2 parents d8613c7 + 0a74492 commit 4fb63ec

10 files changed

+160
-26
lines changed

ConstraintValidator.php

Lines 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

Constraints/AbstractComparisonValidator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public function validate($value, Constraint $constraint)
6565
// This allows to compare with any date/time value supported by
6666
// the DateTime constructor:
6767
// https://php.net/datetime.formats
68-
if (\is_string($comparedValue)) {
69-
if ($value instanceof \DateTimeImmutable) {
70-
// If $value is immutable, convert the compared value to a
71-
// DateTimeImmutable too
72-
$comparedValue = new \DateTimeImmutable($comparedValue);
73-
} elseif ($value instanceof \DateTimeInterface) {
74-
// Otherwise use DateTime
75-
$comparedValue = new \DateTime($comparedValue);
68+
if (\is_string($comparedValue) && $value instanceof \DateTimeInterface) {
69+
// If $value is immutable, convert the compared value to a DateTimeImmutable too, otherwise use DateTime
70+
$dateTimeClass = $value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class;
71+
72+
try {
73+
$comparedValue = new $dateTimeClass($comparedValue);
74+
} catch (\Exception $e) {
75+
throw new ConstraintDefinitionException(sprintf('The compared value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $comparedValue, $dateTimeClass, \get_class($constraint)));
7676
}
7777
}
7878

Constraints/RangeValidator.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1718

1819
/**
@@ -50,12 +51,26 @@ public function validate($value, Constraint $constraint)
5051
// the DateTime constructor:
5152
// https://php.net/datetime.formats
5253
if ($value instanceof \DateTimeInterface) {
54+
$dateTimeClass = null;
55+
5356
if (\is_string($min)) {
54-
$min = new \DateTime($min);
57+
$dateTimeClass = $value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class;
58+
59+
try {
60+
$min = new $dateTimeClass($min);
61+
} catch (\Exception $e) {
62+
throw new ConstraintDefinitionException(sprintf('The min value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $min, $dateTimeClass, \get_class($constraint)));
63+
}
5564
}
5665

5766
if (\is_string($max)) {
58-
$max = new \DateTime($max);
67+
$dateTimeClass = $dateTimeClass ?: ($value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class);
68+
69+
try {
70+
$max = new $dateTimeClass($max);
71+
} catch (\Exception $e) {
72+
throw new ConstraintDefinitionException(sprintf('The max value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $max, $dateTimeClass, \get_class($constraint)));
73+
}
5974
}
6075
}
6176

Tests/ConstraintValidatorTest.php

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 <[email protected]>
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+
}

Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Intl\Util\IntlTestHelper;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Constraints\AbstractComparison;
1617
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1718
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
1819

@@ -211,6 +212,31 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
211212
->assertRaised();
212213
}
213214

215+
/**
216+
* @dataProvider throwsOnInvalidStringDatesProvider
217+
*/
218+
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
219+
{
220+
$this->expectException(ConstraintDefinitionException::class);
221+
$this->expectExceptionMessage($expectedMessage);
222+
223+
$this->validator->validate($value, $constraint);
224+
}
225+
226+
public function throwsOnInvalidStringDatesProvider()
227+
{
228+
$constraint = $this->createConstraint([
229+
'value' => 'foo',
230+
]);
231+
232+
$constraintClass = \get_class($constraint);
233+
234+
return [
235+
[$constraint, sprintf('The compared value "foo" could not be converted to a "DateTimeImmutable" instance in the "%s" constraint.', $constraintClass), new \DateTimeImmutable()],
236+
[$constraint, sprintf('The compared value "foo" could not be converted to a "DateTime" instance in the "%s" constraint.', $constraintClass), new \DateTime()],
237+
];
238+
}
239+
214240
/**
215241
* @return array
216242
*/

Tests/Constraints/GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Validator\Constraints\AbstractComparison;
1415
use Symfony\Component\Validator\Constraints\PositiveOrZero;
1516

1617
/**
@@ -98,10 +99,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
9899
}
99100

100101
/**
101-
* @dataProvider provideValidComparisonsToPropertyPath
102+
* @dataProvider throwsOnInvalidStringDatesProvider
102103
*/
103-
public function testValidComparisonToPropertyPathOnArray($comparedValue)
104+
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
104105
{
105-
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
106+
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
106107
}
107108
}

Tests/Constraints/GreaterThanValidatorWithPositiveConstraintTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Validator\Constraints\AbstractComparison;
1415
use Symfony\Component\Validator\Constraints\Positive;
1516

1617
/**
@@ -101,10 +102,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
101102
}
102103

103104
/**
104-
* @dataProvider provideValidComparisonsToPropertyPath
105+
* @dataProvider throwsOnInvalidStringDatesProvider
105106
*/
106-
public function testValidComparisonToPropertyPathOnArray($comparedValue)
107+
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
107108
{
108-
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
109+
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
109110
}
110111
}

Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Validator\Constraints\AbstractComparison;
1415
use Symfony\Component\Validator\Constraints\NegativeOrZero;
1516

1617
/**
@@ -101,10 +102,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
101102
}
102103

103104
/**
104-
* @dataProvider provideValidComparisonsToPropertyPath
105+
* @dataProvider throwsOnInvalidStringDatesProvider
105106
*/
106-
public function testValidComparisonToPropertyPathOnArray($comparedValue)
107+
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
107108
{
108-
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
109+
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
109110
}
110111
}

Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Validator\Constraints\AbstractComparison;
1415
use Symfony\Component\Validator\Constraints\Negative;
1516

1617
/**
@@ -101,10 +102,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
101102
}
102103

103104
/**
104-
* @dataProvider provideValidComparisonsToPropertyPath
105+
* @dataProvider throwsOnInvalidStringDatesProvider
105106
*/
106-
public function testValidComparisonToPropertyPathOnArray($comparedValue)
107+
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
107108
{
108-
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
109+
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
109110
}
110111
}

Tests/Constraints/RangeValidatorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Intl\Util\IntlTestHelper;
1515
use Symfony\Component\Validator\Constraints\Range;
1616
use Symfony\Component\Validator\Constraints\RangeValidator;
17+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1718
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
1819

1920
class RangeValidatorTest extends ConstraintValidatorTestCase
@@ -389,4 +390,29 @@ public function testNonNumeric()
389390
->setCode(Range::INVALID_CHARACTERS_ERROR)
390391
->assertRaised();
391392
}
393+
394+
/**
395+
* @dataProvider throwsOnInvalidStringDatesProvider
396+
*/
397+
public function testThrowsOnInvalidStringDates($expectedMessage, $value, $min, $max)
398+
{
399+
$this->expectException(ConstraintDefinitionException::class);
400+
$this->expectExceptionMessage($expectedMessage);
401+
402+
$this->validator->validate($value, new Range([
403+
'min' => $min,
404+
'max' => $max,
405+
]));
406+
}
407+
408+
public function throwsOnInvalidStringDatesProvider()
409+
{
410+
return [
411+
['The min value "foo" could not be converted to a "DateTimeImmutable" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTimeImmutable(), 'foo', null],
412+
['The min value "foo" could not be converted to a "DateTime" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTime(), 'foo', null],
413+
['The max value "foo" could not be converted to a "DateTimeImmutable" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTimeImmutable(), null, 'foo'],
414+
['The max value "foo" could not be converted to a "DateTime" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTime(), null, 'foo'],
415+
['The min value "bar" could not be converted to a "DateTimeImmutable" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTimeImmutable(), 'bar', 'ccc'],
416+
];
417+
}
392418
}

0 commit comments

Comments
 (0)