feat: groups price calculator admin crud, booking/offer flow and api
This commit is contained in:
@@ -0,0 +1,411 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Controller\Groups;
|
||||
|
||||
use App\Controller\Groups\OfferController;
|
||||
use App\Entity\Groups\Accommodation;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Repository\Groups\AccommodationBookingRepository;
|
||||
use App\Service\AccommodationBookingBreakdownCalculator;
|
||||
use App\Service\AccommodationBookingLinkSigner;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
|
||||
class OfferControllerTest extends TestCase
|
||||
{
|
||||
public function testAccessAuthorizesSessionAndRedirectsToView(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->with(['uuid' => $booking->getUuid()])->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isValidLinkRequest')->willReturn(true);
|
||||
$linkSigner->expects(self::once())->method('authorizeSession')->with(self::anything(), $booking);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
);
|
||||
|
||||
$response = $controller->access($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid()));
|
||||
|
||||
self::assertInstanceOf(RedirectResponse::class, $response);
|
||||
self::assertSame('/app_groups_booking_offer_view?uuid='.$booking->getUuid(), $response->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testAccessRendersUnavailableForInvalidLink(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isValidLinkRequest')->willReturn(false);
|
||||
$linkSigner->expects(self::never())->method('authorizeSession');
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
);
|
||||
|
||||
$response = $controller->access($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid()));
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertSame('groups/booking/offer_unavailable.html.twig', $controller->renderedView);
|
||||
}
|
||||
|
||||
public function testAccessRendersUnavailableForUnknownUuid(): void
|
||||
{
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn(null);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->expects(self::never())->method('isValidLinkRequest');
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
);
|
||||
|
||||
$response = $controller->access('unknown-uuid', Request::create('/groups/booking/offer/unknown-uuid'));
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertSame('groups/booking/offer_unavailable.html.twig', $controller->renderedView);
|
||||
}
|
||||
|
||||
public function testViewRendersOfferWhenSessionAuthorized(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(true);
|
||||
$booking->setAccommodation(new Accommodation());
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->with(['uuid' => $booking->getUuid()])->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(true);
|
||||
|
||||
$breakdownCalculator = $this->createMock(AccommodationBookingBreakdownCalculator::class);
|
||||
$breakdownCalculator->method('compute')->with($booking)->willReturn(['total' => 1000]);
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::never())->method('acceptBooking');
|
||||
$bookingService->method('loadHotelCmsData')->willReturn(null);
|
||||
|
||||
$controller = new TestableOfferController($bookingRepository, $linkSigner, $breakdownCalculator, $bookingService);
|
||||
|
||||
$request = Request::create('/groups/booking/offer/'.$booking->getUuid().'/view');
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
$response = $controller->view($booking->getUuid(), $request);
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertSame('<html>offer</html>', $response->getContent());
|
||||
self::assertSame('groups/booking/offer.html.twig', $controller->renderedView);
|
||||
self::assertSame($booking, $controller->renderedParameters['booking']);
|
||||
self::assertSame(['total' => 1000], $controller->renderedParameters['priceBreakdown']);
|
||||
self::assertSame($booking->getAccommodation(), $controller->renderedParameters['ctx']->accommodation);
|
||||
}
|
||||
|
||||
public function testViewRendersUnavailableWhenSessionNotAuthorized(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(false);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
);
|
||||
|
||||
$response = $controller->view($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid().'/view'));
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertSame('groups/booking/offer_unavailable.html.twig', $controller->renderedView);
|
||||
}
|
||||
|
||||
public function testConfirmGetRendersModalWithFreshForm(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(true);
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->with(['uuid' => $booking->getUuid()])->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(true);
|
||||
|
||||
$confirmationForm = $this->createMock(FormInterface::class);
|
||||
$confirmationForm->method('handleRequest')->willReturnSelf();
|
||||
$confirmationForm->method('isSubmitted')->willReturn(false);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
$confirmationForm,
|
||||
);
|
||||
|
||||
$response = $controller->confirm($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid().'/confirm'));
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertSame('groups/booking/_offer_accept_confirmation_modal.html.twig', $controller->renderedView);
|
||||
self::assertSame($booking, $controller->renderedParameters['booking']);
|
||||
self::assertSame($confirmationForm, $controller->renderedParameters['confirmationForm']);
|
||||
}
|
||||
|
||||
public function testConfirmPostValidAcceptsBookingAndRedirects(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(true);
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(true);
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::once())->method('acceptBooking')->with($booking);
|
||||
|
||||
$confirmationForm = $this->createMock(FormInterface::class);
|
||||
$confirmationForm->method('handleRequest')->willReturnSelf();
|
||||
$confirmationForm->method('isSubmitted')->willReturn(true);
|
||||
$confirmationForm->method('isValid')->willReturn(true);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$bookingService,
|
||||
$confirmationForm,
|
||||
);
|
||||
|
||||
$request = Request::create('/groups/booking/offer/'.$booking->getUuid().'/confirm', 'POST');
|
||||
$response = $controller->confirm($booking->getUuid(), $request);
|
||||
|
||||
self::assertInstanceOf(RedirectResponse::class, $response);
|
||||
self::assertSame('/app_groups_booking_offer_view?uuid='.$booking->getUuid(), $response->getTargetUrl());
|
||||
self::assertSame([['type' => 'success', 'message' => 'Deine Buchung ist bestätigt.']], $controller->flashes);
|
||||
}
|
||||
|
||||
public function testConfirmPostValidViaHtmxReturnsHxRedirect(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(true);
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(true);
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::once())->method('acceptBooking')->with($booking);
|
||||
|
||||
$confirmationForm = $this->createMock(FormInterface::class);
|
||||
$confirmationForm->method('handleRequest')->willReturnSelf();
|
||||
$confirmationForm->method('isSubmitted')->willReturn(true);
|
||||
$confirmationForm->method('isValid')->willReturn(true);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$bookingService,
|
||||
$confirmationForm,
|
||||
);
|
||||
|
||||
$request = Request::create('/groups/booking/offer/'.$booking->getUuid().'/confirm', 'POST');
|
||||
$request->headers->set('HX-Request', 'true');
|
||||
$response = $controller->confirm($booking->getUuid(), $request);
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertTrue($response->headers->has('HX-Redirect'));
|
||||
}
|
||||
|
||||
public function testConfirmPostInvalidReRendersModalWithoutAccepting(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(true);
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(true);
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::never())->method('acceptBooking');
|
||||
|
||||
$confirmationForm = $this->createMock(FormInterface::class);
|
||||
$confirmationForm->method('handleRequest')->willReturnSelf();
|
||||
$confirmationForm->method('isSubmitted')->willReturn(true);
|
||||
$confirmationForm->method('isValid')->willReturn(false);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$bookingService,
|
||||
$confirmationForm,
|
||||
);
|
||||
|
||||
$response = $controller->confirm($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid().'/confirm', 'POST'));
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertFalse($response->headers->has('HX-Redirect'));
|
||||
self::assertSame('groups/booking/_offer_accept_confirmation_modal.html.twig', $controller->renderedView);
|
||||
}
|
||||
|
||||
public function testConfirmRedirectsWhenAlreadyAccepted(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(false);
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(true);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
);
|
||||
|
||||
$response = $controller->confirm($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid().'/confirm'));
|
||||
|
||||
self::assertInstanceOf(RedirectResponse::class, $response);
|
||||
}
|
||||
|
||||
public function testConfirmRedirectsWhenSessionNotAuthorized(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn($booking);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('isSessionAuthorized')->willReturn(false);
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
);
|
||||
|
||||
$response = $controller->confirm($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid().'/confirm'));
|
||||
|
||||
self::assertInstanceOf(RedirectResponse::class, $response);
|
||||
}
|
||||
|
||||
public function testConfirmRedirectsForUnknownUuid(): void
|
||||
{
|
||||
$bookingRepository = $this->createMock(AccommodationBookingRepository::class);
|
||||
$bookingRepository->method('findOneBy')->willReturn(null);
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->expects(self::never())->method('isSessionAuthorized');
|
||||
|
||||
$controller = new TestableOfferController(
|
||||
$bookingRepository,
|
||||
$linkSigner,
|
||||
$this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
$this->createMock(AccommodationBookingService::class),
|
||||
);
|
||||
|
||||
$response = $controller->confirm('unknown-uuid', Request::create('/groups/booking/offer/unknown-uuid/confirm'));
|
||||
|
||||
self::assertInstanceOf(RedirectResponse::class, $response);
|
||||
}
|
||||
}
|
||||
|
||||
final class TestableOfferController extends OfferController
|
||||
{
|
||||
public ?string $renderedView = null;
|
||||
|
||||
/** @var array<string, mixed> */
|
||||
public array $renderedParameters = [];
|
||||
|
||||
public function __construct(
|
||||
AccommodationBookingRepository $bookingRepository,
|
||||
AccommodationBookingLinkSigner $linkSigner,
|
||||
AccommodationBookingBreakdownCalculator $breakdownCalculator,
|
||||
AccommodationBookingService $bookingService,
|
||||
private readonly ?FormInterface $confirmationForm = null,
|
||||
) {
|
||||
parent::__construct($bookingRepository, $linkSigner, $breakdownCalculator, $bookingService);
|
||||
}
|
||||
|
||||
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
|
||||
{
|
||||
return $this->confirmationForm ?? throw new \LogicException('No confirmation form mock configured for this test.');
|
||||
}
|
||||
|
||||
protected function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
|
||||
{
|
||||
return 'https://example.test/agb/';
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array<int, array{type: string, message: mixed}>
|
||||
*/
|
||||
public array $flashes = [];
|
||||
|
||||
protected function addFlash(string $type, mixed $message): void
|
||||
{
|
||||
$this->flashes[] = ['type' => $type, 'message' => $message];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $parameters
|
||||
*/
|
||||
protected function generateUrl(string $route, array $parameters = [], int $referenceType = 1): string
|
||||
{
|
||||
return '/'.$route.'?'.http_build_query($parameters);
|
||||
}
|
||||
|
||||
protected function render(string $view, array $parameters = [], ?Response $response = null): Response
|
||||
{
|
||||
$this->renderedView = $view;
|
||||
$this->renderedParameters = $parameters;
|
||||
|
||||
if ('groups/booking/offer.html.twig' === $view) {
|
||||
$content = $parameters['booking']->isInquiry() ? '<html>offer</html>' : '<html>confirmed</html>';
|
||||
} else {
|
||||
$content = '<html>unavailable</html>';
|
||||
}
|
||||
|
||||
$response ??= new Response();
|
||||
$response->setContent($content);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user