Skip to content

Commit b29bdfd

Browse files
Hugo Hamonfabpot
authored andcommitted
[Validator] make DateTime objects represented as strings in the violation message.
1 parent c819f5f commit b29bdfd

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

Constraints/RangeValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function validate($value, Constraint $constraint)
3535

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

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

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

7272
if (null !== $constraint->min && $value < $min) {
7373
$this->buildViolation($constraint->minMessage)
74-
->setParameter('{{ value }}', $value)
74+
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
7575
->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
7676
->setCode(Range::BELOW_RANGE_ERROR)
7777
->addViolation();

Tests/Constraints/RangeValidatorTest.php

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ public function getTenToTwenty()
5252
public function getLessThanTen()
5353
{
5454
return array(
55-
array(9.99999),
56-
array('9.99999'),
57-
array(5),
58-
array(1.0),
55+
array(9.99999, '9.99999'),
56+
array('9.99999', '"9.99999"'),
57+
array(5, '5'),
58+
array(1.0, '1.0'),
5959
);
6060
}
6161

6262
public function getMoreThanTwenty()
6363
{
6464
return array(
65-
array(20.000001),
66-
array('20.000001'),
67-
array(21),
68-
array(30.0),
65+
array(20.000001, '20.000001'),
66+
array('20.000001', '"20.000001"'),
67+
array(21, '21'),
68+
array(30.0, '30.0'),
6969
);
7070
}
7171

@@ -105,7 +105,7 @@ public function testValidValuesMinMax($value)
105105
/**
106106
* @dataProvider getLessThanTen
107107
*/
108-
public function testInvalidValuesMin($value)
108+
public function testInvalidValuesMin($value, $formattedValue)
109109
{
110110
$constraint = new Range(array(
111111
'min' => 10,
@@ -115,7 +115,7 @@ public function testInvalidValuesMin($value)
115115
$this->validator->validate($value, $constraint);
116116

117117
$this->buildViolation('myMessage')
118-
->setParameter('{{ value }}', $value)
118+
->setParameter('{{ value }}', $formattedValue)
119119
->setParameter('{{ limit }}', 10)
120120
->setCode(Range::BELOW_RANGE_ERROR)
121121
->assertRaised();
@@ -124,7 +124,7 @@ public function testInvalidValuesMin($value)
124124
/**
125125
* @dataProvider getMoreThanTwenty
126126
*/
127-
public function testInvalidValuesMax($value)
127+
public function testInvalidValuesMax($value, $formattedValue)
128128
{
129129
$constraint = new Range(array(
130130
'max' => 20,
@@ -134,7 +134,7 @@ public function testInvalidValuesMax($value)
134134
$this->validator->validate($value, $constraint);
135135

136136
$this->buildViolation('myMessage')
137-
->setParameter('{{ value }}', $value)
137+
->setParameter('{{ value }}', $formattedValue)
138138
->setParameter('{{ limit }}', 20)
139139
->setCode(Range::BEYOND_RANGE_ERROR)
140140
->assertRaised();
@@ -143,7 +143,7 @@ public function testInvalidValuesMax($value)
143143
/**
144144
* @dataProvider getMoreThanTwenty
145145
*/
146-
public function testInvalidValuesCombinedMax($value)
146+
public function testInvalidValuesCombinedMax($value, $formattedValue)
147147
{
148148
$constraint = new Range(array(
149149
'min' => 10,
@@ -155,7 +155,7 @@ public function testInvalidValuesCombinedMax($value)
155155
$this->validator->validate($value, $constraint);
156156

157157
$this->buildViolation('myMaxMessage')
158-
->setParameter('{{ value }}', $value)
158+
->setParameter('{{ value }}', $formattedValue)
159159
->setParameter('{{ limit }}', 20)
160160
->setCode(Range::BEYOND_RANGE_ERROR)
161161
->assertRaised();
@@ -164,7 +164,7 @@ public function testInvalidValuesCombinedMax($value)
164164
/**
165165
* @dataProvider getLessThanTen
166166
*/
167-
public function testInvalidValuesCombinedMin($value)
167+
public function testInvalidValuesCombinedMin($value, $formattedValue)
168168
{
169169
$constraint = new Range(array(
170170
'min' => 10,
@@ -176,7 +176,7 @@ public function testInvalidValuesCombinedMin($value)
176176
$this->validator->validate($value, $constraint);
177177

178178
$this->buildViolation('myMinMessage')
179-
->setParameter('{{ value }}', $value)
179+
->setParameter('{{ value }}', $formattedValue)
180180
->setParameter('{{ limit }}', 10)
181181
->setCode(Range::BELOW_RANGE_ERROR)
182182
->assertRaised();
@@ -212,13 +212,13 @@ public function getSoonerThanTenthMarch2014()
212212
$this->setDefaultTimezone('UTC');
213213

214214
$tests = array(
215-
array(new \DateTime('March 20, 2013')),
216-
array(new \DateTime('March 9, 2014')),
215+
array(new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'),
216+
array(new \DateTime('March 9, 2014'), 'Mar 9, 2014, 12:00 AM'),
217217
);
218218

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

224224
$this->restoreDefaultTimezone();
@@ -233,13 +233,13 @@ public function getLaterThanTwentiethMarch2014()
233233
$this->setDefaultTimezone('UTC');
234234

235235
$tests = array(
236-
array(new \DateTime('March 21, 2014')),
237-
array(new \DateTime('March 9, 2015')),
236+
array(new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'),
237+
array(new \DateTime('March 9, 2015'), 'Mar 9, 2015, 12:00 AM'),
238238
);
239239

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

245245
$this->restoreDefaultTimezone();
@@ -283,7 +283,7 @@ public function testValidDatesMinMax($value)
283283
/**
284284
* @dataProvider getSoonerThanTenthMarch2014
285285
*/
286-
public function testInvalidDatesMin($value)
286+
public function testInvalidDatesMin($value, $dateTimeAsString)
287287
{
288288
// Conversion of dates to string differs between ICU versions
289289
// Make sure we have the correct version loaded
@@ -297,7 +297,7 @@ public function testInvalidDatesMin($value)
297297
$this->validator->validate($value, $constraint);
298298

299299
$this->buildViolation('myMessage')
300-
->setParameter('{{ value }}', $value)
300+
->setParameter('{{ value }}', $dateTimeAsString)
301301
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
302302
->setCode(Range::BELOW_RANGE_ERROR)
303303
->assertRaised();
@@ -306,7 +306,7 @@ public function testInvalidDatesMin($value)
306306
/**
307307
* @dataProvider getLaterThanTwentiethMarch2014
308308
*/
309-
public function testInvalidDatesMax($value)
309+
public function testInvalidDatesMax($value, $dateTimeAsString)
310310
{
311311
// Conversion of dates to string differs between ICU versions
312312
// Make sure we have the correct version loaded
@@ -320,7 +320,7 @@ public function testInvalidDatesMax($value)
320320
$this->validator->validate($value, $constraint);
321321

322322
$this->buildViolation('myMessage')
323-
->setParameter('{{ value }}', $value)
323+
->setParameter('{{ value }}', $dateTimeAsString)
324324
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
325325
->setCode(Range::BEYOND_RANGE_ERROR)
326326
->assertRaised();
@@ -329,7 +329,7 @@ public function testInvalidDatesMax($value)
329329
/**
330330
* @dataProvider getLaterThanTwentiethMarch2014
331331
*/
332-
public function testInvalidDatesCombinedMax($value)
332+
public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
333333
{
334334
// Conversion of dates to string differs between ICU versions
335335
// Make sure we have the correct version loaded
@@ -345,7 +345,7 @@ public function testInvalidDatesCombinedMax($value)
345345
$this->validator->validate($value, $constraint);
346346

347347
$this->buildViolation('myMaxMessage')
348-
->setParameter('{{ value }}', $value)
348+
->setParameter('{{ value }}', $dateTimeAsString)
349349
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
350350
->setCode(Range::BEYOND_RANGE_ERROR)
351351
->assertRaised();
@@ -354,7 +354,7 @@ public function testInvalidDatesCombinedMax($value)
354354
/**
355355
* @dataProvider getSoonerThanTenthMarch2014
356356
*/
357-
public function testInvalidDatesCombinedMin($value)
357+
public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
358358
{
359359
// Conversion of dates to string differs between ICU versions
360360
// Make sure we have the correct version loaded
@@ -370,7 +370,7 @@ public function testInvalidDatesCombinedMin($value)
370370
$this->validator->validate($value, $constraint);
371371

372372
$this->buildViolation('myMinMessage')
373-
->setParameter('{{ value }}', $value)
373+
->setParameter('{{ value }}', $dateTimeAsString)
374374
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
375375
->setCode(Range::BELOW_RANGE_ERROR)
376376
->assertRaised();

0 commit comments

Comments
 (0)