8000 Add collection validator group tests. · symfony/symfony@5c3e710 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c3e710

Browse files
committed
Add collection validator group tests.
1 parent 7217c65 commit 5c3e710

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,116 @@ public function testObjectShouldBeLeftUnchanged()
386386
'foo' => 3,
387387
), (array) $value);
388388
}
389+
390+
/**
391+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
392+
*/
393+
public function testGroupShouldBePassedToTheContainingConstraint()
394+
{
395+
$value = array('baz' => 2);
396+
397+
$constraint = new Collection(
398+
array(
399+
'fields' => array(
400+
'baz' => array(
401+
new Range(array(
402+
'min' => 1,
403+
'groups' => 'bar',
404+
)),
405+
),
406+
),
407+
'groups' => 'foo',
408+
)
409+
);
410+
411+
$this->validator->validate($this->prepareTestData($value), $constraint);
412+
}
413+
414+
/**
415+
* @dataProvider multipleGroupsForCollectionProvider
416+
*
417+
* @param array $fooGroups
418+
* @param array $barGroups
419+
* @param array $collectionGroups
420+
* @param array $expectedGroups
421+
*/
422+
public function testValidateMultipleGroupsForCollectionConstraint(
423+
array $fooGroups,
424+
array $barGroups,
425+
array $collectionGroups,
426+
array $expectedGroups
427+
) {
428+
$value = array('baz' => 2);
429+
430+
$fooConstraint = new Range(array(
431+
'min' => 3,
432+
'minMessage' => 'Group foo',
433+
'groups' => $fooGroups,
434+
));
435+
$barConstraint = new Range(array(
436+
'min' => 5,
437+
'minMessage' => 'Group bar',
438+
'groups' => $barGroups,
439+
));
440+
441+
$constraint = new Collection(
442+
array(
443+
'fields' => array(
444+
'baz' => array(
445+
$fooConstraint,
446+
$barConstraint,
447+
),
448+
),
449+
'groups' => $collectionGroups,
450+
)
451+
);
452+
453+
$data = $this->prepareTestData($value);
454+
455+
$this->expectValidateValueAt(0, '[baz]', $value['baz'], array($fooConstraint, $barConstraint), $expectedGroups);
456+
457+
$this->validator->validate($data, $constraint);
458+
}
459+
460+
public static function multipleGroupsForCollectionProvider()
461+
{
462+
return array(
463+
array(
464+
array('foo', 'bar'),
465+
array('foo', 'bar'),
466+
array('foo', 'bar'),
467+
array('foo', 'bar'),
468+
),
469+
array(
470+
array('foo', 'bar'),
471+
array('bar'),
472+
array('foo', 'bar'),
473+
array('foo', 'bar'),
474+
),
475+
array(
476+
array('foo'),
477+
array('foo', 'bar'),
478+
array('foo', 'bar'),
479+
array('foo', 'bar'),
480+
),
481+
array(
482+
array('foo'),
483+
array('bar'),
484+
array('foo', 'bar'),
485+
array('foo', 'bar'),
486+
),
487+
array(
488+
array('foo'),
489+
array('foo'),
490+
array('foo', 'bar'),
491+
array('foo'),
492+
),
493+
array(
494+
array('foo'),
495+
array('foo'),
496+
array('foo'),
497+
array('foo'),
498+
),
499+
);
500+
}
389501
}

0 commit comments

Comments
 (0)
0