feat: consolidated insurance service

This commit is contained in:
Björn Fromme
2026-03-16 11:59:11 +01:00
parent 4f951cdf8a
commit a0d5768752
12 changed files with 681 additions and 545 deletions
@@ -16,8 +16,8 @@ use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Service\InsuranceMatchingService;
use App\Service\InsuranceTypeFilterService;
use App\Service\BookingPriceCalculatorService;
use App\Service\InsuranceService;
use PHPUnit\Framework\TestCase;
/**
@@ -32,15 +32,19 @@ class BookingDataProcessorTest extends TestCase
protected function setUp(): void
{
// Create a mock InsuranceMatchingService
$insuranceMatchingService = $this->createMock(InsuranceMatchingService::class);
// Create a mock InsuranceService
$insuranceService = $this->createMock(InsuranceService::class);
$priceCalculatorService = $this->createMock(BookingPriceCalculatorService::class);
// Create a mock InsuranceTypeFilterService that simply returns the input
$insuranceTypeFilterService = $this->createMock(InsuranceTypeFilterService::class);
$insuranceTypeFilterService->method('filterNonComplementary')
->willReturnArgument(0);
// Mock getSelectableInsurances to return empty array (not used in these tests)
$insuranceService->method('getSelectableInsurances')
->willReturn([]);
$this->processor = new BookingDataProcessor($insuranceMatchingService, $insuranceTypeFilterService);
// Mock price calculator to return default prices (not used in most tests)
$priceCalculatorService->method('calculateIndividualParticipantPriceExcludingInsurance')
->willReturn(500.0);
$this->processor = new BookingDataProcessor($insuranceService, $priceCalculatorService);
}
public function testCreateUpdateRequestPayloadWithCompleteData(): void
@@ -9,24 +9,30 @@ use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantInsuranceFieldHandler;
use App\Service\InsuranceMatchingService;
use App\Service\InsuranceTypeFilterService;
use App\Service\BookingPriceCalculatorService;
use App\Service\InsuranceService;
use PHPUnit\Framework\TestCase;
class ParticipantInsuranceFieldHandlerTest extends TestCase
{
private ParticipantInsuranceFieldHandler $handler;
private InsuranceMatchingService $insuranceMatchingService;
private InsuranceService $insuranceService;
private BookingPriceCalculatorService $priceCalculatorService;
protected function setUp(): void
{
$this->insuranceMatchingService = $this->createMock(InsuranceMatchingService::class);
$this->insuranceService = $this->createMock(InsuranceService::class);
$this->priceCalculatorService = $this->createMock(BookingPriceCalculatorService::class);
$insuranceTypeFilterService = $this->createMock(InsuranceTypeFilterService::class);
$insuranceTypeFilterService->method('filterNonComplementary')
->willReturnArgument(0);
// Mock getSelectableInsurances to return input array
$this->insuranceService->method('getSelectableInsurances')
->willReturnCallback(fn (Travel $travel) => $travel->insurances ?? []);
$this->handler = new ParticipantInsuranceFieldHandler($this->insuranceMatchingService, $insuranceTypeFilterService);
// Mock price calculator to return a default price
$this->priceCalculatorService->method('calculateIndividualParticipantPriceExcludingInsurance')
->willReturn(500.0);
$this->handler = new ParticipantInsuranceFieldHandler($this->insuranceService, $this->priceCalculatorService);
}
public function testGetFieldName(): void
@@ -85,8 +91,12 @@ class ParticipantInsuranceFieldHandlerTest extends TestCase
$participant = new ParticipantDto();
$participant->insurance = $this->createInsurance('123');
$travel = new Travel();
$travel->insurances = [];
$bookingDto = $this->createMockBookingDto();
$bookingDto->method('getParticipant')->with(0)->willReturn($participant);
$bookingDto->travel = $travel;
$this->handler->processField(['insurance' => null], $bookingDto, 0);
@@ -98,8 +108,12 @@ class ParticipantInsuranceFieldHandlerTest extends TestCase
$participant = new ParticipantDto();
$participant->insurance = $this->createInsurance('123');
$travel = new Travel();
$travel->insurances = [];
$bookingDto = $this->createMockBookingDto();
$bookingDto->method('getParticipant')->with(0)->willReturn($participant);
$bookingDto->travel = $travel;
$this->handler->processField(['insurance' => ''], $bookingDto, 0);
@@ -111,8 +125,12 @@ class ParticipantInsuranceFieldHandlerTest extends TestCase
$participant = new ParticipantDto();
$participant->insurance = $this->createInsurance('123');
$travel = new Travel();
$travel->insurances = [];
$bookingDto = $this->createMockBookingDto();
$bookingDto->method('getParticipant')->with(0)->willReturn($participant);
$bookingDto->travel = $travel;
$this->handler->processField([], $bookingDto, 0);
@@ -145,10 +163,10 @@ class ParticipantInsuranceFieldHandlerTest extends TestCase
$bookingDto->method('getParticipant')->with(0)->willReturn($participant);
$bookingDto->travel = $travel;
$this->insuranceMatchingService
$this->insuranceService
->expects($this->once())
->method('getEligibleInsurances')
->with([$insurance], $participant, $bookingDto)
->with([$insurance], $participant, $bookingDto, 500.0)
->willReturn([$insurance]);
$this->handler->processField(['insurance' => '123'], $bookingDto, 0);
@@ -167,10 +185,10 @@ class ParticipantInsuranceFieldHandlerTest extends TestCase
$bookingDto->method('getParticipant')->with(0)->willReturn($participant);
$bookingDto->travel = $travel;
$this->insuranceMatchingService
$this->insuranceService
->expects($this->once())
->method('getEligibleInsurances')
->with([$insurance], $participant, $bookingDto)
->with([$insurance], $participant, $bookingDto, 500.0)
->willReturn([]); // Not eligible
$this->handler->processField(['insurance' => '123'], $bookingDto, 0);
@@ -189,7 +207,7 @@ class ParticipantInsuranceFieldHandlerTest extends TestCase
$bookingDto->method('getParticipant')->with(0)->willReturn($participant);
$bookingDto->travel = $travel;
$this->insuranceMatchingService
$this->insuranceService
->expects($this->once())
->method('getEligibleInsurances')
->willReturn([$insurance]);
@@ -23,13 +23,12 @@ class BookingPriceCalculatorServiceTest extends TestCase
$participantEligibilityService = $this->createMock(ParticipantEligibilityService::class);
$participantEligibilityService->method('isParticipantEligible')->willReturn(true);
$insuranceTypeFilterService = new \App\Service\InsuranceTypeFilterService();
$insuranceEligibilityService = new \App\Service\InsuranceEligibilityService();
// Create a real InsuranceService (it will use a mocked price calculator internally when needed)
$insuranceService = new \App\Service\InsuranceService($this->service ?? $this->createMock(BookingPriceCalculatorService::class));
$this->service = new BookingPriceCalculatorService(
$participantEligibilityService,
$insuranceTypeFilterService,
$insuranceEligibilityService
$insuranceService
);
}
@@ -494,13 +493,13 @@ class BookingPriceCalculatorServiceTest extends TestCase
$participantEligibilityService->method('isParticipantEligible')
->willReturnCallback(fn ($booking, $index) => 0 === $index); // Only first participant eligible
$insuranceTypeFilterService = new \App\Service\InsuranceTypeFilterService();
$insuranceEligibilityService = new \App\Service\InsuranceEligibilityService();
// Create a real InsuranceService with mocked price calculator
$mockPriceCalculator = $this->createMock(BookingPriceCalculatorService::class);
$insuranceService = new \App\Service\InsuranceService($mockPriceCalculator);
$this->service = new BookingPriceCalculatorService(
$participantEligibilityService,
$insuranceTypeFilterService,
$insuranceEligibilityService
$insuranceService
);
$result = $this->service->calculateServicePricing($bookingDto);
@@ -5,42 +5,21 @@ declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\Model\Insurance;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Service\BookingPriceCalculatorService;
use App\Service\InsuranceEligibilityService;
use App\Service\InsuranceMatchingService;
use App\Service\InsuranceTypeFilterService;
use App\Service\InsuranceService;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
class InsuranceMatchingServiceTest extends TestCase
class InsuranceServiceTest extends TestCase
{
private InsuranceMatchingService $service;
private BookingPriceCalculatorService $priceCalculator;
private InsuranceService $service;
protected function setUp(): void
{
// Mock the price calculator service
$this->priceCalculator = $this->createMock(BookingPriceCalculatorService::class);
$this->priceCalculator
->method('calculateIndividualParticipantPriceExcludingInsurance')
->willReturn(500.0); // Default test price
// Mock InsuranceTypeFilterService - just return the input array for filterByType
$insuranceTypeFilterService = $this->createMock(InsuranceTypeFilterService::class);
$insuranceTypeFilterService->method('filterByType')
->willReturnArgument(0);
// Use real InsuranceEligibilityService since it has no dependencies
// and these tests are actually testing the eligibility filtering logic
$insuranceEligibilityService = new InsuranceEligibilityService();
$this->service = new InsuranceMatchingService(
$this->priceCalculator,
$insuranceTypeFilterService,
$insuranceEligibilityService
);
// InsuranceService is stateless and has no dependencies
$this->service = new InsuranceService();
// Set a fixed test date for consistent test results
Carbon::setTestNow('2024-06-01 12:00:00');
@@ -52,6 +31,36 @@ class InsuranceMatchingServiceTest extends TestCase
Carbon::setTestNow();
}
public function testGetSelectableInsurancesFiltersComplementary(): void
{
$regularInsurance = $this->createInsurance(['id' => '1', 'complementary' => false]);
$complementaryInsurance = $this->createInsurance(['id' => '2', 'complementary' => true]);
$travel = new Travel();
$travel->id = 123;
$travel->insurances = [$regularInsurance, $complementaryInsurance];
$result = $this->service->getSelectableInsurances($travel);
$this->assertCount(1, $result);
$this->assertSame($regularInsurance, $result[0]);
}
public function testGetSelectableInsurancesCachesResults(): void
{
$insurance = $this->createInsurance(['id' => '1', 'complementary' => false]);
$travel = new Travel();
$travel->id = 456;
$travel->insurances = [$insurance];
// Call twice - second call should use cache
$result1 = $this->service->getSelectableInsurances($travel);
$result2 = $this->service->getSelectableInsurances($travel);
$this->assertSame($result1, $result2);
}
public function testGetEligibleInsurancesWithMatchingCriteria(): void
{
$participant = $this->createParticipant('1990-06-15');
@@ -70,7 +79,7 @@ class InsuranceMatchingServiceTest extends TestCase
'travelDurationTo' => 14,
]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertCount(1, $result);
$this->assertContains($insurance, $result);
@@ -83,7 +92,7 @@ class InsuranceMatchingServiceTest extends TestCase
$insurance = $this->createInsurance(['ageFrom' => 18, 'ageTo' => 65]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -95,7 +104,7 @@ class InsuranceMatchingServiceTest extends TestCase
$insurance = $this->createInsurance(['ageFrom' => 18, 'ageTo' => 65]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -110,7 +119,7 @@ class InsuranceMatchingServiceTest extends TestCase
'travelDateTo' => new \DateTimeImmutable('2024-12-31'),
]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -125,7 +134,7 @@ class InsuranceMatchingServiceTest extends TestCase
'travelDateTo' => new \DateTimeImmutable('2024-07-31'),
]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -140,7 +149,7 @@ class InsuranceMatchingServiceTest extends TestCase
'bookingDateTo' => new \DateTimeImmutable('2025-12-31'),
]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -155,7 +164,33 @@ class InsuranceMatchingServiceTest extends TestCase
'bookingDateTo' => new \DateTimeImmutable('2023-12-31'),
]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
public function testGetEligibleInsurancesExcludesInsuranceWithTravelPriceTooLow(): void
{
$participant = $this->createParticipant('1990-06-15');
$booking = $this->createBooking('2024-08-01', '2024-08-08');
// Price calculator returns 500.0, insurance requires at least 1000.0
$insurance = $this->createInsurance(['travelPriceFrom' => 1000.0, 'travelPriceTo' => 2000.0]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
public function testGetEligibleInsurancesExcludesInsuranceWithTravelPriceTooHigh(): void
{
$participant = $this->createParticipant('1990-06-15');
$booking = $this->createBooking('2024-08-01', '2024-08-08');
// Price calculator returns 500.0, insurance allows maximum 100.0
$insurance = $this->createInsurance(['travelPriceFrom' => 0.0, 'travelPriceTo' => 100.0]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -167,7 +202,7 @@ class InsuranceMatchingServiceTest extends TestCase
$insurance = $this->createInsurance(['travelDurationFrom' => 7, 'travelDurationTo' => 30]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -179,7 +214,7 @@ class InsuranceMatchingServiceTest extends TestCase
$insurance = $this->createInsurance(['travelDurationFrom' => 1, 'travelDurationTo' => 14]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertEmpty($result);
}
@@ -187,15 +222,15 @@ class InsuranceMatchingServiceTest extends TestCase
public function testGetEligibleInsurancesHandlesParticipantWithoutBirthDate(): void
{
$participant = new ParticipantDto();
$participant->index = 0; // Set participant index for price calculations
$participant->index = 0;
$participant->dateOfBirth = null;
$booking = $this->createBooking('2024-08-01', '2024-08-08');
$insurance = $this->createInsurance(['ageFrom' => 18, 'ageTo' => 65]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
// Participants without birth date should now have insurances available
// Participants without birth date should have insurances available
// (field visibility is controlled by field state conditions, not service logic)
$this->assertNotEmpty($result);
$this->assertCount(1, $result);
@@ -210,7 +245,7 @@ class InsuranceMatchingServiceTest extends TestCase
// Insurance with no constraints should match any criteria
$insurance = $this->createInsurance([]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertCount(1, $result);
$this->assertContains($insurance, $result);
@@ -226,7 +261,7 @@ class InsuranceMatchingServiceTest extends TestCase
$ineligibleInsurance = $this->createInsurance(['ageFrom' => 60, 'ageTo' => 80]);
$insurances = [$eligibleInsurance1, $eligibleInsurance2, $ineligibleInsurance];
$result = $this->service->getEligibleInsurances($insurances, $participant, $booking);
$result = $this->service->getEligibleInsurances($insurances, $participant, $booking, 500.0);
$this->assertCount(2, $result);
$this->assertContains($eligibleInsurance1, $result);
@@ -241,7 +276,7 @@ class InsuranceMatchingServiceTest extends TestCase
$insurance = $this->createInsurance(['ageFrom' => 34, 'ageTo' => 34]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertCount(1, $result);
$this->assertContains($insurance, $result);
@@ -254,16 +289,146 @@ class InsuranceMatchingServiceTest extends TestCase
$insurance = $this->createInsurance(['travelDurationFrom' => 7, 'travelDurationTo' => 7]);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking);
$result = $this->service->getEligibleInsurances([$insurance], $participant, $booking, 500.0);
$this->assertCount(1, $result);
$this->assertContains($insurance, $result);
}
public function testFilterByTypeForPackages(): void
{
$packageInsurance1 = $this->createInsurance([
'id' => '1',
'package' => true,
'label' => 'Premium Package',
'familyInsurance' => false,
]);
$packageInsurance2 = $this->createInsurance([
'id' => '2',
'package' => true,
'label' => 'Premium Package',
'familyInsurance' => false,
]);
$differentPackage = $this->createInsurance([
'id' => '3',
'package' => true,
'label' => 'Basic Package',
'familyInsurance' => false,
]);
$insurances = [$packageInsurance1, $packageInsurance2, $differentPackage];
$result = $this->service->filterByType($insurances, $packageInsurance1);
$this->assertCount(2, $result);
$this->assertContains($packageInsurance1, $result);
$this->assertContains($packageInsurance2, $result);
}
public function testFilterByTypeForIndividualInsurances(): void
{
$insurance1 = $this->createInsurance([
'id' => '1',
'package' => false,
'subType' => 'RRV',
'familyInsurance' => false,
]);
$insurance2 = $this->createInsurance([
'id' => '2',
'package' => false,
'subType' => 'RRV',
'familyInsurance' => false,
]);
$differentType = $this->createInsurance([
'id' => '3',
'package' => false,
'subType' => 'PAK',
'familyInsurance' => false,
]);
$insurances = [$insurance1, $insurance2, $differentType];
$result = $this->service->filterByType($insurances, $insurance1);
$this->assertCount(2, $result);
$this->assertContains($insurance1, $result);
$this->assertContains($insurance2, $result);
}
public function testReassignInsuranceForPriceChange(): void
{
$participant = $this->createParticipant('1990-06-15');
$booking = $this->createBooking('2024-08-01', '2024-08-08');
$currentInsurance = $this->createInsurance([
'id' => '1',
'package' => false,
'subType' => 'RRV',
'familyInsurance' => false,
'travelPriceFrom' => 0.0,
'travelPriceTo' => 300.0,
]);
$newTierInsurance = $this->createInsurance([
'id' => '2',
'package' => false,
'subType' => 'RRV',
'familyInsurance' => false,
'travelPriceFrom' => 300.0,
'travelPriceTo' => 600.0,
]);
$availableInsurances = [$currentInsurance, $newTierInsurance];
$result = $this->service->reassignInsuranceForPriceChange(
$availableInsurances,
$currentInsurance,
$participant,
$booking,
500.0
);
$this->assertSame($newTierInsurance, $result);
}
public function testBatchAssignInsuranceToParticipants(): void
{
$booking = $this->createBooking('2024-08-01', '2024-08-08');
$participant1 = $this->createParticipant('1990-06-15');
$participant1->index = 0;
$participant2 = $this->createParticipant('1995-03-20');
$participant2->index = 1;
$booking->participants = [$participant1, $participant2];
$selectedInsurance = $this->createInsurance([
'id' => '1',
'package' => false,
'subType' => 'RRV',
'familyInsurance' => false,
]);
$eligibleInsurance = $this->createInsurance([
'id' => '2',
'package' => false,
'subType' => 'RRV',
'familyInsurance' => false,
]);
$availableInsurances = [$selectedInsurance, $eligibleInsurance];
$participantPrices = [0 => 500.0, 1 => 500.0];
$result = $this->service->batchAssignInsuranceToParticipants(
$availableInsurances,
$selectedInsurance,
$booking,
$participantPrices
);
$this->assertCount(2, $result);
$this->assertNotNull($result[0]);
$this->assertNotNull($result[1]);
}
private function createParticipant(string $dateOfBirth): ParticipantDto
{
$participant = new ParticipantDto();
$participant->index = 0; // Set participant index for price calculations
$participant->index = 0;
$participant->dateOfBirth = new \DateTimeImmutable($dateOfBirth);
return $participant;
@@ -277,20 +442,26 @@ class InsuranceMatchingServiceTest extends TestCase
return $booking;
}
private function createTravel(string $dateFrom, string $dateTo): \App\BusProNet\Model\Travel
private function createTravel(string $dateFrom, string $dateTo): Travel
{
$travel = new \App\BusProNet\Model\Travel();
$travel = new Travel();
$travel->id = random_int(1, 999999);
$travel->dateFrom = new \DateTimeImmutable($dateFrom);
$travel->dateTo = new \DateTimeImmutable($dateTo);
return $travel;
}
private function createInsurance(array $constraints = []): Insurance
private function createInsurance(array $properties = []): Insurance
{
$insurance = new Insurance();
foreach ($constraints as $property => $value) {
// Set defaults
$insurance->complementary = false;
$insurance->package = false;
$insurance->familyInsurance = false;
foreach ($properties as $property => $value) {
$insurance->$property = $value;
}