Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator][4.3] Sync string to date behavior and throw a better exception #33451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public function validate($value, Constraint $constraint)
// This allows to compare with any date/time value supported by
// the DateTime constructor:
// https://php.net/datetime.formats
if (\is_string($comparedValue)) {
if ($value instanceof \DateTimeImmutable) {
// If $value is immutable, convert the compared value to a
// DateTimeImmutable too
$comparedValue = new \DateTimeImmutable($comparedValue);
} elseif ($value instanceof \DateTimeInterface) {
// Otherwise use DateTime
$comparedValue = new \DateTime($comparedValue);
if (\is_string($comparedValue) && $value instanceof \DateTimeInterface) {
// If $value is immutable, convert the compared value to a DateTimeImmutable too, otherwise use DateTime
$dateTimeClass = $value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class;

try {
$comparedValue = new $dateTimeClass($comparedValue);
} catch (\Exception $e) {
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)));
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/Symfony/Component/Validator/Constraints/RangeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
Expand Down Expand Up @@ -50,12 +51,26 @@ public function validate($value, Constraint $constraint)
// the DateTime constructor:
// https://php.net/datetime.formats
if ($value instanceof \DateTimeInterface) {
$dateTimeClass = null;

if (\is_string($min)) {
$min = new \DateTime($min);
$dateTimeClass = $value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class;

try {
$min = new $dateTimeClass($min);
} catch (\Exception $e) {
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)));
}
}

if (\is_string($max)) {
$max = new \DateTime($max);
$dateTimeClass = $dateTimeClass ?: ($value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class);

try {
$max = new $dateTimeClass($max);
} catch (\Exception $e) {
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)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Intl\Util\IntlTestHelper;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\AbstractComparison;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

Expand Down Expand Up @@ -211,6 +212,31 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
->assertRaised();
}

/**
* @dataProvider throwsOnInvalidStringDatesProvider
*/
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
{
$this->expectException(ConstraintDefinitionException::class);
$this->expectExceptionMessage($expectedMessage);

$this->validator->validate($value, $constraint);
}

public function throwsOnInvalidStringDatesProvider()
{
$constraint = $this->createConstraint([
'value' => 'foo',
]);

$constraintClass = \get_class($constraint);

return [
[$constraint, sprintf('The compared value "foo" could not be converted to a "DateTimeImmutable" instance in the "%s" constraint.', $constraintClass), new \DateTimeImmutable()],
[$constraint, sprintf('The compared value "foo" could not be converted to a "DateTime" instance in the "%s" constraint.', $constraintClass), new \DateTime()],
];
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\AbstractComparison;
use Symfony\Component\Validator\Constraints\PositiveOrZero;

/**
Expand Down Expand Up @@ -98,10 +99,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
* @dataProvider throwsOnInvalidStringDatesProvider
*/
public function testValidComparisonToPropertyPathOnArray($comparedValue)
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't change the method. I removed an useless skipped test (from a method that doesn't exist anymore) and added the new one.

{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\AbstractComparison;
use Symfony\Component\Validator\Constraints\Positive;

/**
Expand Down Expand Up @@ -101,10 +102,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
* @dataProvider throwsOnInvalidStringDatesProvider
*/
public function testValidComparisonToPropertyPathOnArray($comparedValue)
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\AbstractComparison;
use Symfony\Component\Validator\Constraints\NegativeOrZero;

/**
Expand Down Expand Up @@ -101,10 +102,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
* @dataProvider throwsOnInvalidStringDatesProvider
*/
public function testValidComparisonToPropertyPathOnArray($comparedValue)
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
{
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\AbstractComparison;
use Symfony\Component\Validator\Constraints\Negative;

/**
Expand Down Expand Up @@ -101,10 +102,10 @@ public function testValidComparisonToPropertyPath($comparedValue)
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
* @dataProvider throwsOnInvalidStringDatesProvider
*/
public function testValidComparisonToPropertyPathOnArray($comparedValue)
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Intl\Util\IntlTestHelper;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\RangeValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class RangeValidatorTest extends ConstraintValidatorTestCase
Expand Down Expand Up @@ -389,4 +390,29 @@ public function testNonNumeric()
->setCode(Range::INVALID_CHARACTERS_ERROR)
->assertRaised();
}

/**
* @dataProvider throwsOnInvalidStringDatesProvider
*/
public function testThrowsOnInvalidStringDates($expectedMessage, $value, $min, $max)
{
$this->expectException(ConstraintDefinitionException::class);
$this->expectExceptionMessage($expectedMessage);

$this->validator->validate($value, new Range([
'min' => $min,
'max' => $max,
]));
}

public function throwsOnInvalidStringDatesProvider()
{
return [
['The min value "foo" could not be converted to a "DateTimeImmutable" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTimeImmutable(), 'foo', null],
['The min value "foo" could not be converted to a "DateTime" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTime(), 'foo', null],
['The max value "foo" could not be converted to a "DateTimeImmutable" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTimeImmutable(), null, 'foo'],
['The max value "foo" could not be converted to a "DateTime" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTime(), null, 'foo'],
['The min value "bar" could not be converted to a "DateTimeImmutable" instance in the "Symfony\Component\Validator\Constraints\Range" constraint.', new \DateTimeImmutable(), 'bar', 'ccc'],
];
}
}