fix: enable bookings without ski passes
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Form\Model;
|
||||
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\BusProNet\Model\Address;
|
||||
use App\Form\Model\AddressDto;
|
||||
@@ -17,10 +18,12 @@ use App\Form\Model\ParticipantEditDto;
|
||||
use App\Form\Service\ServiceAgeEvaluator;
|
||||
use App\Service\BookingPriceCalculator;
|
||||
use App\Service\ParticipantEligibilityChecker;
|
||||
use App\Service\ServiceAvailabilityCalculator;
|
||||
use App\Service\VoucherValidator;
|
||||
use App\Validator\Constraints\MandatoryAdditionalServicesSelectedValidator;
|
||||
use App\Validator\Constraints\PromoVoucherValidator;
|
||||
use App\Validator\Constraints\PurchaseVoucherValidator;
|
||||
use App\Validator\Constraints\SkiPassSelectionValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints\Email;
|
||||
@@ -49,13 +52,21 @@ class ParticipantEditDtoTest extends TestCase
|
||||
$mockParticipantEligibilityChecker = $this->createMock(ParticipantEligibilityChecker::class);
|
||||
$mockServiceAgeEvaluator = $this->createMock(ServiceAgeEvaluator::class);
|
||||
|
||||
// Ski pass validation is exercised against the real gate, not a mock: which participant
|
||||
// needs a pass is exactly the behaviour under test here.
|
||||
$participantEligibilityChecker = new ParticipantEligibilityChecker(
|
||||
new ServiceAgeEvaluator(),
|
||||
new ServiceAvailabilityCalculator()
|
||||
);
|
||||
|
||||
// Create custom validator factory that can inject dependencies
|
||||
$validatorFactory = new class($mockVoucherService, $mockPriceCalculatorService, $mockParticipantEligibilityChecker, $mockServiceAgeEvaluator) implements ConstraintValidatorFactoryInterface {
|
||||
$validatorFactory = new class($mockVoucherService, $mockPriceCalculatorService, $mockParticipantEligibilityChecker, $mockServiceAgeEvaluator, $participantEligibilityChecker) implements ConstraintValidatorFactoryInterface {
|
||||
public function __construct(
|
||||
private readonly VoucherValidator $voucherService,
|
||||
private readonly BookingPriceCalculator $priceCalculatorService,
|
||||
private readonly ParticipantEligibilityChecker $participantEligibilityService,
|
||||
private readonly ServiceAgeEvaluator $serviceAgeEvaluator,
|
||||
private readonly ParticipantEligibilityChecker $realEligibilityChecker,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -75,6 +86,10 @@ class ParticipantEditDtoTest extends TestCase
|
||||
return new EmailValidator(Email::VALIDATION_MODE_HTML5);
|
||||
}
|
||||
|
||||
if (SkiPassSelectionValidator::class === $className) {
|
||||
return new SkiPassSelectionValidator($this->realEligibilityChecker);
|
||||
}
|
||||
|
||||
if (MandatoryAdditionalServicesSelectedValidator::class === $className) {
|
||||
return new MandatoryAdditionalServicesSelectedValidator(
|
||||
$this->participantEligibilityService,
|
||||
@@ -312,9 +327,7 @@ class ParticipantEditDtoTest extends TestCase
|
||||
|
||||
public function testValidationInEditMode(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
$travel = $this->createTravelOfferingSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1); // hotelId must be int
|
||||
|
||||
@@ -483,9 +496,7 @@ class ParticipantEditDtoTest extends TestCase
|
||||
|
||||
public function testSkiPassNotRequiredForTwoYearOldInCreateMode(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
$travel = $this->createTravelOfferingSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
@@ -520,9 +531,7 @@ class ParticipantEditDtoTest extends TestCase
|
||||
|
||||
public function testSkiPassRequiredForThreeYearOldInCreateMode(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
$travel = $this->createTravelOfferingSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
@@ -555,11 +564,81 @@ class ParticipantEditDtoTest extends TestCase
|
||||
$this->assertCount(1, $skiPassViolations, 'Participant over BABY_MAX_AGE (3 years) should require ski pass');
|
||||
}
|
||||
|
||||
public function testSkiPassNotRequiredWhenTravelOffersNone(): void
|
||||
{
|
||||
$travel = $this->createTravelWithoutSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
|
||||
$participant->dateOfBirth = new \DateTimeImmutable('1990-01-01');
|
||||
$participant->index = 0;
|
||||
$participant->mobile = '+49 123 456789';
|
||||
$participant->address = new Address();
|
||||
$participant->address->street = 'Test Street 1';
|
||||
$participant->address->postCode = '12345';
|
||||
$participant->address->city = 'Test City';
|
||||
$participant->address->country = 'DE';
|
||||
|
||||
$bookingDto->participants = [$participant];
|
||||
|
||||
$wrapper = new ParticipantEditDto(
|
||||
participant: $bookingDto->participants[0],
|
||||
bookingContext: $bookingDto,
|
||||
);
|
||||
|
||||
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
|
||||
|
||||
$skiPassViolations = array_filter(
|
||||
iterator_to_array($violations),
|
||||
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
|
||||
);
|
||||
|
||||
$this->assertCount(0, $skiPassViolations, 'A travel offering no ski passes must be bookable without one');
|
||||
}
|
||||
|
||||
public function testIneligibleParticipantIsReportedAsNotBookable(): void
|
||||
{
|
||||
// Ski passes exist, but the only one starts at 18 - a 14 year old cannot obtain any
|
||||
$travel = $this->createTravelOfferingSkiPasses(ageFrom: 18);
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = $this->createParticipantWithoutSkiPass('[email protected]');
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-14 years');
|
||||
$participant->index = 0;
|
||||
$participant->mobile = '+49 123 456789';
|
||||
$participant->address = new Address();
|
||||
$participant->address->street = 'Test Street 1';
|
||||
$participant->address->postCode = '12345';
|
||||
$participant->address->city = 'Test City';
|
||||
$participant->address->country = 'DE';
|
||||
|
||||
$bookingDto->participants = [$participant];
|
||||
|
||||
$wrapper = new ParticipantEditDto(
|
||||
participant: $bookingDto->participants[0],
|
||||
bookingContext: $bookingDto,
|
||||
);
|
||||
|
||||
$violations = $this->validator->validate($wrapper, null, ['strict_required']);
|
||||
|
||||
$skiPassViolations = array_values(array_filter(
|
||||
iterator_to_array($violations),
|
||||
fn ($v) => 'participant.skiPass' === $v->getPropertyPath()
|
||||
));
|
||||
|
||||
$this->assertCount(1, $skiPassViolations);
|
||||
$this->assertSame(
|
||||
'Für Teilnehmer:in 1 ist keine Buchung möglich',
|
||||
$skiPassViolations[0]->getMessage(),
|
||||
'The user must not be asked to select from a field that is not rendered'
|
||||
);
|
||||
}
|
||||
|
||||
public function testSkiPassValidationSkippedInEditMode(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
$travel = $this->createTravelOfferingSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
@@ -599,9 +678,7 @@ class ParticipantEditDtoTest extends TestCase
|
||||
|
||||
public function testEditSubmissionValidatesApplicantAddressOnlyForFirstParticipant(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
$travel = $this->createTravelOfferingSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
$bookingDto->booking = new Booking();
|
||||
@@ -644,9 +721,7 @@ class ParticipantEditDtoTest extends TestCase
|
||||
|
||||
public function testSkiPassAgeCalculatedAtTravelDate(): void
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
$travel = $this->createTravelOfferingSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
@@ -776,12 +851,42 @@ class ParticipantEditDtoTest extends TestCase
|
||||
$this->assertCount(1, $insuranceViolations);
|
||||
}
|
||||
|
||||
private function createBookingDtoWithParticipants(array $participants): BookingDto
|
||||
/**
|
||||
* Builds the default travel fixture: a regular travel that offers one unconstrained,
|
||||
* available ski pass. Ski pass validation depends on the travel actually offering
|
||||
* passes, so a travel without them is a distinct fixture, not the default.
|
||||
*/
|
||||
private function createTravelOfferingSkiPasses(?int $ageFrom = null): Travel
|
||||
{
|
||||
$travel = $this->createTravelWithoutSkiPasses();
|
||||
|
||||
$skiPass = new Service();
|
||||
$skiPass->id = 900;
|
||||
$skiPass->label = 'Test Ski Pass';
|
||||
$skiPass->subType = Constants::TOKEN_SKI_PASS;
|
||||
$skiPass->available = 10;
|
||||
$skiPass->price = 100.0;
|
||||
$skiPass->ageConstraintType = null === $ageFrom ? null : 'absolute_age';
|
||||
$skiPass->ageFrom = $ageFrom;
|
||||
|
||||
$travel->additionalServices = [900 => $skiPass];
|
||||
|
||||
return $travel;
|
||||
}
|
||||
|
||||
private function createTravelWithoutSkiPasses(): Travel
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('2025-06-01');
|
||||
$travel->dateTo = new \DateTimeImmutable('2025-06-08');
|
||||
|
||||
return $travel;
|
||||
}
|
||||
|
||||
private function createBookingDtoWithParticipants(array $participants): BookingDto
|
||||
{
|
||||
$travel = $this->createTravelOfferingSkiPasses();
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1); // hotelId must be int
|
||||
|
||||
// Set index for each participant
|
||||
|
||||
Reference in New Issue
Block a user