feat: auto-booking services

addresses #869cfcptv
This commit is contained in:
Björn Fromme
2026-03-16 20:24:58 +01:00
parent 6d9265de0f
commit c510cbf590
19 changed files with 1829 additions and 72 deletions
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantAdditionalServicesFieldHandler;
use PHPUnit\Framework\TestCase;
class ParticipantAdditionalServicesFieldHandlerTest extends TestCase
{
public function testDeselectAutoBookServiceAddsOptOutId(): void
{
$autoBookService = new Service();
$autoBookService->id = 10;
$autoBookService->subType = Constants::TOKEN_ADDITIONAL;
$autoBookService->label = 'Auto Book';
$autoBookService->autoBook = true;
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$travel->additionalServices = [10 => $autoBookService];
$bookingDto = new BookingDto($travel, 1);
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
$participant->additionalServices = [$autoBookService];
$bookingDto->participants[0] = $participant;
$handler = new ParticipantAdditionalServicesFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([10], $participant->autoBookOptOutServiceIds);
$this->assertSame([], $participant->additionalServices);
}
public function testDeselectMandatoryAutoBookServiceDoesNotAddOptOutId(): void
{
$service = new Service();
$service->id = 11;
$service->subType = Constants::TOKEN_ADDITIONAL;
$service->label = 'Mandatory Auto Book';
$service->mandatory = true;
$service->autoBook = true;
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$travel->additionalServices = [11 => $service];
$bookingDto = new BookingDto($travel, 1);
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
$participant->additionalServices = [$service];
$bookingDto->participants[0] = $participant;
$handler = new ParticipantAdditionalServicesFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([], $participant->autoBookOptOutServiceIds);
}
}
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantBoardFieldHandler;
use PHPUnit\Framework\TestCase;
class ParticipantBoardFieldHandlerTest extends TestCase
{
public function testDeselectAutoBookBoardAddsOptOutId(): void
{
$autoBookBoard = new Service();
$autoBookBoard->id = 60;
$autoBookBoard->subType = Constants::TOKEN_BOARD;
$autoBookBoard->label = 'Auto Book Board';
$autoBookBoard->autoBook = true;
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$travel->additionalServices = [60 => $autoBookBoard];
$bookingDto = new BookingDto($travel, 1);
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
$participant->board = [$autoBookBoard];
$bookingDto->participants[0] = $participant;
$handler = new ParticipantBoardFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([60], $participant->autoBookOptOutBoardIds);
$this->assertSame([], $participant->board);
}
public function testDeselectMandatoryAutoBookBoardDoesNotAddOptOutId(): void
{
$mandatoryAutoBookBoard = new Service();
$mandatoryAutoBookBoard->id = 61;
$mandatoryAutoBookBoard->subType = Constants::TOKEN_BOARD;
$mandatoryAutoBookBoard->label = 'Mandatory Auto Book Board';
$mandatoryAutoBookBoard->mandatory = true;
$mandatoryAutoBookBoard->autoBook = true;
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$travel->additionalServices = [61 => $mandatoryAutoBookBoard];
$bookingDto = new BookingDto($travel, 1);
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
$participant->board = [$mandatoryAutoBookBoard];
$bookingDto->participants[0] = $participant;
$handler = new ParticipantBoardFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([], $participant->autoBookOptOutBoardIds);
}
public function testReselectAutoBookBoardRemovesOptOutId(): void
{
$autoBookBoard = new Service();
$autoBookBoard->id = 62;
$autoBookBoard->subType = Constants::TOKEN_BOARD;
$autoBookBoard->label = 'Auto Book Board';
$autoBookBoard->autoBook = true;
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$travel->additionalServices = [62 => $autoBookBoard];
$bookingDto = new BookingDto($travel, 1);
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
$participant->autoBookOptOutBoardIds = [62];
$bookingDto->participants[0] = $participant;
$handler = new ParticipantBoardFieldHandler();
$handler->processField(['board' => ['62']], $bookingDto, 0);
$this->assertSame([], $participant->autoBookOptOutBoardIds);
$this->assertCount(1, $participant->board);
$this->assertSame(62, $participant->board[0]->id);
}
}
@@ -91,6 +91,7 @@ class ParticipantFieldOptionsProviderMandatoryServiceTest extends TestCase
$nonMandatoryService->available = 10;
$nonMandatoryService->price = 10.0;
$nonMandatoryService->mandatory = false;
$nonMandatoryService->autoBook = true;
$travel = $this->createTravelWithAdditionalServices([$nonMandatoryService]);
$bookingDto = new BookingDto($travel, 1);
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantRentalsFieldHandler;
use PHPUnit\Framework\TestCase;
class ParticipantRentalsFieldHandlerTest extends TestCase
{
public function testDeselectAutoBookRentalAddsOptOutId(): void
{
$skiPass = $this->createSkiPass(70);
$autoBookRental = $this->createRental(71, autoBook: true, mandatory: false);
$bookingDto = $this->createBookingDtoWithSkiPassAndRental($skiPass, $autoBookRental);
$participant = $bookingDto->participants[0];
$participant->rentals = [$autoBookRental];
$handler = new ParticipantRentalsFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([71], $participant->autoBookOptOutRentalIds);
$this->assertSame([], $participant->rentals);
}
public function testDeselectMandatoryAutoBookRentalDoesNotAddOptOutId(): void
{
$skiPass = $this->createSkiPass(72);
$mandatoryAutoBookRental = $this->createRental(73, autoBook: true, mandatory: true);
$bookingDto = $this->createBookingDtoWithSkiPassAndRental($skiPass, $mandatoryAutoBookRental);
$participant = $bookingDto->participants[0];
$participant->rentals = [$mandatoryAutoBookRental];
$handler = new ParticipantRentalsFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([], $participant->autoBookOptOutRentalIds);
}
public function testReselectAutoBookRentalRemovesOptOutId(): void
{
$skiPass = $this->createSkiPass(74);
$autoBookRental = $this->createRental(75, autoBook: true, mandatory: false);
$bookingDto = $this->createBookingDtoWithSkiPassAndRental($skiPass, $autoBookRental);
$participant = $bookingDto->participants[0];
$participant->autoBookOptOutRentalIds = [75];
$handler = new ParticipantRentalsFieldHandler();
$handler->processField(['rentals' => ['75']], $bookingDto, 0);
$this->assertSame([], $participant->autoBookOptOutRentalIds);
$this->assertCount(1, $participant->rentals);
$this->assertSame(75, $participant->rentals[0]->id);
}
private function createBookingDtoWithSkiPassAndRental(Service $skiPass, Service $rental): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-08');
$travel->additionalServices = [
$skiPass->id => $skiPass,
$rental->id => $rental,
];
$bookingDto = new BookingDto($travel, 1);
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
$participant->skiPass = $skiPass;
$bookingDto->participants[0] = $participant;
return $bookingDto;
}
private function createSkiPass(int $id): Service
{
$service = new Service();
$service->id = $id;
$service->subType = Constants::TOKEN_SKI_PASS;
$service->label = 'Ski Pass';
$service->dateFrom = new \DateTimeImmutable('2030-01-01');
$service->dateTo = new \DateTimeImmutable('2030-01-07');
return $service;
}
private function createRental(int $id, bool $autoBook, bool $mandatory): Service
{
$service = new Service();
$service->id = $id;
$service->subType = 'VER';
$service->label = 'Rental';
$service->autoBook = $autoBook;
$service->mandatory = $mandatory;
$service->dateFrom = new \DateTimeImmutable('2030-01-01');
$service->dateTo = new \DateTimeImmutable('2030-01-07');
return $service;
}
}
@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantSkiPassFieldHandler;
use PHPUnit\Framework\TestCase;
class ParticipantSkiPassFieldHandlerTest extends TestCase
{
public function testDeselectAutoBookSkiPassAddsOptOutId(): void
{
$autoBookSkiPass = $this->createSkiPass(31, autoBook: true, mandatory: false);
$bookingDto = $this->createBookingDtoWithSkiPass($autoBookSkiPass);
$participant = $bookingDto->participants[0];
$participant->skiPass = $autoBookSkiPass;
$handler = new ParticipantSkiPassFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([31], $participant->autoBookOptOutSkiPassIds);
$this->assertNull($participant->skiPass);
}
public function testDeselectMandatoryAutoBookSkiPassDoesNotAddOptOutId(): void
{
$mandatoryAutoBookSkiPass = $this->createSkiPass(32, autoBook: true, mandatory: true);
$bookingDto = $this->createBookingDtoWithSkiPass($mandatoryAutoBookSkiPass);
$participant = $bookingDto->participants[0];
$participant->skiPass = $mandatoryAutoBookSkiPass;
$handler = new ParticipantSkiPassFieldHandler();
$handler->processField([], $bookingDto, 0);
$this->assertSame([], $participant->autoBookOptOutSkiPassIds);
}
public function testReselectAutoBookSkiPassRemovesOptOutId(): void
{
$autoBookSkiPass = $this->createSkiPass(33, autoBook: true, mandatory: false);
$bookingDto = $this->createBookingDtoWithSkiPass($autoBookSkiPass);
$participant = $bookingDto->participants[0];
$participant->autoBookOptOutSkiPassIds = [33];
$handler = new ParticipantSkiPassFieldHandler();
$handler->processField(['skiPass' => '33'], $bookingDto, 0);
$this->assertSame([], $participant->autoBookOptOutSkiPassIds);
$this->assertNotNull($participant->skiPass);
$this->assertSame(33, $participant->skiPass?->id);
}
private function createBookingDtoWithSkiPass(Service $skiPass): BookingDto
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+30 days');
$travel->dateTo = new \DateTimeImmutable('+37 days');
$travel->additionalServices = [$skiPass->id => $skiPass];
$bookingDto = new BookingDto($travel, 1);
$participant = new ParticipantDto();
$participant->index = 0;
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
$bookingDto->participants[0] = $participant;
return $bookingDto;
}
private function createSkiPass(int $id, bool $autoBook, bool $mandatory): Service
{
$service = new Service();
$service->id = $id;
$service->subType = Constants::TOKEN_SKI_PASS;
$service->label = 'Ski Pass';
$service->autoBook = $autoBook;
$service->mandatory = $mandatory;
$service->dateFrom = new \DateTimeImmutable('+30 days');
$service->dateTo = new \DateTimeImmutable('+36 days');
return $service;
}
}