feat: extract booking participant count service

This commit is contained in:
Björn Fromme
2026-04-11 18:28:32 +02:00
parent f31ac660d2
commit dbc0b4acee
5 changed files with 182 additions and 59 deletions
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Travel;
use App\BusProNet\Constants;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Model\RoomSelectionDto;
use App\Service\BookingParticipantCountService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\User\UserInterface;
class BookingParticipantCountServiceTest extends TestCase
{
private BookingParticipantCountService $service;
protected function setUp(): void
{
$this->service = new BookingParticipantCountService();
}
public function testEnsureCorrectNumberOfParticipantsExpandsToCalculatedCount(): void
{
$travel = $this->createTravel([
1 => 2,
2 => 3,
]);
$bookingDto = new BookingDto($travel, 123);
$bookingDto->roomSelections = [
$this->createRoomSelection(1, 1),
$this->createRoomSelection(2, 2),
];
$bookingDto->participants = [new ParticipantDto()];
$this->service->ensureCorrectNumberOfParticipants($bookingDto);
$this->assertCount(8, $bookingDto->participants);
$this->assertSame(0, $bookingDto->participants[0]->index);
$this->assertSame(7, $bookingDto->participants[7]->index);
}
public function testEnsureCorrectNumberOfParticipantsPrepopulatesFreshApplicant(): void
{
$travel = $this->createTravel([1 => 2]);
$bookingDto = new BookingDto($travel, 123);
$bookingDto->roomSelections = [$this->createRoomSelection(1, 1)];
$bookingDto->participants = [new ParticipantDto()];
$user = $this->createMock(UserInterface::class);
$callbackCalled = false;
$this->service->ensureCorrectNumberOfParticipants(
$bookingDto,
$user,
function (UserInterface $userArg, ParticipantDto $participant) use (&$callbackCalled): ParticipantDto {
$callbackCalled = true;
$participant->firstName = 'Alex';
return $participant;
}
);
$this->assertTrue($callbackCalled);
$this->assertSame('Alex', $bookingDto->participants[0]->firstName);
}
private function createTravel(array $roomCapacities): Travel
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$rooms = [];
foreach ($roomCapacities as $id => $minPax) {
$room = new Room();
$room->id = $id;
$room->label = 'Room '.$id;
$room->minPax = $minPax;
$room->maxPax = $minPax + 1;
$room->available = 5;
$room->status = Constants::STATUS_AVAILABLE;
$rooms[$id] = $room;
}
$travel->rooms = $rooms;
return $travel;
}
private function createRoomSelection(int $id, int $quantity): RoomSelectionDto
{
$selection = new RoomSelectionDto();
$selection->id = $id;
$selection->quantity = $quantity;
return $selection;
}
}