90 lines
2.9 KiB
PHP
90 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Controller\Groups\Booking;
|
|
|
|
use App\Controller\Groups\Booking\IndexController;
|
|
use App\Service\AccommodationBookingService;
|
|
use App\Service\AccommodationSessionManager;
|
|
use PHPUnit\Framework\TestCase;
|
|
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 IndexControllerTest extends TestCase
|
|
{
|
|
public function testSuccessRendersAndConsumesFlashOnFirstLoad(): void
|
|
{
|
|
$session = new Session(new MockArraySessionStorage());
|
|
$session->getFlashBag()->add('groups_booking_result', 'booking');
|
|
|
|
$request = Request::create('/groups/booking/success', 'GET');
|
|
$request->setSession($session);
|
|
|
|
$controller = $this->makeController();
|
|
|
|
$response = $controller->success($request);
|
|
|
|
self::assertSame(200, $response->getStatusCode());
|
|
self::assertSame('groups/booking/success.html.twig', $controller->renderedView);
|
|
self::assertSame('booking', $controller->renderedParameters['resultType']);
|
|
// The flash is read-once: a second read on the same request returns nothing.
|
|
self::assertSame([], $session->getFlashBag()->get('groups_booking_result'));
|
|
}
|
|
|
|
public function testSuccessRedirectsToLoginWhenFlashIsMissing(): void
|
|
{
|
|
$session = new Session(new MockArraySessionStorage());
|
|
|
|
$request = Request::create('/groups/booking/success', 'GET');
|
|
$request->setSession($session);
|
|
|
|
$controller = $this->makeController();
|
|
|
|
$response = $controller->success($request);
|
|
|
|
self::assertInstanceOf(RedirectResponse::class, $response);
|
|
self::assertSame('/app_login', $response->headers->get('Location'));
|
|
}
|
|
|
|
private function makeController(): TestableAccommodationIndexController
|
|
{
|
|
return new TestableAccommodationIndexController(
|
|
$this->createMock(AccommodationBookingService::class),
|
|
$this->createMock(AccommodationSessionManager::class),
|
|
);
|
|
}
|
|
}
|
|
|
|
final class TestableAccommodationIndexController extends IndexController
|
|
{
|
|
/**
|
|
* @var array<string, mixed>
|
|
*/
|
|
public array $renderedParameters = [];
|
|
|
|
public string $renderedView = '';
|
|
|
|
/**
|
|
* @param array<string, mixed> $parameters
|
|
*/
|
|
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
|
|
{
|
|
return new RedirectResponse('/'.$route, $status);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $parameters
|
|
*/
|
|
protected function render(string $view, array $parameters = [], ?Response $response = null): Response
|
|
{
|
|
$this->renderedView = $view;
|
|
$this->renderedParameters = $parameters;
|
|
|
|
return new Response('ok');
|
|
}
|
|
}
|