fix comparisons with null values at property paths

This commit is contained in:
Christian Flothmann 2019-01-10 21:23:54 +01:00
parent fb2a7a3d35
commit ffa5db74c1
13 changed files with 87 additions and 4 deletions

View File

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

View File

@ -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 * @return array
*/ */
@ -258,6 +283,8 @@ abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTe
*/ */
abstract public function provideInvalidComparisons(); abstract public function provideInvalidComparisons();
abstract public function provideComparisonsToNullValueAtPropertyPath();
/** /**
* @param array|null $options Options for the constraint * @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'], [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'], ['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'], ['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'], [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'], ['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'], ['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'], [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; return $comparisons;
} }
public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
} }