139 lines
4.3 KiB
PHP
139 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\MyEp\Import;
|
|
|
|
use App\MyEp\Api\AccessTokenProvider;
|
|
use App\MyEp\Api\ApiClient;
|
|
use App\MyEp\Api\ApiException;
|
|
use App\MyEp\Import\TravelExportClient;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
|
|
|
class TravelExportClientTest extends TestCase
|
|
{
|
|
public function testEveryPageIsRead(): void
|
|
{
|
|
$httpClient = $this->mockClient([
|
|
$this->page([$this->row(1, 100), $this->row(2, 200)], 1, 2),
|
|
$this->page([$this->row(3, 300)], 2, 2),
|
|
]);
|
|
|
|
$rows = $this->getClientInstance($httpClient)->fetchAll();
|
|
|
|
$this->assertCount(3, $rows);
|
|
$this->assertEquals([1, 2, 3], array_map(fn ($row) => $row->busProId, $rows));
|
|
$this->assertEquals(2, $httpClient->getRequestsCount());
|
|
}
|
|
|
|
public function testASinglePageIsNotFollowedByAnother(): void
|
|
{
|
|
$httpClient = $this->mockClient([$this->page([$this->row(1, 100)], 1, 1)]);
|
|
|
|
$this->getClientInstance($httpClient)->fetchAll();
|
|
|
|
$this->assertEquals(1, $httpClient->getRequestsCount());
|
|
}
|
|
|
|
public function testTheRequestedPageIsPassedOn(): void
|
|
{
|
|
$urls = [];
|
|
$httpClient = new MockHttpClient(function (string $method, string $url) use (&$urls) {
|
|
$urls[] = $url;
|
|
|
|
return $this->page([$this->row(count($urls), 100)], count($urls), 2);
|
|
}, 'https://myep.example/');
|
|
|
|
$this->getClientInstance($httpClient)->fetchAll();
|
|
|
|
$this->assertStringContainsString('page=1', $urls[0]);
|
|
$this->assertStringContainsString('page=2', $urls[1]);
|
|
$this->assertStringContainsString('perPage=500', $urls[0]);
|
|
}
|
|
|
|
public function testAnUnusableRowIsSkippedRatherThanFatal(): void
|
|
{
|
|
$httpClient = $this->mockClient([
|
|
$this->page([
|
|
['busProId' => 1],
|
|
$this->row(2, 200),
|
|
], 1, 1),
|
|
]);
|
|
|
|
$rows = $this->getClientInstance($httpClient)->fetchAll();
|
|
|
|
$this->assertCount(1, $rows);
|
|
$this->assertEquals(2, $rows[0]->busProId);
|
|
}
|
|
|
|
public function testAnEmptyExportYieldsNoRows(): void
|
|
{
|
|
$httpClient = $this->mockClient([$this->page([], 1, 1)]);
|
|
|
|
$this->assertEquals([], $this->getClientInstance($httpClient)->fetchAll());
|
|
}
|
|
|
|
public function testAPayloadWithoutADataListFails(): void
|
|
{
|
|
$httpClient = $this->mockClient([new MockResponse('{"meta":{"totalPages":1}}')]);
|
|
|
|
$this->expectException(ApiException::class);
|
|
|
|
$this->getClientInstance($httpClient)->fetchAll();
|
|
}
|
|
|
|
private function row(int $busProId, int $hotelBusProId): array
|
|
{
|
|
return [
|
|
'busProId' => $busProId,
|
|
'code' => 'CODE'.$busProId,
|
|
'product' => 'Produkt '.$busProId,
|
|
'dateFrom' => '2026-04-04',
|
|
'dateTo' => '2026-04-11',
|
|
'hotel' => [
|
|
'busProId' => $hotelBusProId,
|
|
'code' => 'AAA',
|
|
'name' => 'Haus',
|
|
'costUnit' => null,
|
|
'country' => 'ch',
|
|
],
|
|
'pickups' => [],
|
|
];
|
|
}
|
|
|
|
private function page(array $rows, int $page, int $totalPages): MockResponse
|
|
{
|
|
return new MockResponse(json_encode([
|
|
'data' => $rows,
|
|
'meta' => [
|
|
'page' => $page,
|
|
'perPage' => 500,
|
|
'total' => count($rows),
|
|
'totalPages' => $totalPages,
|
|
],
|
|
]));
|
|
}
|
|
|
|
private function mockClient(array $responses): MockHttpClient
|
|
{
|
|
return new MockHttpClient($responses, 'https://myep.example/');
|
|
}
|
|
|
|
private function getClientInstance(MockHttpClient $httpClient): TravelExportClient
|
|
{
|
|
$tokenClient = new MockHttpClient(
|
|
fn () => new MockResponse('{"access_token":"token","expires_in":600}'),
|
|
'https://myep.example/',
|
|
);
|
|
|
|
$tokens = new AccessTokenProvider($tokenClient, new ArrayAdapter(), new NullLogger(), [
|
|
'client_id' => 'team-app',
|
|
'client_secret' => 'secret',
|
|
]);
|
|
|
|
return new TravelExportClient(new ApiClient($httpClient, $tokens, new NullLogger()), new NullLogger());
|
|
}
|
|
}
|