entity person: create a validator to check a person entity is linked

with a center

This validator take a parameter in configuration
This commit is contained in:
2021-09-01 14:04:40 +02:00
parent 03e8624528
commit eec798cfd3
5 changed files with 127 additions and 15 deletions

View File

@@ -0,0 +1,50 @@
<?php
namespace Validator\Person;
use Chill\MainBundle\Entity\Center;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Validator\Constraints\Person\PersonHasCenter;
use Chill\PersonBundle\Validator\Constraints\Person\PersonHasCenterValidator;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class PersonHasCenterValidatorTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase
{
public function testValidateRequired()
{
$constraint = $this->getConstraint();
$personHasCenter = (new Person())->setCenter(new Center());
$personNoCenter = new Person();
$this->validator->validate($personHasCenter, $constraint);
$this->assertNoViolation();
$this->validator->validate($personNoCenter, $constraint);
$this->buildViolation('msg')
->atPath('property.path.center')
->assertRaised();
}
protected function getConstraint()
{
return new PersonHasCenter([
'message' => 'msg'
]);
}
protected function createValidator()
{
$parameterBag = $this->createMock(ParameterBagInterface::class);
$parameterBag
->method('get')
->with($this->equalTo('chill_person'))
->willReturn([
'validation' => [
'center_required' => true
]
])
;
return new PersonHasCenterValidator($parameterBag);
}
}