Files
myep/tests/Service/TravelDataServiceTest.php
T

316 lines
8.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\ApiClient;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\HotelLoader;
use App\BusProNet\XmlLoader\PickupLoader;
use App\BusProNet\XmlLoader\TravelLoader;
use App\Service\TravelDataService;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class TravelDataServiceTest extends TestCase
{
private TravelDataService $service;
private TravelLoader $travelLoader;
private HotelLoader $hotelLoader;
private PickupLoader $pickupLoader;
private ApiClient $apiClient;
private CacheInterface $cache;
private LoggerInterface $logger;
protected function setUp(): void
{
$this->travelLoader = $this->createMock(TravelLoader::class);
$this->hotelLoader = $this->createMock(HotelLoader::class);
$this->pickupLoader = $this->createMock(PickupLoader::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->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)
->willReturn(null);
$this->pickupLoader
->expects($this->never())
->method('patchPickupsDetails');
$this->hotelLoader
->expects($this->never())
->method('patchHotelDetails');
$result = $this->service->getTravelDataFromXml($dateId, $hotelId);
$this->assertNull($result);
}
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, $travel) {
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);
}
}