feat: split booking participant controllers

This commit is contained in:
Björn Fromme
2026-04-11 18:28:32 +02:00
parent 346256f895
commit f22ceae41c
10 changed files with 767 additions and 673 deletions
@@ -1,147 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Tests\Controller\Booking;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Travel;
use App\Controller\Booking\Traits\ParticipantCardFlowTrait;
use App\Form\Model\BookingDto;
use App\Form\Model\BookingSummaryDto;
use App\Form\Model\ParticipantDto;
use App\Service\BookingService;
use App\Service\BookingSummaryDataService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ParticipantCardFlowTraitTest extends TestCase
{
public function testHandleParticipantRefreshReappliesDefaultPreselectionInEditMode(): void
{
$bookingService = $this->createMock(BookingService::class);
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
$formOne = $this->createMock(FormInterface::class);
$formOne->expects($this->once())
->method('handleRequest');
$formOne->method('isSubmitted')
->willReturn(true);
$formTwo = $this->createMock(FormInterface::class);
$formTwo->expects($this->once())
->method('createView')
->willReturn(new FormView());
$bookingDto = $this->createEditModeBookingDto();
$request = new Request();
$bookingService->expects($this->once())
->method('preselectDefaultServices')
->with($bookingDto);
$bookingService->expects($this->once())
->method('saveBookingDto')
->with($request, $bookingDto, BookingDto::MODE_EDIT);
$summaryDataService->expects($this->once())
->method('getSummaryData')
->with($bookingDto)
->willReturn(new BookingSummaryDto([], 1, 0.0, 0.0, [], [], [], null));
$controller = new class($bookingService, $summaryDataService, [$formOne, $formTwo]) {
use ParticipantCardFlowTrait;
public BookingService $bookingService;
public BookingSummaryDataService $summaryDataService;
public object $participantCardService;
/** @var FormInterface[] */
private array $forms;
public function __construct(BookingService $bookingService, BookingSummaryDataService $summaryDataService, array $forms)
{
$this->bookingService = $bookingService;
$this->summaryDataService = $summaryDataService;
$this->forms = $forms;
$this->participantCardService = new class() {
public function getAllCardsData(BookingDto $bookingDto): array
{
return [];
}
};
}
public function callHandleParticipantRefresh(
Request $request,
BookingDto $bookingDto,
int $index,
string $refreshRouteName,
): Response {
$method = new \ReflectionMethod($this, 'handleParticipantRefresh');
$method->setAccessible(true);
return $method->invoke($this, $request, $bookingDto, $index, $refreshRouteName);
}
private function htmxOobResponse(string $template, array $blocks, array $parameters): Response
{
return new Response('ok');
}
protected function getParameter(string $name): mixed
{
return [];
}
private function createForm(string $type, $data = null, array $options = []): FormInterface
{
return array_shift($this->forms);
}
private function render(string $view, array $parameters = [], ?Response $response = null): Response
{
return new Response('rendered');
}
protected function addFlash(string $type, mixed $message): void
{
}
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
{
return new RedirectResponse('/');
}
};
$response = $controller->callHandleParticipantRefresh(
$request,
$bookingDto,
0,
'app_booking_edit_step_2_participant_refresh'
);
$this->assertSame('ok', $response->getContent());
}
private function createEditModeBookingDto(): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$bookingDto->booking = new Booking(); // Marks DTO as edit mode
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$bookingDto->participants[0] = $participant;
return $bookingDto;
}
}
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\Model\BaseData;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Service\BookingEditDataLoaderService;
use App\Service\BookingEditDraftService;
use App\Service\BookingEditParticipantFormService;
use App\Service\BookingService;
use App\Service\BookingSummaryDataService;
use App\Service\TravelDataService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
class BookingEditParticipantFormServiceTest extends TestCase
{
public function testLoadBookingDtoDelegatesToBookingService(): void
{
$request = new Request();
$bookingDto = $this->createBookingDto();
$bookingService = $this->createMock(BookingService::class);
$bookingService->expects($this->once())
->method('getBookingDto')
->with($request, BookingDto::MODE_EDIT)
->willReturn($bookingDto);
$service = $this->createService(bookingService: $bookingService);
$this->assertSame($bookingDto, $service->loadBookingDto($request));
}
public function testIsParticipantCanceledUsesBookingStatus(): void
{
$service = $this->createService();
$booking = new Booking();
$booking->participantsStatus = [0 => 'S', 1 => 'A'];
$this->assertTrue($service->isParticipantCanceled($booking, 0));
$this->assertFalse($service->isParticipantCanceled($booking, 1));
}
public function testGetMutableDataDelegatesToTravelDataService(): void
{
$mutableData = new BaseData([]);
$travelDataService = $this->createMock(TravelDataService::class);
$travelDataService->expects($this->once())
->method('getMutabilityData')
->with(1234)
->willReturn($mutableData);
$service = new BookingEditParticipantFormService(
$this->createMock(BookingEditDataLoaderService::class),
$this->createMock(BookingEditDraftService::class),
$this->createMock(BookingService::class),
$this->createMock(BookingSummaryDataService::class),
$travelDataService,
);
$this->assertSame($mutableData, $service->getMutableData(1234));
}
private function createService(
?BookingEditDataLoaderService $dataLoader = null,
?BookingEditDraftService $draftService = null,
?BookingService $bookingService = null,
?BookingSummaryDataService $summaryDataService = null,
?TravelDataService $travelDataService = null,
): BookingEditParticipantFormService {
return new BookingEditParticipantFormService(
$dataLoader ?? $this->createMock(BookingEditDataLoaderService::class),
$draftService ?? $this->createMock(BookingEditDraftService::class),
$bookingService ?? $this->createMock(BookingService::class),
$summaryDataService ?? $this->createMock(BookingSummaryDataService::class),
$travelDataService ?? $this->createMock(TravelDataService::class),
);
}
private function createBookingDto(): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$bookingDto->booking = new Booking();
return $bookingDto;
}
}
@@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Service\ParticipantFormSupportService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class ParticipantFormSupportServiceTest extends TestCase
{
public function testEnsureParticipantExistsThrowsForMissingIndex(): void
{
$service = $this->createService();
$bookingDto = $this->createBookingDto();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Participant at index 2 does not exist');
$service->ensureParticipantExists($bookingDto, 2);
}
public function testCreateParticipantEditDtoWrapsParticipant(): void
{
$service = $this->createService();
$bookingDto = $this->createBookingDto();
$wrapper = $service->createParticipantEditDto($bookingDto, 0);
$this->assertSame($bookingDto, $wrapper->bookingContext);
$this->assertSame($bookingDto->participants[0], $wrapper->participant);
}
public function testCollectAndClearNotificationsFlattensAndClearsNotifications(): void
{
$service = $this->createService();
$bookingDto = $this->createBookingDto();
$bookingDto->participants[0]->notifications = [
['type' => 'info', 'message' => 'First'],
];
$bookingDto->participants[1]->notifications = [
['type' => 'warning', 'message' => 'Second'],
];
$notifications = $service->collectAndClearNotifications($bookingDto);
$this->assertSame([
['type' => 'info', 'message' => 'First'],
['type' => 'warning', 'message' => 'Second'],
], $notifications);
$this->assertSame([], $bookingDto->participants[0]->notifications);
$this->assertSame([], $bookingDto->participants[1]->notifications);
}
public function testGetParticipantFormOptionsIncludesValidationToggle(): void
{
$parameterBag = $this->createMock(ParameterBagInterface::class);
$parameterBag->expects($this->exactly(4))
->method('get')
->willReturnMap([
['body_dimensions.height_choices', ['bis 148cm' => '-148']],
['body_dimensions.weight_choices', ['42 - 48kg' => '42-48']],
['body_dimensions.shoe_size_min', 36],
['body_dimensions.shoe_size_max', 48],
]);
$service = $this->createService(parameterBag: $parameterBag);
$bookingDto = $this->createBookingDto();
$options = $service->getParticipantFormOptions($bookingDto, true);
$this->assertSame($bookingDto, $options['booking_context']);
$this->assertSame(['bis 148cm' => '-148'], $options['height_choices']);
$this->assertSame(['42 - 48kg' => '42-48'], $options['weight_choices']);
$this->assertSame(36, $options['shoe_size_min']);
$this->assertSame(48, $options['shoe_size_max']);
$this->assertFalse($options['validation_groups']);
}
private function createService(?ParameterBagInterface $parameterBag = null): ParticipantFormSupportService
{
$parameterBag ??= $this->createMock(ParameterBagInterface::class);
return new ParticipantFormSupportService($parameterBag);
}
private function createBookingDto(): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$bookingDto->booking = new Booking();
$participantOne = new ParticipantDto();
$participantOne->index = 0;
$participantOne->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$bookingDto->participants[0] = $participantOne;
$participantTwo = new ParticipantDto();
$participantTwo->index = 1;
$participantTwo->dateOfBirth = new \DateTimeImmutable('1995-01-01');
$bookingDto->participants[1] = $participantTwo;
return $bookingDto;
}
}