From df9f30265f438584ae9f25d50319045c4be29c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 30 Aug 2023 14:14:36 +0200 Subject: [PATCH] Fix Birthdate validator DI and dedicated test --- .../Person/BirthdateValidatorTest.php | 23 ++++++++++++++++--- .../ChillPersonBundle/config/services.yaml | 7 ------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Tests/Validator/Person/BirthdateValidatorTest.php b/src/Bundle/ChillPersonBundle/Tests/Validator/Person/BirthdateValidatorTest.php index 476f7045f..d32cd4c02 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Validator/Person/BirthdateValidatorTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Validator/Person/BirthdateValidatorTest.php @@ -14,6 +14,8 @@ namespace Chill\PersonBundle\Tests\Validator\Person; use Chill\PersonBundle\Validator\Constraints\Person\Birthdate; use Chill\PersonBundle\Validator\Constraints\Person\BirthdateValidator; use DateTime; +use Symfony\Component\Clock\MockClock; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; /** @@ -29,7 +31,7 @@ final class BirthdateValidatorTest extends ConstraintValidatorTestCase $bornAfter = new DateTime('+2 days'); $this->validator->validate($bornAfter, $this->createConstraint()); $this->buildViolation('msg') - ->setParameter('%date%', (new DateTime('yesterday'))->format('d-m-Y')) + ->setParameter('%date%', (new DateTime('2023-08-29'))->format('d-m-Y')) ->setCode('3f42fd96-0b2d-11ec-8cf3-0f3b1b1ca1c4') ->assertRaised(); } @@ -39,7 +41,18 @@ final class BirthdateValidatorTest extends ConstraintValidatorTestCase $bornToday = new DateTime('now'); $this->validator->validate($bornToday, $this->createConstraint()); $this->buildViolation('msg') - ->setParameter('%date%', (new DateTime('yesterday'))->format('d-m-Y')) + ->setParameter('%date%', (new DateTime('2023-08-29'))->format('d-m-Y')) + ->setCode('3f42fd96-0b2d-11ec-8cf3-0f3b1b1ca1c4') + ->assertRaised(); + } + + public function testValidateTooEarlyDate(): void + { + $bornLongTimeAgo = new DateTime('1871-03-18'); + $this->validator->validate($bornLongTimeAgo, $this->createConstraint()); + + $this->buildViolation('below') + ->setParameter('%date%', (new DateTime('1873-08-30'))->format('d-m-Y')) ->setCode('3f42fd96-0b2d-11ec-8cf3-0f3b1b1ca1c4') ->assertRaised(); } @@ -53,13 +66,17 @@ final class BirthdateValidatorTest extends ConstraintValidatorTestCase protected function createValidator() { - return new BirthdateValidator('P1D'); + return new BirthdateValidator( + new ParameterBag(['chill_person' => ['validation' => ['birthdate_not_after' => 'P1D']]]), + new MockClock(new \DateTimeImmutable('2023-08-30T14:12:00')) + ); } private function createConstraint() { return new Birthdate([ 'message' => 'msg', + 'belowMessage' => 'below' ]); } } diff --git a/src/Bundle/ChillPersonBundle/config/services.yaml b/src/Bundle/ChillPersonBundle/config/services.yaml index 45548d843..526f5cbbd 100644 --- a/src/Bundle/ChillPersonBundle/config/services.yaml +++ b/src/Bundle/ChillPersonBundle/config/services.yaml @@ -59,13 +59,6 @@ services: autoconfigure: true resource: '../Validator/Constraints/' - # override default config, must be loaded after resource - Chill\PersonBundle\Validator\Constraints\BirthdateValidator: - arguments: - - "%chill_person.validation.birtdate_not_before%" - tags: - - { name: validator.constraint_validator, alias: birthdate_not_before } - Chill\PersonBundle\Repository\: autowire: true autoconfigure: true