move validation on person into annotations

This commit is contained in:
2021-09-01 16:06:20 +02:00
parent 7faddbe3fe
commit 41d76542b4
9 changed files with 165 additions and 97 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace Validator\Person;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Validator\Constraints\Person\Birthdate;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class PersonValidationTest extends KernelTestCase
{
private ValidatorInterface $validator;
protected function setUp()
{
self::bootKernel();
$this->validator = self::$container->get(ValidatorInterface::class);
}
public function testFirstnameValidation()
{
$person = (new Person())
->setFirstname(\str_repeat('a', 500));
$errors = $this->validator->validate($person, null, ["creation"]);
foreach ($errors->getIterator() as $error) {
if (Length::TOO_LONG_ERROR === $error->getCode()) {
$this->assertTrue(true,
"error code for firstname too long is present");
return;
}
}
$this->assertTrue(false,
"error code for fistname too long is present");
}
public function testBirthdateInFuture()
{
$person = (new Person())
->setBirthdate(new \Datetime('+2 months'));
$errors = $this->validator->validate($person, null, ["creation"]);
foreach ($errors->getIterator() as $error) {
if (Birthdate::BIRTHDATE_INVALID_CODE === $error->getCode()) {
$this->assertTrue(true,
"error code for birthdate invalid is present");
return;
}
}
$this->assertTrue(false,
"error code for birthdate invalid is present");
}
}