feat: special treatment of participants with age 0-2 (babies)
This commit is contained in:
@@ -363,6 +363,208 @@ class ParticipantEditDtoTest extends TestCase
|
||||
$this->assertCount(0, $violations5);
|
||||
}
|
||||
|
||||
// =========================================
|
||||
// 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 = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
|
||||
$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 \App\BusProNet\Model\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 = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
|
||||
$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 \App\BusProNet\Model\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 testSkiPassValidationSkippedInEditMode(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create mock booking to simulate edit mode
|
||||
$mockBooking = new \App\BusProNet\Model\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 \App\BusProNet\Model\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 testSkiPassAgeCalculatedAtTravelDate(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
|
||||
$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 \App\BusProNet\Model\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');
|
||||
}
|
||||
|
||||
private function createBookingDtoWithParticipants(array $participants): BookingDto
|
||||
{
|
||||
$travel = new Travel();
|
||||
@@ -465,4 +667,56 @@ class ParticipantEditDtoTest extends TestCase
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
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\Service\ParticipantEligibilityService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ParticipantEligibilityServiceTest extends TestCase
|
||||
{
|
||||
private ParticipantEligibilityService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->service = new ParticipantEligibilityService();
|
||||
}
|
||||
|
||||
public function testBabyParticipantIsAlwaysEligible(): void
|
||||
{
|
||||
$travel = $this->createTravelWithNoSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create a baby participant (1 year old at travel date)
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-1 year');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertTrue($result, 'Baby participant (1 year old) should be eligible regardless of ski pass availability');
|
||||
}
|
||||
|
||||
public function testTwoYearOldParticipantIsEligibleAsBaby(): void
|
||||
{
|
||||
$travel = $this->createTravelWithNoSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create a participant exactly at BABY_MAX_AGE (2 years old at travel date)
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-2 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertTrue($result, 'Participant at BABY_MAX_AGE (2 years) should be eligible');
|
||||
}
|
||||
|
||||
public function testThreeYearOldParticipantIsNotBabyEligible(): void
|
||||
{
|
||||
$travel = $this->createTravelWithNoSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create a participant just over BABY_MAX_AGE (3 years old at travel date)
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-3 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertFalse($result, 'Participant over BABY_MAX_AGE (3 years) should not be eligible without ski passes');
|
||||
}
|
||||
|
||||
public function testAdultParticipantWithSkiPassIsEligible(): void
|
||||
{
|
||||
$travel = $this->createTravelWithSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create an adult participant (25 years old at travel date)
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertTrue($result, 'Adult participant with available ski passes should be eligible');
|
||||
}
|
||||
|
||||
public function testParticipantWithoutDateOfBirthIsNotEligible(): void
|
||||
{
|
||||
$travel = $this->createTravelWithSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create a participant without date of birth
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = null;
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertFalse($result, 'Participant without date of birth should not be eligible');
|
||||
}
|
||||
|
||||
public function testNewbornBabyIsEligible(): void
|
||||
{
|
||||
$travel = $this->createTravelWithNoSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create a newborn (born on travel date)
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom;
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertTrue($result, 'Newborn baby (0 years old) should be eligible');
|
||||
}
|
||||
|
||||
public function testBabyAgeIsCalculatedAtTravelDate(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
$travel->additionalServices = [];
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create a participant who will be 2 at travel date but 3 now
|
||||
// Born 2023-06-02, travel date 2025-06-01 = 1 year 364 days = 1 year old
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = new \DateTimeImmutable('2023-06-02');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertTrue($result, 'Age should be calculated at travel date, not current date');
|
||||
}
|
||||
|
||||
private function createTravelWithNoSkiPasses(): Travel
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('+30 days');
|
||||
$travel->dateTo = new \DateTimeImmutable('+37 days');
|
||||
$travel->additionalServices = [];
|
||||
|
||||
return $travel;
|
||||
}
|
||||
|
||||
private function createTravelWithSkiPasses(): Travel
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('+30 days');
|
||||
$travel->dateTo = new \DateTimeImmutable('+37 days');
|
||||
|
||||
// Create a ski pass service available for all ages
|
||||
$skiPass = new Service();
|
||||
$skiPass->id = 1;
|
||||
$skiPass->label = 'Test Ski Pass';
|
||||
$skiPass->subType = Constants::TOKEN_SKI_PASS;
|
||||
$skiPass->available = 10;
|
||||
$skiPass->price = 100.0;
|
||||
$skiPass->ageConstraintType = null; // No age constraint
|
||||
$skiPass->dateFrom = $travel->dateFrom;
|
||||
$skiPass->dateTo = $travel->dateTo;
|
||||
|
||||
$travel->additionalServices = [1 => $skiPass];
|
||||
|
||||
return $travel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user