mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
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.
This commit is contained in:
parent
a563ba644e
commit
da6589ba87
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace ChillThirdParty\Tests\Validator;
|
||||
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
|
||||
use Chill\ThirdPartyBundle\Validator\ThirdPartyHasEmail;
|
||||
use Chill\ThirdPartyBundle\Validator\ThirdPartyHasEmailValidator;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class ThirdPartyHasEmailValidatorTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testThirdPartyWithEmail(): void
|
||||
{
|
||||
$thirdParty = new ThirdParty();
|
||||
$thirdParty->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());
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Add a constraint to check that the thirdparty has an email address.
|
||||
*/
|
||||
class ThirdPartyHasEmail extends Constraint
|
||||
{
|
||||
public string $message = 'thirdParty.thirdParty_has_no_email';
|
||||
public string $code = 'f2da71f8-818c-11ef-9f6a-3ba3597ab60b';
|
||||
|
||||
/**
|
||||
* @return array<string>|string
|
||||
*/
|
||||
public function getTargets(): array|string
|
||||
{
|
||||
return [self::PROPERTY_CONSTRAINT];
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Validator;
|
||||
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
final class ThirdPartyHasEmailValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(private readonly ThirdPartyRender $thirdPartyRender) {}
|
||||
|
||||
public function validate($value, Constraint $constraint): void
|
||||
{
|
||||
if (!$constraint instanceof ThirdPartyHasEmail) {
|
||||
throw new UnexpectedTypeException($constraint, ThirdPartyHasEmail::class);
|
||||
}
|
||||
|
||||
if (null === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$value instanceof ThirdParty) {
|
||||
throw new UnexpectedTypeException($value, ThirdParty::class);
|
||||
}
|
||||
|
||||
if (null === $value->getEmail() || '' === $value->getEmail()) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setParameter('{{ thirdParty }}', $this->thirdPartyRender->renderString($value, []))
|
||||
->setCode($constraint->code)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
@ -11,3 +11,8 @@ services:
|
||||
autoconfigure: true
|
||||
resource: '../Export/'
|
||||
|
||||
Chill\ThirdPartyBundle\Validator\:
|
||||
autoconfigure: true
|
||||
autowire: true
|
||||
resource: '../Validator/'
|
||||
|
||||
|
@ -0,0 +1,2 @@
|
||||
thirdParty:
|
||||
thirdParty_has_no_email: Le tiers {{ thirdParty }} n'a pas d'adresse email configurée.
|
Loading…
x
Reference in New Issue
Block a user