feat: extract booking session handling

This commit is contained in:
Björn Fromme
2026-04-11 18:28:32 +02:00
parent ca052682bb
commit 8b72f8c277
21 changed files with 485 additions and 330 deletions
+115
View File
@@ -0,0 +1,115 @@
<?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\RoomSelectionDto;
use App\Service\BookingSessionService;
use App\Service\TravelDataService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
class BookingSessionServiceTest extends TestCase
{
private TravelDataService $travelDataService;
private BookingSessionService $service;
protected function setUp(): void
{
$this->travelDataService = $this->createMock(TravelDataService::class);
$this->service = new BookingSessionService($this->travelDataService);
}
public function testGetOrCreateBookingCreateDtoThrowsWhenSessionMissing(): void
{
$request = $this->createRequestWithSession();
$this->expectException(\App\Exception\BookingSessionNotFoundException::class);
$this->service->getOrCreateBookingCreateDto($request);
}
public function testSaveAndLoadBookingDtoHydratesTravel(): void
{
$request = $this->createRequestWithSession();
$storedTravel = new Travel();
$storedTravel->id = 12;
$storedTravel->hotelId = 34;
$bookingDto = new BookingDto($storedTravel, 34);
$bookingDto->booking = new Booking();
$bookingDto->booking->travelData = $storedTravel;
$hydratedTravel = new Travel();
$hydratedTravel->id = 12;
$hydratedTravel->hotelId = 34;
$hydratedTravel->label = 'Hydrated travel';
$this->travelDataService
->expects($this->once())
->method('getTravelData')
->with(12, 34)
->willReturn($hydratedTravel);
$this->service->saveBookingDto($request, $bookingDto, BookingDto::MODE_CREATE);
$loadedDto = $this->service->getBookingDto($request, BookingDto::MODE_CREATE);
$this->assertNotNull($loadedDto);
$this->assertSame($hydratedTravel, $loadedDto?->travel);
$this->assertSame($hydratedTravel, $loadedDto?->booking?->travelData);
}
public function testBaselineSnapshotIsCreatedAndReused(): void
{
$request = $this->createRequestWithSession();
$bookingDto = new BookingDto(new Travel(), 99);
$selection = new RoomSelectionDto();
$selection->id = 1;
$selection->quantity = 2;
$bookingDto->roomSelections = [$selection];
$first = $this->service->getOrCreateBaselineSnapshot($request, $bookingDto);
$this->assertSame([[1, 2]], $first);
$selection->quantity = 5;
$second = $this->service->getOrCreateBaselineSnapshot($request, $bookingDto);
$this->assertSame($first, $second);
$this->assertSame([[1, 2]], $second);
}
public function testStoreReturnUrlFallsBackToDefaultForInvalidInput(): void
{
$request = $this->createRequestWithSession();
$this->service->storeReturnUrl($request, 'javascript:alert(1)');
$this->assertSame(BookingSessionService::DEFAULT_RETURN_URL, $this->service->getReturnUrl($request));
}
public function testClearBookingSessionPreservesReturnUrl(): void
{
$request = $this->createRequestWithSession();
$this->service->storeReturnUrl($request, 'https://example.test/after-booking');
$this->service->clearBookingSession($request);
$this->assertSame('https://example.test/after-booking', $this->service->getReturnUrl($request));
}
private function createRequestWithSession(): Request
{
$session = new Session(new MockArraySessionStorage());
$request = new Request();
$request->setSession($session);
return $request;
}
}