Skip to content

Commit

Permalink
[Validator] make DateTime objects represented as strings in the viola…
Browse files Browse the repository at this point in the history
…tion message.
  • Loading branch information
Hugo Hamon authored and fabpot committed Nov 28, 2014
1 parent 79c7849 commit b753218
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
Expand Up @@ -35,7 +35,7 @@ public function validate($value, Constraint $constraint)

if (!is_numeric($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$this->buildViolation($constraint->invalidMessage)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setCode(Range::INVALID_VALUE_ERROR)
->addViolation();

Expand All @@ -61,7 +61,7 @@ public function validate($value, Constraint $constraint)

if (null !== $constraint->max && $value > $max) {
$this->buildViolation($constraint->maxMessage)
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
->setCode(Range::BEYOND_RANGE_ERROR)
->addViolation();
Expand All @@ -71,7 +71,7 @@ public function validate($value, Constraint $constraint)

if (null !== $constraint->min && $value < $min) {
$this->buildViolation($constraint->minMessage)
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
->setCode(Range::BELOW_RANGE_ERROR)
->addViolation();
Expand Down
Expand Up @@ -52,20 +52,20 @@ public function getTenToTwenty()
public function getLessThanTen()
{
return array(
array(9.99999),
array('9.99999'),
array(5),
array(1.0),
array(9.99999, '9.99999'),
array('9.99999', '"9.99999"'),
array(5, '5'),
array(1.0, '1.0'),
);
}

public function getMoreThanTwenty()
{
return array(
array(20.000001),
array('20.000001'),
array(21),
array(30.0),
array(20.000001, '20.000001'),
array('20.000001', '"20.000001"'),
array(21, '21'),
array(30.0, '30.0'),
);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public function testValidValuesMinMax($value)
/**
* @dataProvider getLessThanTen
*/
public function testInvalidValuesMin($value)
public function testInvalidValuesMin($value, $formattedValue)
{
$constraint = new Range(array(
'min' => 10,
Expand All @@ -115,7 +115,7 @@ public function testInvalidValuesMin($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 10)
->setCode(Range::BELOW_RANGE_ERROR)
->assertRaised();
Expand All @@ -124,7 +124,7 @@ public function testInvalidValuesMin($value)
/**
* @dataProvider getMoreThanTwenty
*/
public function testInvalidValuesMax($value)
public function testInvalidValuesMax($value, $formattedValue)
{
$constraint = new Range(array(
'max' => 20,
Expand All @@ -134,7 +134,7 @@ public function testInvalidValuesMax($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 20)
->setCode(Range::BEYOND_RANGE_ERROR)
->assertRaised();
Expand All @@ -143,7 +143,7 @@ public function testInvalidValuesMax($value)
/**
* @dataProvider getMoreThanTwenty
*/
public function testInvalidValuesCombinedMax($value)
public function testInvalidValuesCombinedMax($value, $formattedValue)
{
$constraint = new Range(array(
'min' => 10,
Expand All @@ -155,7 +155,7 @@ public function testInvalidValuesCombinedMax($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMaxMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 20)
->setCode(Range::BEYOND_RANGE_ERROR)
->assertRaised();
Expand All @@ -164,7 +164,7 @@ public function testInvalidValuesCombinedMax($value)
/**
* @dataProvider getLessThanTen
*/
public function testInvalidValuesCombinedMin($value)
public function testInvalidValuesCombinedMin($value, $formattedValue)
{
$constraint = new Range(array(
'min' => 10,
Expand All @@ -176,7 +176,7 @@ public function testInvalidValuesCombinedMin($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMinMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 10)
->setCode(Range::BELOW_RANGE_ERROR)
->assertRaised();
Expand Down Expand Up @@ -212,13 +212,13 @@ public function getSoonerThanTenthMarch2014()
$this->setDefaultTimezone('UTC');

$tests = array(
array(new \DateTime('March 20, 2013')),
array(new \DateTime('March 9, 2014')),
array(new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'),
array(new \DateTime('March 9, 2014'), 'Mar 9, 2014, 12:00 AM'),
);

if (version_compare(PHP_VERSION, '5.5.0-dev', '>=')) {
$tests[] = array(new \DateTimeImmutable('March 20, 2013'));
$tests[] = array(new \DateTimeImmutable('March 9, 2014'));
$tests[] = array(new \DateTimeImmutable('March 20, 2013'), 'Mar 20, 2013, 12:00 AM');
$tests[] = array(new \DateTimeImmutable('March 9, 2014'), 'Mar 9, 2014, 12:00 AM');
}

$this->restoreDefaultTimezone();
Expand All @@ -233,13 +233,13 @@ public function getLaterThanTwentiethMarch2014()
$this->setDefaultTimezone('UTC');

$tests = array(
array(new \DateTime('March 21, 2014')),
array(new \DateTime('March 9, 2015')),
array(new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'),
array(new \DateTime('March 9, 2015'), 'Mar 9, 2015, 12:00 AM'),
);

if (version_compare(PHP_VERSION, '5.5.0-dev', '>=')) {
$tests[] = array(new \DateTimeImmutable('March 21, 2014'));
$tests[] = array(new \DateTimeImmutable('March 9, 2015'));
$tests[] = array(new \DateTimeImmutable('March 21, 2014'), 'Mar 21, 2014, 12:00 AM');
$tests[] = array(new \DateTimeImmutable('March 9, 2015'), 'Mar 9, 2015, 12:00 AM');
}

$this->restoreDefaultTimezone();
Expand Down Expand Up @@ -283,7 +283,7 @@ public function testValidDatesMinMax($value)
/**
* @dataProvider getSoonerThanTenthMarch2014
*/
public function testInvalidDatesMin($value)
public function testInvalidDatesMin($value, $dateTimeAsString)
{
// Conversion of dates to string differs between ICU versions
// Make sure we have the correct version loaded
Expand All @@ -297,7 +297,7 @@ public function testInvalidDatesMin($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
->setCode(Range::BELOW_RANGE_ERROR)
->assertRaised();
Expand All @@ -306,7 +306,7 @@ public function testInvalidDatesMin($value)
/**
* @dataProvider getLaterThanTwentiethMarch2014
*/
public function testInvalidDatesMax($value)
public function testInvalidDatesMax($value, $dateTimeAsString)
{
// Conversion of dates to string differs between ICU versions
// Make sure we have the correct version loaded
Expand All @@ -320,7 +320,7 @@ public function testInvalidDatesMax($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
->setCode(Range::BEYOND_RANGE_ERROR)
->assertRaised();
Expand All @@ -329,7 +329,7 @@ public function testInvalidDatesMax($value)
/**
* @dataProvider getLaterThanTwentiethMarch2014
*/
public function testInvalidDatesCombinedMax($value)
public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
{
// Conversion of dates to string differs between ICU versions
// Make sure we have the correct version loaded
Expand All @@ -345,7 +345,7 @@ public function testInvalidDatesCombinedMax($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMaxMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
->setCode(Range::BEYOND_RANGE_ERROR)
->assertRaised();
Expand All @@ -354,7 +354,7 @@ public function testInvalidDatesCombinedMax($value)
/**
* @dataProvider getSoonerThanTenthMarch2014
*/
public function testInvalidDatesCombinedMin($value)
public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
{
// Conversion of dates to string differs between ICU versions
// Make sure we have the correct version loaded
Expand All @@ -370,7 +370,7 @@ public function testInvalidDatesCombinedMin($value)
$this->validator->validate($value, $constraint);

$this->buildViolation('myMinMessage')
->setParameter('{{ value }}', $value)
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
->setCode(Range::BELOW_RANGE_ERROR)
->assertRaised();
Expand Down

0 comments on commit b753218

Please sign in to comment.