Skip to content

Commit 3c97aee

Browse files
committed
Reword
1 parent 72236a2 commit 3c97aee

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

validation.rst

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -801,49 +801,6 @@ You can also validate all the classes stored in a given directory:
801801
802802
$ php bin/console debug:validator src/Entity
803803
804-
Testing Custom Constraints
805-
-------------------------
806-
807-
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:
808-
809-
.. code-block:: php
810-
class IsFalseValidatorTest extends ConstraintValidatorTestCase
811-
{
812-
protected function createValidator()
813-
{
814-
return new IsFalseValidator();
815-
}
816-
817-
public function testNullIsValid()
818-
{
819-
$this->validator->validate(null, new IsFalse());
820-
821-
$this->assertNoViolation();
822-
}
823-
824-
/**
825-
* @dataProvider provideInvalidConstraints
826-
*/
827-
public function testTrueIsInvalid(IsFalse $constraint)
828-
{
829-
$this->validator->validate(true, $constraint);
830-
831-
$this->buildViolation('myMessage')
832-
->setParameter('{{ value }}', 'true')
833-
->setCode(IsFalse::NOT_FALSE_ERROR)
834-
->assertRaised();
835-
}
836-
837-
public function provideInvalidConstraints(): iterable
838-
{
839-
yield 'Doctrine style' => [new IsFalse([
840-
'message' => 'myMessage',
841-
])];
842-
yield 'named parameters' => [new IsFalse(message: 'myMessage')];
843-
}
844-
845-
}
846-
847804
Final Thoughts
848805
--------------
849806

validation/custom_constraint.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,46 @@ not to the property:
338338
$metadata->addConstraint(new ProtocolClass());
339339
}
340340
}
341+
342+
Testing Custom Constraints
343+
--------------------------
344+
345+
Use the ``ConstraintValidatorTestCase`` utility to simplify the creation of
346+
unit tests for your custom constraints::
347+
348+
// ...
349+
use App\Validator\ContainsAlphanumeric;
350+
use App\Validator\ContainsAlphanumericValidator;
351+
352+
class ContainsAlphanumericValidatorTest extends ConstraintValidatorTestCase
353+
{
354+
protected function createValidator()
355+
{
356+
return new ContainsAlphanumericValidator();
357+
}
358+
359+
public function testNullIsValid()
360+
{
361+
$this->validator->validate(null, new ContainsAlphanumeric());
362+
363+
$this->assertNoViolation();
364+
}
365+
366+
/**
367+
* @dataProvider provideInvalidConstraints
368+
*/
369+
public function testTrueIsInvalid(ContainsAlphanumeric $constraint)
370+
{
371+
$this->validator->validate('...', $constraint);
372+
373+
$this->buildViolation('myMessage')
374+
->setParameter('{{ string }}', '...')
375+
->assertRaised();
376+
}
377+
378+
public function provideInvalidConstraints(): iterable
379+
{
380+
yield [new ContainsAlphanumeric(message: 'myMessage')];
381+
// ...
382+
}
383+
}

0 commit comments

Comments
 (0)