336 lines
11 KiB
PHP
336 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Validator\Constraints;
|
|
|
|
use App\BusProNet\Model\CrmSelection;
|
|
use App\BusProNet\Model\CrmSelectionGroup;
|
|
use App\BusProNet\Model\Pickup;
|
|
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\Validator\Constraints\Participant;
|
|
use App\Validator\Constraints\ParticipantValidator;
|
|
use App\Validator\SelectionBasedBodyDimensionExemption;
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
|
|
|
class ParticipantValidatorTest extends ConstraintValidatorTestCase
|
|
{
|
|
protected function createValidator(): ParticipantValidator
|
|
{
|
|
return new ParticipantValidator(
|
|
bodyDimensionRanges: [],
|
|
bodyDimensionExemption: new SelectionBasedBodyDimensionExemption()
|
|
);
|
|
}
|
|
|
|
public function testParticipantWithRentalsAndBodyMeasurementsPassesValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [$this->createMockService()];
|
|
$participant->height = '170';
|
|
$participant->weight = '70';
|
|
$participant->shoeSize = '42';
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithRentalsAndMissingHeightPassesValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [$this->createMockService()];
|
|
$participant->height = null;
|
|
$participant->weight = '70';
|
|
$participant->shoeSize = '42';
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithRentalsAndMissingWeightPassesValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [$this->createMockService()];
|
|
$participant->height = '170';
|
|
$participant->weight = null;
|
|
$participant->shoeSize = '42';
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithRentalsAndMissingShoeSizePassesValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [$this->createMockService()];
|
|
$participant->height = '170';
|
|
$participant->weight = '70';
|
|
$participant->shoeSize = null;
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithoutRentalsAndNoBodyMeasurementsPassesValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [];
|
|
$participant->height = null;
|
|
$participant->weight = null;
|
|
$participant->shoeSize = null;
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithoutRentalsAndOutOfRangeBodyMeasurementsPassValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [];
|
|
$participant->height = '999';
|
|
$participant->weight = '999';
|
|
$participant->shoeSize = '999';
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithRentalsAndOutOfRangeShoeSizeFailsValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [$this->createMockService()];
|
|
$participant->height = '170';
|
|
$participant->weight = '70';
|
|
$participant->shoeSize = '51';
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->buildViolation('Bitte gib eine Zahl zwischen 35 und 50 ein. Falls du außerhalb dieses Bereichs bist, ruf gerne unser Kundenoffice an.')
|
|
->atPath('property.path.shoeSize')
|
|
->assertRaised();
|
|
}
|
|
|
|
public function testParticipantWithOutOfRangeBodyDimensionsFailsValidationWithoutSelection1475(): void
|
|
{
|
|
$wrapper = $this->createWrapperWithParticipantAndTravelSelections(
|
|
$this->createValidParticipantWithOutOfRangeBodyDimensions(),
|
|
[2001]
|
|
);
|
|
|
|
$this->setRoot($wrapper);
|
|
$this->validator->validate($wrapper->participant, new Participant());
|
|
|
|
$this->assertCount(3, $this->context->getViolations());
|
|
}
|
|
|
|
public function testParticipantWithOutOfRangeBodyDimensionsPassesValidationInCreateModeWithSelection1475(): void
|
|
{
|
|
$wrapper = $this->createWrapperWithParticipantAndTravelSelections(
|
|
$this->createValidParticipantWithOutOfRangeBodyDimensions(),
|
|
[1475]
|
|
);
|
|
|
|
$this->setRoot($wrapper);
|
|
$this->validator->validate($wrapper->participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithOutOfRangeBodyDimensionsPassesValidationInEditModeWithSelection1475(): void
|
|
{
|
|
$wrapper = $this->createWrapperWithParticipantAndTravelSelections(
|
|
$this->createValidParticipantWithOutOfRangeBodyDimensions(),
|
|
[1475],
|
|
true
|
|
);
|
|
|
|
$this->setRoot($wrapper);
|
|
$this->validator->validate($wrapper->participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testCanceledParticipantSkipsValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->status = 'S'; // Canceled
|
|
$participant->transportationOutbound = null;
|
|
$participant->transportationInbound = null;
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithBusTransportationButNoPickupFailsValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
|
|
$busService = new Service();
|
|
$busService->subType = 'BUS';
|
|
$participant->transportationOutbound = $busService;
|
|
$participant->transportationInbound = $this->createMockService();
|
|
$participant->pickup = null;
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->buildViolation('Bitte auswählen')
|
|
->atPath('property.path.pickup')
|
|
->assertRaised();
|
|
}
|
|
|
|
public function testParticipantWithInboundBusOnlyAndNoPickupPassesValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->transportationOutbound = $this->createMockService();
|
|
|
|
$busService = new Service();
|
|
$busService->subType = 'BUS';
|
|
$participant->transportationInbound = $busService;
|
|
|
|
$participant->pickup = null;
|
|
$participant->dropOff = $this->createMockPickup();
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testParticipantWithPkwOutboundBusInboundRequiresDropOffButNotPickup(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
|
|
$carService = new Service();
|
|
$carService->subType = 'PKW';
|
|
$participant->transportationOutbound = $carService;
|
|
|
|
$busService = new Service();
|
|
$busService->subType = 'BUS';
|
|
$participant->transportationInbound = $busService;
|
|
|
|
$participant->pickup = null;
|
|
$participant->dropOff = null;
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->buildViolation('Bitte auswählen')
|
|
->atPath('property.path.dropOff')
|
|
->assertRaised();
|
|
$this->assertCount(1, $this->context->getViolations());
|
|
}
|
|
|
|
public function testParticipantWithNonBusTransportationPassesPickupValidation(): void
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
|
|
$trainService = new Service();
|
|
$trainService->subType = 'TRAIN';
|
|
$participant->transportationOutbound = $trainService;
|
|
$participant->pickup = null; // Not required for non-bus
|
|
|
|
$this->validator->validate($participant, new Participant());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
private function createValidParticipant(): ParticipantDto
|
|
{
|
|
$participant = new ParticipantDto();
|
|
|
|
// Set valid transportation to avoid transportation validation errors
|
|
$participant->transportationOutbound = $this->createMockService();
|
|
$participant->transportationInbound = $this->createMockService();
|
|
|
|
// Set status to active
|
|
$participant->status = 'F';
|
|
|
|
return $participant;
|
|
}
|
|
|
|
private function createMockService(): Service
|
|
{
|
|
$service = new Service();
|
|
$service->id = 123;
|
|
$service->label = 'Test Service';
|
|
$service->subType = 'TRAIN'; // Non-bus to avoid pickup validation
|
|
|
|
return $service;
|
|
}
|
|
|
|
private function createMockPickup(): Pickup
|
|
{
|
|
$pickup = new Pickup();
|
|
$pickup->id = 123;
|
|
$pickup->city = 'Test City';
|
|
|
|
return $pickup;
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $selectionIds
|
|
*/
|
|
private function createWrapperWithParticipantAndTravelSelections(
|
|
ParticipantDto $participant,
|
|
array $selectionIds,
|
|
bool $editMode = false,
|
|
): ParticipantEditDto {
|
|
$travel = new Travel();
|
|
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
|
$travel->dateTo = new \DateTimeImmutable('2030-01-08');
|
|
$travel->selectionGroups = [
|
|
1 => $this->createSelectionGroup(1, $selectionIds),
|
|
];
|
|
|
|
$bookingDto = new BookingDto($travel, 1);
|
|
$bookingDto->participants = [$participant];
|
|
|
|
if (true === $editMode) {
|
|
$bookingDto->booking = new \App\BusProNet\Model\Booking();
|
|
}
|
|
|
|
return new ParticipantEditDto(
|
|
participant: $participant,
|
|
bookingContext: $bookingDto,
|
|
);
|
|
}
|
|
|
|
private function createValidParticipantWithOutOfRangeBodyDimensions(): ParticipantDto
|
|
{
|
|
$participant = $this->createValidParticipant();
|
|
$participant->rentals = [$this->createMockService()];
|
|
$participant->height = '999';
|
|
$participant->weight = '999';
|
|
$participant->shoeSize = '999';
|
|
|
|
return $participant;
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $selectionIds
|
|
*/
|
|
private function createSelectionGroup(int $groupId, array $selectionIds): CrmSelectionGroup
|
|
{
|
|
$group = new CrmSelectionGroup();
|
|
$group->id = $groupId;
|
|
$group->selections = [];
|
|
|
|
foreach ($selectionIds as $selectionId) {
|
|
$selection = new CrmSelection();
|
|
$selection->id = $selectionId;
|
|
$group->selections[] = $selection;
|
|
}
|
|
|
|
return $group;
|
|
}
|
|
}
|