561 lines
17 KiB
PHP
561 lines
17 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\Model\Insurance;
|
|
use App\BusProNet\Model\Travel;
|
|
use App\BusProNet\XmlLoader\HotelLoader;
|
|
use App\BusProNet\XmlLoader\InsuranceLoader;
|
|
use App\BusProNet\XmlLoader\PickupLoader;
|
|
use App\BusProNet\XmlLoader\TravelLoader;
|
|
use App\Exception\TravelNotFoundException;
|
|
use App\Service\TravelDataService;
|
|
use App\Service\TravelSnapshotService;
|
|
use Flagception\Manager\FeatureManagerInterface;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Contracts\Cache\CacheInterface;
|
|
|
|
class TravelDataServiceTest extends TestCase
|
|
{
|
|
private TravelDataService $service;
|
|
private TravelLoader $travelLoader;
|
|
private HotelLoader $hotelLoader;
|
|
private PickupLoader $pickupLoader;
|
|
private InsuranceLoader $insuranceLoader;
|
|
private ApiClient $apiClient;
|
|
private CacheInterface $cache;
|
|
private LoggerInterface $logger;
|
|
private TravelSnapshotService $travelSnapshotService;
|
|
private FeatureManagerInterface $featureManager;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->travelLoader = $this->createMock(TravelLoader::class);
|
|
$this->hotelLoader = $this->createMock(HotelLoader::class);
|
|
$this->pickupLoader = $this->createMock(PickupLoader::class);
|
|
$this->insuranceLoader = $this->createMock(InsuranceLoader::class);
|
|
$this->apiClient = $this->createMock(ApiClient::class);
|
|
$this->cache = $this->createMock(CacheInterface::class);
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
$this->travelSnapshotService = $this->createMock(TravelSnapshotService::class);
|
|
$this->featureManager = $this->createMock(FeatureManagerInterface::class);
|
|
$this->featureManager->method('isActive')->with('travel_snapshot')->willReturn(true);
|
|
|
|
$this->service = new TravelDataService(
|
|
$this->travelLoader,
|
|
$this->hotelLoader,
|
|
$this->pickupLoader,
|
|
$this->insuranceLoader,
|
|
$this->apiClient,
|
|
$this->cache,
|
|
$this->logger,
|
|
$this->travelSnapshotService,
|
|
$this->featureManager,
|
|
false, // preferRemote
|
|
true, // enableFallback
|
|
);
|
|
}
|
|
|
|
public function testGetTravelDataFromXmlSuccess(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$this->pickupLoader
|
|
->expects($this->once())
|
|
->method('patchPickupsDetails')
|
|
->with($travel);
|
|
|
|
$this->hotelLoader
|
|
->expects($this->once())
|
|
->method('patchHotelDetails')
|
|
->with($travel);
|
|
|
|
$result = $this->service->getTravelDataFromXml($dateId, $hotelId);
|
|
|
|
$this->assertSame($travel, $result);
|
|
}
|
|
|
|
public function testGetTravelDataFromXmlNotFound(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willThrowException(new TravelNotFoundException($dateId));
|
|
|
|
$this->pickupLoader
|
|
->expects($this->never())
|
|
->method('patchPickupsDetails');
|
|
|
|
$this->hotelLoader
|
|
->expects($this->never())
|
|
->method('patchHotelDetails');
|
|
|
|
$this->expectException(TravelNotFoundException::class);
|
|
$this->service->getTravelDataFromXml($dateId, $hotelId);
|
|
}
|
|
|
|
public function testGetTravelDataFromApiSuccess(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$productId = 555;
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('mapDateIdToProductId')
|
|
->with($dateId)
|
|
->willReturn($productId);
|
|
|
|
$this->apiClient
|
|
->expects($this->once())
|
|
->method('getTravelData')
|
|
->with($productId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$result = $this->service->getTravelDataFromApi($dateId, $hotelId);
|
|
|
|
$this->assertSame($travel, $result);
|
|
}
|
|
|
|
public function testGetTravelDataFromApiCannotMapDateId(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('mapDateIdToProductId')
|
|
->with($dateId)
|
|
->willReturn(null);
|
|
|
|
$this->apiClient
|
|
->expects($this->never())
|
|
->method('getTravelData');
|
|
|
|
$result = $this->service->getTravelDataFromApi($dateId, $hotelId);
|
|
|
|
$this->assertNull($result);
|
|
}
|
|
|
|
public function testExistsLocallyTrueFromSnapshot(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('exists')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(true);
|
|
|
|
$this->travelLoader
|
|
->expects($this->never())
|
|
->method('generateFilesMap');
|
|
|
|
$result = $this->service->existsLocally($dateId, $hotelId);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testExistsLocallyTrueFromXmlMap(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$mapping = [
|
|
$dateId => [
|
|
'id' => $dateId,
|
|
'hotels' => [
|
|
$hotelId => ['id' => $hotelId, 'name' => 'Test Hotel'],
|
|
],
|
|
],
|
|
];
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('exists')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(false);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('generateFilesMap')
|
|
->willReturn($mapping);
|
|
|
|
$result = $this->service->existsLocally($dateId, $hotelId);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testExistsLocallyFalseNoTravel(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$mapping = [];
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('exists')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(false);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('generateFilesMap')
|
|
->willReturn($mapping);
|
|
|
|
$result = $this->service->existsLocally($dateId, $hotelId);
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
public function testExistsLocallyFalseNoHotel(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$mapping = [
|
|
$dateId => [
|
|
'id' => $dateId,
|
|
'hotels' => [],
|
|
],
|
|
];
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('exists')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(false);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('generateFilesMap')
|
|
->willReturn($mapping);
|
|
|
|
$result = $this->service->existsLocally($dateId, $hotelId);
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
public function testGetAvailableSources(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$productId = 555;
|
|
$mapping = [
|
|
$dateId => [
|
|
'id' => $dateId,
|
|
'hotels' => [
|
|
$hotelId => ['id' => $hotelId, 'name' => 'Test Hotel'],
|
|
],
|
|
],
|
|
];
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('exists')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(false);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('generateFilesMap')
|
|
->willReturn($mapping);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('mapDateIdToProductId')
|
|
->with($dateId)
|
|
->willReturn($productId);
|
|
|
|
$result = $this->service->getAvailableSources($dateId, $hotelId);
|
|
|
|
$this->assertEquals([
|
|
'local' => true,
|
|
'remote' => true,
|
|
], $result);
|
|
}
|
|
|
|
public function testGetTravelDataFromLocalPrefersSnapshot(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('loadTravel')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$this->travelLoader
|
|
->expects($this->never())
|
|
->method('loadById');
|
|
|
|
$result = $this->service->getTravelDataFromLocal($dateId, $hotelId);
|
|
|
|
$this->assertSame($travel, $result);
|
|
}
|
|
|
|
public function testGetTravelDataFromLocalRehydratesInsurancePackagesFromSnapshot(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
|
|
// Simulate a deserialized snapshot state: containedInsuranceIds are present but
|
|
// containedInsurances is empty (circular-reference prevention strips it during serialization).
|
|
$individual = new Insurance();
|
|
$individual->id = '10';
|
|
$individual->package = false;
|
|
|
|
$package = new Insurance();
|
|
$package->id = '20';
|
|
$package->package = true;
|
|
$package->containedInsuranceIds = ['10'];
|
|
$package->containedInsurances = []; // as it arrives after deserialization
|
|
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
$travel->insurances = [$individual, $package];
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('loadTravel')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$result = $this->service->getTravelDataFromLocal($dateId, $hotelId);
|
|
|
|
$this->assertSame($travel, $result);
|
|
$this->assertCount(1, $package->containedInsurances);
|
|
$this->assertSame($individual, $package->containedInsurances[0]);
|
|
}
|
|
|
|
public function testGetTravelDataFromLocalFallsBackToXmlWhenSnapshotMissing(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('loadTravel')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(null);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$this->pickupLoader
|
|
->expects($this->once())
|
|
->method('patchPickupsDetails')
|
|
->with($travel);
|
|
|
|
$this->hotelLoader
|
|
->expects($this->once())
|
|
->method('patchHotelDetails')
|
|
->with($travel);
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('upsertFromTravel')
|
|
->with($travel);
|
|
|
|
$result = $this->service->getTravelDataFromLocal($dateId, $hotelId);
|
|
|
|
$this->assertSame($travel, $result);
|
|
}
|
|
|
|
public function testGetTravelDataFromLocalRehydratesInsurancePackagesFromXmlFallback(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
|
|
// insuranceLoader->loadAll() returns these during enrichTravelData(); the package's
|
|
// containedInsurances starts empty (as it would from a fresh XML load before hydration).
|
|
$individual = new Insurance();
|
|
$individual->id = '10';
|
|
$individual->package = false;
|
|
|
|
$package = new Insurance();
|
|
$package->id = '20';
|
|
$package->package = true;
|
|
$package->containedInsuranceIds = ['10'];
|
|
$package->containedInsurances = [];
|
|
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('loadTravel')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(null);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$this->pickupLoader->expects($this->once())->method('patchPickupsDetails')->with($travel);
|
|
$this->hotelLoader->expects($this->once())->method('patchHotelDetails')->with($travel);
|
|
$this->insuranceLoader->expects($this->once())->method('loadAll')->willReturn([$individual, $package]);
|
|
$this->travelSnapshotService->expects($this->once())->method('upsertFromTravel')->with($travel);
|
|
|
|
$result = $this->service->getTravelDataFromLocal($dateId, $hotelId);
|
|
|
|
$this->assertSame($travel, $result);
|
|
$this->assertCount(1, $package->containedInsurances);
|
|
$this->assertSame($individual, $package->containedInsurances[0]);
|
|
}
|
|
|
|
public function testGetTravelDataDelegatesToLoadUncached(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->cache
|
|
->expects($this->never())
|
|
->method('get');
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('loadTravel')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(null);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$this->pickupLoader
|
|
->expects($this->once())
|
|
->method('patchPickupsDetails')
|
|
->with($travel);
|
|
|
|
$this->hotelLoader
|
|
->expects($this->once())
|
|
->method('patchHotelDetails')
|
|
->with($travel);
|
|
|
|
$result = $this->service->getTravelData($dateId, $hotelId, false);
|
|
|
|
$this->assertSame($travel, $result);
|
|
}
|
|
|
|
public function testGetTravelDataFromLocalPropagatesTravelNotFoundFromXmlFallback(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('loadTravel')
|
|
->with($dateId, $hotelId)
|
|
->willReturn(null);
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willThrowException(new TravelNotFoundException($dateId));
|
|
|
|
$this->expectException(TravelNotFoundException::class);
|
|
$this->service->getTravelDataFromLocal($dateId, $hotelId);
|
|
}
|
|
|
|
public function testSnapshotLoadFailureFallsBackToXml(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('loadTravel')
|
|
->with($dateId, $hotelId)
|
|
->willThrowException(new \RuntimeException('DB connection failed'));
|
|
|
|
$this->logger
|
|
->expects($this->once())
|
|
->method('warning')
|
|
->with('Snapshot lookup failed, falling back to XML', $this->arrayHasKey('dateId'));
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$result = $this->service->getTravelDataFromLocal($dateId, $hotelId);
|
|
|
|
$this->assertSame($travel, $result);
|
|
}
|
|
|
|
public function testSnapshotPersistenceFailureDoesNotDiscardXmlTravel(): void
|
|
{
|
|
$dateId = 12345;
|
|
$hotelId = 67890;
|
|
$travel = new Travel();
|
|
$travel->id = $dateId;
|
|
$travel->hotelId = $hotelId;
|
|
|
|
$this->travelLoader
|
|
->expects($this->once())
|
|
->method('loadById')
|
|
->with($dateId, $hotelId)
|
|
->willReturn($travel);
|
|
|
|
$this->travelSnapshotService
|
|
->expects($this->once())
|
|
->method('upsertFromTravel')
|
|
->willThrowException(new \RuntimeException('DB write failed'));
|
|
|
|
$this->logger
|
|
->expects($this->once())
|
|
->method('warning')
|
|
->with('Failed to persist travel snapshot after XML load', $this->arrayHasKey('dateId'));
|
|
|
|
$result = $this->service->getTravelDataFromXml($dateId, $hotelId);
|
|
|
|
$this->assertNotNull($result);
|
|
$this->assertSame($travel, $result);
|
|
}
|
|
}
|