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 @@ -272,3 +272,46 @@ not to the property:
272272 $metadata->addConstraint(new ProtocolClass());
273273 }
274274 }
275+
276+ Testing Custom Constraints
277+ --------------------------
278+
279+ Use the ``ConstraintValidatorTestCase `` utility to simplify the creation of
280+ unit tests for your custom constraints::
281+
282+ // ...
283+ use App\Validator\ContainsAlphanumeric;
284+ use App\Validator\ContainsAlphanumericValidator;
285+
286+ class ContainsAlphanumericValidatorTest extends ConstraintValidatorTestCase
287+ {
288+ protected function createValidator()
289+ {
290+ return new ContainsAlphanumericValidator();
291+ }
292+
293+ public function testNullIsValid()
294+ {
295+ $this->validator->validate(null, new ContainsAlphanumeric());
296+
297+ $this->assertNoViolation();
298+ }
299+
300+ /**
301+ * @dataProvider provideInvalidConstraints
302+ */
303+ public function testTrueIsInvalid(ContainsAlphanumeric $constraint)
304+ {
305+ $this->validator->validate('...', $constraint);
306+
307+ $this->buildViolation('myMessage')
308+ ->setParameter('{{ string }}', '...')
309+ ->assertRaised();
310+ }
311+
312+ public function provideInvalidConstraints(): iterable
313+ {
314+ yield [new ContainsAlphanumeric(message: 'myMessage')];
315+ // ...
316+ }
317+ }
You can’t perform that action at this time.
0 commit comments