8000 fix comparisons with null values at property paths · symfony/symfony@e559914 · GitHub
[go: up one dir, main page]

Skip to content

Commit e559914

Browse files
committed
fix comparisons with null values at property paths
1 parent fb2a7a3 commit e559914

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
@@ -237,6 +237,31 @@ public function throwsOnInvalidStringDatesProvider()
237237
];
238238
}
239239

240+
/**
241+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
242+
*/
243+
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
244+
{
245+
$constraint = $this->createConstraint(['propertyPath' => 'value']);
246+
$constraint->message = 'Constraint Message';
247+
248+
$object = new ComparisonTest_Class(null);
249+
$this->setObject($object);
250+
251+
$this->validator->validate($dirtyValue, $constraint);
252+
253+
if ($isValid) {
254+
$this->assertNoViolation();
255+
} else {
256+
$this->buildViolation('Constraint Message')
257+
->setParameter('{{ value }}', $dirtyValueAsString)
258+
->setParameter('{{ compared_value }}', 'null')
259+
A93C ->setParameter('{{ compared_value_type }}', 'NULL')
260+
->setCode($this->getErrorCode())
261+
->assertRaised();
262+
}
263+
}
264+
240265
/**
241266
* @return array
242267
*/
@@ -258,6 +283,8 @@ public function provideAllInvalidComparisons()
258283
*/
259284
abstract public function provideInvalidComparisons();
260285

286+
abstract public function provideComparisonsToNullValueAtPropertyPath();
287+
261288
/**
262289
* @param array|null $options Options for the constraint
263290
*

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
}

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

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

src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.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(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
7676
];
7777
}
78+
79+
public function provideComparisonsToNullValueAtPropertyPath()
80+
{
81+
return [
82+
[5, '5', true],
83+
];
84+
}
7885
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,11 @@ public function provideInvalidComparisons()
9393

9494
return $comparisons;
9595
}
96+
97+
public function provideComparisonsToNullValueAtPropertyPath()
98+
{
99+
return [
100+
[5, '5', true],
101+
];
10 3C6A 2+
}
96103
}

0 commit comments

Comments
 (0)
0