feat: stop booking flow when BusPro is unreachable

This commit is contained in:
fromme committed 2026-11-11 19:51:52 +01:00
1 parent ac23baf625
commit 55378b2bbc
9 files changed
+249 -43

No files matched your search

+48
View File
@@ -5,8 +5,11 @@ declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\Client\TravelClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\TimeoutException;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\TravelLoader;
use App\Exception\BookingBackendUnavailableException;
use App\Exception\TravelNotFoundException;
use App\Service\TravelDataProvider;
use App\Service\TravelEnricher;
@@ -14,6 +17,7 @@ use App\Service\TravelIndex;
use App\Service\TravelSnapshotManager;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Contracts\Cache\CacheInterface;
class TravelDataProviderTest extends TestCase
@@ -494,4 +498,48 @@ class TravelDataProviderTest extends TestCase
$this->assertSame($travel, $result);
}
public function testGetAvailabilityDataReturnsNullWhenBusProIsUnreachable(): void
{
$this->travelClient = $this->createStub(TravelClient::class);
$this->travelClient->method('getAvailabilities')->willThrowException(new TimeoutException('Stream timeout'));
$this->assertNull($this->service()->getAvailabilityData(4711));
}
public function testGetAvailabilityDataThrowsWhenBusProIsUnreachableAndRequired(): void
{
$this->travelClient = $this->createStub(TravelClient::class);
$this->travelClient->method('getAvailabilities')->willThrowException(new ApiClientException('Unable to open socket'));
$this->expectException(BookingBackendUnavailableException::class);
$this->service()->getAvailabilityData(4711, requireReachable: true);
}
public function testGetAvailabilityDataDoesNotCacheAFailedConnection(): void
{
$this->travelClient = $this->createStub(TravelClient::class);
$this->travelClient->method('getAvailabilities')->willThrowException(new ApiClientException('Unable to open socket'));
$cache = new ArrayAdapter();
$this->cache = $cache;
$this->assertNull($this->service()->getAvailabilityData(4711, cached: true));
$this->assertFalse($cache->hasItem('availability_4711'), 'The next call must ask BusPro again');
}
public function testEnrichWithFreshAvailabilitiesThrowsWhenBusProIsUnreachableAndRequired(): void
{
$this->travelClient = $this->createStub(TravelClient::class);
$this->travelClient->method('getAvailabilities')->willThrowException(new ApiClientException('Unable to open socket'));
$this->cache = new ArrayAdapter();
$travel = new Travel();
$travel->id = 4711;
$this->expectException(BookingBackendUnavailableException::class);
$this->service()->enrichWithFreshAvailabilities($travel, requireReachable: true);
}
}