fix: consider parking space availability
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\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\Service\ServiceAvailabilityCalculator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ServiceAvailabilityCalculatorTest extends TestCase
|
||||
{
|
||||
private ServiceAvailabilityCalculator $calculator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->calculator = new ServiceAvailabilityCalculator();
|
||||
}
|
||||
|
||||
public function testParkingServiceIsUnavailableWhenSoldOut(): void
|
||||
{
|
||||
$parkingService = $this->createParkingService(id: 1, available: 0);
|
||||
$bookingDto = $this->createBookingDtoWithServices([$parkingService]);
|
||||
|
||||
$this->assertTrue($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testParkingServiceIsAvailableWhenQuotaRemains(): void
|
||||
{
|
||||
$parkingService = $this->createParkingService(id: 1, available: 5);
|
||||
$bookingDto = $this->createBookingDtoWithServices([$parkingService]);
|
||||
|
||||
$this->assertFalse($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testParkingSelectedByOtherParticipantCountsTowardUsage(): void
|
||||
{
|
||||
$parkingService = $this->createParkingService(id: 1, available: 1);
|
||||
$bookingDto = $this->createBookingDtoWithServices([$parkingService]);
|
||||
|
||||
$otherParticipant = new ParticipantDto();
|
||||
$otherParticipant->parkingService = $parkingService;
|
||||
$bookingDto->participants[1] = $otherParticipant;
|
||||
|
||||
// Only one parking slot remains, and another participant already claimed it,
|
||||
// so it must show unavailable for the current participant (index 0)
|
||||
$this->assertTrue($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
private function createParkingService(int $id, int $available): Service
|
||||
{
|
||||
$service = new Service();
|
||||
$service->id = $id;
|
||||
$service->label = 'Parkplatz';
|
||||
$service->subType = Constants::TOKEN_PARKING;
|
||||
$service->available = $available;
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
private function createBookingDtoWithServices(array $services): BookingDto
|
||||
{
|
||||
$travel = new Travel();
|
||||
|
||||
$indexed = [];
|
||||
foreach ($services as $service) {
|
||||
$indexed[$service->id] = $service;
|
||||
}
|
||||
$travel->additionalServices = $indexed;
|
||||
|
||||
$bookingDto = new BookingDto($travel, 1);
|
||||
$bookingDto->participants[0] = new ParticipantDto();
|
||||
|
||||
return $bookingDto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user