Files
myep/tests/Form/Model/ParticipantEditDtoTest.php
T

1040 lines
41 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Form\Model;
use App\BusProNet\Constants;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Address;
use App\Form\Model\AddressDto;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Insurance;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Model\ParticipantEditDto;
use App\Form\Service\ServiceAgeEvaluator;
use App\Service\BookingPriceCalculator;
use App\Service\ParticipantEligibilityChecker;
use App\Service\ServiceAvailabilityCalculator;
use App\Service\VoucherValidator;
use App\Validator\Constraints\MandatoryAdditionalServicesSelectedValidator;
use App\Validator\Constraints\PromoVoucherValidator;
use App\Validator\Constraints\PurchaseVoucherValidator;
use App\Validator\Constraints\SkiPassSelectionValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EmailValidator;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Tests for ParticipantEditDto email uniqueness validation.
*
* This test suite validates the wrapper DTO pattern used for participant email
* uniqueness validation during form submission. The wrapper enables clean validation
* without requiring back-references or form coupling.
*/
class ParticipantEditDtoTest extends TestCase
{
private ValidatorInterface $validator;
protected function setUp(): void
{
// Create mock services for validator dependencies
$mockVoucherService = $this->createMock(VoucherValidator::class);
$mockPriceCalculatorService = $this->createMock(BookingPriceCalculator::class);
$mockParticipantEligibilityChecker = $this->createMock(ParticipantEligibilityChecker::class);
$mockServiceAgeEvaluator = $this->createMock(ServiceAgeEvaluator::class);
// Ski pass validation is exercised against the real gate, not a mock: which participant
// needs a pass is exactly the behaviour under test here.
$participantEligibilityChecker = new ParticipantEligibilityChecker(
new ServiceAgeEvaluator(),
new ServiceAvailabilityCalculator()
);
// Create custom validator factory that can inject dependencies
$validatorFactory = new class($mockVoucherService, $mockPriceCalculatorService, $mockParticipantEligibilityChecker, $mockServiceAgeEvaluator, $participantEligibilityChecker) implements ConstraintValidatorFactoryInterface {
public function __construct(
private readonly VoucherValidator $voucherService,
private readonly BookingPriceCalculator $priceCalculatorService,
private readonly ParticipantEligibilityChecker $participantEligibilityService,
private readonly ServiceAgeEvaluator $serviceAgeEvaluator,
private readonly ParticipantEligibilityChecker $realEligibilityChecker,
) {
}
public function getInstance(Constraint $constraint): ConstraintValidatorInterface
{
$className = $constraint->validatedBy();
if (PurchaseVoucherValidator::class === $className) {
return new PurchaseVoucherValidator($this->voucherService);
}
if (PromoVoucherValidator::class === $className) {
return new PromoVoucherValidator($this->voucherService, $this->priceCalculatorService);
}
if (EmailValidator::class === $className) {
return new EmailValidator(Email::VALIDATION_MODE_HTML5);
}
if (SkiPassSelectionValidator::class === $className) {
return new SkiPassSelectionValidator($this->realEligibilityChecker);
}
if (MandatoryAdditionalServicesSelectedValidator::class === $className) {
return new MandatoryAdditionalServicesSelectedValidator(
$this->participantEligibilityService,
$this->serviceAgeEvaluator
);
}
return new $className();
}
};
$this->validator = Validation::createValidatorBuilder()
->enableAttributeMapping()
->setConstraintValidatorFactory($validatorFactory)
->getValidator();
}
public function testAdultWithUniqueEmailPassesValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createValidAdultParticipant('[email protected]'),
$this->createValidAdultParticipant('[email protected]'),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(0, $violations);
}
public function testAdultWithDuplicateEmailFailsValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
// Validate second participant (index 1) - applicant (index 0) is exempt from email uniqueness
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(1, $violations);
$this->assertSame('participant.email', $violations->get(0)->getPropertyPath());
$this->assertSame(
'Diese E-Mail Adresse wird bereits von einem anderen Teilnehmer verwendet',
$violations->get(0)->getMessage()
);
}
public function testChildWithDuplicateEmailPassesValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createChildParticipant('[email protected]'),
]);
// Validate the child participant (index 1)
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(0, $violations);
}
public function testEmptyEmailPassesValidation(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant(''),
$this->createAdultParticipant(''),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
// Should not trigger uniqueness validation (handled by @Email and @NotBlank constraints)
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// May have other violations, but not uniqueness
$hasUniquenessViolation = false;
foreach ($violations as $violation) {
if (str_contains($violation->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')) {
$hasUniquenessViolation = true;
break;
}
}
$this->assertFalse($hasUniquenessViolation, 'Should not have uniqueness violation for empty emails');
}
public function testNullEmailPassesValidation(): void
{
$participant1 = $this->createAdultParticipant('[email protected]');
$participant1->email = null;
$participant2 = $this->createAdultParticipant('[email protected]');
$participant2->email = null;
$bookingDto = $this->createBookingDtoWithParticipants([$participant1, $participant2]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
// Should not trigger uniqueness validation (handled by @Email and @NotBlank constraints)
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// May have other violations, but not uniqueness
$hasUniquenessViolation = false;
foreach ($violations as $violation) {
if (str_contains($violation->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')) {
$hasUniquenessViolation = true;
break;
}
}
$this->assertFalse($hasUniquenessViolation, 'Should not have uniqueness violation for null emails');
}
public function testCaseInsensitiveEmailComparison(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
// Validate second participant (index 1) - applicant (index 0) is exempt from email uniqueness
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// Should have uniqueness violation (case-insensitive comparison)
$uniquenessViolations = array_filter(
iterator_to_array($violations),
fn ($v) => str_contains($v->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')
);
$this->assertCount(1, $uniquenessViolations);
}
public function testWhitespaceNormalizationInEmailComparison(): void
{
$participant1 = $this->createAdultParticipant('[email protected]');
$participant2 = $this->createAdultParticipant('[email protected]');
$participant2->email = ' [email protected] ';
$bookingDto = $this->createBookingDtoWithParticipants([$participant1, $participant2]);
// Validate second participant (index 1) - applicant (index 0) is exempt from email uniqueness
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
// Should have uniqueness violation (whitespace trimmed during comparison)
$uniquenessViolations = array_filter(
iterator_to_array($violations),
fn ($v) => str_contains($v->getMessage(), 'wird bereits von einem anderen Teilnehmer verwendet')
);
$this->assertCount(1, $uniquenessViolations);
}
public function testMultipleDuplicatesDetectedCorrectly(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
// Validate first participant (applicant) - should pass (applicant is exempt from email uniqueness)
$wrapper1 = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations1 = $this->validator->validate($wrapper1, null, ['booking_create']);
$this->assertCount(0, $violations1);
// Validate second participant - should fail (duplicate with applicant and third participant)
$wrapper2 = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations2 = $this->validator->validate($wrapper2, null, ['booking_create']);
$this->assertCount(1, $violations2);
// Validate third participant - should fail (duplicate with applicant and second participant)
$wrapper3 = new ParticipantEditDto(
participant: $bookingDto->participants[2],
bookingContext: $bookingDto,
);
$violations3 = $this->validator->validate($wrapper3, null, ['booking_create']);
$this->assertCount(1, $violations3);
}
public function testSelfComparisonSkipped(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
]);
// Participant should not validate against itself
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(0, $violations);
}
public function testValidationInEditMode(): void
{
$travel = $this->createTravelOfferingSkiPasses();
$bookingDto = new BookingDto($travel, 1); // hotelId must be int
// Create mock booking to simulate edit mode
$mockBooking = new Booking();
$bookingDto->booking = $mockBooking;
// Create valid participants with all required fields for strict validation
$participant1 = $this->createAdultParticipant('[email protected]');
$participant1->index = 0;
$participant1->mutable = false; // Immutable - strict validation applies
$participant1->mobile = '+49 123 456789'; // Required for applicant
$participant1->address = new Address();
$participant1->address->street = 'Test Street 1';
$participant1->address->postCode = '12345';
$participant1->address->city = 'Test City';
$participant1->address->country = 'DE';
$participant2 = $this->createAdultParticipant('[email protected]');
$participant2->index = 1;
$participant2->mutable = false;
$bookingDto->participants = [$participant1, $participant2];
// Validate second participant (index 1) - applicant (index 0) is exempt from email uniqueness
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_edit']);
// Should have uniqueness violation (edit mode still validates email uniqueness for non-applicants)
$this->assertCount(1, $violations);
}
public function testMixedScenario(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'), // Unique - OK
$this->createAdultParticipant('[email protected]'), // Duplicate - ERROR
$this->createChildParticipant('[email protected]'), // Child with duplicate - OK
$this->createAdultParticipant('[email protected]'), // Duplicate - ERROR
$this->createChildParticipant('[email protected]'), // Unique child - OK
$this->createAdultParticipant('[email protected]'), // Unique - OK
]);
// Validate participant at index 0 - should pass (unique)
$wrapper0 = new ParticipantEditDto($bookingDto->participants[0], $bookingDto);
$violations0 = $this->validator->validate($wrapper0, null, ['booking_create']);
$this->assertCount(0, $violations0);
// Validate participant at index 1 - should fail (duplicate)
$wrapper1 = new ParticipantEditDto($bookingDto->participants[1], $bookingDto);
$violations1 = $this->validator->validate($wrapper1, null, ['booking_create']);
$this->assertCount(1, $violations1);
// Validate participant at index 2 - should pass (child exempt)
$wrapper2 = new ParticipantEditDto($bookingDto->participants[2], $bookingDto);
$violations2 = $this->validator->validate($wrapper2, null, ['booking_create']);
$this->assertCount(0, $violations2);
// Validate participant at index 3 - should fail (duplicate)
$wrapper3 = new ParticipantEditDto($bookingDto->participants[3], $bookingDto);
$violations3 = $this->validator->validate($wrapper3, null, ['booking_create']);
$this->assertCount(1, $violations3);
// Validate participant at index 4 - should pass (unique child)
$wrapper4 = new ParticipantEditDto($bookingDto->participants[4], $bookingDto);
$violations4 = $this->validator->validate($wrapper4, null, ['booking_create']);
$this->assertCount(0, $violations4);
// Validate participant at index 5 - should pass (unique)
$wrapper5 = new ParticipantEditDto($bookingDto->participants[5], $bookingDto);
$violations5 = $this->validator->validate($wrapper5, null, ['booking_create']);
$this->assertCount(0, $violations5);
}
public function testInternalAgencyBookingSkipsEmailUniqueness(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
// Mark as internal agency booking
$bookingDto->agencyCode = AgencyLoader::INTERNAL_AGENCY_CODE;
// Validate second participant (non-applicant adult with duplicate email)
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(0, $violations, 'Internal agency bookings should skip email uniqueness validation');
}
public function testNonAgencyBookingStillEnforcesEmailUniqueness(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipant('[email protected]'),
$this->createAdultParticipant('[email protected]'),
]);
// Non-agency booking (default agencyCode is null)
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['booking_create']);
$this->assertCount(1, $violations, 'Non-agency bookings should still enforce email uniqueness');
}
// =========================================
// Ski Pass Validation Tests (Baby Age Exemption)
// =========================================
public function testSkiPassRequiredForAdultInCreateMode(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createAdultParticipantWithoutSkiPass('[email protected]'),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
// Should have ski pass violation
$skiPassViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
);
$this->assertCount(1, $skiPassViolations, 'Adult should require ski pass in create mode');
}
public function testSkiPassNotRequiredForBabyInCreateMode(): void
{
$bookingDto = $this->createBookingDtoWithParticipants([
$this->createBabyParticipantWithoutSkiPass('[email protected]'),
]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
// Should NOT have ski pass violation for baby
$skiPassViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
);
$this->assertCount(0, $skiPassViolations, 'Baby (0-2 years) should not require ski pass');
}
public function testSkiPassNotRequiredForTwoYearOldInCreateMode(): void
{
$travel = $this->createTravelOfferingSkiPasses();
$bookingDto = new BookingDto($travel, 1);
// Create a participant exactly at BABY_MAX_AGE (2 years old at travel date)
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
$participant->dateOfBirth = $travel->dateFrom->modify('-2 years');
$participant->index = 0;
$participant->mobile = '+49 123 456789';
$participant->address = new Address();
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
$bookingDto->participants = [$participant];
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
// Should NOT have ski pass violation for 2-year-old
$skiPassViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
);
$this->assertCount(0, $skiPassViolations, 'Participant at BABY_MAX_AGE (2 years) should not require ski pass');
}
public function testSkiPassRequiredForThreeYearOldInCreateMode(): void
{
$travel = $this->createTravelOfferingSkiPasses();
$bookingDto = new BookingDto($travel, 1);
// Create a participant just over BABY_MAX_AGE (3 years old at travel date)
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
$participant->dateOfBirth = $travel->dateFrom->modify('-3 years');
$participant->index = 0;
$participant->mobile = '+49 123 456789';
$participant->address = new Address();
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
$bookingDto->participants = [$participant];
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
// Should have ski pass violation for 3-year-old
$skiPassViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
);
$this->assertCount(1, $skiPassViolations, 'Participant over BABY_MAX_AGE (3 years) should require ski pass');
}
public function testSkiPassNotRequiredWhenTravelOffersNone(): void
{
$travel = $this->createTravelWithoutSkiPasses();
$bookingDto = new BookingDto($travel, 1);
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
$participant->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$participant->index = 0;
$participant->mobile = '+49 123 456789';
$participant->address = new Address();
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
$bookingDto->participants = [$participant];
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
$skiPassViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
);
$this->assertCount(0, $skiPassViolations, 'A travel offering no ski passes must be bookable without one');
}
public function testIneligibleParticipantIsReportedAsNotBookable(): void
{
// Ski passes exist, but the only one starts at 18 - a 14 year old cannot obtain any
$travel = $this->createTravelOfferingSkiPasses(ageFrom: 18);
$bookingDto = new BookingDto($travel, 1);
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
$participant->dateOfBirth = $travel->dateFrom->modify('-14 years');
$participant->index = 0;
$participant->mobile = '+49 123 456789';
$participant->address = new Address();
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
$bookingDto->participants = [$participant];
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
$skiPassViolations = array_values(array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
));
$this->assertCount(1, $skiPassViolations);
$this->assertSame(
'Für Teilnehmer:in 1 ist keine Buchung möglich',
$skiPassViolations[0]->getMessage(),
'The user must not be asked to select from a field that is not rendered'
);
}
public function testSkiPassValidationSkippedInEditMode(): void
{
$travel = $this->createTravelOfferingSkiPasses();
$bookingDto = new BookingDto($travel, 1);
// Create mock booking to simulate edit mode
$mockBooking = new Booking();
$bookingDto->booking = $mockBooking;
// Create adult participant without ski pass in edit mode
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
$participant->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$participant->index = 0;
$participant->mutable = false;
$participant->mobile = '+49 123 456789';
$participant->address = new Address();
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
$bookingDto->participants = [$participant];
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
// Should NOT have ski pass violation in edit mode
$skiPassViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
);
$this->assertCount(0, $skiPassViolations, 'Ski pass validation should be skipped in edit mode');
}
public function testEditSubmissionValidatesApplicantAddressOnlyForFirstParticipant(): void
{
$travel = $this->createTravelOfferingSkiPasses();
$bookingDto = new BookingDto($travel, 1);
$bookingDto->booking = new Booking();
$bookingDto->agencyCode = AgencyLoader::INTERNAL_AGENCY_CODE;
$participant0 = $this->createAdultParticipant('[email protected]');
$participant0->index = 0;
$participant0->address = null;
$participant1 = $this->createAdultParticipant('[email protected]');
$participant1->index = 1;
$participant1->address = null;
$bookingDto->participants = [$participant0, $participant1];
$wrapper0 = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$wrapper1 = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations0 = $this->validator->validate($wrapper0, null, ['booking_edit', 'strict_required']);
$addressViolations0 = array_filter(
iterator_to_array($violations0),
fn ($v) => 'address' === $v->getPropertyPath() || 'participant.address' === $v->getPropertyPath()
);
$violations1 = $this->validator->validate($wrapper1, null, ['booking_edit', 'strict_required']);
$addressViolations1 = array_filter(
iterator_to_array($violations1),
fn ($v) => 'address' === $v->getPropertyPath() || 'participant.address' === $v->getPropertyPath()
);
$this->assertCount(1, $addressViolations0, 'Applicant address should still be required in edit submission');
$this->assertCount(0, $addressViolations1, 'Non-applicant address should not be required in edit submission');
}
public function testSkiPassAgeCalculatedAtTravelDate(): void
{
$travel = $this->createTravelOfferingSkiPasses();
$bookingDto = new BookingDto($travel, 1);
// Create a participant who will be 2 at travel date
// Born 2023-06-02, travel date 2025-06-01 = 1 year 364 days = 1 year old
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
$participant->dateOfBirth = new \DateTimeImmutable('2023-06-02');
$participant->index = 0;
$participant->mobile = '+49 123 456789';
$participant->address = new Address();
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
$bookingDto->participants = [$participant];
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[0],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
// Should NOT have ski pass violation (age calculated at travel date)
$skiPassViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
);
$this->assertCount(0, $skiPassViolations, 'Age should be calculated at travel date for baby exemption');
}
public function testDependentWithoutInsurancePassesWhenApplicantUsesBulkInsurance(): void
{
$applicant = $this->createAdultParticipant('[email protected]');
$applicant->bulkInsuranceBooking = true;
$dependent = $this->createAdultParticipant('[email protected]');
$dependent->insurance = null;
$bookingDto = $this->createBookingDtoWithParticipants([$applicant, $dependent]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
$insuranceViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.insurance' === $v->getPropertyPath()
);
$this->assertCount(0, $insuranceViolations);
}
public function testDependentWithoutInsuranceFailsWhenApplicantDoesNotUseBulkInsurance(): void
{
$applicant = $this->createAdultParticipant('[email protected]');
$applicant->bulkInsuranceBooking = false;
$dependent = $this->createAdultParticipant('[email protected]');
$dependent->insurance = null;
$bookingDto = $this->createBookingDtoWithParticipants([$applicant, $dependent]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
$insuranceViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.insurance' === $v->getPropertyPath()
);
$this->assertCount(1, $insuranceViolations);
}
public function testDependentWithoutInsurancePassesWhenApplicantHasFamilyInsurance(): void
{
$applicant = $this->createAdultParticipant('[email protected]');
$applicant->insurance = $this->createMockInsurance(familyInsurance: true);
$dependent = $this->createAdultParticipant('[email protected]');
$dependent->insurance = null;
$bookingDto = $this->createBookingDtoWithParticipants([$applicant, $dependent]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
$insuranceViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.insurance' === $v->getPropertyPath()
);
$this->assertCount(0, $insuranceViolations);
}
public function testDependentWithoutInsuranceFailsWhenApplicantHasNonFamilyInsurance(): void
{
$applicant = $this->createAdultParticipant('[email protected]');
$applicant->insurance = $this->createMockInsurance(familyInsurance: false);
$dependent = $this->createAdultParticipant('[email protected]');
$dependent->insurance = null;
$bookingDto = $this->createBookingDtoWithParticipants([$applicant, $dependent]);
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[1],
bookingContext: $bookingDto,
);
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
$insuranceViolations = array_filter(
iterator_to_array($violations),
fn ($v) => 'participant.insurance' === $v->getPropertyPath()
);
$this->assertCount(1, $insuranceViolations);
}
/**
* Builds the default travel fixture: a regular travel that offers one unconstrained,
* available ski pass. Ski pass validation depends on the travel actually offering
* passes, so a travel without them is a distinct fixture, not the default.
*/
private function createTravelOfferingSkiPasses(?int $ageFrom = null): Travel
{
$travel = $this->createTravelWithoutSkiPasses();
$skiPass = new Service();
$skiPass->id = 900;
$skiPass->label = 'Test Ski Pass';
$skiPass->subType = Constants::TOKEN_SKI_PASS;
$skiPass->available = 10;
$skiPass->price = 100.0;
$skiPass->ageConstraintType = null === $ageFrom ? null : 'absolute_age';
$skiPass->ageFrom = $ageFrom;
$travel->additionalServices = [900 => $skiPass];
return $travel;
}
private function createTravelWithoutSkiPasses(): Travel
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
return $travel;
}
private function createBookingDtoWithParticipants(array $participants): BookingDto
{
$travel = $this->createTravelOfferingSkiPasses();
$bookingDto = new BookingDto($travel, 1); // hotelId must be int
// Set index for each participant
foreach ($participants as $index => $participant) {
$participant->index = $index;
// First participant (applicant) needs address and mobile
if (0 === $index) {
$participant->mobile = '+49 123 456789';
if (null === $participant->address) {
$participant->address = new AddressDto();
}
$participant->address->street = 'Test Street 1';
$participant->address->postCode = '12345';
$participant->address->city = 'Test City';
$participant->address->country = 'DE';
}
}
$bookingDto->participants = $participants;
return $bookingDto;
}
private function createAdultParticipant(string $email): ParticipantDto
{
$participant = $this->createValidParticipant(
new \DateTimeImmutable('1990-01-01'), // Adult (over 18)
$email
);
// Add required fields for full validation
$participant->assignedRoomId = 1;
$participant->skiPass = $this->createMockService();
$participant->transportationOutbound = $this->createMockService();
$participant->transportationInbound = $this->createMockService();
$participant->insurance = $this->createMockInsurance();
return $participant;
}
private function createValidAdultParticipant(string $email): ParticipantDto
{
// Same as createAdultParticipant - all adults need full validation
return $this->createAdultParticipant($email);
}
private function createChildParticipant(string $email): ParticipantDto
{
$participant = $this->createValidParticipant(
new \DateTimeImmutable('2015-01-01'), // Child (under 16)
$email
);
// Children also need transportation (required for all participants)
$participant->assignedRoomId = 1;
$participant->skiPass = $this->createMockService();
$participant->transportationOutbound = $this->createMockService();
$participant->transportationInbound = $this->createMockService();
$participant->insurance = $this->createMockInsurance();
return $participant;
}
private function createValidParticipant(\DateTimeImmutable $dateOfBirth, string $email): ParticipantDto
{
$participant = new ParticipantDto();
$participant->firstName = 'John';
$participant->lastName = 'Doe';
$participant->dateOfBirth = $dateOfBirth;
$participant->email = $email;
return $participant;
}
private function createMockService(): Service
{
$service = new Service();
$service->id = 1;
$service->label = 'Test Service';
$service->subType = 'TEST';
$service->price = 0.0;
return $service;
}
private function createMockInsurance(bool $familyInsurance = false): Insurance
{
$insurance = new Insurance();
$insurance->id = '1';
$insurance->label = 'Test Insurance';
$insurance->price = 10.0;
$insurance->familyInsurance = $familyInsurance;
return $insurance;
}
private function createAdultParticipantWithoutSkiPass(string $email): ParticipantDto
{
$participant = $this->createValidParticipant(
new \DateTimeImmutable('1990-01-01'), // Adult (over 18)
$email
);
// Add required fields EXCEPT ski pass
$participant->assignedRoomId = 1;
$participant->skiPass = null; // No ski pass
$participant->transportationOutbound = $this->createMockService();
$participant->transportationInbound = $this->createMockService();
$participant->insurance = $this->createMockInsurance();
return $participant;
}
private function createBabyParticipantWithoutSkiPass(string $email): ParticipantDto
{
// Baby is 1 year old at travel date (2025-06-01)
$participant = $this->createValidParticipant(
new \DateTimeImmutable('2024-06-01'), // 1 year old at travel date
$email
);
// Add required fields EXCEPT ski pass (babies don't need ski pass)
$participant->assignedRoomId = 1;
$participant->skiPass = null; // No ski pass
$participant->transportationOutbound = $this->createMockService();
$participant->transportationInbound = $this->createMockService();
$participant->insurance = $this->createMockInsurance();
return $participant;
}
private function createParticipantWithoutSkiPass(string $email): ParticipantDto
{
$participant = new ParticipantDto();
$participant->firstName = 'John';
$participant->lastName = 'Doe';
$participant->email = $email;
// Add required fields EXCEPT ski pass
$participant->assignedRoomId = 1;
$participant->skiPass = null; // No ski pass
$participant->transportationOutbound = $this->createMockService();
$participant->transportationInbound = $this->createMockService();
$participant->insurance = $this->createMockInsurance();
return $participant;
}
}