feat: split nights off an accommodation booking as separate periods
addresses #869f6z36v
This commit is contained in:
1 parent
972839286c
commit
7001bc0cf2
35 files changed
+2221
-25
No files matched your search
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Controller\Admin\AccommodationBooking;
|
||||
|
||||
use App\Controller\Admin\AccommodationBooking\EditPeriodController;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Entity\Groups\AccommodationBookingPeriod;
|
||||
use App\Enum\Groups\AccommodationBookingPeriodPosition;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
/** The modal's error handling is shared with SplitPeriodController and covered there. */
|
||||
class EditPeriodControllerTest extends TestCase
|
||||
{
|
||||
public function testAValidSubmitUpdatesThePeriodOfItsOwnBooking(): void
|
||||
{
|
||||
$booking = (new AccommodationBooking())
|
||||
->setDateFrom(new \DateTimeImmutable('2026-07-01'))
|
||||
->setDateTo(new \DateTimeImmutable('2026-07-08'));
|
||||
$period = $booking->splitOffPeriod(AccommodationBookingPeriodPosition::Trailing, 1);
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::once())->method('updatePeriod')
|
||||
->with($booking, $period)
|
||||
->willReturn(new ConstraintViolationList());
|
||||
|
||||
$form = $this->createStub(FormInterface::class);
|
||||
$form->method('handleRequest')->willReturn($form);
|
||||
$form->method('isSubmitted')->willReturn(true);
|
||||
$form->method('isValid')->willReturn(true);
|
||||
|
||||
$controller = new TestableEditPeriodController($bookingService, $this->createStub(LoggerInterface::class), $form);
|
||||
|
||||
$response = $controller->index($period, Request::create('/admin/accommodation-booking-period/1/edit', 'POST', server: ['HTTP_HX-Request' => 'true']));
|
||||
|
||||
self::assertStringStartsWith('/app_admin_accommodationbooking_edit', (string) $response->headers->get('HX-Redirect'));
|
||||
self::assertSame('success', $controller->flashes[0]['type']);
|
||||
}
|
||||
|
||||
public function testAPeriodWithoutABookingIsNotFound(): void
|
||||
{
|
||||
$controller = new TestableEditPeriodController(
|
||||
$this->createStub(AccommodationBookingService::class),
|
||||
$this->createStub(LoggerInterface::class),
|
||||
$this->createStub(FormInterface::class),
|
||||
);
|
||||
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
|
||||
$controller->index(
|
||||
new AccommodationBookingPeriod(AccommodationBookingPeriodPosition::Leading, new \DateTimeImmutable('2026-07-01'), new \DateTimeImmutable('2026-07-02')),
|
||||
Request::create('/admin/accommodation-booking-period/1/edit'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final class TestableEditPeriodController extends EditPeriodController
|
||||
{
|
||||
/** @var list<array{type: string, message: mixed}> */
|
||||
public array $flashes = [];
|
||||
|
||||
public function __construct(
|
||||
AccommodationBookingService $bookingService,
|
||||
LoggerInterface $logger,
|
||||
private readonly FormInterface $form,
|
||||
) {
|
||||
parent::__construct($bookingService, $logger);
|
||||
}
|
||||
|
||||
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
|
||||
{
|
||||
return $this->form;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $parameters
|
||||
*/
|
||||
protected function render(string $view, array $parameters = [], ?Response $response = null): Response
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Controller\Admin\AccommodationBooking;
|
||||
|
||||
use App\Controller\Admin\AccommodationBooking\RemovePeriodController;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Entity\Groups\AccommodationBookingPeriod;
|
||||
use App\Enum\Groups\AccommodationBookingPeriodPosition;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
class RemovePeriodControllerTest extends TestCase
|
||||
{
|
||||
public function testGetAsksForConfirmation(): void
|
||||
{
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::never())->method('removePeriod');
|
||||
|
||||
$controller = new TestableRemovePeriodController($bookingService, $this->createStub(LoggerInterface::class));
|
||||
|
||||
$controller->index($this->period(), $this->request());
|
||||
|
||||
self::assertSame('admin/accommodation_booking/modal_remove_period.html.twig', $controller->renderedView);
|
||||
}
|
||||
|
||||
public function testConfirmingRemovesThePeriodAndReturnsToTheEditForm(): void
|
||||
{
|
||||
$period = $this->period();
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::once())->method('removePeriod')
|
||||
->with($period->getBooking(), $period)
|
||||
->willReturn(new ConstraintViolationList());
|
||||
|
||||
$controller = new TestableRemovePeriodController($bookingService, $this->createStub(LoggerInterface::class));
|
||||
|
||||
$response = $controller->index($period, $this->request('POST'));
|
||||
|
||||
self::assertStringStartsWith('/app_admin_accommodationbooking_edit', (string) $response->headers->get('HX-Redirect'));
|
||||
self::assertSame('success', $controller->flashes[0]['type']);
|
||||
}
|
||||
|
||||
public function testAnInvalidTokenRemovesNothing(): void
|
||||
{
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::never())->method('removePeriod');
|
||||
|
||||
$controller = new TestableRemovePeriodController($bookingService, $this->createStub(LoggerInterface::class));
|
||||
$controller->tokenValid = false;
|
||||
|
||||
$this->expectException(AccessDeniedException::class);
|
||||
|
||||
$controller->index($this->period(), $this->request('POST'));
|
||||
}
|
||||
|
||||
public function testAPeriodWithoutABookingIsNotFound(): void
|
||||
{
|
||||
$controller = new TestableRemovePeriodController($this->createStub(AccommodationBookingService::class), $this->createStub(LoggerInterface::class));
|
||||
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
|
||||
$controller->index(
|
||||
new AccommodationBookingPeriod(AccommodationBookingPeriodPosition::Leading, new \DateTimeImmutable('2026-07-01'), new \DateTimeImmutable('2026-07-02')),
|
||||
$this->request(),
|
||||
);
|
||||
}
|
||||
|
||||
private function period(): AccommodationBookingPeriod
|
||||
{
|
||||
$booking = (new AccommodationBooking())
|
||||
->setDateFrom(new \DateTimeImmutable('2026-07-01'))
|
||||
->setDateTo(new \DateTimeImmutable('2026-07-08'));
|
||||
|
||||
return $booking->splitOffPeriod(AccommodationBookingPeriodPosition::Leading, 1);
|
||||
}
|
||||
|
||||
private function request(string $method = 'GET'): Request
|
||||
{
|
||||
return Request::create('/admin/accommodation-booking-period/1/remove', $method, server: ['HTTP_HX-Request' => 'true']);
|
||||
}
|
||||
}
|
||||
|
||||
final class TestableRemovePeriodController extends RemovePeriodController
|
||||
{
|
||||
public ?string $renderedView = null;
|
||||
|
||||
public bool $tokenValid = true;
|
||||
|
||||
/** @var list<array{type: string, message: mixed}> */
|
||||
public array $flashes = [];
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Controller\Admin\AccommodationBooking;
|
||||
|
||||
use App\Controller\Admin\AccommodationBooking\SplitPeriodController;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Enum\Groups\AccommodationBookingPeriodPosition;
|
||||
use App\Exception\ImplausibleBookingPriceException;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
/**
|
||||
* Covers how the modal answers — the split itself is tested in AccommodationBookingServiceTest
|
||||
* and AccommodationBookingTest.
|
||||
*/
|
||||
class SplitPeriodControllerTest extends TestCase
|
||||
{
|
||||
public function testGetRendersTheModal(): void
|
||||
{
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::never())->method('splitOffPeriod');
|
||||
|
||||
$controller = $this->buildController($bookingService, $this->form(submitted: false));
|
||||
|
||||
$response = $controller->index(new AccommodationBooking(), AccommodationBookingPeriodPosition::Leading, $this->request());
|
||||
|
||||
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
|
||||
self::assertSame('admin/accommodation_booking/modal_period.html.twig', $controller->renderedView);
|
||||
}
|
||||
|
||||
public function testASideThatWasAlreadySplitIsNotSplitAgain(): void
|
||||
{
|
||||
// The edit page only offers free sides; this is a page left open while the side was taken.
|
||||
$booking = (new AccommodationBooking())
|
||||
->setDateFrom(new \DateTimeImmutable('2026-07-01'))
|
||||
->setDateTo(new \DateTimeImmutable('2026-07-08'));
|
||||
$booking->splitOffPeriod(AccommodationBookingPeriodPosition::Leading, 1);
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::never())->method('splitOffPeriod');
|
||||
|
||||
$controller = $this->buildController($bookingService, $this->form(submitted: true));
|
||||
|
||||
$response = $controller->index($booking, AccommodationBookingPeriodPosition::Leading, $this->request('POST'));
|
||||
|
||||
self::assertTrue($response->headers->has('HX-Redirect'));
|
||||
self::assertNull($controller->renderedView);
|
||||
self::assertSame('error', $controller->flashes[0]['type']);
|
||||
}
|
||||
|
||||
public function testAValidSplitSendsTheBrowserBackToTheEditForm(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
|
||||
$bookingService = $this->createMock(AccommodationBookingService::class);
|
||||
$bookingService->expects(self::once())->method('splitOffPeriod')
|
||||
->with($booking, AccommodationBookingPeriodPosition::Trailing)
|
||||
->willReturn(new ConstraintViolationList());
|
||||
|
||||
$controller = $this->buildController($bookingService, $this->form(submitted: true));
|
||||
|
||||
$response = $controller->index($booking, AccommodationBookingPeriodPosition::Trailing, $this->request('POST'));
|
||||
|
||||
self::assertSame(
|
||||
'/app_admin_accommodationbooking_edit?r=%2Fadmin%2Faccommodation-booking%3Fpage%3D2',
|
||||
$response->headers->get('HX-Redirect'),
|
||||
);
|
||||
self::assertSame('success', $controller->flashes[0]['type']);
|
||||
}
|
||||
|
||||
public function testAViolationOfTheBookingIsReportedInTheModal(): void
|
||||
{
|
||||
$violations = new ConstraintViolationList([
|
||||
new ConstraintViolation('Nach dem Abteilen der Zeiträume muss mindestens eine Nacht für die Gruppe bleiben.', null, [], null, 'dateTo', null),
|
||||
]);
|
||||
$bookingService = $this->createStub(AccommodationBookingService::class);
|
||||
$bookingService->method('splitOffPeriod')->willReturn($violations);
|
||||
|
||||
$controller = $this->buildController($bookingService, $this->form(submitted: true, expectsError: true));
|
||||
|
||||
$response = $controller->index(new AccommodationBooking(), AccommodationBookingPeriodPosition::Leading, $this->request('POST'));
|
||||
|
||||
self::assertFalse($response->headers->has('HX-Redirect'), 'the modal stays open on the error');
|
||||
self::assertSame('admin/accommodation_booking/modal_period.html.twig', $controller->renderedView);
|
||||
self::assertSame([], $controller->flashes);
|
||||
}
|
||||
|
||||
public function testAnImplausibleTotalIsReportedInTheModal(): void
|
||||
{
|
||||
$bookingService = $this->createStub(AccommodationBookingService::class);
|
||||
$bookingService->method('splitOffPeriod')
|
||||
->willThrowException(new ImplausibleBookingPriceException(AccommodationBooking::MAX_TOTAL_PRICE + 1, 40, 2));
|
||||
|
||||
$controller = $this->buildController($bookingService, $this->form(submitted: true, expectsError: true));
|
||||
|
||||
$response = $controller->index(new AccommodationBooking(), AccommodationBookingPeriodPosition::Leading, $this->request('POST'));
|
||||
|
||||
self::assertFalse($response->headers->has('HX-Redirect'));
|
||||
self::assertSame('admin/accommodation_booking/modal_period.html.twig', $controller->renderedView);
|
||||
}
|
||||
|
||||
private function request(string $method = 'GET'): Request
|
||||
{
|
||||
return Request::create(
|
||||
'/admin/accommodation-booking/1/period/split/leading?r=%2Fadmin%2Faccommodation-booking%3Fpage%3D2',
|
||||
$method,
|
||||
server: ['HTTP_HX-Request' => 'true'],
|
||||
);
|
||||
}
|
||||
|
||||
private function form(bool $submitted, bool $expectsError = false): FormInterface
|
||||
{
|
||||
$form = $expectsError ? $this->createMock(FormInterface::class) : $this->createStub(FormInterface::class);
|
||||
if ($form instanceof \PHPUnit\Framework\MockObject\MockObject) {
|
||||
$form->expects(self::once())->method('addError');
|
||||
}
|
||||
$form->method('handleRequest')->willReturn($form);
|
||||
$form->method('isSubmitted')->willReturn($submitted);
|
||||
$form->method('isValid')->willReturn(true);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
private function buildController(AccommodationBookingService $bookingService, FormInterface $form): TestableSplitPeriodController
|
||||
{
|
||||
return new TestableSplitPeriodController($bookingService, $this->createStub(LoggerInterface::class), $form);
|
||||
}
|
||||
}
|
||||
|
||||
final class TestableSplitPeriodController extends SplitPeriodController
|
||||
{
|
||||
public ?string $renderedView = null;
|
||||
|
||||
/** @var list<array{type: string, message: mixed}> */
|
||||
public array $flashes = [];
|
||||
|
||||
public function __construct(
|
||||
AccommodationBookingService $bookingService,
|
||||
LoggerInterface $logger,
|
||||
private readonly FormInterface $form,
|
||||
) {
|
||||
parent::__construct($bookingService, $logger);
|
||||
}
|
||||
|
||||
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
|
||||
{
|
||||
return $this->form;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user