Files
myep/tests/Controller/Groups/Offer/IndexControllerTest.php
T

128 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller\Groups\Offer;
use App\Controller\Groups\Offer\IndexController;
use App\Entity\Groups\AccommodationBooking;
use App\Enum\Groups\AccommodationBookingStatus;
use App\Repository\Groups\AccommodationBookingRepository;
use App\Service\AccommodationBookingLinkSigner;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class IndexControllerTest extends TestCase
{
public function testAuthorizesSessionAndRedirectsToView(): void
{
$booking = new AccommodationBooking();
$booking->setStatus(AccommodationBookingStatus::Open);
$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 TestableOfferIndexController($bookingRepository, $linkSigner);
$response = $controller->index($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid()));
self::assertInstanceOf(RedirectResponse::class, $response);
self::assertSame('/app_groups_offer_view?uuid='.$booking->getUuid(), $response->getTargetUrl());
}
public function testRendersUnavailableForInvalidLink(): void
{
$booking = new AccommodationBooking();
$bookingRepository = $this->createStub(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 TestableOfferIndexController($bookingRepository, $linkSigner);
$response = $controller->index($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid()));
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
self::assertSame('groups/offer/unavailable.html.twig', $controller->renderedView);
}
public function testRendersUnavailableForUnknownUuid(): void
{
$bookingRepository = $this->createStub(AccommodationBookingRepository::class);
$bookingRepository->method('findOneBy')->willReturn(null);
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
$linkSigner->expects(self::never())->method('isValidLinkRequest');
$controller = new TestableOfferIndexController($bookingRepository, $linkSigner);
$response = $controller->index('unknown-uuid', Request::create('/groups/booking/offer/unknown-uuid'));
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
self::assertSame('groups/offer/unavailable.html.twig', $controller->renderedView);
}
#[DataProvider('statusesTheCustomerMustNotSee')]
public function testRendersUnavailableForABookingThatIsNotOfferedYet(AccommodationBookingStatus $status): void
{
$booking = new AccommodationBooking();
$booking->setStatus($status);
$bookingRepository = $this->createStub(AccommodationBookingRepository::class);
$bookingRepository->method('findOneBy')->willReturn($booking);
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
$linkSigner->method('isValidLinkRequest')->willReturn(true);
$linkSigner->expects(self::never())->method('authorizeSession');
$controller = new TestableOfferIndexController($bookingRepository, $linkSigner);
$response = $controller->index($booking->getUuid(), Request::create('/groups/booking/offer/'.$booking->getUuid()));
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
self::assertSame('groups/offer/unavailable.html.twig', $controller->renderedView);
}
/**
* @return iterable<string, array{AccommodationBookingStatus}>
*/
public static function statusesTheCustomerMustNotSee(): iterable
{
// A scratch record and an inquiry the office has not worked through yet are both
// unfinished; a discarded one is over. All three behave like an expired link.
yield 'draft' => [AccommodationBookingStatus::Draft];
yield 'requested' => [AccommodationBookingStatus::Requested];
yield 'discarded' => [AccommodationBookingStatus::Discarded];
}
}
final class TestableOfferIndexController extends IndexController
{
public ?string $renderedView = null;
/**
* @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;
return $response ?? new Response('<html>unavailable</html>');
}
}