Skip to content

Commit 835102a

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: Add hint for testing custom constraints
2 parents 4d62bf3 + 43c70d1 commit 835102a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

validation/custom_constraint.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,46 @@ not to the property:
381381
$metadata->addConstraint(new ProtocolClass());
382382
}
383383
}
384+
385+
Testing Custom Constraints
386+
--------------------------
387+
388+
Use the ``ConstraintValidatorTestCase`` utility to simplify the creation of
389+
unit tests for your custom constraints::
390+
391+
// ...
392+
use App\Validator\ContainsAlphanumeric;
393+
use App\Validator\ContainsAlphanumericValidator;
394+
395+
class ContainsAlphanumericValidatorTest extends ConstraintValidatorTestCase
396+
{
397+
protected function createValidator()
398+
{
399+
return new ContainsAlphanumericValidator();
400+
}
401+
402+
public function testNullIsValid()
403+
{
404+
$this->validator->validate(null, new ContainsAlphanumeric());
405+
406+
$this->assertNoViolation();
407+
}
408+
409+
/**
410+
* @dataProvider provideInvalidConstraints
411+
*/
412+
public function testTrueIsInvalid(ContainsAlphanumeric $constraint)
413+
{
414+
$this->validator->validate('...', $constraint);
415+
416+
$this->buildViolation('myMessage')
417+
->setParameter('{{ string }}', '...')
418+
->assertRaised();
419+
}
420+
421+
public function provideInvalidConstraints(): iterable
422+
{
423+
yield [new ContainsAlphanumeric(message: 'myMessage')];
424+
// ...
425+
}
426+
}

0 commit comments

Comments
 (0)