diff --git a/src/Bundle/ChillPersonBundle/Tests/Validator/AccompanyingPeriod/LocationValidityValidatorTest.php b/src/Bundle/ChillPersonBundle/Tests/Validator/AccompanyingPeriod/LocationValidityValidatorTest.php new file mode 100644 index 000000000..b70a8146b --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Validator/AccompanyingPeriod/LocationValidityValidatorTest.php @@ -0,0 +1,122 @@ +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); + } + +} diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php index 9a64b3cf6..75c10ca75 100644 --- a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidity.php @@ -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() { diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidityValidator.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidityValidator.php index 3a0b1a3b3..a92120f2d 100644 --- a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidityValidator.php +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/LocationValidityValidator.php @@ -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() + ; } }