mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-09 10:26:14 +00:00
Add SetPersonCommandConstraint and its validator with test coverage for ChillTicketBundle
This commit is contained in:
parent
2b22d4cb7c
commit
42d6c9e672
@ -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(
|
||||
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\TicketBundle\Validation\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
#[\Attribute]
|
||||
class SetPersonCommandConstraint extends Constraint
|
||||
{
|
||||
public string $notMulti = 'ticket.set_persons.Only one person can be set.';
|
||||
|
||||
public function getTargets(): string
|
||||
{
|
||||
return Constraint::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\TicketBundle\Validation\Validator;
|
||||
|
||||
use Chill\TicketBundle\Action\Ticket\SetPersonsCommand;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
|
||||
class SetPersonCommandConstraintValidator extends ConstraintValidator
|
||||
{
|
||||
private bool $isMulti;
|
||||
|
||||
public function __construct(ParameterBagInterface $parameterBag)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -23,5 +23,8 @@ services:
|
||||
Chill\TicketBundle\Menu\:
|
||||
resource: '../Menu/'
|
||||
|
||||
Chill\TicketBundle\Validation\:
|
||||
resource: '../Validation/'
|
||||
|
||||
Chill\TicketBundle\DataFixtures\:
|
||||
resource: '../DataFixtures/'
|
||||
|
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Validation\Validator;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\TicketBundle\Action\Ticket\SetPersonsCommand;
|
||||
use Chill\TicketBundle\Validation\Validator\SetPersonCommandConstraint;
|
||||
use Chill\TicketBundle\Validation\Validator\SetPersonCommandConstraintValidator;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class SetPersonCommandConstraintValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
private string $personPerTicket = 'many';
|
||||
|
||||
protected function createValidator()
|
||||
{
|
||||
return new SetPersonCommandConstraintValidator(
|
||||
new ParameterBag(['chill_ticket' => ['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();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user