260 lines
8.4 KiB
PHP
260 lines
8.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Controller\Booking\Edit;
|
|
|
|
use App\BusProNet\Model\Booking;
|
|
use App\BusProNet\Model\Travel;
|
|
use App\BusProNet\ApiClient;
|
|
use App\Controller\Booking\Edit\ParticipantController;
|
|
use App\Entity\User;
|
|
use App\Form\BookingParticipantType;
|
|
use App\Form\Model\BookingDto;
|
|
use App\Form\Model\BookingEditContext;
|
|
use App\Form\Model\BookingSummaryDto;
|
|
use App\Form\Model\ParticipantDto;
|
|
use App\Form\Model\ParticipantEditDto;
|
|
use App\Service\BookingEditContextFactory;
|
|
use App\Service\BookingEditDataLoader;
|
|
use App\Service\BookingEditDraftManager;
|
|
use App\Service\BookingSessionManager;
|
|
use App\Service\ParticipantDataPrefiller;
|
|
use App\Service\ParticipantFormSupport;
|
|
use App\Security\Crypt;
|
|
use Carbon\CarbonImmutable;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
|
|
class ParticipantControllerTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
CarbonImmutable::setTestNow();
|
|
}
|
|
|
|
public function testDummyTokenPrefillsParticipantInEditMode(): void
|
|
{
|
|
CarbonImmutable::setTestNow(CarbonImmutable::create(2026, 1, 15, 14, 30));
|
|
|
|
$request = Request::create('/bookings/42/edit/participants/0', 'POST');
|
|
$user = $this->createUser();
|
|
$bookingDto = $this->createBookingDto();
|
|
$bookingData = $this->createBooking();
|
|
$participant = $bookingDto->participants[0];
|
|
$participant->lastName = ParticipantDataPrefiller::TOKEN;
|
|
|
|
$form = $this->createMock(FormInterface::class);
|
|
$form->expects($this->once())
|
|
->method('handleRequest')
|
|
->with($request)
|
|
->willReturnSelf();
|
|
$form->expects($this->once())
|
|
->method('isSubmitted')
|
|
->willReturn(true);
|
|
$form->expects($this->never())
|
|
->method('isValid');
|
|
|
|
$dataLoader = $this->createMock(BookingEditDataLoader::class);
|
|
$dataLoader->expects($this->once())
|
|
->method('fetchBookingData')
|
|
->with(42, $user)
|
|
->willReturn($bookingData);
|
|
|
|
$bookingSessionService = $this->createMock(BookingSessionManager::class);
|
|
$bookingSessionService->expects($this->once())
|
|
->method('getBookingDto')
|
|
->with($request, BookingDto::MODE_EDIT)
|
|
->willReturn($bookingDto);
|
|
$bookingSessionService->expects($this->once())
|
|
->method('saveBookingDto')
|
|
->with($request, $bookingDto, BookingDto::MODE_EDIT);
|
|
|
|
$draftService = $this->createMock(BookingEditDraftManager::class);
|
|
$draftService->expects($this->once())
|
|
->method('saveDraft')
|
|
->with($user, 42, $bookingDto);
|
|
|
|
$prepopulationService = new ParticipantDataPrefiller(
|
|
$this->createMock(ApiClient::class),
|
|
$this->createMock(Crypt::class),
|
|
$this->createMock(LoggerInterface::class),
|
|
);
|
|
|
|
$participantFormSupportService = $this->createMock(ParticipantFormSupport::class);
|
|
$participantFormSupportService->expects($this->once())
|
|
->method('ensureParticipantExists')
|
|
->with($bookingDto, 0)
|
|
->willReturn($participant);
|
|
$participantFormSupportService->expects($this->exactly(2))
|
|
->method('createParticipantEditDto')
|
|
->with($bookingDto, 0)
|
|
->willReturn(new ParticipantEditDto($participant, $bookingDto));
|
|
$participantFormSupportService->expects($this->exactly(2))
|
|
->method('getParticipantFormOptions')
|
|
->with($bookingDto)
|
|
->willReturn([
|
|
'booking_context' => $bookingDto,
|
|
'body_dimension_ranges' => [
|
|
'height_min' => 100,
|
|
'height_max' => 250,
|
|
'weight_min' => 20,
|
|
'weight_max' => 200,
|
|
'shoe_size_min' => 20,
|
|
'shoe_size_max' => 55,
|
|
],
|
|
]);
|
|
|
|
$summaryData = $this->createMock(BookingSummaryDto::class);
|
|
$context = new BookingEditContext($bookingDto, $bookingData, null, $summaryData);
|
|
|
|
$contextFactory = $this->createMock(BookingEditContextFactory::class);
|
|
$contextFactory->expects($this->once())
|
|
->method('prepareBookingDto')
|
|
->with($bookingDto);
|
|
$contextFactory->expects($this->once())
|
|
->method('createParticipantContext')
|
|
->with($bookingDto, $bookingData)
|
|
->willReturn($context);
|
|
|
|
$controller = new TestableParticipantController(
|
|
$dataLoader,
|
|
$draftService,
|
|
$contextFactory,
|
|
$bookingSessionService,
|
|
$prepopulationService,
|
|
$participantFormSupportService,
|
|
$user,
|
|
$form,
|
|
);
|
|
|
|
$response = $controller->editParticipant(42, 0, $request);
|
|
|
|
$this->assertInstanceOf(Response::class, $response);
|
|
$this->assertSame('booking/edit/participant.html.twig', $controller->renderTemplate);
|
|
$this->assertSame('Vorname 1', $participant->firstName);
|
|
$this->assertSame('Muster 1 14:30', $participant->lastName);
|
|
$this->assertSame('[email protected]', $participant->email);
|
|
$this->assertSame('2006-01-15', $participant->dateOfBirth?->format('Y-m-d'));
|
|
$this->assertSame(BookingParticipantType::class, $controller->createdFormTypes[0]);
|
|
$this->assertSame(BookingParticipantType::class, $controller->createdFormTypes[1]);
|
|
}
|
|
|
|
private function createUser(): User
|
|
{
|
|
return new User('[email protected]');
|
|
}
|
|
|
|
private function createBookingDto(): BookingDto
|
|
{
|
|
$travel = new Travel();
|
|
$travel->id = 1234;
|
|
|
|
$bookingDto = new BookingDto($travel, 77);
|
|
$bookingDto->booking = new Booking();
|
|
$bookingDto->booking->id = 42;
|
|
$bookingDto->participants = [new ParticipantDto()];
|
|
$bookingDto->participants[0]->index = 0;
|
|
|
|
return $bookingDto;
|
|
}
|
|
|
|
private function createBooking(): Booking
|
|
{
|
|
$booking = new Booking();
|
|
$booking->id = 42;
|
|
$booking->dateId = 1234;
|
|
$booking->participantsStatus = ['F'];
|
|
|
|
return $booking;
|
|
}
|
|
}
|
|
|
|
final class TestableParticipantController extends ParticipantController
|
|
{
|
|
/**
|
|
* @var array<int, string>
|
|
*/
|
|
public array $createdFormTypes = [];
|
|
|
|
public ?string $renderTemplate = null;
|
|
|
|
/**
|
|
* @var array<string, mixed>
|
|
*/
|
|
public array $renderParameters = [];
|
|
|
|
/**
|
|
* @var FormInterface<mixed>
|
|
*/
|
|
private readonly FormInterface $form;
|
|
|
|
/**
|
|
* @param FormInterface<mixed> $form
|
|
*/
|
|
public function __construct(
|
|
BookingEditDataLoader $dataLoader,
|
|
BookingEditDraftManager $draftService,
|
|
BookingEditContextFactory $editContextFactory,
|
|
BookingSessionManager $bookingSessionService,
|
|
ParticipantDataPrefiller $prepopulationService,
|
|
ParticipantFormSupport $participantFormSupportService,
|
|
private readonly User $user,
|
|
FormInterface $form,
|
|
) {
|
|
$this->form = $form;
|
|
|
|
parent::__construct(
|
|
$dataLoader,
|
|
$draftService,
|
|
$editContextFactory,
|
|
$bookingSessionService,
|
|
$prepopulationService,
|
|
$participantFormSupportService,
|
|
);
|
|
}
|
|
|
|
protected function getUser(): UserInterface
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
/**
|
|
* @return FormInterface<mixed>
|
|
*/
|
|
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
|
|
{
|
|
$this->createdFormTypes[] = $type;
|
|
|
|
return $this->form;
|
|
}
|
|
|
|
protected function addFlash(string $type, mixed $message): void
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @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->renderTemplate = $view;
|
|
$this->renderParameters = $parameters;
|
|
|
|
return new Response('');
|
|
}
|
|
}
|