diff --git a/src/Bundle/ChillTicketBundle/src/Action/Ticket/SetPersonsCommand.php b/src/Bundle/ChillTicketBundle/src/Action/Ticket/SetPersonsCommand.php index 69ba61977..aab0b968b 100644 --- a/src/Bundle/ChillTicketBundle/src/Action/Ticket/SetPersonsCommand.php +++ b/src/Bundle/ChillTicketBundle/src/Action/Ticket/SetPersonsCommand.php @@ -12,9 +12,11 @@ declare(strict_types=1); namespace Chill\TicketBundle\Action\Ticket; use Chill\PersonBundle\Entity\Person; +use Chill\TicketBundle\Validation\Validator\SetPersonCommandConstraint; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Validator\Constraints\GreaterThan; +#[SetPersonCommandConstraint] class SetPersonsCommand { public function __construct( diff --git a/src/Bundle/ChillTicketBundle/src/Validation/Validator/SetPersonCommandConstraint.php b/src/Bundle/ChillTicketBundle/src/Validation/Validator/SetPersonCommandConstraint.php new file mode 100644 index 000000000..402a0d650 --- /dev/null +++ b/src/Bundle/ChillTicketBundle/src/Validation/Validator/SetPersonCommandConstraint.php @@ -0,0 +1,25 @@ +isMulti = 'multi' === $parameterBag->get('chill_ticket')['ticket']['person_per_ticket']; + } + + public function validate($value, Constraint $constraint) + { + if (!$value instanceof SetPersonsCommand) { + throw new UnexpectedValueException($value, SetPersonsCommand::class); + } + + if (!$constraint instanceof SetPersonCommandConstraint) { + throw new UnexpectedValueException($constraint, SetPersonCommandConstraint::class); + } + + if (!$this->isMulti) { + if (1 < count($value->persons)) { + $this->context->buildViolation($constraint->notMulti) + ->atPath('persons') + ->addViolation(); + } + } + } +} diff --git a/src/Bundle/ChillTicketBundle/src/config/services.yaml b/src/Bundle/ChillTicketBundle/src/config/services.yaml index e5618284a..7e3d6050b 100644 --- a/src/Bundle/ChillTicketBundle/src/config/services.yaml +++ b/src/Bundle/ChillTicketBundle/src/config/services.yaml @@ -23,5 +23,8 @@ services: Chill\TicketBundle\Menu\: resource: '../Menu/' + Chill\TicketBundle\Validation\: + resource: '../Validation/' + Chill\TicketBundle\DataFixtures\: resource: '../DataFixtures/' diff --git a/src/Bundle/ChillTicketBundle/tests/Validation/Validator/SetPersonCommandConstraintValidatorTest.php b/src/Bundle/ChillTicketBundle/tests/Validation/Validator/SetPersonCommandConstraintValidatorTest.php new file mode 100644 index 000000000..fd164354a --- /dev/null +++ b/src/Bundle/ChillTicketBundle/tests/Validation/Validator/SetPersonCommandConstraintValidatorTest.php @@ -0,0 +1,90 @@ + ['ticket' => ['person_per_ticket' => $this->personPerTicket]]]) + ); + } + + /** + * Helper method to set the configuration and recreate the validator. + */ + private function setPersonPerTicket(string $personPerTicket): void + { + $this->personPerTicket = $personPerTicket; + $this->validator = $this->createValidator(); + $this->validator->initialize($this->context); + } + + public function testValidatorWithManyPersonsPerTicket(): void + { + $this->setPersonPerTicket('multi'); + + $command = new SetPersonsCommand([new Person(), new Person()]); + + $this->validator->validate($command, new SetPersonCommandConstraint()); + + $this->assertNoViolation(); + } + + public function testValidatorWithSinglePersonPerTicket(): void + { + $this->setPersonPerTicket('single'); + + $command = new SetPersonsCommand([new Person(), new Person()]); + + $this->validator->validate($command, $command = new SetPersonCommandConstraint()); + + $this->buildViolation($command->notMulti)->atPath('property.path.persons')->assertRaised(); + } + + public function testValidatorWithSinglePersonPerTicketAndEmptyPersons(): void + { + $this->setPersonPerTicket('single'); + + $command = new SetPersonsCommand([]); + + $this->validator->validate($command, new SetPersonCommandConstraint()); + + $this->assertNoViolation(); + } + + public function testValidatorWithSinglePersonPerTicketAndOnlyOnePerson(): void + { + $this->setPersonPerTicket('single'); + + $command = new SetPersonsCommand([new Person()]); + + $this->validator->validate($command, new SetPersonCommandConstraint()); + + $this->assertNoViolation(); + } +}