bug #29839 [Validator] fix comparisons with null values at property paths (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] fix comparisons with null values at property paths

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29831
| License       | MIT
| Doc PR        |

Commits
-------

ffa5db74c1 fix comparisons with null values at property paths
This commit is contained in:
Christian Flothmann 2019-12-16 09:55:48 +01:00
commit a073606ea8
13 changed files with 87 additions and 4 deletions

View File

@ -24,7 +24,7 @@ class GreaterThanOrEqualValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 >= $value2;
return null === $value2 || $value1 >= $value2;
}
/**

View File

@ -24,7 +24,7 @@ class GreaterThanValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 > $value2;
return null === $value2 || $value1 > $value2;
}
/**

View File

@ -24,7 +24,7 @@ class LessThanOrEqualValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 <= $value2;
return null === $value2 || $value1 <= $value2;
}
/**

View File

@ -24,7 +24,7 @@ class LessThanValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 < $value2;
return null === $value2 || $value1 < $value2;
}
/**

View File

@ -234,6 +234,31 @@ abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTe
];
}
/**
* @dataProvider provideComparisonsToNullValueAtPropertyPath
*/
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
{
$constraint = $this->createConstraint(['propertyPath' => 'value']);
$constraint->message = 'Constraint Message';
$object = new ComparisonTest_Class(null);
$this->setObject($object);
$this->validator->validate($dirtyValue, $constraint);
if ($isValid) {
$this->assertNoViolation();
} else {
$this->buildViolation('Constraint Message')
->setParameter('{{ value }}', $dirtyValueAsString)
->setParameter('{{ compared_value }}', 'null')
->setParameter('{{ compared_value_type }}', 'NULL')
->setCode($this->getErrorCode())
->assertRaised();
}
}
/**
* @return array
*/
@ -255,6 +280,8 @@ abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTe
*/
abstract public function provideInvalidComparisons();
abstract public function provideComparisonsToNullValueAtPropertyPath();
/**
* @param array|null $options Options for the constraint
*

View File

@ -75,4 +75,11 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
];
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', false],
];
}
}

View File

@ -78,4 +78,11 @@ class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCas
['b', '"b"', 'c', '"c"', 'string'],
];
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}

View File

@ -80,4 +80,11 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
['22', '"22"', '22', '"22"', 'string'],
];
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}

View File

@ -93,4 +93,11 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
];
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', false],
];
}
}

View File

@ -81,4 +81,11 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
['c', '"c"', 'b', '"b"', 'string'],
];
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}

View File

@ -79,4 +79,11 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase
['333', '"333"', '22', '"22"', 'string'],
];
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}

View File

@ -75,4 +75,11 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase
[new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
];
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}

View File

@ -93,4 +93,11 @@ class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
return $comparisons;
}
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}