From 7d8f0b35893d0ff038867939987d4cd8e513164f Mon Sep 17 00:00:00 2001 From: Tim Jabs Date: Sat, 6 Aug 2022 23:24:59 +0200 Subject: [PATCH] Add hint for testing custom constraints --- validation.rst | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/validation.rst b/validation.rst index 27f970a701d..a83e1630ed7 100644 --- a/validation.rst +++ b/validation.rst @@ -801,6 +801,49 @@ You can also validate all the classes stored in a given directory: $ php bin/console debug:validator src/Entity +Testing Custom Constraints +------------------------- + +Since custom constraints contain meaningful logic for your application, writing tests is crucial. You can use the ``ConstraintValidatorTestCase`` to write unit tests for custom constraints: + +.. code-block:: php + class IsFalseValidatorTest extends ConstraintValidatorTestCase + { + protected function createValidator() + { + return new IsFalseValidator(); + } + + public function testNullIsValid() + { + $this->validator->validate(null, new IsFalse()); + + $this->assertNoViolation(); + } + + /** + * @dataProvider provideInvalidConstraints + */ + public function testTrueIsInvalid(IsFalse $constraint) + { + $this->validator->validate(true, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'true') + ->setCode(IsFalse::NOT_FALSE_ERROR) + ->assertRaised(); + } + + public function provideInvalidConstraints(): iterable + { + yield 'Doctrine style' => [new IsFalse([ + 'message' => 'myMessage', + ])]; + yield 'named parameters' => [new IsFalse(message: 'myMessage')]; + } + + } + Final Thoughts --------------