Files
myep/tests/Controller/Booking/Edit/ParticipantControllerTest.php
T

503 lines
18 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller\Booking\Edit;
use App\BusProNet\ApiClient;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Insurance;
use App\BusProNet\Model\Travel;
use App\Controller\Booking\Edit\ParticipantController;
use App\Entity\User;
use App\Form\BookingParticipantType;
use App\Form\Model\BookingDto;
use App\Form\Model\BookingEditContext;
use App\Form\Model\BookingSummaryDto;
use App\Form\Model\ParticipantDto;
use App\Form\Model\ParticipantEditDto;
use App\Security\Crypt;
use App\Service\BookingChangeTracker;
use App\Service\BookingConfigurator;
use App\Service\BookingEditContextFactory;
use App\Service\BookingEditDataLoader;
use App\Service\BookingEditDraftManager;
use App\Service\BookingSessionManager;
use App\Service\ParticipantDataPrefiller;
use App\Service\ParticipantFormSupport;
use Carbon\CarbonImmutable;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
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;
use Symfony\Component\Security\Core\User\UserInterface;
class ParticipantControllerTest extends TestCase
{
protected function tearDown(): void
{
CarbonImmutable::setTestNow();
}
public function testDummyTokenPrefillsParticipantInEditMode(): void
{
CarbonImmutable::setTestNow(CarbonImmutable::create(2026, 1, 15, 14, 30));
$request = Request::create('/bookings/42/edit/participants/0', 'POST');
$user = $this->createUser();
$bookingDto = $this->createBookingDto();
$bookingData = $this->createBooking();
$participant = $bookingDto->participants[0];
$participant->lastName = ParticipantDataPrefiller::TOKEN;
$form = $this->createMock(FormInterface::class);
$form->expects($this->once())
->method('handleRequest')
->with($request)
->willReturnSelf();
$form->expects($this->once())
->method('isSubmitted')
->willReturn(true);
$form->expects($this->never())
->method('isValid');
$dataLoader = $this->createMock(BookingEditDataLoader::class);
$dataLoader->expects($this->once())
->method('fetchBookingData')
->with(42, $user)
->willReturn($bookingData);
$bookingSessionService = $this->createMock(BookingSessionManager::class);
$bookingSessionService->expects($this->once())
->method('getBookingDto')
->with($request, BookingDto::MODE_EDIT)
->willReturn($bookingDto);
$bookingSessionService->expects($this->once())
->method('saveBookingDto')
->with($request, $bookingDto, BookingDto::MODE_EDIT);
$draftService = $this->createMock(BookingEditDraftManager::class);
$draftService->expects($this->once())
->method('saveDraft')
->with($user, 42, $bookingDto);
$prepopulationService = new ParticipantDataPrefiller(
$this->createMock(ApiClient::class),
$this->createMock(Crypt::class),
$this->createMock(LoggerInterface::class),
);
$participantFormSupportService = $this->createMock(ParticipantFormSupport::class);
$participantFormSupportService->expects($this->once())
->method('ensureParticipantExists')
->with($bookingDto, 0)
->willReturn($participant);
$participantFormSupportService->expects($this->exactly(2))
->method('createParticipantEditDto')
->with($bookingDto, 0)
->willReturn(new ParticipantEditDto($participant, $bookingDto));
$participantFormSupportService->expects($this->exactly(2))
->method('getParticipantFormOptions')
->with($bookingDto)
->willReturn([
'booking_context' => $bookingDto,
'body_dimension_ranges' => [
'height_min' => 100,
'height_max' => 250,
'weight_min' => 20,
'weight_max' => 200,
'shoe_size_min' => 20,
'shoe_size_max' => 55,
],
]);
$summaryData = $this->createMock(BookingSummaryDto::class);
$context = new BookingEditContext($bookingDto, $bookingData, null, $summaryData);
$contextFactory = $this->createMock(BookingEditContextFactory::class);
$contextFactory->expects($this->once())
->method('prepareBookingDto')
->with($bookingDto);
$contextFactory->expects($this->once())
->method('createParticipantContext')
->with($bookingDto, $bookingData)
->willReturn($context);
$controller = new TestableParticipantController(
$dataLoader,
$draftService,
$contextFactory,
$this->createMock(BookingConfigurator::class),
$bookingSessionService,
$prepopulationService,
$participantFormSupportService,
$user,
$form,
);
$response = $controller->editParticipant(42, 0, $request);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('booking/edit/participant.html.twig', $controller->renderTemplate);
$this->assertSame('Vorname 1', $participant->firstName);
$this->assertSame('Muster 1 14:30', $participant->lastName);
$this->assertSame('[email protected]', $participant->email);
$this->assertSame('2006-01-15', $participant->dateOfBirth?->format('Y-m-d'));
$this->assertSame(BookingParticipantType::class, $controller->createdFormTypes[0]);
$this->assertSame(BookingParticipantType::class, $controller->createdFormTypes[1]);
}
public function testRefreshPreselectsDefaultServicesAndRerendersParticipantForm(): void
{
$request = Request::create('/bookings/42/edit/participants/0/refresh', 'POST');
$user = $this->createUser();
$bookingDto = $this->createBookingDto();
$bookingData = $this->createBooking();
$participant = $bookingDto->participants[0];
$form = $this->createMock(FormInterface::class);
$form->expects($this->once())
->method('handleRequest')
->with($request)
->willReturnSelf();
$form->expects($this->once())
->method('createView')
->willReturn(new FormView());
$dataLoader = $this->createMock(BookingEditDataLoader::class);
$dataLoader->expects($this->once())
->method('fetchBookingData')
->with(42, $user)
->willReturn($bookingData);
$bookingSessionService = $this->createMock(BookingSessionManager::class);
$bookingSessionService->expects($this->once())
->method('getBookingDto')
->with($request, BookingDto::MODE_EDIT)
->willReturn($bookingDto);
$bookingSessionService->expects($this->once())
->method('saveBookingDto')
->with($request, $bookingDto, BookingDto::MODE_EDIT);
$bookingConfigurator = $this->createMock(BookingConfigurator::class);
$bookingConfigurator->expects($this->once())
->method('preselectDefaultServices')
->with($bookingDto);
$participantFormSupportService = $this->createMock(ParticipantFormSupport::class);
$participantFormSupportService->expects($this->once())
->method('ensureParticipantExists')
->with($bookingDto, 0)
->willReturn($participant);
$participantFormSupportService->expects($this->exactly(2))
->method('createParticipantEditDto')
->with($bookingDto, 0)
->willReturn(new ParticipantEditDto($participant, $bookingDto));
$participantFormSupportService->expects($this->exactly(2))
->method('getParticipantFormOptions')
->with($bookingDto, true)
->willReturn([
'booking_context' => $bookingDto,
'body_dimension_ranges' => [
'height_min' => 100,
'height_max' => 250,
'weight_min' => 20,
'weight_max' => 200,
'shoe_size_min' => 20,
'shoe_size_max' => 55,
],
'validation_groups' => false,
]);
$participantFormSupportService->expects($this->once())
->method('collectAndClearNotifications')
->with($bookingDto)
->willReturn([]);
$summaryData = $this->createMock(BookingSummaryDto::class);
$context = new BookingEditContext($bookingDto, $bookingData, null, $summaryData);
$contextFactory = $this->createMock(BookingEditContextFactory::class);
$contextFactory->expects($this->once())
->method('prepareBookingDto')
->with($bookingDto);
$contextFactory->expects($this->once())
->method('createParticipantContext')
->with($bookingDto, $bookingData)
->willReturn($context);
$prepopulationService = new ParticipantDataPrefiller(
$this->createMock(ApiClient::class),
$this->createMock(Crypt::class),
$this->createMock(LoggerInterface::class),
);
$controller = new TestableParticipantController(
$dataLoader,
$this->createMock(BookingEditDraftManager::class),
$contextFactory,
$bookingConfigurator,
$bookingSessionService,
$prepopulationService,
$participantFormSupportService,
$user,
$form,
);
$response = $controller->refreshParticipantForm(42, 0, $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame([BookingParticipantType::class, BookingParticipantType::class], $controller->createdFormTypes);
$this->assertSame(['participant_form', 'booking_summary'], $controller->renderedBlocks);
}
private function createUser(): User
{
return new User('[email protected]');
}
/**
* Regression: the edit flow must not mutate the BookingDto on a page view.
*
* BookingChangeTracker hashes each participant's insurance into the fingerprint that
* drives unsaved-changes detection, and the baseline is taken from un-cascaded API data
* in BookingEditDataLoader before the user reaches this route. An earlier revision called
* ApplicantInsuranceCascade here, which cleared dependents' BPN-loaded insurance and
* re-tiered the applicant's. That made the booking permanently "dirty": the overview only
* re-baselines while still clean, and the reload-from-BusPro path is gated on the same
* flag, so BusPro-side changes stopped surfacing for the rest of the session.
*/
public function testRefreshDoesNotDirtyFingerprintForFamilyInsuranceBooking(): void
{
$request = Request::create('/bookings/42/edit/participants/0/refresh', 'POST');
$user = $this->createUser();
$bookingDto = $this->createFamilyInsuranceBookingDto();
$bookingData = $this->createBooking();
$bookingData->participantsStatus = ['F', 'F'];
$participant = $bookingDto->participants[0];
$applicantInsurance = $bookingDto->participants[0]->insurance;
$dependentInsurance = $bookingDto->participants[1]->insurance;
$changeTracker = new BookingChangeTracker();
$bookingDto->originalFingerprint = $changeTracker->generateFingerprint($bookingDto);
$this->assertFalse($changeTracker->isDirty($bookingDto), 'Baseline must start clean');
$form = $this->createMock(FormInterface::class);
$form->method('handleRequest')->willReturnSelf();
$form->method('createView')->willReturn(new FormView());
$dataLoader = $this->createMock(BookingEditDataLoader::class);
$dataLoader->method('fetchBookingData')->willReturn($bookingData);
$bookingSessionService = $this->createMock(BookingSessionManager::class);
$bookingSessionService->method('getBookingDto')->willReturn($bookingDto);
$participantFormSupportService = $this->createMock(ParticipantFormSupport::class);
$participantFormSupportService->method('ensureParticipantExists')->willReturn($participant);
$participantFormSupportService->method('createParticipantEditDto')
->willReturn(new ParticipantEditDto($participant, $bookingDto));
$participantFormSupportService->method('getParticipantFormOptions')->willReturn([
'booking_context' => $bookingDto,
'validation_groups' => false,
]);
$participantFormSupportService->method('collectAndClearNotifications')->willReturn([]);
$summaryData = $this->createMock(BookingSummaryDto::class);
$contextFactory = $this->createMock(BookingEditContextFactory::class);
$contextFactory->method('createParticipantContext')
->willReturn(new BookingEditContext($bookingDto, $bookingData, null, $summaryData));
$controller = new TestableParticipantController(
$dataLoader,
$this->createMock(BookingEditDraftManager::class),
$contextFactory,
$this->createMock(BookingConfigurator::class),
$bookingSessionService,
new ParticipantDataPrefiller(
$this->createMock(ApiClient::class),
$this->createMock(Crypt::class),
$this->createMock(LoggerInterface::class),
),
$participantFormSupportService,
$user,
$form,
);
$controller->refreshParticipantForm(42, 0, $request);
$this->assertFalse(
$changeTracker->isDirty($bookingDto),
'Viewing a participant must not mark an unchanged booking as dirty'
);
$this->assertSame(
$applicantInsurance,
$bookingDto->participants[0]->insurance,
"Applicant's insurance must not be re-tiered on a page view"
);
$this->assertSame(
$dependentInsurance,
$bookingDto->participants[1]->insurance,
"Dependent's BusPro-loaded insurance must survive a page view"
);
}
private function createBookingDto(): BookingDto
{
$travel = new Travel();
$travel->id = 1234;
$bookingDto = new BookingDto($travel, 77);
$bookingDto->booking = new Booking();
$bookingDto->booking->id = 42;
$bookingDto->participants = [new ParticipantDto()];
$bookingDto->participants[0]->index = 0;
return $bookingDto;
}
private function createFamilyInsuranceBookingDto(): BookingDto
{
$bookingDto = $this->createBookingDto();
$familyInsurance = new Insurance();
$familyInsurance->id = 'family-1';
$familyInsurance->label = 'Familienversicherung';
$familyInsurance->price = 120.0;
$familyInsurance->subType = 'RRV';
$familyInsurance->familyInsurance = true;
// BusPro can map one policy to several participants via <zuordnung>, so a dependent
// may legitimately load holding it too.
$dependentInsurance = new Insurance();
$dependentInsurance->id = 'dependent-own';
$dependentInsurance->label = 'Reise-Rucktritt';
$dependentInsurance->price = 50.0;
$dependentInsurance->subType = 'RRV';
$dependentInsurance->familyInsurance = false;
$bookingDto->participants[0]->insurance = $familyInsurance;
$dependent = new ParticipantDto();
$dependent->index = 1;
$dependent->insurance = $dependentInsurance;
$bookingDto->participants[] = $dependent;
return $bookingDto;
}
private function createBooking(): Booking
{
$booking = new Booking();
$booking->id = 42;
$booking->dateId = 1234;
$booking->participantsStatus = ['F'];
return $booking;
}
}
final class TestableParticipantController extends ParticipantController
{
/**
* @var array<int, string>
*/
public array $createdFormTypes = [];
public ?string $renderTemplate = null;
/**
* @var list<string>
*/
public array $renderedBlocks = [];
/**
* @var array<string, mixed>
*/
public array $renderParameters = [];
/**
* @var FormInterface<mixed>
*/
private readonly FormInterface $form;
/**
* @param FormInterface<mixed> $form
*/
public function __construct(
BookingEditDataLoader $dataLoader,
BookingEditDraftManager $draftService,
BookingEditContextFactory $editContextFactory,
BookingConfigurator $bookingService,
BookingSessionManager $bookingSessionService,
ParticipantDataPrefiller $prepopulationService,
ParticipantFormSupport $participantFormSupportService,
private readonly User $user,
FormInterface $form,
) {
$this->form = $form;
parent::__construct(
$dataLoader,
$draftService,
$editContextFactory,
$bookingService,
$bookingSessionService,
$prepopulationService,
$participantFormSupportService,
new NullLogger(),
);
}
protected function getUser(): UserInterface
{
return $this->user;
}
/**
* @return FormInterface<mixed>
*/
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
{
$this->createdFormTypes[] = $type;
return $this->form;
}
protected function addFlash(string $type, mixed $message): void
{
}
/**
* @param array<string, mixed> $parameters
*/
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
{
return new RedirectResponse('/'.$route, $status);
}
/**
* @param array<string, mixed> $parameters
*/
protected function render(string $view, array $parameters = [], ?Response $response = null): Response
{
$this->renderTemplate = $view;
$this->renderParameters = $parameters;
return new Response('');
}
/**
* @param array<string, mixed> $parameters
*/
protected function renderBlockView(string $view, string $block, array $parameters = []): string
{
$this->renderedBlocks[] = $block;
return '<div></div>';
}
}