Skip to content

Commit d4b3baa

Browse files
committed
fix comparisons with null values at property paths
1 parent bd498f2 commit d4b3baa

13 files changed

+87
-4
lines changed

src/Symfony/Component/Validator/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
/**

src/Symfony/Component/Validator/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
/**

src/Symfony/Component/Validator/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
/**

src/Symfony/Component/Validator/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
/**

src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,31 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
217217
->assertRaised();
218218
}
219219

220+
/**
221+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
222+
*/
223+
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
224+
{
225+
$constraint = $this->createConstraint(['propertyPath' => 'value']);
226+
$constraint->message = 'Constraint Message';
227+
228+
$object = new ComparisonTest_Class(null);
229+
$this->setObject($object);
230+
231+
$this->validator->validate($dirtyValue, $constraint);
232+
233+
if ($isValid) {
234+
$this->assertNoViolation();
235+
} else {
236+
$this->buildViolation('Constraint Message')
237+
->setParameter('{{ value }}', $dirtyValueAsString)
238+
->setParameter('{{ compared_value }}', 'null')
239+
->setParameter('{{ compared_value_type }}', 'NULL')
240+
->setCode($this->getErrorCode())
241+
->assertRaised();
242+
}
243+
}
244+
220245
/**
221246
* @return array
222247
*/
@@ -238,6 +263,8 @@ public function provideAllInvalidComparisons()
238263
*/
239264
abstract public function provideInvalidComparisons();
240265

266+
abstract public function provideComparisonsToNullValueAtPropertyPath();
267+
241268
/**
242269
* @param array|null $options Options for the constraint
243270
*

src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php

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

src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php

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

src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@ public function provideInvalidComparisons()
8080
['22', '"22"', '22', '"22"', 'string'],
8181
];
8282
}
83+
84+
public function provideComparisonsToNullValueAtPropertyPath()
85+
{
86+
return [
87+
[5, '5', false],
88+
];
89+
}
8390
}

src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,11 @@ public function provideInvalidComparisons()
9393
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
9494
];
9595
}
96+
97+
public function provideComparisonsToNullValueAtPropertyPath()
98+
{
99+
return [
100+
[5, '5', false],
101+
];
102+
}
96103
}

src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,11 @@ public function provideInvalidComparisons()
8181
['c', '"c"', 'b', '"b"', 'string'],
8282
];
8383
}
84+
85+
public function provideComparisonsToNullValueAtPropertyPath()
86+
{
87+
return [
88+
[5, '5', false],
89+
];
90+
}
8491
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy