Skip to content

[Validator] fix comparisons with null values at property paths #29839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GreaterThanOrEqualValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 >= $value2;
return null === $value2 || $value1 >= $value2;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GreaterThanValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 > $value2;
return null === $value2 || $value1 > $value2;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LessThanOrEqualValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 <= $value2;
return null === $value2 || $value1 <= $value2;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LessThanValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return $value1 < $value2;
return null === $value2 || $value1 < $value2;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,31 @@ public function throwsOnInvalidStringDatesProvider()
];
}

/**
* @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
*/
Expand All @@ -258,6 +283,8 @@ public function provideAllInvalidComparisons()
*/
abstract public function provideInvalidComparisons();

abstract public function provideComparisonsToNullValueAtPropertyPath();

/**
* @param array|null $options Options for the constraint
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,11 @@ public function provideInvalidComparisons()
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
];
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', false],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,11 @@ public function provideInvalidComparisons()
['b', '"b"', 'c', '"c"', 'string'],
];
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ public function provideInvalidComparisons()
['22', '"22"', '22', '"22"', 'string'],
];
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,11 @@ public function provideInvalidComparisons()
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
];
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', false],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ public function provideInvalidComparisons()
['c', '"c"', 'b', '"b"', 'string'],
];
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ public function provideInvalidComparisons()
['333', '"333"', '22', '"22"', 'string'],
];
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,11 @@ public function provideInvalidComparisons()
[new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
];
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,11 @@ public function provideInvalidComparisons()

return $comparisons;
}

public function provideComparisonsToNullValueAtPropertyPath()
{
return [
[5, '5', true],
];
}
}