Files
myep/tests/Controller/Booking/Create/Step2ParticipantControllerTest.php
T

128 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller\Booking\Create;
use App\BusProNet\Model\Travel;
use App\Controller\Booking\Create\Step2ParticipantController;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Service\ApplicantInsuranceCascade;
use App\Service\BookingConfigurator;
use App\Service\BookingCreateContextFactory;
use App\Service\BookingSessionManager;
use App\Service\ParticipantDataPrefiller;
use App\Service\ParticipantFormSupport;
use App\Service\TravelDataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class Step2ParticipantControllerTest extends TestCase
{
public function testToggleChangesNothingWhereTheTravelDateDoesNotOfferTheChoice(): void
{
$request = Request::create('/bookings/create/applicant-toggle', 'POST');
$bookingDto = $this->createBookingDto();
$bookingSessionService = $this->createMock(BookingSessionManager::class);
$bookingSessionService->method('getBookingDto')->willReturn($bookingDto);
$bookingSessionService->expects($this->never())->method('saveBookingDto');
$controller = $this->createController($bookingSessionService);
$response = $controller->toggleApplicantTravels($request);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('/app_booking_create_step_2', $response->getTargetUrl());
$this->assertSame(0, $controller->createdForms, 'The submitted choice is never bound');
$this->assertTrue($bookingDto->applicantTravels);
}
public function testApplicantFormIsNotServedWhereTheTravelDateDoesNotOfferTheChoice(): void
{
$request = Request::create('/bookings/create/applicant', 'GET');
$bookingDto = $this->createBookingDto();
$bookingDto->applicantTravels = false;
$bookingDto->applicant = new ParticipantDto();
$bookingSessionService = $this->createMock(BookingSessionManager::class);
$bookingSessionService->method('getBookingDto')->willReturn($bookingDto);
$bookingSessionService->expects($this->never())->method('saveBookingDto');
$controller = $this->createController($bookingSessionService);
$response = $controller->editApplicant($request);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('/app_booking_create_step_2', $response->getTargetUrl());
$this->assertSame(0, $controller->createdForms);
}
private function createBookingDto(): BookingDto
{
$travel = new Travel();
$travel->id = 4711;
$participant = new ParticipantDto();
$participant->index = 0;
$bookingDto = new BookingDto($travel, 1);
$bookingDto->participants = [$participant];
return $bookingDto;
}
private function createController(BookingSessionManager $bookingSessionService): TestableStep2ParticipantController
{
return new TestableStep2ParticipantController(
$this->createStub(BookingConfigurator::class),
$bookingSessionService,
$this->createStub(BookingCreateContextFactory::class),
$this->createStub(TravelDataProvider::class),
$this->createStub(ParticipantDataPrefiller::class),
$this->createStub(ParticipantFormSupport::class),
$this->createStub(ApplicantInsuranceCascade::class),
);
}
}
final class TestableStep2ParticipantController extends Step2ParticipantController
{
public int $createdForms = 0;
/**
* @return FormInterface<mixed>
*/
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
{
++$this->createdForms;
throw new \LogicException('No form is expected to be built.');
}
/**
* @param array<string, mixed> $parameters
*/
protected function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
return '/'.$route;
}
protected function redirect(string $url, int $status = 302): RedirectResponse
{
return new RedirectResponse($url, $status);
}
/**
* @param array<string, mixed> $parameters
*/
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
{
return new RedirectResponse('/'.$route, $status);
}
}