Skip to content

Commit d5be012

Browse files
Merge branch '5.0'
* 5.0: (21 commits) fix merge CS [FrameworkBundle][ContainerLintCommand] Improve messages when the kernel or the container is not supported [Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer stop using deprecated Doctrine persistence classes [Cache] Fix wrong classname in deprecation message Fix regex lookahead syntax in ApplicationTest Fixed syntax in comment [SecurityBundle][FirewallMap] Remove unused property [Messenger][AMQP] Use delivery_mode=2 by default [FrameworkBundle][DependencyInjection] Skip removed ids in the lint container command and its associated pass [SECURITY] Revert "AbstractAuthenticationListener.php error instead info. Rebase of #28462" [FrameworkBundle][Secrets] Hook configured local dotenv file [DI] Improve performance of processDefinition fix redis multi host dsn not recognized fix constructor argument type declaration Fix invalid Windows path normalization [Validator][ConstraintValidator] Safe fail on invalid timezones [DoctrineBridge] Fixed submitting invalid ids when using queries with limit [FrameworkBundle] Add info & example to auto_mapping config ...
2 parents d3b4df0 + ca67836 commit d5be012

16 files changed

+108
-15
lines changed

ConstraintValidator.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,12 @@ protected function formatValue($value, int $format = 0)
8787
{
8888
if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
8989
if (class_exists('IntlDateFormatter')) {
90-
$locale = \Locale::getDefault();
91-
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $value->getTimezone());
90+
$formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC');
9291

93-
// neither the native nor the stub IntlDateFormatter support
94-
// DateTimeImmutable as of yet
95-
if (!$value instanceof \DateTime) {
96-
$value = new \DateTime($value->format('Y-m-d H:i:s.u e'));
97-
}
98-
99-
return $formatter->format($value);
92+
return $formatter->format(new \DateTime(
93+
$value->format('Y-m-d H:i:s.u'),
94+
new \DateTimeZone('UTC')
95+
));
10096
}
10197

10298
return $value->format('Y-m-d H:i:s');

Constraints/GreaterThanOrEqualValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GreaterThanOrEqualValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 >= $value2;
27+
return null === $value2 || $value1 >= $value2;
2828
}
2929

3030
/**

Constraints/GreaterThanValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GreaterThanValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 > $value2;
27+
return null === $value2 || $value1 > $value2;
2828
}
2929

3030
/**

Constraints/LessThanOrEqualValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class LessThanOrEqualValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 <= $value2;
27+
return null === $value2 || $value1 <= $value2;
2828
}
2929

3030
/**

Constraints/LessThanValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class LessThanValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 < $value2;
27+
return null === $value2 || $value1 < $value2;
2828
}
2929

3030
/**

Tests/ConstraintValidatorTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function testFormatValue($expected, $value, $format = 0)
2727

2828
public function formatValueProvider()
2929
{
30+
$defaultTimezone = date_default_timezone_get();
31+
date_default_timezone_set('Europe/Moscow'); // GMT+3
32+
3033
$data = [
3134
['true', true],
3235
['false', false],
@@ -36,10 +39,15 @@ public function formatValueProvider()
3639
['array', []],
3740
['object', $toString = new TestToStringObject()],
3841
['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],
42+
['object', $dateTime = new \DateTimeImmutable('1971-02-02T08:00:00UTC')],
43+
[class_exists(\IntlDateFormatter::class) ? 'Oct 4, 2019, 11:02 AM' : '2019-10-04 11:02:03', new \DateTimeImmutable('2019-10-04T11:02:03+09:00'), ConstraintValidator::PRETTY_DATE],
44+
[class_exists(\IntlDateFormatter::class) ? 'Feb 2, 1971, 8:00 AM' : '1971-02-02 08:00:00', $dateTime, ConstraintValidator::PRETTY_DATE],
45+
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 6:00 AM' : '1970-01-01 06:00:00', new \DateTimeImmutable('1970-01-01T06:00:00Z'), ConstraintValidator::PRETTY_DATE],
46+
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 3:00 PM' : '1970-01-01 15:00:00', (new \DateTimeImmutable('1970-01-01T23:00:00'))->setTimezone(new \DateTimeZone('America/New_York')), ConstraintValidator::PRETTY_DATE],
4147
];
4248

49+
date_default_timezone_set($defaultTimezone);
50+
4351
return $data;
4452
}
4553
}

Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,32 @@ public function throwsOnInvalidStringDatesProvider(): array
247247
];
248248
}
249249

250+
/**
251+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
252+
*/
253+
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
254+
{
255+
$constraint = $this->createConstraint(['propertyPath' => 'value']);
256+
$constraint->message = 'Constraint Message';
257+
258+
$object = new ComparisonTest_Class(null);
259+
$this->setObject($object);
260+
261+
$this->validator->validate($dirtyValue, $constraint);
262+
263+
if ($isValid) {
264+
$this->assertNoViolation();
265+
} else {
266+
$this->buildViolation('Constraint Message')
267+
->setParameter('{{ value }}', $dirtyValueAsString)
268+
->setParameter('{{ compared_value }}', 'null')
269+
->setParameter('{{ compared_value_type }}', 'NULL')
270+
->setParameter('{{ compared_value_path }}', 'value')
271+
->setCode($this->getErrorCode())
272+
->assertRaised();
273+
}
274+
}
275+
250276
public function provideAllInvalidComparisons(): array
251277
{
252278
// The provider runs before setUp(), so we need to manually fix
@@ -262,6 +288,8 @@ public function provideAllInvalidComparisons(): array
262288

263289
abstract public function provideInvalidComparisons(): array;
264290

291+
abstract public function provideComparisonsToNullValueAtPropertyPath();
292+
265293
/**
266294
* @param array|null $options Options for the constraint
267295
*/

Tests/Constraints/DivisibleByValidatorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,9 @@ public function throwsOnNonNumericValuesProvider()
9898
[\ArrayIterator::class, new \ArrayIterator(), 12],
9999
];
100100
}
101+
102+
public function provideComparisonsToNullValueAtPropertyPath()
103+
{
104+
$this->markTestSkipped('DivisibleByValidator rejects null values.');
105+
}
101106
}

Tests/Constraints/EqualToValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,11 @@ public function provideInvalidComparisons(): array
7676
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
7777
];
7878
}
79+
80+
public function provideComparisonsToNullValueAtPropertyPath()
81+
{
82+
return [
83+
[5, '5', false],
84+
];
85+
}
7986
}

Tests/Constraints/GreaterThanOrEqualValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,11 @@ public function provideInvalidComparisons(): array
7979
['b', '"b"', 'c', '"c"', 'string'],
8080
];
8181
}
82+
83+
public function provideComparisonsToNullValueAtPropertyPath()
84+
{
85+
return [
86+
[5, '5', true],
87+
];
88+
}
8289
}

0 commit comments

Comments
 (0)