102 lines
3.5 KiB
PHP
102 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Controller\Admin\AccommodationBooking;
|
|
|
|
use App\Controller\Admin\AccommodationBooking\PdfController;
|
|
use App\Entity\Groups\AccommodationBooking;
|
|
use App\Enum\Groups\AccommodationBookingStatus;
|
|
use App\Service\AccommodationBookingPdfGenerator;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* Covers the guards around the PDF download — the rendering itself is tested in
|
|
* AccommodationBookingPdfGeneratorTest.
|
|
*/
|
|
class PdfControllerTest extends TestCase
|
|
{
|
|
public function testAConfirmedBookingIsDownloadedAsPdf(): void
|
|
{
|
|
$booking = $this->booking(AccommodationBookingStatus::Confirmed);
|
|
|
|
$generator = $this->createMock(AccommodationBookingPdfGenerator::class);
|
|
$generator->expects(self::once())
|
|
->method('createDownloadResponse')
|
|
->with($booking)
|
|
->willReturn(new Response('%PDF-1.7'));
|
|
|
|
$response = (new TestablePdfController($generator))->index($booking);
|
|
|
|
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
|
}
|
|
|
|
#[DataProvider('nonConfirmedStatuses')]
|
|
public function testABookingThatIsNotConfirmedCannotBeDownloaded(AccommodationBookingStatus $status): void
|
|
{
|
|
$generator = $this->createMock(AccommodationBookingPdfGenerator::class);
|
|
$generator->expects(self::never())->method('createDownloadResponse');
|
|
|
|
$controller = new TestablePdfController($generator);
|
|
|
|
$response = $controller->index($this->booking($status));
|
|
|
|
self::assertSame(Response::HTTP_FOUND, $response->getStatusCode());
|
|
self::assertSame(['warning'], array_column($controller->flashes, 'type'));
|
|
}
|
|
|
|
/**
|
|
* @return iterable<string, array{AccommodationBookingStatus}>
|
|
*/
|
|
public static function nonConfirmedStatuses(): iterable
|
|
{
|
|
yield 'draft' => [AccommodationBookingStatus::Draft];
|
|
yield 'open' => [AccommodationBookingStatus::Open];
|
|
yield 'received' => [AccommodationBookingStatus::Received];
|
|
yield 'discarded' => [AccommodationBookingStatus::Discarded];
|
|
}
|
|
|
|
public function testAFailingGeneratorRedirectsWithAFlash(): void
|
|
{
|
|
$generator = $this->createStub(AccommodationBookingPdfGenerator::class);
|
|
$generator->method('createDownloadResponse')->willThrowException(new \RuntimeException('keine Preise'));
|
|
|
|
$controller = new TestablePdfController($generator);
|
|
|
|
$response = $controller->index($this->booking(AccommodationBookingStatus::Confirmed));
|
|
|
|
self::assertSame(Response::HTTP_FOUND, $response->getStatusCode());
|
|
self::assertStringContainsString('keine Preise', (string) $controller->flashes[0]['message']);
|
|
}
|
|
|
|
private function booking(AccommodationBookingStatus $status): AccommodationBooking
|
|
{
|
|
$booking = new AccommodationBooking();
|
|
$booking->setStatus($status);
|
|
$booking->setGroupName('Schulklasse 7b');
|
|
|
|
return $booking;
|
|
}
|
|
}
|
|
|
|
final class TestablePdfController extends PdfController
|
|
{
|
|
/** @var list<array{type: string, message: mixed}> */
|
|
public array $flashes = [];
|
|
|
|
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);
|
|
}
|
|
}
|