fix comparisons with null values at property paths
This commit is contained in:
parent
fb2a7a3d35
commit
ffa5db74c1
@ -24,7 +24,7 @@ class GreaterThanOrEqualValidator extends AbstractComparisonValidator
|
||||
*/
|
||||
protected function compareValues($value1, $value2)
|
||||
{
|
||||
return $value1 >= $value2;
|
||||
return null === $value2 || $value1 >= $value2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +24,7 @@ class GreaterThanValidator extends AbstractComparisonValidator
|
||||
*/
|
||||
protected function compareValues($value1, $value2)
|
||||
{
|
||||
return $value1 > $value2;
|
||||
return null === $value2 || $value1 > $value2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +24,7 @@ class LessThanOrEqualValidator extends AbstractComparisonValidator
|
||||
*/
|
||||
protected function compareValues($value1, $value2)
|
||||
{
|
||||
return $value1 <= $value2;
|
||||
return null === $value2 || $value1 <= $value2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +24,7 @@ class LessThanValidator extends AbstractComparisonValidator
|
||||
*/
|
||||
protected function compareValues($value1, $value2)
|
||||
{
|
||||
return $value1 < $value2;
|
||||
return null === $value2 || $value1 < $value2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -237,6 +237,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
|
||||
*/
|
||||
@ -258,6 +283,8 @@ abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTe
|
||||
*/
|
||||
abstract public function provideInvalidComparisons();
|
||||
|
||||
abstract public function provideComparisonsToNullValueAtPropertyPath();
|
||||
|
||||
/**
|
||||
* @param array|null $options Options for the constraint
|
||||
*
|
||||
|
@ -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],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -78,4 +78,11 @@ class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCas
|
||||
['b', '"b"', 'c', '"c"', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
public function provideComparisonsToNullValueAtPropertyPath()
|
||||
{
|
||||
return [
|
||||
[5, '5', true],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -80,4 +80,11 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
|
||||
['22', '"22"', '22', '"22"', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
public function provideComparisonsToNullValueAtPropertyPath()
|
||||
{
|
||||
return [
|
||||
[5, '5', true],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -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],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -81,4 +81,11 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
|
||||
['c', '"c"', 'b', '"b"', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
public function provideComparisonsToNullValueAtPropertyPath()
|
||||
{
|
||||
return [
|
||||
[5, '5', true],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -79,4 +79,11 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase
|
||||
['333', '"333"', '22', '"22"', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
public function provideComparisonsToNullValueAtPropertyPath()
|
||||
{
|
||||
return [
|
||||
[5, '5', true],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -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],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -93,4 +93,11 @@ class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
|
||||
|
||||
return $comparisons;
|
||||
}
|
||||
|
||||
public function provideComparisonsToNullValueAtPropertyPath()
|
||||
{
|
||||
return [
|
||||
[5, '5', true],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user