fix: validate mandatory services selection
This commit is contained in:
@@ -19,6 +19,7 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
|||||||
*/
|
*/
|
||||||
#[AppAssert\PurchaseVoucher(groups: ['booking_create', 'booking_edit'])]
|
#[AppAssert\PurchaseVoucher(groups: ['booking_create', 'booking_edit'])]
|
||||||
#[AppAssert\PromoVoucher(groups: ['booking_create', 'booking_edit'])]
|
#[AppAssert\PromoVoucher(groups: ['booking_create', 'booking_edit'])]
|
||||||
|
#[AppAssert\MandatoryAdditionalServicesSelected(groups: ['booking_create', 'booking_edit'])]
|
||||||
class ParticipantEditDto
|
class ParticipantEditDto
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that all mandatory additional services are selected.
|
||||||
|
*
|
||||||
|
* Applied on ParticipantEditDto to enforce mandatory additional services based
|
||||||
|
* on participant age and booking eligibility in both create and edit flows.
|
||||||
|
*/
|
||||||
|
#[\Attribute]
|
||||||
|
class MandatoryAdditionalServicesSelected extends Constraint
|
||||||
|
{
|
||||||
|
public string $message = 'Bitte wähle alle Pflichtleistungen aus.';
|
||||||
|
|
||||||
|
public function getTargets(): array|string
|
||||||
|
{
|
||||||
|
return static::CLASS_CONSTRAINT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Validator\Constraints;
|
||||||
|
|
||||||
|
use App\BusProNet\Constants;
|
||||||
|
use App\BusProNet\Model\Service;
|
||||||
|
use App\Form\Model\ParticipantEditDto;
|
||||||
|
use App\Form\Service\ServiceAgeEvaluator;
|
||||||
|
use App\Service\ParticipantEligibilityService;
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enforces selection of age-eligible mandatory additional services.
|
||||||
|
*/
|
||||||
|
class MandatoryAdditionalServicesSelectedValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly ParticipantEligibilityService $participantEligibilityService,
|
||||||
|
private readonly ServiceAgeEvaluator $serviceAgeEvaluator,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate(mixed $value, Constraint $constraint): void
|
||||||
|
{
|
||||||
|
if (false === $constraint instanceof MandatoryAdditionalServicesSelected) {
|
||||||
|
throw new UnexpectedTypeException($constraint, MandatoryAdditionalServicesSelected::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false === $value instanceof ParticipantEditDto) {
|
||||||
|
throw new UnexpectedTypeException($value, ParticipantEditDto::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
$participant = $value->participant;
|
||||||
|
if (true === $participant->isCanceled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$participantIndex = $participant->index ?? 0;
|
||||||
|
if (false === $this->participantEligibilityService->isParticipantEligible($value->bookingContext, $participantIndex)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$mandatoryAdditionalServices = array_filter(
|
||||||
|
$value->bookingContext->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_ADDITIONAL),
|
||||||
|
function (Service $service) use ($value, $participantIndex): bool {
|
||||||
|
if (true !== $service->mandatory) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true === $this->serviceAgeEvaluator->canEvaluate($service)) {
|
||||||
|
return $this->serviceAgeEvaluator->isServiceAvailableForParticipant(
|
||||||
|
$service,
|
||||||
|
$value->bookingContext,
|
||||||
|
$participantIndex
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (true === empty($mandatoryAdditionalServices)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$selectedAdditionalServiceIds = array_map(
|
||||||
|
static fn (Service $service): int => $service->id,
|
||||||
|
$participant->additionalServices
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($mandatoryAdditionalServices as $mandatoryService) {
|
||||||
|
if (false === in_array($mandatoryService->id, $selectedAdditionalServiceIds, true)) {
|
||||||
|
$this->context->buildViolation($constraint->message)
|
||||||
|
->atPath('participant.additionalServices')
|
||||||
|
->addViolation();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Tests\Validator\Constraints;
|
||||||
|
|
||||||
|
use App\BusProNet\Constants;
|
||||||
|
use App\BusProNet\Model\Service;
|
||||||
|
use App\BusProNet\Model\Travel;
|
||||||
|
use App\Form\Model\BookingDto;
|
||||||
|
use App\Form\Model\ParticipantDto;
|
||||||
|
use App\Form\Model\ParticipantEditDto;
|
||||||
|
use App\Form\Service\ServiceAgeEvaluator;
|
||||||
|
use App\Service\ParticipantEligibilityService;
|
||||||
|
use App\Validator\Constraints\MandatoryAdditionalServicesSelected;
|
||||||
|
use App\Validator\Constraints\MandatoryAdditionalServicesSelectedValidator;
|
||||||
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||||
|
|
||||||
|
class MandatoryAdditionalServicesSelectedValidatorTest extends ConstraintValidatorTestCase
|
||||||
|
{
|
||||||
|
private ParticipantEligibilityService $participantEligibilityService;
|
||||||
|
private bool $participantEligible = true;
|
||||||
|
|
||||||
|
protected function createValidator(): MandatoryAdditionalServicesSelectedValidator
|
||||||
|
{
|
||||||
|
$this->participantEligibilityService = $this->createMock(ParticipantEligibilityService::class);
|
||||||
|
$this->participantEligibilityService
|
||||||
|
->method('isParticipantEligible')
|
||||||
|
->willReturnCallback(fn (): bool => $this->participantEligible);
|
||||||
|
|
||||||
|
return new MandatoryAdditionalServicesSelectedValidator(
|
||||||
|
$this->participantEligibilityService,
|
||||||
|
new ServiceAgeEvaluator()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMissingMandatoryAdditionalServiceCreatesViolation(): void
|
||||||
|
{
|
||||||
|
$participantEditDto = $this->createWrapperWithParticipant(
|
||||||
|
$this->createParticipant(),
|
||||||
|
[
|
||||||
|
$this->createMandatoryAdditionalService(194695, 'Ortstaxe'),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->validator->validate($participantEditDto, new MandatoryAdditionalServicesSelected());
|
||||||
|
|
||||||
|
$this->buildViolation('Bitte wähle alle Pflichtleistungen aus.')
|
||||||
|
->atPath('property.path.participant.additionalServices')
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSelectedMandatoryAdditionalServicePassesValidation(): void
|
||||||
|
{
|
||||||
|
$mandatoryService = $this->createMandatoryAdditionalService(194695, 'Ortstaxe');
|
||||||
|
|
||||||
|
$participant = $this->createParticipant();
|
||||||
|
$participant->additionalServices = [$mandatoryService];
|
||||||
|
|
||||||
|
$participantEditDto = $this->createWrapperWithParticipant($participant, [$mandatoryService]);
|
||||||
|
|
||||||
|
$this->validator->validate($participantEditDto, new MandatoryAdditionalServicesSelected());
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAgeEligibleMandatoryServiceMissingCreatesViolation(): void
|
||||||
|
{
|
||||||
|
$adultMandatory = $this->createMandatoryAdditionalService(
|
||||||
|
id: 2,
|
||||||
|
label: 'Adult Mandatory',
|
||||||
|
ageConstraintType: 'absolute_age',
|
||||||
|
ageFrom: 18,
|
||||||
|
ageTo: 99
|
||||||
|
);
|
||||||
|
$teenMandatory = $this->createMandatoryAdditionalService(
|
||||||
|
id: 1,
|
||||||
|
label: 'Teen Mandatory',
|
||||||
|
ageConstraintType: 'absolute_age',
|
||||||
|
ageFrom: 12,
|
||||||
|
ageTo: 17
|
||||||
|
);
|
||||||
|
|
||||||
|
$participant = $this->createParticipant();
|
||||||
|
$participant->additionalServices = [$teenMandatory];
|
||||||
|
$participant->dateOfBirth = new \DateTimeImmutable('2000-01-01');
|
||||||
|
|
||||||
|
$participantEditDto = $this->createWrapperWithParticipant($participant, [$teenMandatory, $adultMandatory]);
|
||||||
|
|
||||||
|
$this->validator->validate($participantEditDto, new MandatoryAdditionalServicesSelected());
|
||||||
|
|
||||||
|
$this->buildViolation('Bitte wähle alle Pflichtleistungen aus.')
|
||||||
|
->atPath('property.path.participant.additionalServices')
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanceledParticipantSkipsValidation(): void
|
||||||
|
{
|
||||||
|
$participant = $this->createParticipant();
|
||||||
|
$participant->status = 'S';
|
||||||
|
|
||||||
|
$participantEditDto = $this->createWrapperWithParticipant(
|
||||||
|
$participant,
|
||||||
|
[$this->createMandatoryAdditionalService(194695, 'Ortstaxe')]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->validator->validate($participantEditDto, new MandatoryAdditionalServicesSelected());
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIneligibleParticipantSkipsValidation(): void
|
||||||
|
{
|
||||||
|
$this->participantEligible = false;
|
||||||
|
|
||||||
|
$participantEditDto = $this->createWrapperWithParticipant(
|
||||||
|
$this->createParticipant(),
|
||||||
|
[$this->createMandatoryAdditionalService(194695, 'Ortstaxe')]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->validator->validate($participantEditDto, new MandatoryAdditionalServicesSelected());
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createWrapperWithParticipant(ParticipantDto $participant, array $additionalServices): ParticipantEditDto
|
||||||
|
{
|
||||||
|
$travel = new Travel();
|
||||||
|
$travel->dateFrom = new \DateTimeImmutable('2026-04-11');
|
||||||
|
$travel->additionalServices = array_reduce(
|
||||||
|
$additionalServices,
|
||||||
|
function (array $carry, Service $service): array {
|
||||||
|
$carry[$service->id] = $service;
|
||||||
|
|
||||||
|
return $carry;
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
$bookingDto = new BookingDto($travel, 1);
|
||||||
|
$bookingDto->participants = [$participant];
|
||||||
|
|
||||||
|
return new ParticipantEditDto(
|
||||||
|
participant: $participant,
|
||||||
|
bookingContext: $bookingDto,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createParticipant(): ParticipantDto
|
||||||
|
{
|
||||||
|
$participant = new ParticipantDto();
|
||||||
|
$participant->index = 0;
|
||||||
|
$participant->status = 'F';
|
||||||
|
$participant->dateOfBirth = new \DateTimeImmutable('2005-01-01');
|
||||||
|
$participant->additionalServices = [];
|
||||||
|
|
||||||
|
return $participant;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createMandatoryAdditionalService(
|
||||||
|
int $id,
|
||||||
|
string $label,
|
||||||
|
?string $ageConstraintType = null,
|
||||||
|
?int $ageFrom = null,
|
||||||
|
?int $ageTo = null,
|
||||||
|
): Service {
|
||||||
|
$service = new Service();
|
||||||
|
$service->id = $id;
|
||||||
|
$service->label = $label;
|
||||||
|
$service->subType = Constants::TOKEN_ADDITIONAL;
|
||||||
|
$service->mandatory = true;
|
||||||
|
$service->ageConstraintType = $ageConstraintType;
|
||||||
|
$service->ageFrom = $ageFrom;
|
||||||
|
$service->ageTo = $ageTo;
|
||||||
|
|
||||||
|
return $service;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user