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 @@ -381,3 +381,46 @@ not to the property:
381
381
$metadata->addConstraint(new ProtocolClass());
382
382
}
383
383
}
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
+ }
You can’t perform that action at this time.
0 commit comments