88 lines
2.8 KiB
PHP
88 lines
2.8 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
|
|
{
|
|
#[DataProvider('statuses')]
|
|
public function testABookingInAnyStatusIsDownloadedAsPdf(AccommodationBookingStatus $status): void
|
|
{
|
|
$booking = $this->booking($status);
|
|
|
|
$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());
|
|
}
|
|
|
|
/**
|
|
* @return iterable<string, array{AccommodationBookingStatus}>
|
|
*/
|
|
public static function statuses(): iterable
|
|
{
|
|
foreach (AccommodationBookingStatus::cases() as $status) {
|
|
yield $status->name => [$status];
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|