Files
myep/tests/Controller/Booking/ParticipantCardFlowTraitTest.php
T
2026-03-16 20:24:58 +01:00

148 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller\Booking;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Travel;
use App\Controller\Booking\Traits\ParticipantCardFlowTrait;
use App\Form\Model\BookingDto;
use App\Form\Model\BookingSummaryDto;
use App\Form\Model\ParticipantDto;
use App\Service\BookingService;
use App\Service\BookingSummaryDataService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ParticipantCardFlowTraitTest extends TestCase
{
public function testHandleParticipantRefreshReappliesDefaultPreselectionInEditMode(): void
{
$bookingService = $this->createMock(BookingService::class);
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
$formOne = $this->createMock(FormInterface::class);
$formOne->expects($this->once())
->method('handleRequest');
$formOne->method('isSubmitted')
->willReturn(true);
$formTwo = $this->createMock(FormInterface::class);
$formTwo->expects($this->once())
->method('createView')
->willReturn(new FormView());
$bookingDto = $this->createEditModeBookingDto();
$request = new Request();
$bookingService->expects($this->once())
->method('preselectDefaultServices')
->with($bookingDto);
$bookingService->expects($this->once())
->method('saveBookingDto')
->with($request, $bookingDto, BookingDto::MODE_EDIT);
$summaryDataService->expects($this->once())
->method('getSummaryData')
->with($bookingDto)
->willReturn(new BookingSummaryDto([], 1, 0.0, 0.0, [], [], [], null));
$controller = new class($bookingService, $summaryDataService, [$formOne, $formTwo]) {
use ParticipantCardFlowTrait;
public BookingService $bookingService;
public BookingSummaryDataService $summaryDataService;
public object $participantCardService;
/** @var FormInterface[] */
private array $forms;
public function __construct(BookingService $bookingService, BookingSummaryDataService $summaryDataService, array $forms)
{
$this->bookingService = $bookingService;
$this->summaryDataService = $summaryDataService;
$this->forms = $forms;
$this->participantCardService = new class() {
public function getAllCardsData(BookingDto $bookingDto): array
{
return [];
}
};
}
public function callHandleParticipantRefresh(
Request $request,
BookingDto $bookingDto,
int $index,
string $refreshRouteName,
): Response {
$method = new \ReflectionMethod($this, 'handleParticipantRefresh');
$method->setAccessible(true);
return $method->invoke($this, $request, $bookingDto, $index, $refreshRouteName);
}
private function htmxOobResponse(string $template, array $blocks, array $parameters): Response
{
return new Response('ok');
}
protected function getParameter(string $name): mixed
{
return [];
}
private function createForm(string $type, $data = null, array $options = []): FormInterface
{
return array_shift($this->forms);
}
private function render(string $view, array $parameters = [], ?Response $response = null): Response
{
return new Response('rendered');
}
protected function addFlash(string $type, mixed $message): void
{
}
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
{
return new RedirectResponse('/');
}
};
$response = $controller->callHandleParticipantRefresh(
$request,
$bookingDto,
0,
'app_booking_edit_step_2_participant_refresh'
);
$this->assertSame('ok', $response->getContent());
}
private function createEditModeBookingDto(): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$bookingDto->booking = new Booking(); // Marks DTO as edit mode
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$bookingDto->participants[0] = $participant;
return $bookingDto;
}
}