Files
myep/tests/Controller/Admin/AccommodationBooking/CreateControllerTest.php
T

119 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller\Admin\AccommodationBooking;
use App\Controller\Admin\AccommodationBooking\CreateController;
use App\Entity\Groups\AccommodationBooking;
use App\Entity\User;
use App\Exception\ImplausibleBookingPriceException;
use App\Service\AccommodationBookingService;
use Doctrine\ORM\EntityManagerInterface;
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\Security\Core\User\UserInterface;
/**
* Covers the Betreuer default only — the form itself is exercised through
* AccommodationBookingCreateType.
*/
class CreateControllerTest extends TestCase
{
public function testTheCreatingUserBecomesTheBetreuer(): void
{
$user = new User('[email protected]');
$controller = $this->buildController($user);
$controller->index(Request::create('/admin/accommodation-booking/create'));
self::assertSame($user, $controller->capturedBooking?->getManagedBy());
}
public function testAnImplausiblePriceComesBackAsAFormErrorWithoutSaving(): void
{
// Same as on the edit form: the fix is in the house's price rows, so the office is told
// on the form it is standing in front of, and nothing is written.
$bookingService = $this->createStub(AccommodationBookingService::class);
$bookingService->method('refreshPriceSnapshot')
->willThrowException(new ImplausibleBookingPriceException(AccommodationBooking::MAX_TOTAL_PRICE + 1, 40, 2));
$form = $this->createMock(FormInterface::class);
$form->method('handleRequest')->willReturn($form);
$form->method('isSubmitted')->willReturn(true);
$form->method('isValid')->willReturn(true);
$form->expects(self::once())->method('addError');
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->expects(self::never())->method('persist');
$entityManager->expects(self::never())->method('flush');
$controller = new TestableCreateController(
$entityManager,
$this->createStub(LoggerInterface::class),
$bookingService,
$form,
new User('[email protected]'),
);
$response = $controller->index(Request::create('/admin/accommodation-booking/create', 'POST'));
self::assertSame(Response::HTTP_OK, $response->getStatusCode(), 'the modal is re-rendered, not redirected');
}
private function buildController(?UserInterface $user): TestableCreateController
{
$form = $this->createStub(FormInterface::class);
$form->method('handleRequest')->willReturn($form);
$form->method('isSubmitted')->willReturn(false);
return new TestableCreateController(
$this->createStub(EntityManagerInterface::class),
$this->createStub(LoggerInterface::class),
$this->createStub(AccommodationBookingService::class),
$form,
$user,
);
}
}
final class TestableCreateController extends CreateController
{
public ?AccommodationBooking $capturedBooking = null;
public function __construct(
EntityManagerInterface $entityManager,
LoggerInterface $logger,
AccommodationBookingService $bookingService,
private readonly FormInterface $form,
private readonly ?UserInterface $user,
) {
parent::__construct($entityManager, $logger, $bookingService);
}
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
{
if ($data instanceof AccommodationBooking) {
$this->capturedBooking = $data;
}
return $this->form;
}
protected function getUser(): ?UserInterface
{
return $this->user;
}
/**
* @param array<string, mixed> $parameters
*/
protected function render(string $view, array $parameters = [], ?Response $response = null): Response
{
return new Response();
}
}