createMock(AccommodationBookingService::class); $bookingService->expects(self::never())->method('sendOffer'); $controller = new TestableSendOfferController($bookingService, $this->createMock(LoggerInterface::class)); $response = $controller->index($this->requestedBooking(), Request::create('/admin/accommodation-booking/1/send-offer')); self::assertSame(Response::HTTP_OK, $response->getStatusCode()); self::assertSame('admin/accommodation_booking/modal_send_offer.html.twig', $controller->renderedView); } /** * @dataProvider statusesAwaitingAnOffer */ public function testPostSendsTheOfferAndRedirectsTheBrowser(AccommodationBookingStatus $status): void { $booking = $this->requestedBooking(); $booking->setStatus($status); $bookingService = $this->createMock(AccommodationBookingService::class); $bookingService->expects(self::once())->method('sendOffer')->with($booking); $controller = new TestableSendOfferController($bookingService, $this->createMock(LoggerInterface::class)); $response = $controller->index($booking, Request::create('/admin/accommodation-booking/1/send-offer', 'POST')); self::assertTrue($response->headers->has('HX-Redirect')); } /** * @return iterable */ public static function statusesAwaitingAnOffer(): iterable { // The admin-authored offer and the customer's inquiry converge on the same action. yield 'draft' => [AccommodationBookingStatus::Draft]; yield 'requested' => [AccommodationBookingStatus::Requested]; } /** * @dataProvider statusesPastTheOffer */ public function testABookingThatIsNotAwaitingAnOfferGetsNone(AccommodationBookingStatus $status): void { $booking = $this->requestedBooking(); $booking->setStatus($status); $bookingService = $this->createMock(AccommodationBookingService::class); $bookingService->expects(self::never())->method('sendOffer'); $controller = new TestableSendOfferController($bookingService, $this->createMock(LoggerInterface::class)); $response = $controller->index($booking, Request::create('/admin/accommodation-booking/1/send-offer', 'POST')); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); } /** * @return iterable */ public static function statusesPastTheOffer(): iterable { yield 'open' => [AccommodationBookingStatus::Open]; yield 'received' => [AccommodationBookingStatus::Received]; yield 'confirmed' => [AccommodationBookingStatus::Confirmed]; yield 'discarded' => [AccommodationBookingStatus::Discarded]; } public function testPostWithAnInvalidTokenIsDenied(): void { $bookingService = $this->createMock(AccommodationBookingService::class); $bookingService->expects(self::never())->method('sendOffer'); $controller = new TestableSendOfferController($bookingService, $this->createMock(LoggerInterface::class), tokenValid: false); $this->expectException(AccessDeniedException::class); $controller->index($this->requestedBooking(), Request::create('/admin/accommodation-booking/1/send-offer', 'POST')); } public function testABookingWithoutAnEmailAddressGetsNoOffer(): void { // The offer only exists for the customer as the mail carrying its link, so sending // it nowhere would leave the booking waiting in Offen for an acceptance that cannot // come — the dead end this whole flow is meant to remove. $booking = $this->requestedBooking(); $booking->setEmail(null); $bookingService = $this->createMock(AccommodationBookingService::class); $bookingService->expects(self::never())->method('sendOffer'); $controller = new TestableSendOfferController($bookingService, $this->createMock(LoggerInterface::class)); $response = $controller->index($booking, Request::create('/admin/accommodation-booking/1/send-offer', 'POST')); self::assertSame(['error'], array_column($controller->flashes, 'type')); self::assertStringContainsString('app_admin_accommodationbooking_edit', (string) $response->headers->get('HX-Redirect')); } private function requestedBooking(): AccommodationBooking { $booking = new AccommodationBooking(); $booking->setStatus(AccommodationBookingStatus::Requested); $booking->setGroupName('Schulklasse 7b'); $booking->setEmail('customer@example.com'); return $booking; } } final class TestableSendOfferController extends SendOfferController { public ?string $renderedView = null; /** @var list */ public array $flashes = []; public function __construct( AccommodationBookingService $bookingService, LoggerInterface $logger, private readonly bool $tokenValid = true, ) { parent::__construct($bookingService, $logger); } protected function isCsrfTokenValid(string $id, #[\SensitiveParameter] ?string $token): bool { return $this->tokenValid; } /** * @param array $parameters */ protected function render(string $view, array $parameters = [], ?Response $response = null): Response { $this->renderedView = $view; return new Response(); } protected function addFlash(string $type, mixed $message): void { $this->flashes[] = ['type' => $type, 'message' => $message]; } /** * @param array $parameters */ protected function generateUrl(string $route, array $parameters = [], int $referenceType = 1): string { return '/'.$route.'?'.http_build_query($parameters); } }