8000 [Validator] Add `CompoundConstraintTestCase` to ease testing Compound constraints by alexandre-daubois · Pull Request #20150 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add CompoundConstraintTestCase to ease testing Compound constraints #20150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
74 changes: 74 additions & 0 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ A class constraint validator must be applied to the class itself:
Testing Custom Constraints
--------------------------

Atomic Constraints
~~~~~~~~~~~~~~~~~~

Use the :class:`Symfony\\Component\\Validator\\Test\\ConstraintValidatorTestCase`
class to simplify writing unit tests for your custom constraints::

Expand Down Expand Up @@ -545,3 +548,74 @@ class to simplify writing unit tests for your custom constraints::
// ...
}
}

Compound Constraints
~~~~~~~~~~~~~~~~~~~~

Let's say you create a compound constraint that checks if a string meets
your minimum requirements for your password policy::

// src/Validator/PasswordRequirements.php
namespace App\Validator;

use Symfony\Component\Validator\Constraints as Assert;

#[\Attribute]
class PasswordRequirements extends Assert\Compound
{
protected function getConstraints(array $options): array
{
return [
new Assert\NotBlank(allowNull: false),
new Assert\Length(min: 8, max: 255),
new Assert\NotCompromisedPassword(),
new Assert\Type('string'),
new Assert\Regex('/[A-Z]+/'),
];
}
}

You can use the :class:`Symfony\\Component\\Validator\\Test\\CompoundConstraintTestCase`
class to check precisely which of the constraints failed to pass::

// tests/Validator/PasswordRequirementsTest.php
namespace App\Tests\Validator;

use App\Validator\PasswordRequirements;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Test\CompoundConstraintTestCase;

/**
* @extends CompoundConstraintTestCase<PasswordRequirements>
*/
class PasswordRequirementsTest extends CompoundConstraintTestCase
{
public function createCompound(): Assert\Compound
{
return new PasswordRequirements();
}

public function testInvalidPassword(): void
{
$this->validateValue('azerty123');

// check all constraints pass except for the
// password leak and the uppercase letter checks
$this->assertViolationsRaisedByCompound([
new Assert\NotCompromisedPassword(),
new Assert\Regex('/[A-Z]+/'),
]);
}

public function testValid(): void
{
$this->validateValue('VERYSTR0NGP4$$WORD#%!');

$this->assertNoViolation();
}
}

.. versionadded:: 7.2

The :class:`Symfony\\Component\\Validator\\Test\\CompoundConstraintTestCase`
class was introduced in Symfony 7.2.
Loading
0