buildController($user); $controller->index(Request::create('/admin/accommodation-booking/create')); self::assertSame($user, $controller->capturedBooking?->getManagedBy()); } public function testAnImplausiblePriceComesBackAsAFormErrorWithoutSaving(): void { // Same as on the edit form: the fix is in the house's price rows, so the office is told // on the form it is standing in front of, and nothing is written. $bookingService = $this->createStub(AccommodationBookingService::class); $bookingService->method('refreshPriceSnapshot') ->willThrowException(new ImplausibleBookingPriceException(AccommodationBooking::MAX_TOTAL_PRICE + 1, 40, 2)); $form = $this->createMock(FormInterface::class); $form->method('handleRequest')->willReturn($form); $form->method('isSubmitted')->willReturn(true); $form->method('isValid')->willReturn(true); $form->expects(self::once())->method('addError'); $entityManager = $this->createMock(EntityManagerInterface::class); $entityManager->expects(self::never())->method('persist'); $entityManager->expects(self::never())->method('flush'); $controller = new TestableCreateController( $entityManager, $this->createStub(LoggerInterface::class), $bookingService, $form, new User('manager@example.test'), ); $response = $controller->index(Request::create('/admin/accommodation-booking/create', 'POST')); self::assertSame(Response::HTTP_OK, $response->getStatusCode(), 'the modal is re-rendered, not redirected'); } private function buildController(?UserInterface $user): TestableCreateController { $form = $this->createStub(FormInterface::class); $form->method('handleRequest')->willReturn($form); $form->method('isSubmitted')->willReturn(false); return new TestableCreateController( $this->createStub(EntityManagerInterface::class), $this->createStub(LoggerInterface::class), $this->createStub(AccommodationBookingService::class), $form, $user, ); } } final class TestableCreateController extends CreateController { public ?AccommodationBooking $capturedBooking = null; public function __construct( EntityManagerInterface $entityManager, LoggerInterface $logger, AccommodationBookingService $bookingService, private readonly FormInterface $form, private readonly ?UserInterface $user, ) { parent::__construct($entityManager, $logger, $bookingService); } protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface { if ($data instanceof AccommodationBooking) { $this->capturedBooking = $data; } return $this->form; } protected function getUser(): ?UserInterface { return $this->user; } /** * @param array $parameters */ protected function render(string $view, array $parameters = [], ?Response $response = null): Response { return new Response(); } }