feat: special treatment of services for participants of age 0-2

This commit is contained in:
Björn Fromme
2026-03-16 12:00:55 +01:00
parent 28c426ca3c
commit cca8554e3d
7 changed files with 635 additions and 18 deletions
@@ -0,0 +1,364 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\BusProNet\Utility\DirectionMapper;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\Factory\ParticipantRoomChoiceLoaderFactory;
use App\Form\Service\ParticipantFieldOptionsProvider;
use App\Service\BookingPriceCalculatorService;
use App\Service\InsuranceService;
use App\Service\ServiceAvailabilityCalculator;
use PHPUnit\Framework\TestCase;
class ParticipantFieldOptionsProviderBabyTest extends TestCase
{
private ParticipantFieldOptionsProvider $provider;
private ServiceAvailabilityCalculator $serviceAvailabilityCalculator;
protected function setUp(): void
{
$roomChoiceLoaderFactory = $this->createMock(ParticipantRoomChoiceLoaderFactory::class);
$this->serviceAvailabilityCalculator = $this->createMock(ServiceAvailabilityCalculator::class);
$insuranceService = $this->createMock(InsuranceService::class);
$priceCalculatorService = $this->createMock(BookingPriceCalculatorService::class);
$this->provider = new ParticipantFieldOptionsProvider(
$roomChoiceLoaderFactory,
$this->serviceAvailabilityCalculator,
$insuranceService,
$priceCalculatorService
);
}
public function testBabyOnlySeesServicesWithExplicitAgeRange(): void
{
$travel = $this->createTravelWithMixedServices();
$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;
$options = $this->provider->getFieldOptions('additionalServices', $bookingDto, 0);
$choices = $options['choices'];
$choiceIds = array_map(fn (Service $service) => $service->id, $choices);
// Baby should see only service with age range 0-14 that includes them
$this->assertContains(1, $choiceIds, 'Service with age range 0-14 should be shown for baby');
$this->assertNotContains(2, $choiceIds, 'Service with age range 6-14 should be hidden for baby (age < 6)');
$this->assertNotContains(3, $choiceIds, 'Service without age restrictions should be hidden for baby');
}
public function testTwoYearOldOnlySeesServicesWithExplicitAgeRange(): void
{
$travel = $this->createTravelWithMixedServices();
$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;
$options = $this->provider->getFieldOptions('additionalServices', $bookingDto, 0);
$choices = $options['choices'];
$choiceIds = array_map(fn (Service $service) => $service->id, $choices);
// 2-year-old should see only service with age range that includes them
$this->assertContains(1, $choiceIds, 'Service with age range 0-14 should be shown for 2-year-old');
$this->assertNotContains(2, $choiceIds, 'Service with age range 6-14 should be hidden for 2-year-old');
$this->assertNotContains(3, $choiceIds, 'Service without age restrictions should be hidden for 2-year-old');
}
public function testThreeYearOldSeesAllApplicableServices(): void
{
$travel = $this->createTravelWithMixedServices();
$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;
$options = $this->provider->getFieldOptions('additionalServices', $bookingDto, 0);
$choices = $options['choices'];
$choiceIds = array_map(fn (Service $service) => $service->id, $choices);
// 3-year-old is not a baby, should see service with age range that includes them AND no-restriction services
$this->assertContains(1, $choiceIds, 'Service with age range 0-14 should be shown for 3-year-old');
$this->assertNotContains(2, $choiceIds, 'Service with age range 6-14 should be hidden for 3-year-old (age < 6)');
$this->assertContains(3, $choiceIds, 'Service without age restrictions should be shown for non-baby');
}
public function testAdultSeesAllApplicableServices(): void
{
$travel = $this->createTravelWithMixedServices();
$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;
$options = $this->provider->getFieldOptions('additionalServices', $bookingDto, 0);
$choices = $options['choices'];
$choiceIds = array_map(fn (Service $service) => $service->id, $choices);
// Adult should see service without age restrictions but not children services
$this->assertNotContains(1, $choiceIds, 'Service with age range 0-14 should be hidden for adult (age > 14)');
$this->assertNotContains(2, $choiceIds, 'Service with age range 6-14 should be hidden for adult (age > 14)');
$this->assertContains(3, $choiceIds, 'Service without age restrictions should be shown for adult');
}
public function testBabyGetRegularPkwInTransportation(): void
{
$travel = $this->createTravelWithTransportation();
$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;
$options = $this->provider->getFieldOptions('transportationOutbound', $bookingDto, 0);
$choices = $options['choices'];
$choiceIds = array_map(fn (Service $service) => $service->id, $choices);
// Baby should see BUS and regular PKW, but NOT discounted PKW
$this->assertContains(1, $choiceIds, 'BUS should be shown for baby');
$this->assertContains(3, $choiceIds, 'Regular PKW should be shown for baby');
$this->assertNotContains(2, $choiceIds, 'Discounted PKW should be hidden for baby');
}
public function testTwoYearOldGetRegularPkwInTransportation(): void
{
$travel = $this->createTravelWithTransportation();
$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;
$options = $this->provider->getFieldOptions('transportationOutbound', $bookingDto, 0);
$choices = $options['choices'];
$choiceIds = array_map(fn (Service $service) => $service->id, $choices);
// 2-year-old (at BABY_MAX_AGE) should see BUS and regular PKW, but NOT discounted PKW
$this->assertContains(1, $choiceIds, 'BUS should be shown for 2-year-old');
$this->assertContains(3, $choiceIds, 'Regular PKW should be shown for 2-year-old');
$this->assertNotContains(2, $choiceIds, 'Discounted PKW should be hidden for 2-year-old');
}
public function testThreeYearOldGetSmartPkwFiltering(): void
{
$travel = $this->createTravelWithTransportation();
$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;
// Mock availability: discounted PKW is available
$this->serviceAvailabilityCalculator
->method('isServiceUnavailable')
->willReturn(false);
$options = $this->provider->getFieldOptions('transportationOutbound', $bookingDto, 0);
$choices = $options['choices'];
$choiceIds = array_map(fn (Service $service) => $service->id, $choices);
// 3-year-old is not a baby, should get smart filtering (discounted when available)
$this->assertContains(1, $choiceIds, 'BUS should be shown for 3-year-old');
$this->assertContains(2, $choiceIds, 'Discounted PKW should be shown for 3-year-old when available');
$this->assertNotContains(3, $choiceIds, 'Regular PKW should be hidden when discounted is available');
}
public function testBabyFilteringAppliesToMultipleServiceTypes(): void
{
$travel = $this->createTravelWithMultipleServiceTypes();
$bookingDto = new BookingDto($travel, 1);
// Create a baby participant
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-1 year');
$bookingDto->participants[0] = $participant;
// Test board services
$boardOptions = $this->provider->getFieldOptions('board', $bookingDto, 0);
$boardChoiceIds = array_map(fn (Service $service) => $service->id, $boardOptions['choices']);
$this->assertContains(10, $boardChoiceIds, 'Board with age range 0-5 should be shown for baby');
$this->assertNotContains(11, $boardChoiceIds, 'Board without age range should be hidden for baby');
// Test courses services
$coursesOptions = $this->provider->getFieldOptions('courses', $bookingDto, 0);
$coursesChoiceIds = array_map(fn (Service $service) => $service->id, $coursesOptions['choices']);
$this->assertContains(20, $coursesChoiceIds, 'Course with age range 0-3 should be shown for baby');
$this->assertNotContains(21, $coursesChoiceIds, 'Course without age range should be hidden for baby');
}
private function createTravelWithMixedServices(): Travel
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
// Service with explicit age range including babies (0-14)
$serviceWithBabyAge = new Service();
$serviceWithBabyAge->id = 1;
$serviceWithBabyAge->label = 'Ortstaxe Kinder';
$serviceWithBabyAge->subType = Constants::TOKEN_ADDITIONAL;
$serviceWithBabyAge->available = 10;
$serviceWithBabyAge->price = 5.0;
$serviceWithBabyAge->ageConstraintType = 'absolute_age';
$serviceWithBabyAge->ageFrom = 0;
$serviceWithBabyAge->ageTo = 14;
// Service with explicit age range NOT including babies (6-14)
$serviceWithOlderAge = new Service();
$serviceWithOlderAge->id = 2;
$serviceWithOlderAge->label = 'Mobilitätsabgabe Kinder';
$serviceWithOlderAge->subType = Constants::TOKEN_ADDITIONAL;
$serviceWithOlderAge->available = 10;
$serviceWithOlderAge->price = 3.0;
$serviceWithOlderAge->ageConstraintType = 'absolute_age';
$serviceWithOlderAge->ageFrom = 6;
$serviceWithOlderAge->ageTo = 14;
// Service without age restrictions (generic service)
$serviceNoAge = new Service();
$serviceNoAge->id = 3;
$serviceNoAge->label = 'Keycard-Pfand';
$serviceNoAge->subType = Constants::TOKEN_ADDITIONAL;
$serviceNoAge->available = 10;
$serviceNoAge->price = 10.0;
$serviceNoAge->ageConstraintType = null;
$travel->additionalServices = [
1 => $serviceWithBabyAge,
2 => $serviceWithOlderAge,
3 => $serviceNoAge,
];
return $travel;
}
private function createTravelWithTransportation(): Travel
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
// BUS service
$busService = new Service();
$busService->id = 1;
$busService->label = 'Bus';
$busService->subType = DirectionMapper::SUBTYPE_BUS_API;
$busService->direction = DirectionMapper::OUTBOUND_TRAVEL;
$busService->available = 10;
$busService->price = 0.0;
// Discounted PKW (negative price)
$discountedPkw = new Service();
$discountedPkw->id = 2;
$discountedPkw->label = 'Selbstorganisiert (-15€ Rabatt)';
$discountedPkw->subType = DirectionMapper::SUBTYPE_CAR_API;
$discountedPkw->direction = DirectionMapper::OUTBOUND_TRAVEL;
$discountedPkw->available = 5;
$discountedPkw->price = -15.0;
// Regular PKW
$regularPkw = new Service();
$regularPkw->id = 3;
$regularPkw->label = 'Selbstorganisiert';
$regularPkw->subType = DirectionMapper::SUBTYPE_CAR_API;
$regularPkw->direction = DirectionMapper::OUTBOUND_TRAVEL;
$regularPkw->available = 100;
$regularPkw->price = 0.0;
$travel->transportationServices = [$busService, $discountedPkw, $regularPkw];
return $travel;
}
private function createTravelWithMultipleServiceTypes(): Travel
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
// Board with baby age range
$boardWithBabyAge = new Service();
$boardWithBabyAge->id = 10;
$boardWithBabyAge->label = 'Baby Verpflegung';
$boardWithBabyAge->subType = Constants::TOKEN_BOARD;
$boardWithBabyAge->available = 10;
$boardWithBabyAge->price = 0.0;
$boardWithBabyAge->ageConstraintType = 'absolute_age';
$boardWithBabyAge->ageFrom = 0;
$boardWithBabyAge->ageTo = 5;
// Board without age range
$boardNoAge = new Service();
$boardNoAge->id = 11;
$boardNoAge->label = 'Verpflegung Standard';
$boardNoAge->subType = Constants::TOKEN_BOARD;
$boardNoAge->available = 10;
$boardNoAge->price = 50.0;
$boardNoAge->ageConstraintType = null;
// Course with baby age range
$courseWithBabyAge = new Service();
$courseWithBabyAge->id = 20;
$courseWithBabyAge->label = 'Baby Kurs';
$courseWithBabyAge->subType = Constants::TOKEN_COURSES;
$courseWithBabyAge->available = 10;
$courseWithBabyAge->price = 20.0;
$courseWithBabyAge->ageConstraintType = 'absolute_age';
$courseWithBabyAge->ageFrom = 0;
$courseWithBabyAge->ageTo = 3;
// Course without age range
$courseNoAge = new Service();
$courseNoAge->id = 21;
$courseNoAge->label = 'Standard Kurs';
$courseNoAge->subType = Constants::TOKEN_COURSES;
$courseNoAge->available = 10;
$courseNoAge->price = 100.0;
$courseNoAge->ageConstraintType = null;
$travel->additionalServices = [
10 => $boardWithBabyAge,
11 => $boardNoAge,
20 => $courseWithBabyAge,
21 => $courseNoAge,
];
return $travel;
}
}