File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments