142 lines
4.5 KiB
PHP
142 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\BusProNet\XmlLoader;
|
|
|
|
use App\BusProNet\Constants;
|
|
use App\BusProNet\Model\Availability;
|
|
use App\BusProNet\Model\Service;
|
|
use App\BusProNet\Model\ServiceAvailabilityResponse;
|
|
use App\BusProNet\Model\Travel;
|
|
use App\BusProNet\XmlLoader\HotelLoader;
|
|
use App\BusProNet\XmlLoader\TravelLoader;
|
|
use App\BusProNet\XmlParser\TravelParser;
|
|
use League\Flysystem\FilesystemOperator;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Contracts\Cache\CacheInterface;
|
|
|
|
/**
|
|
* Tests that patchAvailabilities() carries the live service status, not just the contingent.
|
|
*
|
|
* When a Leistung's contingent runs out BusPro moves it to 'Anfrage' without necessarily
|
|
* reporting frei="0", so the status is the only reliable signal. Dropping it meant every
|
|
* availability rule read the status from the nightly XML export instead.
|
|
*/
|
|
class TravelLoaderAvailabilityTest extends TestCase
|
|
{
|
|
private TravelLoader $loader;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->loader = new TravelLoader(
|
|
$this->createStub(HotelLoader::class),
|
|
$this->createStub(TravelParser::class),
|
|
'https://example.test',
|
|
$this->createStub(CacheInterface::class),
|
|
$this->createStub(FilesystemOperator::class),
|
|
);
|
|
}
|
|
|
|
public function testPatchesStatusAlongsideAvailability(): void
|
|
{
|
|
$service = $this->createService(60, Constants::STATUS_AVAILABLE, 10);
|
|
$travel = $this->createTravel([$service]);
|
|
|
|
$this->loader->patchAvailabilities(
|
|
$travel,
|
|
$this->createResponse([$this->createAvailability(60, Constants::STATUS_ON_REQUEST, 3)])
|
|
);
|
|
|
|
$this->assertSame(Constants::STATUS_ON_REQUEST, $service->status);
|
|
$this->assertSame(3, $service->available);
|
|
}
|
|
|
|
public function testKeepsExportStatusWhenTheResponseOmitsIt(): void
|
|
{
|
|
$service = $this->createService(60, Constants::STATUS_AVAILABLE, 10);
|
|
$travel = $this->createTravel([$service]);
|
|
|
|
$this->loader->patchAvailabilities(
|
|
$travel,
|
|
$this->createResponse([$this->createAvailability(60, null, 3)])
|
|
);
|
|
|
|
$this->assertSame(Constants::STATUS_AVAILABLE, $service->status);
|
|
$this->assertSame(3, $service->available);
|
|
}
|
|
|
|
public function testKeepsExportStatusWhenTheResponseCarriesABlankOne(): void
|
|
{
|
|
$service = $this->createService(60, Constants::STATUS_AVAILABLE, 10);
|
|
$travel = $this->createTravel([$service]);
|
|
|
|
$this->loader->patchAvailabilities(
|
|
$travel,
|
|
$this->createResponse([$this->createAvailability(60, ' ', 3)])
|
|
);
|
|
|
|
$this->assertSame(Constants::STATUS_AVAILABLE, $service->status);
|
|
}
|
|
|
|
public function testPatchesTransportationServicesToo(): void
|
|
{
|
|
$service = $this->createService(80, Constants::STATUS_AVAILABLE, 10);
|
|
$travel = new Travel();
|
|
$travel->transportationServices = [80 => $service];
|
|
|
|
$this->loader->patchAvailabilities(
|
|
$travel,
|
|
$this->createResponse([$this->createAvailability(80, Constants::STATUS_BLOCKED, 0)])
|
|
);
|
|
|
|
$this->assertSame(Constants::STATUS_BLOCKED, $service->status);
|
|
$this->assertSame(0, $service->available);
|
|
}
|
|
|
|
/** @param array<int, Service> $services */
|
|
private function createTravel(array $services): Travel
|
|
{
|
|
$travel = new Travel();
|
|
|
|
$indexed = [];
|
|
foreach ($services as $service) {
|
|
$indexed[$service->id] = $service;
|
|
}
|
|
$travel->additionalServices = $indexed;
|
|
|
|
return $travel;
|
|
}
|
|
|
|
private function createService(int $id, string $status, ?int $available): Service
|
|
{
|
|
$service = new Service();
|
|
$service->id = $id;
|
|
$service->status = $status;
|
|
$service->available = $available;
|
|
|
|
return $service;
|
|
}
|
|
|
|
private function createAvailability(int $serviceId, ?string $status, ?int $available): Availability
|
|
{
|
|
$availability = new Availability();
|
|
$availability->serviceId = $serviceId;
|
|
$availability->status = $status;
|
|
$availability->available = $available;
|
|
|
|
return $availability;
|
|
}
|
|
|
|
/** @param array<int, Availability> $availabilities */
|
|
private function createResponse(array $availabilities): ServiceAvailabilityResponse
|
|
{
|
|
$indexed = [];
|
|
foreach ($availabilities as $availability) {
|
|
$indexed[$availability->serviceId] = $availability;
|
|
}
|
|
|
|
return new ServiceAvailabilityResponse($indexed);
|
|
}
|
|
}
|