Fix Birthdate validator DI and dedicated test

This commit is contained in:
Julien Fastré 2023-08-30 14:14:36 +02:00
parent 1b8acfab24
commit df9f30265f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 20 additions and 10 deletions

View File

@ -14,6 +14,8 @@ namespace Chill\PersonBundle\Tests\Validator\Person;
use Chill\PersonBundle\Validator\Constraints\Person\Birthdate; use Chill\PersonBundle\Validator\Constraints\Person\Birthdate;
use Chill\PersonBundle\Validator\Constraints\Person\BirthdateValidator; use Chill\PersonBundle\Validator\Constraints\Person\BirthdateValidator;
use DateTime; use DateTime;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/** /**
@ -29,7 +31,7 @@ final class BirthdateValidatorTest extends ConstraintValidatorTestCase
$bornAfter = new DateTime('+2 days'); $bornAfter = new DateTime('+2 days');
$this->validator->validate($bornAfter, $this->createConstraint()); $this->validator->validate($bornAfter, $this->createConstraint());
$this->buildViolation('msg') $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') ->setCode('3f42fd96-0b2d-11ec-8cf3-0f3b1b1ca1c4')
->assertRaised(); ->assertRaised();
} }
@ -39,7 +41,18 @@ final class BirthdateValidatorTest extends ConstraintValidatorTestCase
$bornToday = new DateTime('now'); $bornToday = new DateTime('now');
$this->validator->validate($bornToday, $this->createConstraint()); $this->validator->validate($bornToday, $this->createConstraint());
$this->buildViolation('msg') $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') ->setCode('3f42fd96-0b2d-11ec-8cf3-0f3b1b1ca1c4')
->assertRaised(); ->assertRaised();
} }
@ -53,13 +66,17 @@ final class BirthdateValidatorTest extends ConstraintValidatorTestCase
protected function createValidator() 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() private function createConstraint()
{ {
return new Birthdate([ return new Birthdate([
'message' => 'msg', 'message' => 'msg',
'belowMessage' => 'below'
]); ]);
} }
} }

View File

@ -59,13 +59,6 @@ services:
autoconfigure: true autoconfigure: true
resource: '../Validator/Constraints/' 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\: Chill\PersonBundle\Repository\:
autowire: true autowire: true
autoconfigure: true autoconfigure: true