184 lines
7.0 KiB
PHP
184 lines
7.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Controller\Admin\AccommodationBooking;
|
|
|
|
use App\Controller\Admin\AccommodationBooking\ConfirmController;
|
|
use App\Entity\Groups\AccommodationBooking;
|
|
use App\Enum\Groups\AccommodationBookingStatus;
|
|
use App\Message\CreateClickUpBookingTaskMessage;
|
|
use App\Service\AccommodationBookingService;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Messenger\Envelope;
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
|
|
|
/**
|
|
* Covers the guards around the confirm action — the confirmation itself is tested in
|
|
* AccommodationBookingServiceTest.
|
|
*/
|
|
class ConfirmControllerTest extends TestCase
|
|
{
|
|
public function testGetRendersTheConfirmationModal(): void
|
|
{
|
|
$bookingService = $this->createMock(AccommodationBookingService::class);
|
|
$bookingService->expects(self::never())->method('confirmBooking');
|
|
|
|
$controller = new TestableConfirmController($bookingService, $this->neverDispatchingBus(), $this->createStub(LoggerInterface::class));
|
|
|
|
$response = $controller->index($this->receivedBooking(), Request::create('/admin/accommodation-booking/1/confirm'));
|
|
|
|
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
|
self::assertSame('admin/accommodation_booking/modal_confirm.html.twig', $controller->renderedView);
|
|
}
|
|
|
|
public function testPostConfirmsTheBookingAndRedirectsTheBrowser(): void
|
|
{
|
|
$booking = $this->receivedBooking();
|
|
|
|
$bookingService = $this->createMock(AccommodationBookingService::class);
|
|
$bookingService->expects(self::once())->method('confirmBooking')->with($booking);
|
|
|
|
// The ClickUp task is created asynchronously because the ClickUp API is slow.
|
|
$messageBus = $this->createMock(MessageBusInterface::class);
|
|
$messageBus->expects(self::once())
|
|
->method('dispatch')
|
|
->with(self::isInstanceOf(CreateClickUpBookingTaskMessage::class))
|
|
->willReturnCallback(static fn (object $message): Envelope => new Envelope($message));
|
|
|
|
$controller = new TestableConfirmController($bookingService, $messageBus, $this->createStub(LoggerInterface::class));
|
|
|
|
$response = $controller->index($booking, Request::create('/admin/accommodation-booking/1/confirm', 'POST'));
|
|
|
|
self::assertTrue($response->headers->has('HX-Redirect'));
|
|
}
|
|
|
|
public function testPostWithAnInvalidTokenIsDenied(): void
|
|
{
|
|
$bookingService = $this->createMock(AccommodationBookingService::class);
|
|
$bookingService->expects(self::never())->method('confirmBooking');
|
|
|
|
$controller = new TestableConfirmController($bookingService, $this->neverDispatchingBus(), $this->createStub(LoggerInterface::class), tokenValid: false);
|
|
|
|
$this->expectException(AccessDeniedException::class);
|
|
|
|
$controller->index($this->receivedBooking(), Request::create('/admin/accommodation-booking/1/confirm', 'POST'));
|
|
}
|
|
|
|
#[DataProvider('nonReceivedStatuses')]
|
|
public function testABookingThatIsNotAwaitingValidationCannotBeConfirmed(AccommodationBookingStatus $status): void
|
|
{
|
|
$booking = $this->receivedBooking();
|
|
$booking->setStatus($status);
|
|
|
|
$bookingService = $this->createMock(AccommodationBookingService::class);
|
|
$bookingService->expects(self::never())->method('confirmBooking');
|
|
|
|
$controller = new TestableConfirmController($bookingService, $this->neverDispatchingBus(), $this->createStub(LoggerInterface::class));
|
|
|
|
$response = $controller->index($booking, Request::create('/admin/accommodation-booking/1/confirm', 'POST'));
|
|
|
|
self::assertSame(Response::HTTP_FOUND, $response->getStatusCode());
|
|
}
|
|
|
|
/**
|
|
* @return iterable<string, array{AccommodationBookingStatus}>
|
|
*/
|
|
public static function nonReceivedStatuses(): iterable
|
|
{
|
|
yield 'draft' => [AccommodationBookingStatus::Draft];
|
|
yield 'requested' => [AccommodationBookingStatus::Requested];
|
|
yield 'open' => [AccommodationBookingStatus::Open];
|
|
yield 'confirmed' => [AccommodationBookingStatus::Confirmed];
|
|
yield 'discarded' => [AccommodationBookingStatus::Discarded];
|
|
}
|
|
|
|
public function testABookingWithoutAnEmailAddressIsNotConfirmed(): void
|
|
{
|
|
$booking = $this->receivedBooking();
|
|
$booking->setEmail(null);
|
|
|
|
$bookingService = $this->createMock(AccommodationBookingService::class);
|
|
$bookingService->expects(self::never())->method('confirmBooking');
|
|
|
|
$controller = new TestableConfirmController($bookingService, $this->neverDispatchingBus(), $this->createStub(LoggerInterface::class));
|
|
|
|
$response = $controller->index($booking, Request::create('/admin/accommodation-booking/1/confirm', 'POST'));
|
|
|
|
self::assertSame(['error'], array_column($controller->flashes, 'type'));
|
|
self::assertStringContainsString('app_admin_accommodationbooking_edit', (string) $response->headers->get('HX-Redirect'));
|
|
}
|
|
|
|
/**
|
|
* @return MessageBusInterface&MockObject
|
|
*/
|
|
private function neverDispatchingBus(): MessageBusInterface
|
|
{
|
|
$messageBus = $this->createMock(MessageBusInterface::class);
|
|
$messageBus->expects(self::never())->method('dispatch');
|
|
|
|
return $messageBus;
|
|
}
|
|
|
|
private function receivedBooking(): AccommodationBooking
|
|
{
|
|
$booking = new AccommodationBooking();
|
|
$booking->setStatus(AccommodationBookingStatus::Received);
|
|
$booking->setGroupName('Schulklasse 7b');
|
|
$booking->setEmail('[email protected]');
|
|
|
|
return $booking;
|
|
}
|
|
}
|
|
|
|
final class TestableConfirmController extends ConfirmController
|
|
{
|
|
public ?string $renderedView = null;
|
|
|
|
/** @var list<array{type: string, message: mixed}> */
|
|
public array $flashes = [];
|
|
|
|
public function __construct(
|
|
AccommodationBookingService $bookingService,
|
|
MessageBusInterface $messageBus,
|
|
LoggerInterface $logger,
|
|
private readonly bool $tokenValid = true,
|
|
) {
|
|
parent::__construct($bookingService, $messageBus, $logger);
|
|
}
|
|
|
|
protected function isCsrfTokenValid(string $id, #[\SensitiveParameter] ?string $token): bool
|
|
{
|
|
return $this->tokenValid;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $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<string, mixed> $parameters
|
|
*/
|
|
protected function generateUrl(string $route, array $parameters = [], int $referenceType = 1): string
|
|
{
|
|
return '/'.$route.'?'.http_build_query($parameters);
|
|
}
|
|
}
|