Skip to content

Commit 00ecbd4

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: Add hint for testing custom constraints
2 parents b6db179 + 3c97aee commit 00ecbd4

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
@@ -328,3 +328,46 @@ not to the property:
328328
$metadata->addConstraint(new ProtocolClass());
329329
}
330330
}
331+
332+
Testing Custom Constraints
333+
--------------------------
334+
335+
Use the ``ConstraintValidatorTestCase`` utility to simplify the creation of
336+
unit tests for your custom constraints::
337+
338+
// ...
339+
use App\Validator\ContainsAlphanumeric;
340+
use App\Validator\ContainsAlphanumericValidator;
341+
342+
class ContainsAlphanumericValidatorTest extends ConstraintValidatorTestCase
343+
{
344+
protected function createValidator()
345+
{
346+
return new ContainsAlphanumericValidator();
347+
}
348+
349+
public function testNullIsValid()
350+
{
351+
$this->validator->validate(null, new ContainsAlphanumeric());
352+
353+
$this->assertNoViolation();
354+
}
355+
356+
/**
357+
* @dataProvider provideInvalidConstraints
358+
*/
359+
public function testTrueIsInvalid(ContainsAlphanumeric $constraint)
360+
{
361+
$this->validator->validate('...', $constraint);
362+
363+
$this->buildViolation('myMessage')
364+
->setParameter('{{ string }}', '...')
365+
->assertRaised();
366+
}
367+
368+
public function provideInvalidConstraints(): iterable
369+
{
370+
yield [new ContainsAlphanumeric(message: 'myMessage')];
371+
// ...
372+
}
373+
}

0 commit comments

Comments
 (0)