add test for accompanying period location validity

This commit is contained in:
Julien Fastré 2021-07-28 16:16:57 +02:00 committed by Marc Ducobu
parent a3a5d5cfd0
commit 8d2b1fbe13
3 changed files with 129 additions and 5 deletions

View File

@ -0,0 +1,122 @@
<?php
namespace Chill\PersonBundle\Tests\Validator\AccompanyingPeriod;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\LocationValidityValidator;
use Chill\PersonBundle\Templating\Entity\PersonRender;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\LocationValidity;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
class LocationValidityValidatorTest extends ConstraintValidatorTestCase
{
public function testValidAddress()
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$person = new Person();
$period->addPerson($person);
$period->setPersonLocation($person);
$this->validator->validate($period, $constraint);
$this->assertNoViolation();
}
public function testPeriodDoesNotContainsPersonOnRemovedPerson()
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$person1 = new Person();
$period->addPerson($person1);
$period->setPersonLocation($person1);
$period->removePerson($person1);
$this->validator->validate($period, $constraint);
$this->buildViolation('messagePersonLocatedMustBeAssociated')
->setParameters([
'{{ person_name }}' => 'name'
])
->assertRaised()
;
}
public function testPeriodDoesNotContainsPersonOnOtherPerson()
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$person1 = new Person();
$person2 = new Person();
$period->addPerson($person1);
$period->setPersonLocation($person2);
$this->validator->validate($period, $constraint);
$this->buildViolation('messagePersonLocatedMustBeAssociated')
->setParameters([
'{{ person_name }}' => 'name'
])
->assertRaised()
;
}
public function testPeriodDoesNotContainsPersonOnAnyPerson()
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$person1 = new Person();
$period->setPersonLocation($person1);
$this->validator->validate($period, $constraint);
$this->buildViolation('messagePersonLocatedMustBeAssociated')
->setParameters([
'{{ person_name }}' => 'name'
])
->assertRaised()
;
}
public function testRemoveLocationOnPeriodValidated()
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$period->setStep('not draft');
$this->validator->validate($period, $constraint);
$this->buildViolation('messagePeriodMustRemainsLocated')
->assertRaised()
;
}
protected function getConstraint()
{
return new LocationValidity([
'messagePersonLocatedMustBeAssociated' => 'messagePersonLocatedMustBeAssociated',
'messagePeriodMustRemainsLocated' => 'messagePeriodMustRemainsLocated'
]);
}
protected function createValidator()
{
$render= $this->createMock(PersonRender::class);
$render->method('renderString')
->willReturn('name')
;
return new LocationValidityValidator($render);
}
}

View File

@ -11,7 +11,7 @@ class LocationValidity extends Constraint
{
public $messagePersonLocatedMustBeAssociated = "The person where the course is located must be associated to the course. Change course's location before removing the person.";
public $messagPeriodMustRemainsLocated = "The period must remains located";
public $messagePeriodMustRemainsLocated = "The period must remains located";
public function getTargets()
{

View File

@ -42,10 +42,12 @@ class LocationValidityValidator extends ConstraintValidator
if ($period->getStep() !== AccompanyingPeriod::STEP_DRAFT
&& $period->getLocationStatus() === 'none') {
$this->context->buildViolation($constraint->messagePeriodMustRemainsLocated
->addViolation()
;
$this->context
->buildViolation(
$constraint->messagePeriodMustRemainsLocated
)
->addViolation()
;
}
}