fix: enable bookings without ski passes
This commit is contained in:
@@ -21,6 +21,7 @@ use App\Form\Service\ServiceAgeEvaluator;
|
||||
use App\Validator\Constraints\MandatoryAdditionalServicesSelectedValidator;
|
||||
use App\Validator\Constraints\PromoVoucherValidator;
|
||||
use App\Validator\Constraints\PurchaseVoucherValidator;
|
||||
use App\Validator\Constraints\SkiPassSelectionValidator;
|
||||
use Carbon\CarbonImmutable;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
@@ -291,6 +292,10 @@ class BookingEditPreFlightCheckerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
if (SkiPassSelectionValidator::class === $className) {
|
||||
return new SkiPassSelectionValidator($this->participantEligibilityChecker);
|
||||
}
|
||||
|
||||
return new $className();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,7 +9,9 @@ use App\BusProNet\Model\Service;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use App\Form\Service\ServiceAgeEvaluator;
|
||||
use App\Service\ParticipantEligibilityChecker;
|
||||
use App\Service\ServiceAvailabilityCalculator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ParticipantEligibilityCheckerTest extends TestCase
|
||||
@@ -18,7 +20,10 @@ class ParticipantEligibilityCheckerTest extends TestCase
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->service = new ParticipantEligibilityChecker();
|
||||
$this->service = new ParticipantEligibilityChecker(
|
||||
new ServiceAgeEvaluator(),
|
||||
new ServiceAvailabilityCalculator()
|
||||
);
|
||||
}
|
||||
|
||||
public function testBabyParticipantIsAlwaysEligible(): void
|
||||
@@ -53,9 +58,9 @@ class ParticipantEligibilityCheckerTest extends TestCase
|
||||
$this->assertTrue($result, 'Participant at BABY_MAX_AGE (2 years) should be eligible');
|
||||
}
|
||||
|
||||
public function testThreeYearOldParticipantIsNotBabyEligible(): void
|
||||
public function testThreeYearOldParticipantIsNotExemptAsBaby(): void
|
||||
{
|
||||
$travel = $this->createTravelWithNoSkiPasses();
|
||||
$travel = $this->createTravelWithSkiPasses(ageFrom: 6);
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
// Create a participant just over BABY_MAX_AGE (3 years old at travel date)
|
||||
@@ -64,9 +69,14 @@ class ParticipantEligibilityCheckerTest extends TestCase
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-3 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$result = $this->service->isParticipantEligible($bookingDto, 0);
|
||||
|
||||
$this->assertFalse($result, 'Participant over BABY_MAX_AGE (3 years) should not be eligible without ski passes');
|
||||
$this->assertTrue(
|
||||
$this->service->isSkiPassRequired($bookingDto, 0),
|
||||
'Participant over BABY_MAX_AGE (3 years) should still need a ski pass'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$this->service->isParticipantEligible($bookingDto, 0),
|
||||
'Participant over BABY_MAX_AGE (3 years) should not be eligible when no ski pass fits their age'
|
||||
);
|
||||
}
|
||||
|
||||
public function testAdultParticipantWithSkiPassIsEligible(): void
|
||||
@@ -138,6 +148,128 @@ class ParticipantEligibilityCheckerTest extends TestCase
|
||||
$this->assertTrue($result, 'Age should be calculated at travel date, not current date');
|
||||
}
|
||||
|
||||
public function testAdultIsEligibleOnTravelWithoutSkiPasses(): void
|
||||
{
|
||||
$travel = $this->createTravelWithNoSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$this->assertFalse(
|
||||
$this->service->isSkiPassRequired($bookingDto, 0),
|
||||
'A travel that offers no ski passes cannot require one'
|
||||
);
|
||||
$this->assertTrue(
|
||||
$this->service->isParticipantEligible($bookingDto, 0),
|
||||
'Adult should be eligible on a travel that offers no ski passes at all'
|
||||
);
|
||||
}
|
||||
|
||||
public function testTeenagerIsEligibleOnTravelWithoutSkiPasses(): void
|
||||
{
|
||||
$travel = $this->createTravelWithNoSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-14 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$this->assertFalse($this->service->isSkiPassRequired($bookingDto, 0));
|
||||
$this->assertTrue(
|
||||
$this->service->isParticipantEligible($bookingDto, 0),
|
||||
'Age must not be blamed when there is no ski pass to match against'
|
||||
);
|
||||
}
|
||||
|
||||
public function testParticipantIsNotEligibleWhenNoSkiPassMatchesTheirAge(): void
|
||||
{
|
||||
$travel = $this->createTravelWithSkiPasses(ageFrom: 18);
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-14 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$this->assertTrue($this->service->isSkiPassRequired($bookingDto, 0));
|
||||
$this->assertFalse(
|
||||
$this->service->isParticipantEligible($bookingDto, 0),
|
||||
'Ski passes exist but none fits the age - the participant stays ineligible'
|
||||
);
|
||||
}
|
||||
|
||||
public function testParticipantIsNotEligibleWhenSkiPassIsSoldOut(): void
|
||||
{
|
||||
$travel = $this->createTravelWithSkiPasses(available: 0);
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$this->assertTrue(
|
||||
$this->service->isSkiPassRequired($bookingDto, 0),
|
||||
'A sold out ski pass must not turn the requirement off'
|
||||
);
|
||||
$this->assertFalse($this->service->isParticipantEligible($bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testParticipantIsNotEligibleWhenSkiPassIsOnBookingStop(): void
|
||||
{
|
||||
$travel = $this->createTravelWithSkiPasses(status: Constants::STATUS_BLOCKED);
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-25 years');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$this->assertTrue($this->service->isSkiPassRequired($bookingDto, 0));
|
||||
$this->assertFalse($this->service->isParticipantEligible($bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testParticipantIsNotEligibleWhenLastSkiPassIsTakenByAnotherParticipant(): void
|
||||
{
|
||||
$travel = $this->createTravelWithSkiPasses(available: 1);
|
||||
$bookingDto = new BookingDto($travel, 2);
|
||||
|
||||
$firstParticipant = new ParticipantDto();
|
||||
$firstParticipant->index = 0;
|
||||
$firstParticipant->dateOfBirth = $travel->dateFrom->modify('-25 years');
|
||||
$firstParticipant->skiPass = $travel->additionalServices[1];
|
||||
$bookingDto->participants[0] = $firstParticipant;
|
||||
|
||||
$secondParticipant = new ParticipantDto();
|
||||
$secondParticipant->index = 1;
|
||||
$secondParticipant->dateOfBirth = $travel->dateFrom->modify('-25 years');
|
||||
$bookingDto->participants[1] = $secondParticipant;
|
||||
|
||||
$this->assertTrue($this->service->isParticipantEligible($bookingDto, 0));
|
||||
$this->assertFalse(
|
||||
$this->service->isParticipantEligible($bookingDto, 1),
|
||||
'The only ski pass is consumed by the first participant'
|
||||
);
|
||||
}
|
||||
|
||||
public function testBabyNeedsNoSkiPassEvenWhenTravelOffersThem(): void
|
||||
{
|
||||
$travel = $this->createTravelWithSkiPasses();
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0;
|
||||
$participant->dateOfBirth = $travel->dateFrom->modify('-1 year');
|
||||
$bookingDto->participants[0] = $participant;
|
||||
|
||||
$this->assertFalse($this->service->isSkiPassRequired($bookingDto, 0));
|
||||
$this->assertTrue($this->service->isParticipantEligible($bookingDto, 0));
|
||||
}
|
||||
|
||||
private function createTravelWithNoSkiPasses(): Travel
|
||||
{
|
||||
$travel = new Travel();
|
||||
@@ -148,20 +280,22 @@ class ParticipantEligibilityCheckerTest extends TestCase
|
||||
return $travel;
|
||||
}
|
||||
|
||||
private function createTravelWithSkiPasses(): Travel
|
||||
private function createTravelWithSkiPasses(?int $ageFrom = null, ?int $available = 10, ?string $status = null): Travel
|
||||
{
|
||||
$travel = new Travel();
|
||||
$travel->dateFrom = new \DateTimeImmutable('+30 days');
|
||||
$travel->dateTo = new \DateTimeImmutable('+37 days');
|
||||
|
||||
// Create a ski pass service available for all ages
|
||||
// Create a ski pass service, by default available for all ages
|
||||
$skiPass = new Service();
|
||||
$skiPass->id = 1;
|
||||
$skiPass->label = 'Test Ski Pass';
|
||||
$skiPass->subType = Constants::TOKEN_SKI_PASS;
|
||||
$skiPass->available = 10;
|
||||
$skiPass->available = $available;
|
||||
$skiPass->status = $status;
|
||||
$skiPass->price = 100.0;
|
||||
$skiPass->ageConstraintType = null; // No age constraint
|
||||
$skiPass->ageConstraintType = null === $ageFrom ? null : 'absolute_age';
|
||||
$skiPass->ageFrom = $ageFrom;
|
||||
$skiPass->dateFrom = $travel->dateFrom;
|
||||
$skiPass->dateTo = $travel->dateTo;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user