feat: simplify handling of ski pass service
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\BusProNet\Model;
|
||||
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\BusProNet\Model\Service;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BookingTest extends TestCase
|
||||
{
|
||||
public function testGetSkiPassForParticipantReturnsCorrectService(): void
|
||||
{
|
||||
$booking = new Booking();
|
||||
|
||||
// Create a ski pass service
|
||||
$skiPassService = new Service();
|
||||
$skiPassService->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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user