feat: extract booking session handling
This commit is contained in:
@@ -11,7 +11,7 @@ use App\Form\Model\BookingDto;
|
||||
use App\Service\BookingEditDataLoaderService;
|
||||
use App\Service\BookingEditDraftService;
|
||||
use App\Service\BookingEditParticipantFormService;
|
||||
use App\Service\BookingService;
|
||||
use App\Service\BookingSessionService;
|
||||
use App\Service\BookingSummaryDataService;
|
||||
use App\Service\TravelDataService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -23,13 +23,13 @@ class BookingEditParticipantFormServiceTest extends TestCase
|
||||
{
|
||||
$request = new Request();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
$bookingService = $this->createMock(BookingService::class);
|
||||
$bookingService->expects($this->once())
|
||||
$bookingSessionService = $this->createMock(BookingSessionService::class);
|
||||
$bookingSessionService->expects($this->once())
|
||||
->method('getBookingDto')
|
||||
->with($request, BookingDto::MODE_EDIT)
|
||||
->willReturn($bookingDto);
|
||||
|
||||
$service = $this->createService(bookingService: $bookingService);
|
||||
$service = $this->createService(bookingSessionService: $bookingSessionService);
|
||||
|
||||
$this->assertSame($bookingDto, $service->loadBookingDto($request));
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class BookingEditParticipantFormServiceTest extends TestCase
|
||||
$service = new BookingEditParticipantFormService(
|
||||
$this->createMock(BookingEditDataLoaderService::class),
|
||||
$this->createMock(BookingEditDraftService::class),
|
||||
$this->createMock(BookingService::class),
|
||||
$this->createMock(BookingSessionService::class),
|
||||
$this->createMock(BookingSummaryDataService::class),
|
||||
$travelDataService,
|
||||
);
|
||||
@@ -67,14 +67,14 @@ class BookingEditParticipantFormServiceTest extends TestCase
|
||||
private function createService(
|
||||
?BookingEditDataLoaderService $dataLoader = null,
|
||||
?BookingEditDraftService $draftService = null,
|
||||
?BookingService $bookingService = null,
|
||||
?BookingSessionService $bookingSessionService = 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),
|
||||
$bookingSessionService ?? $this->createMock(BookingSessionService::class),
|
||||
$summaryDataService ?? $this->createMock(BookingSummaryDataService::class),
|
||||
$travelDataService ?? $this->createMock(TravelDataService::class),
|
||||
);
|
||||
|
||||
@@ -13,6 +13,7 @@ use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use App\Service\BookingPriceCalculatorService;
|
||||
use App\Service\BookingService;
|
||||
use App\Service\BookingSessionService;
|
||||
use App\Service\ParticipantEligibilityService;
|
||||
use App\Service\TravelDataService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -32,6 +33,7 @@ class BookingServiceBabyTest extends TestCase
|
||||
$agencyLoader = $this->createMock(AgencyLoader::class);
|
||||
|
||||
$this->bookingService = new BookingService(
|
||||
$this->createMock(BookingSessionService::class),
|
||||
$travelDataService,
|
||||
$priceCalculator,
|
||||
$this->participantEligibilityService,
|
||||
|
||||
@@ -13,6 +13,7 @@ use App\BusProNet\XmlLoader\AgencyLoader;
|
||||
use App\Exception\NoRoomsAvailableException;
|
||||
use App\Service\BookingPriceCalculatorService;
|
||||
use App\Service\BookingService;
|
||||
use App\Service\BookingSessionService;
|
||||
use App\Service\ParticipantEligibilityService;
|
||||
use App\Service\TravelDataService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -27,6 +28,7 @@ class BookingServiceStatusTest extends TestCase
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$bookingSessionService = $this->createMock(BookingSessionService::class);
|
||||
$this->travelDataService = $this->createMock(TravelDataService::class);
|
||||
$priceCalculator = $this->createMock(BookingPriceCalculatorService::class);
|
||||
$participantEligibility = $this->createMock(ParticipantEligibilityService::class);
|
||||
@@ -35,6 +37,7 @@ class BookingServiceStatusTest extends TestCase
|
||||
$agencyLoader = $this->createMock(AgencyLoader::class);
|
||||
|
||||
$this->bookingService = new BookingService(
|
||||
$bookingSessionService,
|
||||
$this->travelDataService,
|
||||
$priceCalculator,
|
||||
$participantEligibility,
|
||||
@@ -178,6 +181,7 @@ class BookingServiceStatusTest extends TestCase
|
||||
$bookingStatusRuleRegistry->method('evaluateStatus')->willReturn('O');
|
||||
|
||||
$bookingService = new BookingService(
|
||||
$this->createMock(BookingSessionService::class),
|
||||
$this->travelDataService,
|
||||
$this->createMock(BookingPriceCalculatorService::class),
|
||||
$this->createMock(ParticipantEligibilityService::class),
|
||||
@@ -209,6 +213,7 @@ class BookingServiceStatusTest extends TestCase
|
||||
$bookingStatusRuleRegistry->expects($this->never())->method('evaluateStatus');
|
||||
|
||||
$bookingService = new BookingService(
|
||||
$this->createMock(BookingSessionService::class),
|
||||
$this->travelDataService,
|
||||
$this->createMock(BookingPriceCalculatorService::class),
|
||||
$this->createMock(ParticipantEligibilityService::class),
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user