add test for household membership sequential validator

This commit is contained in:
Julien Fastré 2021-06-15 21:31:02 +02:00
parent 78ca61c82e
commit 54997e5893
2 changed files with 101 additions and 1 deletions

View File

@ -0,0 +1,100 @@
<?php
namespace Chill\PersonBundle\Tests\Validator\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Templating\Entity\PersonRender;
use Chill\PersonBundle\Validator\Constraints\Household\HouseholdMembershipSequentialValidator;
use Chill\PersonBundle\Validator\Constraints\Household\HouseholdMembershipSequential;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
class HouseholdMembershipSequentialValidatorTest extends ConstraintValidatorTestCase
{
public function testEmptyPerson()
{
$constraint = $this->getConstraint();
$person = new Person();
$this->validator->validate($person, $constraint);
$this->assertNoViolation();
}
public function testMembershipCovering()
{
$constraint = $this->getConstraint();
$person = new Person();
$household = new Household();
$position = (new Position())
->setShareHousehold(true)
;
$membership = (new HouseholdMember())
->setPosition($position)
->setStartDate(new \DateTimeImmutable('2010-01-01'))
->setPerson($person)
;
$membership = (new HouseholdMember())
->setPosition($position)
->setStartDate(new \DateTimeImmutable('2011-01-01'))
->setPerson($person)
;
$this->validator->validate($person, $constraint);
$this->buildViolation('msg')
->setParameters([
'%person_name%' => 'name',
'%from%' => '01-01-2011',
'%nbHousehold%' => 2
])
->assertRaised()
;
}
public function testMembershipCoveringNoShareHousehold()
{
$constraint = $this->getConstraint();
$person = new Person();
$household = new Household();
$position = (new Position())
->setShareHousehold(false)
;
$membership = (new HouseholdMember())
->setPosition($position)
->setStartDate(new \DateTimeImmutable('2010-01-01'))
->setPerson($person)
;
$membership = (new HouseholdMember())
->setPosition($position)
->setStartDate(new \DateTimeImmutable('2011-01-01'))
->setPerson($person)
;
$this->validator->validate($person, $constraint);
$this->assertNoViolation();
}
protected function getConstraint()
{
return new HouseholdMembershipSequential([
'message' => 'msg'
]);
}
protected function createValidator()
{
$render = $this->createMock(PersonRender::class);
$render->method('renderString')
->willReturn('name')
;
return new HouseholdMembershipSequentialValidator($render);
}
}

View File

@ -50,7 +50,7 @@ class HouseholdMembershipSequentialValidator extends ConstraintValidator
$nbHousehold = count($metadata);
$this->context
->buildViolation("household_membership.Person with membership covering")
->buildViolation($constraint->message)
->setParameters([
'%person_name%' => $this->render->renderString(
$participation->getPerson(), []