Skip to content

Commit

Permalink
[Validator] Added extensive test coverage for the constraint validato…
Browse files Browse the repository at this point in the history
…rs for the different APIs
  • Loading branch information
webmozart committed Jul 28, 2014
1 parent 8e461af commit 7504448
Show file tree
Hide file tree
Showing 151 changed files with 4,042 additions and 1,643 deletions.
4 changes: 1 addition & 3 deletions src/Symfony/Component/Validator/Constraints/AllValidator.php
Expand Up @@ -44,9 +44,7 @@ public function validate($value, Constraint $constraint)
$validator = $context->getValidator()->inContext($context);

foreach ($value as $key => $element) {
foreach ($constraint->constraints as $constr) {
$validator->atPath('['.$key.']')->validate($element, $constr, $group);
}
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints, $group);
}
}
}
Expand Up @@ -53,18 +53,20 @@ public function validate($value, Constraint $constraint)
$validator = $context->getValidator()->inContext($context);

foreach ($constraint->fields as $field => $fieldConstraint) {
if (
// bug fix issue #2779
(is_array($value) && array_key_exists($field, $value)) ||
($value instanceof \ArrayAccess && $value->offsetExists($field))
) {
foreach ($fieldConstraint->constraints as $constr) {
$validator->atPath('['.$field.']')->validate($value[$field], $constr, $group);
// bug fix issue #2779
$existsInArray = is_array($value) && array_key_exists($field, $value);
$existsInArrayAccess = $value instanceof \ArrayAccess && $value->offsetExists($field);

if ($existsInArray || $existsInArrayAccess) {
if (count($fieldConstraint->constraints) > 0) {
$validator->atPath('['.$field.']')
->validate($value[$field], $fieldConstraint->constraints, $group);
}
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$context->buildViolation($constraint->missingFieldsMessage)
->atPath('['.$field.']')
->setParameter('{{ field }}', $field)
->setInvalidValue(null)
->addViolation();
}
}
Expand All @@ -75,6 +77,7 @@ public function validate($value, Constraint $constraint)
$context->buildViolation($constraint->extraFieldsMessage)
->atPath('['.$field.']')
->setParameter('{{ field }}', $field)
->setInvalidValue($fieldValue)
->addViolation();
}
}
Expand Down
Expand Up @@ -39,7 +39,7 @@ public function validate($value, Constraint $constraint)
$this->context->buildViolation($constraint->exactMessage)
->setParameter('{{ count }}', $count)
->setParameter('{{ limit }}', $constraint->min)
->setValue($value)
->setInvalidValue($value)
->setPlural((int) $constraint->min)
->addViolation();

Expand All @@ -50,7 +50,7 @@ public function validate($value, Constraint $constraint)
$this->context->buildViolation($constraint->maxMessage)
->setParameter('{{ count }}', $count)
->setParameter('{{ limit }}', $constraint->max)
->setValue($value)
->setInvalidValue($value)
->setPlural((int) $constraint->max)
->addViolation();

Expand All @@ -61,7 +61,7 @@ public function validate($value, Constraint $constraint)
$this->context->buildViolation($constraint->minMessage)
->setParameter('{{ count }}', $count)
->setParameter('{{ limit }}', $constraint->min)
->setValue($value)
->setInvalidValue($value)
->setPlural((int) $constraint->min)
->addViolation();
}
Expand Down
Expand Up @@ -43,9 +43,7 @@ public function validate($value, Constraint $constraint)
$group = $context->getGroup();

foreach ($value as $key => $element) {
foreach ($constraint->constraints as $constr) {
$context->validateValue($element, $constr, '['.$key.']', $group);
}
$context->validateValue($element, $constraint->constraints, '['.$key.']', $group);
}
}
}
Expand Up @@ -70,13 +70,13 @@ public function validate($value, Constraint $constraint)
$count = count($value);

if ($constraint->min !== null && $count < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), $value, (int) $constraint->min);

return;
}

if ($constraint->max !== null && $count > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), $value, (int) $constraint->max);

return;
}
Expand Down
Expand Up @@ -52,13 +52,13 @@ public function validate($value, Constraint $constraint)
$group = $context->getGroup();

foreach ($constraint->fields as $field => $fieldConstraint) {
if (
// bug fix issue #2779
(is_array($value) && array_key_exists($field, $value)) ||
($value instanceof \ArrayAccess && $value->offsetExists($field))
) {
foreach ($fieldConstraint->constraints as $constr) {
$context->validateValue($value[$field], $constr, '['.$field.']', $group);
// bug fix issue #2779
$existsInArray = is_array($value) && array_key_exists($field, $value);
$existsInArrayAccess = $value instanceof \ArrayAccess && $value->offsetExists($field);

if ($existsInArray || $existsInArrayAccess) {
if (count($fieldConstraint->constraints) > 0) {
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']', $group);
}
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
Expand Down
Expand Up @@ -51,7 +51,7 @@ public function validate($value, Constraint $constraint)
$this->context->buildViolation($constraint->exactMessage)
->setParameter('{{ value }}', $stringValue)
->setParameter('{{ limit }}', $constraint->min)
->setValue($value)
->setInvalidValue($value)
->setPlural((int) $constraint->min)
->addViolation();

Expand All @@ -62,7 +62,7 @@ public function validate($value, Constraint $constraint)
$this->context->buildViolation($constraint->maxMessage)
->setParameter('{{ value }}', $stringValue)
->setParameter('{{ limit }}', $constraint->max)
->setValue($value)
->setInvalidValue($value)
->setPlural((int) $constraint->max)
->addViolation();

Expand All @@ -73,7 +73,7 @@ public function validate($value, Constraint $constraint)
$this->context->buildViolation($constraint->minMessage)
->setParameter('{{ value }}', $stringValue)
->setParameter('{{ limit }}', $constraint->min)
->setValue($value)
->setInvalidValue($value)
->setPlural((int) $constraint->min)
->addViolation();
}
Expand Down
Expand Up @@ -32,30 +32,15 @@ public function __toString()
/**
* @author Daniel Holmes <daniel@danielholmes.org>
*/
abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_TestCase
abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest
{
private $validator;
private $context;

protected function setUp()
{
$this->validator = $this->createValidator();
$this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
->disableOriginalConstructor()
->getMock();
$this->validator->initialize($this->context);
}

/**
* @return AbstractComparisonValidator
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
abstract protected function createValidator();

public function testThrowsConstraintExceptionIfNoValueOrProperty()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');

$comparison = $this->createConstraint(array());

$this->validator->validate('some value', $comparison);
}

Expand All @@ -66,16 +51,11 @@ public function testThrowsConstraintExceptionIfNoValueOrProperty()
*/
public function testValidComparisonToValue($dirtyValue, $comparisonValue)
{
$this->context->expects($this->never())
->method('addViolation');

$constraint = $this->createConstraint(array('value' => $comparisonValue));

$this->context->expects($this->any())
->method('getPropertyPath')
->will($this->returnValue('property1'));

$this->validator->validate($dirtyValue, $constraint);

$this->assertNoViolation();
}

/**
Expand All @@ -95,19 +75,13 @@ public function testInvalidComparisonToValue($dirtyValue, $comparedValue, $compa
$constraint = $this->createConstraint(array('value' => $comparedValue));
$constraint->message = 'Constraint Message';

$this->context->expects($this->any())
->method('getPropertyPath')
->will($this->returnValue('property1'));

$this->context->expects($this->once())
->method('addViolation')
->with('Constraint Message', array(
'{{ value }}' => $comparedValueString,
'{{ compared_value }}' => $comparedValueString,
'{{ compared_value_type }}' => $comparedValueType
));

$this->validator->validate($dirtyValue, $constraint);

$this->assertViolation('Constraint Message', array(
'{{ value }}' => $comparedValueString,
'{{ compared_value }}' => $comparedValueString,
'{{ compared_value_type }}' => $comparedValueType
));
}

/**
Expand Down

0 comments on commit 7504448

Please sign in to comment.