From 7ff10e67924db8f78d4b028a3f0ed997710d2514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Fri, 19 Sep 2025 16:19:01 +0200 Subject: [PATCH] feat: simplify handling of ski pass service --- .../DataProcessor/BookingDataProcessor.php | 6 +- src/BusProNet/Model/Booking.php | 23 ++++++ src/Form/Model/BookingEditDto.php | 6 +- tests/BusProNet/Model/BookingTest.php | 72 +++++++++++++++++++ 4 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 tests/BusProNet/Model/BookingTest.php diff --git a/src/BusProNet/DataProcessor/BookingDataProcessor.php b/src/BusProNet/DataProcessor/BookingDataProcessor.php index 38348a2..c865788 100644 --- a/src/BusProNet/DataProcessor/BookingDataProcessor.php +++ b/src/BusProNet/DataProcessor/BookingDataProcessor.php @@ -120,11 +120,15 @@ class BookingDataProcessor $servicesToMap = [ ...$participant->courses, ...$participant->additionalServices, - ...($participant->skiPass ? [$participant->skiPass] : []), ...$participant->board, ...$participant->rentals, ]; + // Add ski pass if selected (single service, not an array) + if (null !== $participant->skiPass) { + $servicesToMap[] = $participant->skiPass; + } + foreach ($servicesToMap as $service) { if (false === isset($bookingData->additionalServices[$service->id])) { $serviceToAdd = $travelData->additionalServices[$service->id] ?? null; diff --git a/src/BusProNet/Model/Booking.php b/src/BusProNet/Model/Booking.php index 63e0418..c9f6ee1 100644 --- a/src/BusProNet/Model/Booking.php +++ b/src/BusProNet/Model/Booking.php @@ -150,6 +150,29 @@ class Booking return null; } + /** + * Retrieves ski pass for a specific participant. + * + * Since each participant can only have one ski pass, this method returns + * the single ski pass assigned to the participant or null if none is assigned. + * + * @param int $participantIndex The participant index to search for + * + * @return Service|null The participant's ski pass or null if not found + */ + public function getSkiPassForParticipant(int $participantIndex): ?Service + { + $skiPasses = $this->getAdditionalServicesByGroup('SPA'); + + foreach ($skiPasses as $service) { + if (in_array($participantIndex, $service->mapping)) { + return $service; + } + } + + return null; + } + /** * Retrieves room assignment for a specific participant. * diff --git a/src/Form/Model/BookingEditDto.php b/src/Form/Model/BookingEditDto.php index 7301092..0c4aa50 100644 --- a/src/Form/Model/BookingEditDto.php +++ b/src/Form/Model/BookingEditDto.php @@ -36,10 +36,8 @@ class BookingEditDto implements BookingDtoInterface $participantData->courses = $booking ->getAdditionalServicesForParticipantByGroup($index, Constants::TOKEN_COURSES); - // Skipass is single selection - take first item from array or null - $skipasses = $booking - ->getAdditionalServicesForParticipantByGroup($index, Constants::TOKEN_SKI_PASS); - $participantData->skiPass = !empty($skipasses) ? $skipasses[0] : null; + // Skipass is single selection - use dedicated method + $participantData->skiPass = $booking->getSkiPassForParticipant($index); $participantData->additionalServices = $booking ->getAdditionalServicesForParticipantByGroup($index, Constants::TOKEN_ADDITIONAL); diff --git a/tests/BusProNet/Model/BookingTest.php b/tests/BusProNet/Model/BookingTest.php new file mode 100644 index 0000000..d38bb21 --- /dev/null +++ b/tests/BusProNet/Model/BookingTest.php @@ -0,0 +1,72 @@ +id = 123; + $skiPassService->subType = 'SPA'; + $skiPassService->mapping = [0, 2]; // Assigned to participants 0 and 2 + + // Create a non-ski pass service + $otherService = new Service(); + $otherService->id = 456; + $otherService->subType = 'OTHER'; + $otherService->mapping = [0]; + + $booking->additionalServices = [ + 123 => $skiPassService, + 456 => $otherService, + ]; + + // Test: participant 0 should get the ski pass + $result = $booking->getSkiPassForParticipant(0); + $this->assertSame($skiPassService, $result); + $this->assertEquals(123, $result->id); + + // Test: participant 1 should get null (no ski pass assigned) + $result = $booking->getSkiPassForParticipant(1); + $this->assertNull($result); + + // Test: participant 2 should get the ski pass + $result = $booking->getSkiPassForParticipant(2); + $this->assertSame($skiPassService, $result); + } + + public function testGetSkiPassForParticipantReturnsNullWhenNoSkiPasses(): void + { + $booking = new Booking(); + + // Only non-ski pass services + $otherService = new Service(); + $otherService->id = 456; + $otherService->subType = 'COURSES'; + $otherService->mapping = [0]; + + $booking->additionalServices = [456 => $otherService]; + + $result = $booking->getSkiPassForParticipant(0); + $this->assertNull($result); + } + + public function testGetSkiPassForParticipantReturnsNullWhenNoServices(): void + { + $booking = new Booking(); + $booking->additionalServices = []; + + $result = $booking->getSkiPassForParticipant(0); + $this->assertNull($result); + } +} \ No newline at end of file