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->service = new TravelDataService( $this->travelLoader, $this->hotelLoader, $this->pickupLoader, $this->insuranceLoader, $this->apiClient, $this->cache, $this->logger, 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 testExistsInXmlTrue(): void { $dateId = 12345; $hotelId = 67890; $mapping = [ $dateId => [ 'id' => $dateId, 'hotels' => [ $hotelId => ['id' => $hotelId, 'name' => 'Test Hotel'], ], ], ]; $this->travelLoader ->expects($this->once()) ->method('generateFilesMap') ->willReturn($mapping); $result = $this->service->existsInXml($dateId, $hotelId); $this->assertTrue($result); } public function testExistsInXmlFalseNoTravel(): void { $dateId = 12345; $hotelId = 67890; $mapping = []; $this->travelLoader ->expects($this->once()) ->method('generateFilesMap') ->willReturn($mapping); $result = $this->service->existsInXml($dateId, $hotelId); $this->assertFalse($result); } public function testExistsInXmlFalseNoHotel(): void { $dateId = 12345; $hotelId = 67890; $mapping = [ $dateId => [ 'id' => $dateId, 'hotels' => [], ], ]; $this->travelLoader ->expects($this->once()) ->method('generateFilesMap') ->willReturn($mapping); $result = $this->service->existsInXml($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->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 testGetTravelDataWithCaching(): void { $dateId = 12345; $hotelId = 67890; $travel = new Travel(); $travel->id = $dateId; $travel->hotelId = $hotelId; $cacheItem = $this->createMock(ItemInterface::class); $cacheItem ->expects($this->once()) ->method('expiresAfter') ->with(300); $this->cache ->expects($this->once()) ->method('get') ->with('travel_unified_12345_67890_local') ->willReturnCallback(function (string $key, callable $callback) use ($cacheItem) { return $callback($cacheItem); }); $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, true); $this->assertSame($travel, $result); } public function testGetTravelDataWithoutCaching(): void { $dateId = 12345; $hotelId = 67890; $travel = new Travel(); $travel->id = $dateId; $travel->hotelId = $hotelId; $this->cache ->expects($this->never()) ->method('get'); $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, false); $this->assertSame($travel, $result); } }