feat: pre-flight check of agency bookings

addresses #869bqxr4q
This commit is contained in:
Björn Fromme
2026-07-10 14:33:09 +02:00
parent f9142e3080
commit b41b19d4c6
26 changed files with 1818 additions and 304 deletions
@@ -6,14 +6,20 @@ namespace App\Tests\Controller\Booking\Edit;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Controller\Booking\Edit\IndexController;
use App\Entity\User;
use App\Form\Model\BookingDto;
use App\Form\Model\BookingEditContext;
use App\Form\Model\BookingSummaryDto;
use App\Form\Model\ParticipantCardDataDto;
use App\Form\Model\ParticipantCardPriceDto;
use App\Model\BookingEditSubmissionResult;
use App\Service\BookingChangeTracker;
use App\Service\BookingEditContextFactory;
use App\Service\BookingEditDataLoader;
use App\Service\BookingEditDraftManager;
use App\Service\BookingEditPreFlightChecker;
use App\Service\BookingEditSubmitter;
use App\Service\BookingSessionManager;
use PHPUnit\Framework\TestCase;
@@ -41,6 +47,309 @@ class IndexControllerTest extends TestCase
);
}
public function testIndexShowsAttentionWarningWithoutBlockingInternalAgencyOverview(): void
{
$request = Request::create('/bookings/42/edit', 'GET');
$user = $this->createUser();
$bookingDto = $this->createBookingDto();
$bookingDto->agencyCode = AgencyLoader::INTERNAL_AGENCY_CODE;
$bookingDto->participants = [
$this->createParticipant(),
$this->createParticipant(),
];
$bookingData = $this->createBooking();
$form = $this->createMock(FormInterface::class);
$form->method('isSubmitted')->willReturn(false);
$fingerprintService = $this->createMock(BookingChangeTracker::class);
$fingerprintService->expects($this->once())
->method('isDirty')
->with($bookingDto)
->willReturn(true);
$dataLoader = $this->createMock(BookingEditDataLoader::class);
$dataLoader->expects($this->once())
->method('loadFormData')
->with($request, 42, $user)
->willReturn($bookingDto);
$dataLoader->expects($this->once())
->method('fetchBookingData')
->with(42, $user)
->willReturn($bookingData);
$dataLoader->expects($this->once())
->method('isDraftRestored')
->willReturn(false);
$preflightChecker = $this->createMock(BookingEditPreFlightChecker::class);
$preflightChecker->expects($this->once())
->method('findMissingValueLabelsByParticipantIndex')
->with($bookingDto)
->willReturn([1 => ['E-Mail', 'Straße']]);
$contextFactory = $this->createMock(BookingEditContextFactory::class);
$contextFactory->expects($this->once())
->method('createOverviewContext')
->with($bookingDto, $bookingData, true, false, false, [1 => ['E-Mail', 'Straße']])
->willReturn($this->createOverviewContext($bookingDto, $bookingData, true, false, false, [1 => ['E-Mail', 'Straße']]));
$controller = new TestableIndexController(
$dataLoader,
$this->createMock(BookingEditDraftManager::class),
$this->createMock(BookingSessionManager::class),
$fingerprintService,
$contextFactory,
$preflightChecker,
$this->createMock(BookingEditSubmitter::class),
$user,
$form,
);
$response = $controller->index(42, $request);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame([], $controller->flashes);
$this->assertTrue($controller->renderParameters['bookingEditContext']->isDirty);
$this->assertFalse($controller->renderParameters['bookingEditContext']->hasBlockingValidationErrors);
$this->assertSame(
[1 => ['E-Mail', 'Straße']],
$controller->renderParameters['bookingEditContext']->missingValueLabelsByParticipantIndex
);
$this->assertTrue($controller->renderParameters['bookingEditContext']->cardsData[0]->isValid);
$this->assertTrue($controller->renderParameters['bookingEditContext']->cardsData[1]->isValid);
}
public function testIndexShowsAttentionWarningWithoutBlockingNonAgencyOverview(): void
{
$request = Request::create('/bookings/42/edit', 'GET');
$user = $this->createUser();
$bookingDto = $this->createBookingDto();
$bookingDto->agencyCode = 'OTHER';
$bookingDto->participants = [
$this->createParticipant(),
$this->createParticipant(),
];
$bookingData = $this->createBooking();
$form = $this->createMock(FormInterface::class);
$form->method('isSubmitted')->willReturn(false);
$fingerprintService = $this->createMock(BookingChangeTracker::class);
$fingerprintService->expects($this->once())
->method('isDirty')
->with($bookingDto)
->willReturn(false);
$dataLoader = $this->createMock(BookingEditDataLoader::class);
$dataLoader->expects($this->once())
->method('loadFormData')
->with($request, 42, $user)
->willReturn($bookingDto);
$dataLoader->expects($this->once())
->method('fetchBookingData')
->with(42, $user)
->willReturn($bookingData);
$dataLoader->expects($this->once())
->method('isDraftRestored')
->willReturn(false);
$preflightChecker = $this->createMock(BookingEditPreFlightChecker::class);
$preflightChecker->expects($this->once())
->method('findMissingValueLabelsByParticipantIndex')
->with($bookingDto)
->willReturn([0 => ['E-Mail']]);
$contextFactory = $this->createMock(BookingEditContextFactory::class);
$contextFactory->expects($this->once())
->method('createOverviewContext')
->with($bookingDto, $bookingData, false, false, false, [0 => ['E-Mail']])
->willReturn($this->createOverviewContext($bookingDto, $bookingData, false, false, false, [0 => ['E-Mail']]));
$controller = new TestableIndexController(
$dataLoader,
$this->createMock(BookingEditDraftManager::class),
$this->createMock(BookingSessionManager::class),
$fingerprintService,
$contextFactory,
$preflightChecker,
$this->createMock(BookingEditSubmitter::class),
$user,
$form,
);
$response = $controller->index(42, $request);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame([], $controller->flashes);
$this->assertFalse($controller->renderParameters['bookingEditContext']->isDirty);
$this->assertFalse($controller->renderParameters['bookingEditContext']->hasBlockingValidationErrors);
$this->assertSame(
[0 => ['E-Mail']],
$controller->renderParameters['bookingEditContext']->missingValueLabelsByParticipantIndex
);
}
public function testIndexStillSubmitsInternalAgencyBookingsWhenOverviewIsInvalid(): void
{
$request = Request::create('/bookings/42/edit', 'POST');
$user = $this->createUser();
$bookingDto = $this->createBookingDto();
$bookingDto->agencyCode = AgencyLoader::INTERNAL_AGENCY_CODE;
$bookingDto->participants = [
$this->createParticipant(),
$this->createParticipant(),
];
$bookingData = $this->createBooking();
$form = $this->createMock(FormInterface::class);
$form->expects($this->once())
->method('handleRequest')
->with($request)
->willReturnSelf();
$form->method('isSubmitted')->willReturn(true);
$form->expects($this->once())
->method('isValid')
->willReturn(false);
$fingerprintService = $this->createMock(BookingChangeTracker::class);
$fingerprintService->expects($this->never())
->method('isDirty');
$dataLoader = $this->createMock(BookingEditDataLoader::class);
$dataLoader->expects($this->once())
->method('loadFormData')
->with($request, 42, $user)
->willReturn($bookingDto);
$dataLoader->expects($this->once())
->method('fetchBookingData')
->with(42, $user)
->willReturn($bookingData);
$dataLoader->expects($this->once())
->method('isDraftRestored')
->willReturn(false);
$preflightChecker = $this->createMock(BookingEditPreFlightChecker::class);
$preflightChecker->expects($this->once())
->method('findMissingValueLabelsByParticipantIndex')
->with($bookingDto)
->willReturn([1 => ['E-Mail', 'Straße']]);
$submitter = $this->createMock(BookingEditSubmitter::class);
$submitter->expects($this->once())
->method('handleSubmission')
->with($request, $bookingDto, 42, $user)
->willReturn(new BookingEditSubmissionResult(
BookingEditSubmissionResult::STATUS_SUCCESS,
null,
false
));
$contextFactory = $this->createMock(BookingEditContextFactory::class);
$contextFactory->expects($this->never())
->method('createOverviewContext');
$controller = new TestableIndexController(
$dataLoader,
$this->createMock(BookingEditDraftManager::class),
$this->createMock(BookingSessionManager::class),
$fingerprintService,
$contextFactory,
$preflightChecker,
$submitter,
$user,
$form,
);
$response = $controller->index(42, $request);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
$this->assertSame(
[
['success', 'Buchung erfolgreich aktualisiert'],
['booking_edit_notice', 'Bitte ladet euch eine aktualisierte Rechnung herunter, damit ihr stets den aktuellen Stand eurer Buchung vorliegen habt.'],
],
$controller->flashes
);
}
public function testIndexBlocksNonInternalAgencyBookingsWhenOverviewIsInvalid(): void
{
$request = Request::create('/bookings/42/edit', 'POST');
$user = $this->createUser();
$bookingDto = $this->createBookingDto();
$bookingDto->agencyCode = 'OTHER';
$bookingDto->participants = [
$this->createParticipant(),
$this->createParticipant(),
];
$bookingData = $this->createBooking();
$form = $this->createMock(FormInterface::class);
$form->expects($this->once())
->method('handleRequest')
->with($request)
->willReturnSelf();
$form->method('isSubmitted')->willReturn(true);
$form->expects($this->once())
->method('isValid')
->willReturn(false);
$fingerprintService = $this->createMock(BookingChangeTracker::class);
$fingerprintService->expects($this->once())
->method('isDirty')
->with($bookingDto)
->willReturn(false);
$dataLoader = $this->createMock(BookingEditDataLoader::class);
$dataLoader->expects($this->once())
->method('loadFormData')
->with($request, 42, $user)
->willReturn($bookingDto);
$dataLoader->expects($this->once())
->method('fetchBookingData')
->with(42, $user)
->willReturn($bookingData);
$dataLoader->expects($this->once())
->method('isDraftRestored')
->willReturn(false);
$preflightChecker = $this->createMock(BookingEditPreFlightChecker::class);
$preflightChecker->expects($this->once())
->method('findMissingValueLabelsByParticipantIndex')
->with($bookingDto)
->willReturn([0 => ['E-Mail']]);
$submitter = $this->createMock(BookingEditSubmitter::class);
$submitter->expects($this->never())
->method('handleSubmission');
$contextFactory = $this->createMock(BookingEditContextFactory::class);
$contextFactory->expects($this->once())
->method('createOverviewContext')
->with($bookingDto, $bookingData, false, true, true, [0 => ['E-Mail']])
->willReturn($this->createOverviewContext($bookingDto, $bookingData, false, true, true, [0 => ['E-Mail']]));
$controller = new TestableIndexController(
$dataLoader,
$this->createMock(BookingEditDraftManager::class),
$this->createMock(BookingSessionManager::class),
$fingerprintService,
$contextFactory,
$preflightChecker,
$submitter,
$user,
$form,
);
$response = $controller->index(42, $request);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame([], $controller->flashes);
$this->assertTrue($controller->renderParameters['bookingEditContext']->hasBlockingValidationErrors);
$this->assertSame([0 => ['E-Mail']], $controller->renderParameters['bookingEditContext']->missingValueLabelsByParticipantIndex);
}
/**
* @dataProvider submissionResultProvider
*
@@ -119,6 +428,49 @@ class IndexControllerTest extends TestCase
return $booking;
}
private function createParticipant(): \App\Form\Model\ParticipantDto
{
return new \App\Form\Model\ParticipantDto();
}
private function createOverviewContext(
BookingDto $bookingDto,
Booking $bookingData,
bool $isDirty,
bool $isSubmitted,
bool $hasBlockingValidationErrors,
array $missingValueLabelsByParticipantIndex = [],
): BookingEditContext {
return new BookingEditContext(
bookingDto: $bookingDto,
bookingData: $bookingData,
mutableData: null,
summaryData: $this->createMock(BookingSummaryDto::class),
cardsData: [
0 => new ParticipantCardDataDto(
'One',
'[email protected]',
'Room A',
new ParticipantCardPriceDto(10.0, false),
false,
true
),
1 => new ParticipantCardDataDto(
'Two',
'[email protected]',
'Room B',
new ParticipantCardPriceDto(20.0, false),
false,
true
),
],
missingValueLabelsByParticipantIndex: $missingValueLabelsByParticipantIndex,
isDirty: $isDirty,
isSubmitted: $isSubmitted,
hasBlockingValidationErrors: $hasBlockingValidationErrors,
);
}
/**
* @param array<int, array{0: string, 1: string}> $expectedFlashes
*/
@@ -131,14 +483,15 @@ class IndexControllerTest extends TestCase
$bookingDto = $this->createBookingDto();
$bookingData = $this->createBooking();
$form = $this->createMock(FormInterface::class);
$fingerprintService = $this->createMock(BookingChangeTracker::class);
$fingerprintService->expects($this->never())
->method('isDirty');
$form->expects($this->once())
->method('handleRequest')
->with($request)
->willReturnSelf();
$form->expects($this->once())
->method('isSubmitted')
->willReturn(true);
$form->method('isSubmitted')->willReturn(true);
$form->expects($this->once())
->method('isValid')
->willReturn(true);
@@ -153,7 +506,14 @@ class IndexControllerTest extends TestCase
->with(42, $user)
->willReturn($bookingData);
$dataLoader->expects($this->once())
->method('isDraftRestored');
->method('isDraftRestored')
->willReturn(false);
$preflightChecker = $this->createMock(BookingEditPreFlightChecker::class);
$preflightChecker->expects($this->once())
->method('findMissingValueLabelsByParticipantIndex')
->with($bookingDto)
->willReturn([]);
$submitter = $this->createMock(BookingEditSubmitter::class);
$submitter->expects($this->once())
@@ -165,8 +525,9 @@ class IndexControllerTest extends TestCase
$dataLoader,
$this->createMock(BookingEditDraftManager::class),
$this->createMock(BookingSessionManager::class),
$this->createMock(BookingChangeTracker::class),
$fingerprintService,
$this->createMock(BookingEditContextFactory::class),
$preflightChecker,
$submitter,
$user,
$form,
@@ -187,6 +548,11 @@ final class TestableIndexController extends IndexController
*/
public array $flashes = [];
/**
* @var array<string, mixed>
*/
public array $renderParameters = [];
/**
* @var FormInterface<mixed>
*/
@@ -201,6 +567,7 @@ final class TestableIndexController extends IndexController
BookingSessionManager $bookingSessionService,
BookingChangeTracker $fingerprintService,
BookingEditContextFactory $editContextFactory,
BookingEditPreFlightChecker $preFlightChecker,
BookingEditSubmitter $formSubmitter,
private readonly User $user,
FormInterface $form,
@@ -213,6 +580,7 @@ final class TestableIndexController extends IndexController
$bookingSessionService,
$fingerprintService,
$editContextFactory,
$preFlightChecker,
$formSubmitter,
);
}
@@ -253,6 +621,8 @@ final class TestableIndexController extends IndexController
*/
protected function render(string $view, array $parameters = [], ?Response $response = null): Response
{
throw new \LogicException('Render should not be called in this test.');
$this->renderParameters = $parameters;
return new Response('');
}
}