feat: centralize create/edit flow contexts, extract edit submit service
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\BusProNet\Model\Room;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Form\Model\BookingCreateContext;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\ParticipantCardDataDto;
|
||||
use App\Form\Model\ParticipantCardPriceDto;
|
||||
use App\Form\Model\BookingSummaryDto;
|
||||
use App\Service\BookingCreateContextFactory;
|
||||
use App\Service\BookingRoomSelectionService;
|
||||
use App\Service\ParticipantCardDataService;
|
||||
use App\Service\BookingSummaryDataService;
|
||||
use App\Service\BookingPriceCalculatorService;
|
||||
use App\Service\RoomPricingCalculator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BookingCreateContextFactoryTest extends TestCase
|
||||
{
|
||||
public function testCreateBuildsSelectionContextWhenRequested(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
|
||||
|
||||
$roomByRoom = new Room();
|
||||
$roomByRoom->id = 10;
|
||||
$roomByRoom->label = 'Doppelzimmer';
|
||||
$roomByRoom->maxPax = 2;
|
||||
$roomByRoom->available = 4;
|
||||
$roomByRoom->status = 'Frei';
|
||||
|
||||
$roomByPax = new Room();
|
||||
$roomByPax->id = 11;
|
||||
$roomByPax->label = '6-Bett Zimmer';
|
||||
$roomByPax->maxPax = 6;
|
||||
$roomByPax->available = 2;
|
||||
$roomByPax->status = 'Frei';
|
||||
|
||||
$travel->rooms = [$roomByRoom, $roomByPax];
|
||||
|
||||
$bookingDto = new BookingDto($travel, 157047);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with(
|
||||
$bookingDto,
|
||||
RoomPricingCalculator::PRICING_MODE_SELECTION
|
||||
)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$roomSelectionService = $this->createMock(BookingRoomSelectionService::class);
|
||||
$roomSelectionService->expects($this->once())
|
||||
->method('groupRoomsBySelectionType')
|
||||
->with([$roomByRoom->id => $roomByRoom, $roomByPax->id => $roomByPax])
|
||||
->willReturn([
|
||||
Room::SELECTION_TYPE_BY_PAX => [$roomByPax->id => $roomByPax],
|
||||
Room::SELECTION_TYPE_BY_ROOM => [$roomByRoom->id => $roomByRoom],
|
||||
]);
|
||||
|
||||
$participantCardDataService = $this->createMock(ParticipantCardDataService::class);
|
||||
$participantCardDataService->expects($this->never())
|
||||
->method('getAllCardsDataWithValidation');
|
||||
|
||||
$priceCalculator = $this->createMock(BookingPriceCalculatorService::class);
|
||||
$priceCalculator->expects($this->never())
|
||||
->method('calculateAllParticipantIndividualPrices');
|
||||
|
||||
$service = new BookingCreateContextFactory(
|
||||
$roomSelectionService,
|
||||
$participantCardDataService,
|
||||
$summaryDataService,
|
||||
$priceCalculator,
|
||||
);
|
||||
|
||||
$context = $service->create($bookingDto, RoomPricingCalculator::PRICING_MODE_SELECTION);
|
||||
|
||||
$this->assertInstanceOf(BookingCreateContext::class, $context);
|
||||
$this->assertSame($bookingDto, $context->bookingDto);
|
||||
$this->assertSame($summaryData, $context->summaryData);
|
||||
$this->assertSame(null, $context->cardsData);
|
||||
$this->assertFalse($context->isSubmitted);
|
||||
$this->assertSame([
|
||||
Room::SELECTION_TYPE_BY_PAX => [$roomByPax->id => $roomByPax],
|
||||
Room::SELECTION_TYPE_BY_ROOM => [$roomByRoom->id => $roomByRoom],
|
||||
], $context->groupedRooms);
|
||||
}
|
||||
|
||||
public function testCreateWithParticipantCardsBuildsStep2Context(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
|
||||
|
||||
$room = new Room();
|
||||
$room->id = 10;
|
||||
$room->label = 'Doppelzimmer';
|
||||
$room->maxPax = 2;
|
||||
$room->available = 4;
|
||||
$room->status = 'Frei';
|
||||
|
||||
$travel->rooms = [$room];
|
||||
|
||||
$bookingDto = new BookingDto($travel, 157047);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
$cardData = new ParticipantCardDataDto(
|
||||
name: 'Max Mustermann',
|
||||
email: '[email protected]',
|
||||
roomName: 'Doppelzimmer',
|
||||
price: new ParticipantCardPriceDto(123.45, false),
|
||||
isCanceled: false,
|
||||
);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto, RoomPricingCalculator::PRICING_MODE_SELECTION)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$roomSelectionService = $this->createMock(BookingRoomSelectionService::class);
|
||||
$roomSelectionService->expects($this->once())
|
||||
->method('groupRoomsBySelectionType')
|
||||
->with([$room->id => $room])
|
||||
->willReturn([
|
||||
Room::SELECTION_TYPE_BY_PAX => [],
|
||||
Room::SELECTION_TYPE_BY_ROOM => [$room->id => $room],
|
||||
]);
|
||||
|
||||
$participantCardDataService = $this->createMock(ParticipantCardDataService::class);
|
||||
$participantCardDataService->expects($this->once())
|
||||
->method('getAllCardsDataWithValidation')
|
||||
->with($bookingDto)
|
||||
->willReturn([$cardData]);
|
||||
|
||||
$priceCalculator = $this->createMock(BookingPriceCalculatorService::class);
|
||||
$priceCalculator->expects($this->never())
|
||||
->method('calculateAllParticipantIndividualPrices');
|
||||
|
||||
$service = new BookingCreateContextFactory(
|
||||
$roomSelectionService,
|
||||
$participantCardDataService,
|
||||
$summaryDataService,
|
||||
$priceCalculator,
|
||||
);
|
||||
|
||||
$context = $service->createWithParticipantCards($bookingDto, true);
|
||||
|
||||
$this->assertInstanceOf(BookingCreateContext::class, $context);
|
||||
$this->assertSame([$cardData], $context->cardsData);
|
||||
$this->assertTrue($context->isSubmitted);
|
||||
}
|
||||
|
||||
public function testCreateWithParticipantPricesBuildsStep4Context(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
|
||||
|
||||
$room = new Room();
|
||||
$room->id = 10;
|
||||
$room->label = 'Doppelzimmer';
|
||||
$room->maxPax = 2;
|
||||
$room->available = 4;
|
||||
$room->status = 'Frei';
|
||||
|
||||
$travel->rooms = [$room];
|
||||
|
||||
$bookingDto = new BookingDto($travel, 157047);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto, RoomPricingCalculator::PRICING_MODE_ASSIGNMENT)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$roomSelectionService = $this->createMock(BookingRoomSelectionService::class);
|
||||
$roomSelectionService->expects($this->once())
|
||||
->method('groupRoomsBySelectionType')
|
||||
->with([$room->id => $room])
|
||||
->willReturn([
|
||||
Room::SELECTION_TYPE_BY_PAX => [],
|
||||
Room::SELECTION_TYPE_BY_ROOM => [$room->id => $room],
|
||||
]);
|
||||
|
||||
$participantCardDataService = $this->createMock(ParticipantCardDataService::class);
|
||||
$participantCardDataService->expects($this->never())
|
||||
->method('getAllCardsDataWithValidation');
|
||||
|
||||
$priceCalculator = $this->createMock(BookingPriceCalculatorService::class);
|
||||
$priceCalculator->expects($this->once())
|
||||
->method('calculateAllParticipantIndividualPrices')
|
||||
->with($bookingDto)
|
||||
->willReturn([123.45]);
|
||||
|
||||
$service = new BookingCreateContextFactory(
|
||||
$roomSelectionService,
|
||||
$participantCardDataService,
|
||||
$summaryDataService,
|
||||
$priceCalculator,
|
||||
);
|
||||
|
||||
$context = $service->createWithParticipantPrices($bookingDto);
|
||||
|
||||
$this->assertInstanceOf(BookingCreateContext::class, $context);
|
||||
$this->assertSame([123.45], $context->participantPrices);
|
||||
$this->assertNull($context->cardsData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
<?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\Form\Model\BookingEditContext;
|
||||
use App\Form\Model\BookingSummaryDto;
|
||||
use App\Service\BookingEditContextFactory;
|
||||
use App\Service\BookingSummaryDataService;
|
||||
use App\Service\ParticipantCardDataService;
|
||||
use App\Service\TravelDataService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BookingEditContextFactoryTest extends TestCase
|
||||
{
|
||||
public function testPrepareBookingDtoRefreshesTravelAvailability(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
|
||||
$bookingDto = new BookingDto($travel, 157047);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('enrichWithFreshAvailabilities')
|
||||
->with($travel);
|
||||
|
||||
$service = new BookingEditContextFactory(
|
||||
$this->createMock(ParticipantCardDataService::class),
|
||||
$this->createMock(BookingSummaryDataService::class),
|
||||
$travelDataService,
|
||||
);
|
||||
|
||||
$service->prepareBookingDto($bookingDto);
|
||||
}
|
||||
|
||||
public function testCreateBuildsEditContext(): void
|
||||
{
|
||||
$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();
|
||||
|
||||
$bookingData = new Booking();
|
||||
$bookingData->dateId = 1234;
|
||||
|
||||
$mutableData = new BaseData([]);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234)
|
||||
->willReturn($mutableData);
|
||||
|
||||
$service = new BookingEditContextFactory(
|
||||
$this->createMock(ParticipantCardDataService::class),
|
||||
$summaryDataService,
|
||||
$travelDataService,
|
||||
);
|
||||
|
||||
$context = $service->createParticipantContext($bookingDto, $bookingData);
|
||||
|
||||
$this->assertInstanceOf(BookingEditContext::class, $context);
|
||||
$this->assertSame($bookingDto, $context->bookingDto);
|
||||
$this->assertSame($bookingData, $context->bookingData);
|
||||
$this->assertSame($mutableData, $context->mutableData);
|
||||
$this->assertSame($summaryData, $context->summaryData);
|
||||
}
|
||||
|
||||
public function testCreateParticipantContextWithoutBookingDataFallsBackToSummaryOnly(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
|
||||
|
||||
$bookingDto = new BookingDto($travel, 157047);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('getMutabilityData');
|
||||
|
||||
$service = new BookingEditContextFactory(
|
||||
$this->createMock(ParticipantCardDataService::class),
|
||||
$summaryDataService,
|
||||
$travelDataService,
|
||||
);
|
||||
|
||||
$context = $service->createParticipantContext($bookingDto, null);
|
||||
|
||||
$this->assertInstanceOf(BookingEditContext::class, $context);
|
||||
$this->assertSame($bookingDto, $context->bookingDto);
|
||||
$this->assertNull($context->bookingData);
|
||||
$this->assertNull($context->mutableData);
|
||||
$this->assertSame($summaryData, $context->summaryData);
|
||||
}
|
||||
|
||||
public function testCreateOverviewContextBuildsEditOverviewPayload(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
|
||||
|
||||
$bookingDto = new BookingDto($travel, 157047);
|
||||
$bookingData = new Booking();
|
||||
$bookingData->dateId = 1234;
|
||||
|
||||
$mutableData = new BaseData([]);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
$cardsData = [];
|
||||
|
||||
$participantCardDataService = $this->createMock(ParticipantCardDataService::class);
|
||||
$participantCardDataService->expects($this->once())
|
||||
->method('getAllCardsDataWithValidation')
|
||||
->with($bookingDto)
|
||||
->willReturn($cardsData);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234)
|
||||
->willReturn($mutableData);
|
||||
|
||||
$service = new BookingEditContextFactory(
|
||||
$participantCardDataService,
|
||||
$summaryDataService,
|
||||
$travelDataService,
|
||||
);
|
||||
|
||||
$context = $service->createOverviewContext($bookingDto, $bookingData, true, false, true);
|
||||
|
||||
$this->assertInstanceOf(BookingEditContext::class, $context);
|
||||
$this->assertSame($cardsData, $context->cardsData);
|
||||
$this->assertTrue($context->isDirty);
|
||||
$this->assertFalse($context->isSubmitted);
|
||||
$this->assertTrue($context->hasValidationErrors);
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
<?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\Form\Model\BookingEditParticipantContext;
|
||||
use App\Form\Model\BookingSummaryDto;
|
||||
use App\Service\BookingEditParticipantContextFactory;
|
||||
use App\Service\BookingSummaryDataService;
|
||||
use App\Service\TravelDataService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BookingEditParticipantContextFactoryTest extends TestCase
|
||||
{
|
||||
public function testPrepareBookingDtoRefreshesTravelAvailability(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
|
||||
$bookingDto = new BookingDto($travel, 157047);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('enrichWithFreshAvailabilities')
|
||||
->with($travel);
|
||||
|
||||
$service = new BookingEditParticipantContextFactory(
|
||||
$this->createMock(BookingSummaryDataService::class),
|
||||
$travelDataService,
|
||||
);
|
||||
|
||||
$service->prepareBookingDto($bookingDto);
|
||||
}
|
||||
|
||||
public function testCreateBuildsEditContext(): void
|
||||
{
|
||||
$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();
|
||||
|
||||
$bookingData = new Booking();
|
||||
$bookingData->dateId = 1234;
|
||||
|
||||
$mutableData = new BaseData([]);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234)
|
||||
->willReturn($mutableData);
|
||||
|
||||
$service = new BookingEditParticipantContextFactory($summaryDataService, $travelDataService);
|
||||
|
||||
$context = $service->create($bookingDto, $bookingData);
|
||||
|
||||
$this->assertInstanceOf(BookingEditParticipantContext::class, $context);
|
||||
$this->assertSame($bookingDto, $context->bookingDto);
|
||||
$this->assertSame($bookingData, $context->bookingData);
|
||||
$this->assertSame($mutableData, $context->mutableData);
|
||||
$this->assertSame($summaryData, $context->summaryData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,482 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Exception\TimeoutException;
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\BusProNet\Model\BookingUpdate;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Entity\User;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Service\BookingEditDataLoaderService;
|
||||
use App\Service\BookingEditDraftService;
|
||||
use App\Service\BookingEditSubmitGuardService;
|
||||
use App\Service\BookingEditSubmitService;
|
||||
use App\Service\BookingSessionService;
|
||||
use App\Service\TravelDataService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class BookingEditSubmitServiceTest extends TestCase
|
||||
{
|
||||
public function testHandleSubmissionReturnsRedirectWhenFreshBookingDataCannotBeLoaded(): void
|
||||
{
|
||||
$request = $this->createRequestWithSession();
|
||||
$user = $this->createUser();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
|
||||
$dataLoader = $this->createMock(BookingEditDataLoaderService::class);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('invalidateBookingCache')
|
||||
->with(42, $user);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('fetchBookingData')
|
||||
->with(42, $user)
|
||||
->willReturn(null);
|
||||
|
||||
$service = $this->createService(
|
||||
dataLoader: $dataLoader,
|
||||
);
|
||||
|
||||
$response = $service->handleSubmission($request, $bookingDto, 42, $user);
|
||||
|
||||
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
|
||||
$this->assertSame(
|
||||
['Buchungsdaten konnten vor dem Speichern nicht neu geladen werden'],
|
||||
$request->getSession()->getFlashBag()->get('error')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider updateFailureProvider
|
||||
*/
|
||||
public function testHandleSubmissionReturnsRedirectWhenUpdateThrows(
|
||||
\Throwable $exception,
|
||||
string $expectedFlashType,
|
||||
string $expectedFlashMessage,
|
||||
): void {
|
||||
$request = $this->createRequestWithSession();
|
||||
$user = $this->createUser();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
$freshBookingData = $this->createFreshBooking();
|
||||
|
||||
$dataLoader = $this->createMock(BookingEditDataLoaderService::class);
|
||||
$dataLoader->expects($this->exactly(1))
|
||||
->method('invalidateBookingCache')
|
||||
->with(42, $user);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('fetchBookingData')
|
||||
->with(42, $user)
|
||||
->willReturn($freshBookingData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234, true, true)
|
||||
->willReturn(null);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('patchMutability');
|
||||
|
||||
$submitGuard = $this->createMock(BookingEditSubmitGuardService::class);
|
||||
$submitGuard->expects($this->once())
|
||||
->method('reconcileImmutableCategories')
|
||||
->with($bookingDto, $freshBookingData)
|
||||
->willReturn(false);
|
||||
|
||||
$apiClient = $this->createMock(\App\BusProNet\ApiClient::class);
|
||||
$apiClient->expects($this->once())
|
||||
->method('updateBooking')
|
||||
->with($bookingDto, true)
|
||||
->willThrowException($exception);
|
||||
|
||||
$service = $this->createService(
|
||||
apiClient: $apiClient,
|
||||
dataLoader: $dataLoader,
|
||||
travelDataService: $travelDataService,
|
||||
submitGuard: $submitGuard,
|
||||
);
|
||||
|
||||
$response = $service->handleSubmission($request, $bookingDto, 42, $user);
|
||||
|
||||
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
|
||||
$this->assertSame([$expectedFlashMessage], $request->getSession()->getFlashBag()->get($expectedFlashType));
|
||||
}
|
||||
|
||||
public function testHandleSubmissionReturnsRedirectWhenUpdateIsUnsuccessful(): void
|
||||
{
|
||||
$request = $this->createRequestWithSession();
|
||||
$user = $this->createUser();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
$freshBookingData = $this->createFreshBooking();
|
||||
|
||||
$dataLoader = $this->createMock(BookingEditDataLoaderService::class);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('invalidateBookingCache')
|
||||
->with(42, $user);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('fetchBookingData')
|
||||
->with(42, $user)
|
||||
->willReturn($freshBookingData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234, true, true)
|
||||
->willReturn(null);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('patchMutability');
|
||||
|
||||
$submitGuard = $this->createMock(BookingEditSubmitGuardService::class);
|
||||
$submitGuard->expects($this->once())
|
||||
->method('reconcileImmutableCategories')
|
||||
->with($bookingDto, $freshBookingData)
|
||||
->willReturn(false);
|
||||
|
||||
$apiClient = $this->createMock(\App\BusProNet\ApiClient::class);
|
||||
$bookingUpdate = new BookingUpdate();
|
||||
$bookingUpdate->success = false;
|
||||
$bookingUpdate->status = 'BPN-FAIL';
|
||||
$apiClient->expects($this->once())
|
||||
->method('updateBooking')
|
||||
->with($bookingDto, true)
|
||||
->willReturn($bookingUpdate);
|
||||
|
||||
$service = $this->createService(
|
||||
apiClient: $apiClient,
|
||||
dataLoader: $dataLoader,
|
||||
travelDataService: $travelDataService,
|
||||
submitGuard: $submitGuard,
|
||||
);
|
||||
|
||||
$response = $service->handleSubmission($request, $bookingDto, 42, $user);
|
||||
|
||||
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
|
||||
$this->assertSame(['BPN-FAIL'], $request->getSession()->getFlashBag()->get('error'));
|
||||
}
|
||||
|
||||
public function testHandleSubmissionStoresInfoForNonErrorNotification(): void
|
||||
{
|
||||
$this->assertNotificationFlash(new Notification(650, 'Alles gut'), 'info');
|
||||
}
|
||||
|
||||
public function testHandleSubmissionStoresErrorForErrorNotification(): void
|
||||
{
|
||||
$this->assertNotificationFlash(new Notification(500, 'Kaputt'), 'error');
|
||||
}
|
||||
|
||||
public function testHandleSubmissionClearsSessionAndDraftOnSuccessfulUpdate(): void
|
||||
{
|
||||
$request = $this->createRequestWithSession();
|
||||
$user = $this->createUser();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
$freshBookingData = $this->createFreshBooking();
|
||||
|
||||
$dataLoader = $this->createMock(BookingEditDataLoaderService::class);
|
||||
$dataLoader->expects($this->exactly(2))
|
||||
->method('invalidateBookingCache')
|
||||
->with(42, $user);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('fetchBookingData')
|
||||
->with(42, $user)
|
||||
->willReturn($freshBookingData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234, true, true)
|
||||
->willReturn(null);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('patchMutability');
|
||||
|
||||
$submitGuard = $this->createMock(BookingEditSubmitGuardService::class);
|
||||
$submitGuard->expects($this->once())
|
||||
->method('reconcileImmutableCategories')
|
||||
->with($bookingDto, $freshBookingData)
|
||||
->willReturn(false);
|
||||
|
||||
$apiClient = $this->createMock(\App\BusProNet\ApiClient::class);
|
||||
$bookingUpdate = new BookingUpdate();
|
||||
$bookingUpdate->success = true;
|
||||
$apiClient->expects($this->once())
|
||||
->method('updateBooking')
|
||||
->with($bookingDto, true)
|
||||
->willReturn($bookingUpdate);
|
||||
|
||||
$bookingSessionService = $this->createMock(BookingSessionService::class);
|
||||
$bookingSessionService->expects($this->once())
|
||||
->method('clearBookingDto')
|
||||
->with($request, BookingDto::MODE_EDIT);
|
||||
$bookingSessionService->expects($this->never())
|
||||
->method('saveBookingDto');
|
||||
|
||||
$draftService = $this->createMock(BookingEditDraftService::class);
|
||||
$draftService->expects($this->once())
|
||||
->method('deleteDraft')
|
||||
->with($user, 42);
|
||||
|
||||
$service = $this->createService(
|
||||
apiClient: $apiClient,
|
||||
dataLoader: $dataLoader,
|
||||
draftService: $draftService,
|
||||
travelDataService: $travelDataService,
|
||||
submitGuard: $submitGuard,
|
||||
bookingSessionService: $bookingSessionService,
|
||||
);
|
||||
|
||||
$response = $service->handleSubmission($request, $bookingDto, 42, $user);
|
||||
|
||||
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
|
||||
$this->assertSame(['Buchung erfolgreich aktualisiert'], $request->getSession()->getFlashBag()->get('success'));
|
||||
$this->assertSame($freshBookingData, $bookingDto->booking);
|
||||
}
|
||||
|
||||
public function testHandleSubmissionPersistsSessionWhenImmutableFieldsWereReverted(): void
|
||||
{
|
||||
$request = $this->createRequestWithSession();
|
||||
$user = $this->createUser();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
$freshBookingData = $this->createFreshBooking();
|
||||
|
||||
$dataLoader = $this->createMock(BookingEditDataLoaderService::class);
|
||||
$dataLoader->expects($this->exactly(2))
|
||||
->method('invalidateBookingCache')
|
||||
->with(42, $user);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('fetchBookingData')
|
||||
->with(42, $user)
|
||||
->willReturn($freshBookingData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234, true, true)
|
||||
->willReturn(null);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('patchMutability');
|
||||
|
||||
$submitGuard = $this->createMock(BookingEditSubmitGuardService::class);
|
||||
$submitGuard->expects($this->once())
|
||||
->method('reconcileImmutableCategories')
|
||||
->with($bookingDto, $freshBookingData)
|
||||
->willReturn(true);
|
||||
|
||||
$apiClient = $this->createMock(\App\BusProNet\ApiClient::class);
|
||||
$bookingUpdate = new BookingUpdate();
|
||||
$bookingUpdate->success = true;
|
||||
$apiClient->expects($this->once())
|
||||
->method('updateBooking')
|
||||
->with($bookingDto, true)
|
||||
->willReturn($bookingUpdate);
|
||||
|
||||
$bookingSessionService = $this->createMock(BookingSessionService::class);
|
||||
$bookingSessionService->expects($this->once())
|
||||
->method('saveBookingDto')
|
||||
->with($request, $bookingDto, BookingDto::MODE_EDIT);
|
||||
$bookingSessionService->expects($this->once())
|
||||
->method('clearBookingDto')
|
||||
->with($request, BookingDto::MODE_EDIT);
|
||||
|
||||
$draftService = $this->createMock(BookingEditDraftService::class);
|
||||
$draftService->expects($this->once())
|
||||
->method('deleteDraft')
|
||||
->with($user, 42);
|
||||
|
||||
$service = $this->createService(
|
||||
apiClient: $apiClient,
|
||||
dataLoader: $dataLoader,
|
||||
draftService: $draftService,
|
||||
travelDataService: $travelDataService,
|
||||
submitGuard: $submitGuard,
|
||||
bookingSessionService: $bookingSessionService,
|
||||
);
|
||||
|
||||
$response = $service->handleSubmission($request, $bookingDto, 42, $user);
|
||||
|
||||
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
|
||||
$this->assertSame(['Einige Änderungen wurden verworfen, da sie aktuell nicht mehr änderbar sind.'], $request->getSession()->getFlashBag()->get('info'));
|
||||
$this->assertSame(['Buchung erfolgreich aktualisiert'], $request->getSession()->getFlashBag()->get('success'));
|
||||
}
|
||||
|
||||
public function testHandleSubmissionReturnsRedirectOnTimeout(): void
|
||||
{
|
||||
$this->assertExceptionFlash(new TimeoutException('slow'), 'Die Anfrage hat zu lange gedauert. Bitte versuchen Sie es erneut.');
|
||||
}
|
||||
|
||||
public function testHandleSubmissionReturnsRedirectOnApiClientException(): void
|
||||
{
|
||||
$this->assertExceptionFlash(new ApiClientException('boom'), 'Es ist ein Fehler in der Kommunikation mit dem Buchungssystem aufgetreten');
|
||||
}
|
||||
|
||||
public static function updateFailureProvider(): array
|
||||
{
|
||||
return [
|
||||
'timeout' => [new TimeoutException('slow'), 'error', 'Die Anfrage hat zu lange gedauert. Bitte versuchen Sie es erneut.'],
|
||||
'api-client' => [new ApiClientException('boom'), 'error', 'Es ist ein Fehler in der Kommunikation mit dem Buchungssystem aufgetreten'],
|
||||
];
|
||||
}
|
||||
|
||||
private function assertNotificationFlash(Notification $notification, string $expectedType): void
|
||||
{
|
||||
$request = $this->createRequestWithSession();
|
||||
$user = $this->createUser();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
$freshBookingData = $this->createFreshBooking();
|
||||
|
||||
$dataLoader = $this->createMock(BookingEditDataLoaderService::class);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('invalidateBookingCache')
|
||||
->with(42, $user);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('fetchBookingData')
|
||||
->with(42, $user)
|
||||
->willReturn($freshBookingData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234, true, true)
|
||||
->willReturn(null);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('patchMutability');
|
||||
|
||||
$submitGuard = $this->createMock(BookingEditSubmitGuardService::class);
|
||||
$submitGuard->expects($this->once())
|
||||
->method('reconcileImmutableCategories')
|
||||
->with($bookingDto, $freshBookingData)
|
||||
->willReturn(false);
|
||||
|
||||
$apiClient = $this->createMock(\App\BusProNet\ApiClient::class);
|
||||
$apiClient->expects($this->once())
|
||||
->method('updateBooking')
|
||||
->with($bookingDto, true)
|
||||
->willReturn($notification);
|
||||
|
||||
$service = $this->createService(
|
||||
apiClient: $apiClient,
|
||||
dataLoader: $dataLoader,
|
||||
travelDataService: $travelDataService,
|
||||
submitGuard: $submitGuard,
|
||||
);
|
||||
|
||||
$response = $service->handleSubmission($request, $bookingDto, 42, $user);
|
||||
|
||||
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
|
||||
$this->assertSame([$notification->message], $request->getSession()->getFlashBag()->get($expectedType));
|
||||
}
|
||||
|
||||
private function assertExceptionFlash(\Throwable $exception, string $expectedMessage): void
|
||||
{
|
||||
$request = $this->createRequestWithSession();
|
||||
$user = $this->createUser();
|
||||
$bookingDto = $this->createBookingDto();
|
||||
$freshBookingData = $this->createFreshBooking();
|
||||
|
||||
$dataLoader = $this->createMock(BookingEditDataLoaderService::class);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('invalidateBookingCache')
|
||||
->with(42, $user);
|
||||
$dataLoader->expects($this->once())
|
||||
->method('fetchBookingData')
|
||||
->with(42, $user)
|
||||
->willReturn($freshBookingData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234, true, true)
|
||||
->willReturn(null);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('patchMutability');
|
||||
|
||||
$submitGuard = $this->createMock(BookingEditSubmitGuardService::class);
|
||||
$submitGuard->expects($this->once())
|
||||
->method('reconcileImmutableCategories')
|
||||
->with($bookingDto, $freshBookingData)
|
||||
->willReturn(false);
|
||||
|
||||
$apiClient = $this->createMock(\App\BusProNet\ApiClient::class);
|
||||
$apiClient->expects($this->once())
|
||||
->method('updateBooking')
|
||||
->with($bookingDto, true)
|
||||
->willThrowException($exception);
|
||||
|
||||
$service = $this->createService(
|
||||
apiClient: $apiClient,
|
||||
dataLoader: $dataLoader,
|
||||
travelDataService: $travelDataService,
|
||||
submitGuard: $submitGuard,
|
||||
);
|
||||
|
||||
$response = $service->handleSubmission($request, $bookingDto, 42, $user);
|
||||
|
||||
$this->assertSame('/bookings/42/edit', $response->headers->get('Location'));
|
||||
$this->assertSame([$expectedMessage], $request->getSession()->getFlashBag()->get('error'));
|
||||
}
|
||||
|
||||
private function createService(
|
||||
?\App\BusProNet\ApiClient $apiClient = null,
|
||||
?BookingEditDataLoaderService $dataLoader = null,
|
||||
?BookingEditDraftService $draftService = null,
|
||||
?TravelDataService $travelDataService = null,
|
||||
?BookingEditSubmitGuardService $submitGuard = null,
|
||||
?BookingSessionService $bookingSessionService = null,
|
||||
): BookingEditSubmitService {
|
||||
return new BookingEditSubmitService(
|
||||
$apiClient ?? $this->createMock(\App\BusProNet\ApiClient::class),
|
||||
$dataLoader ?? $this->createMock(BookingEditDataLoaderService::class),
|
||||
$draftService ?? $this->createMock(BookingEditDraftService::class),
|
||||
$travelDataService ?? $this->createMock(TravelDataService::class),
|
||||
$submitGuard ?? $this->createMock(BookingEditSubmitGuardService::class),
|
||||
$bookingSessionService ?? $this->createMock(BookingSessionService::class),
|
||||
$this->createUrlGenerator(),
|
||||
$this->createMock(LoggerInterface::class),
|
||||
);
|
||||
}
|
||||
|
||||
private function createBookingDto(): BookingDto
|
||||
{
|
||||
return new BookingDto(new Travel(), 1);
|
||||
}
|
||||
|
||||
private function createFreshBooking(): Booking
|
||||
{
|
||||
$booking = new Booking();
|
||||
$booking->dateId = 1234;
|
||||
|
||||
return $booking;
|
||||
}
|
||||
|
||||
private function createUser(): User
|
||||
{
|
||||
$user = new User('[email protected]');
|
||||
$user->setPassword('secret');
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function createRequestWithSession(): Request
|
||||
{
|
||||
$request = Request::create('/bookings/42/edit', Request::METHOD_POST);
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
private function createUrlGenerator(): UrlGeneratorInterface
|
||||
{
|
||||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
$urlGenerator->method('generate')
|
||||
->with('app_booking_edit', ['id' => 42])
|
||||
->willReturn('/bookings/42/edit');
|
||||
|
||||
return $urlGenerator;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user