From da6589ba87024f76a1556e4309fef62076ad8f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 3 Oct 2024 17:14:42 +0200 Subject: [PATCH] Add ThirdPartyHasEmail validator Introduce a new validator that ensures a third party has an email address, including the corresponding translation for error messaging and unit tests to verify its functionality. --- .../ThirdPartyHasEmailValidatorTest.php | 63 +++++++++++++++++++ .../Validator/ThirdPartyHasEmail.php | 31 +++++++++ .../Validator/ThirdPartyHasEmailValidator.php | 45 +++++++++++++ .../config/services.yaml | 5 ++ .../translations/validators.fr.yml | 2 + 5 files changed, 146 insertions(+) create mode 100644 src/Bundle/ChillThirdPartyBundle/Tests/Validator/ThirdPartyHasEmailValidatorTest.php create mode 100644 src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmail.php create mode 100644 src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmailValidator.php create mode 100644 src/Bundle/ChillThirdPartyBundle/translations/validators.fr.yml diff --git a/src/Bundle/ChillThirdPartyBundle/Tests/Validator/ThirdPartyHasEmailValidatorTest.php b/src/Bundle/ChillThirdPartyBundle/Tests/Validator/ThirdPartyHasEmailValidatorTest.php new file mode 100644 index 000000000..46616287c --- /dev/null +++ b/src/Bundle/ChillThirdPartyBundle/Tests/Validator/ThirdPartyHasEmailValidatorTest.php @@ -0,0 +1,63 @@ +setEmail('third@example.com'); + + $this->validator->validate($thirdParty, new ThirdPartyHasEmail()); + + $this->assertNoViolation(); + } + + public function testThirdPartyHasNoEmail(): void + { + $thirdParty = new ThirdParty(); + + $constraint = new ThirdPartyHasEmail(); + $constraint->message = 'message'; + + $this->validator->validate($thirdParty, $constraint); + + $this->buildViolation($constraint->message) + ->setParameter('{{ thirdParty }}', '3party-string') + ->setCode($constraint->code) + ->assertRaised(); + } + + protected function createValidator(): ThirdPartyHasEmailValidator + { + $render = $this->prophesize(ThirdPartyRender::class); + $render->renderString(Argument::type(ThirdParty::class), []) + ->willReturn('3party-string'); + + return new ThirdPartyHasEmailValidator($render->reveal()); + } +} diff --git a/src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmail.php b/src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmail.php new file mode 100644 index 000000000..453c857c5 --- /dev/null +++ b/src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmail.php @@ -0,0 +1,31 @@ +|string + */ + public function getTargets(): array|string + { + return [self::PROPERTY_CONSTRAINT]; + } +} diff --git a/src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmailValidator.php b/src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmailValidator.php new file mode 100644 index 000000000..15f523509 --- /dev/null +++ b/src/Bundle/ChillThirdPartyBundle/Validator/ThirdPartyHasEmailValidator.php @@ -0,0 +1,45 @@ +getEmail() || '' === $value->getEmail()) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ thirdParty }}', $this->thirdPartyRender->renderString($value, [])) + ->setCode($constraint->code) + ->addViolation(); + } + } +} diff --git a/src/Bundle/ChillThirdPartyBundle/config/services.yaml b/src/Bundle/ChillThirdPartyBundle/config/services.yaml index 0024556f6..572026771 100644 --- a/src/Bundle/ChillThirdPartyBundle/config/services.yaml +++ b/src/Bundle/ChillThirdPartyBundle/config/services.yaml @@ -11,3 +11,8 @@ services: autoconfigure: true resource: '../Export/' + Chill\ThirdPartyBundle\Validator\: + autoconfigure: true + autowire: true + resource: '../Validator/' + diff --git a/src/Bundle/ChillThirdPartyBundle/translations/validators.fr.yml b/src/Bundle/ChillThirdPartyBundle/translations/validators.fr.yml new file mode 100644 index 000000000..26e1562dd --- /dev/null +++ b/src/Bundle/ChillThirdPartyBundle/translations/validators.fr.yml @@ -0,0 +1,2 @@ +thirdParty: + thirdParty_has_no_email: Le tiers {{ thirdParty }} n'a pas d'adresse email configurée.