Files
logisticsAPI/tests/Feature/LogisticsServiceTest.php
Marvin 4aef33f270 Implement robust error handling and configuration for Logistics API interactions
- Introduced `LogisticsApiException` to handle connection and request errors with user-friendly messages in French.
- Updated `LogisticsService` to include configurable timeout, connection timeout, retry attempts, and sleep duration for retries.
- Enhanced error handling in Filament pages to catch `LogisticsApiException` and provide clear feedback to users.
- Updated `.env` and `config/logistics.php` to support new configuration options.
- Added logging for failed API requests in `api_request_logs`.
- Created comprehensive API documentation for Logistics endpoints.
2026-02-20 10:06:04 +01:00

187 lines
5.8 KiB
PHP

<?php
use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
beforeEach(function () {
config([
'logistics.base_url' => 'http://test-server.local',
'logistics.api_key' => 'test-api-key',
'logistics.folder' => 'testfolder',
'logistics.timeout' => 30,
'logistics.connect_timeout' => 10,
'logistics.retry.times' => 1,
'logistics.retry.sleep_ms' => 0,
]);
});
it('sends the X-API-KEY header on every request', function () {
Http::fake([
'*' => Http::response(['data' => [], 'metadata' => ['rowcount' => 0, 'issuccess' => true], 'error' => null]),
]);
$service = app(LogisticsService::class);
$service->tablesList();
Http::assertSent(function ($request) {
return $request->hasHeader('X-API-KEY', 'test-api-key');
});
});
it('builds the correct URL for tables_list', function () {
Http::fake([
'*' => Http::response(['data' => ['art', 'cust'], 'metadata' => ['rowcount' => 2, 'issuccess' => true], 'error' => null]),
]);
$service = app(LogisticsService::class);
$result = $service->tablesList();
Http::assertSent(function ($request) {
return str_contains($request->url(), 'http://test-server.local/testfolder/tables_list');
});
expect($result['data'])->toBe(['art', 'cust']);
});
it('builds the correct URL for column_list with table name', function () {
Http::fake([
'*' => Http::response(['data' => [['name' => 'artid']], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
]);
$service = app(LogisticsService::class);
$service->columnList('art');
Http::assertSent(function ($request) {
return str_contains($request->url(), 'testfolder/column_list/art');
});
});
it('sends correct parameters for art_list', function () {
Http::fake([
'*' => Http::response(['data' => [], 'metadata' => ['rowcount' => 0, 'issuccess' => true], 'error' => null]),
]);
$service = app(LogisticsService::class);
$service->artList(['select' => 'artid,artname', 'search' => 'test']);
Http::assertSent(function ($request) {
$body = $request->data();
return $body['select'] === 'artid,artname' && $body['search'] === 'test';
});
});
it('sends ARTID for art_getstk', function () {
Http::fake([
'*' => Http::response(['data' => ['stock' => 42], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
]);
$service = app(LogisticsService::class);
$result = $service->artGetStock('ART001');
Http::assertSent(function ($request) {
return $request->data()['ARTID'] === 'ART001';
});
expect($result['data']['stock'])->toBe(42);
});
it('sends correct parameters for document_detail', function () {
Http::fake([
'*' => Http::response(['data' => ['jnl' => 'VEN', 'number' => '1'], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
]);
$service = app(LogisticsService::class);
$service->documentDetail('VEN', '1');
Http::assertSent(function ($request) {
$body = $request->data();
return $body['jnl'] === 'VEN' && $body['number'] === '1';
});
});
it('sends correct parameters for third_list', function () {
Http::fake([
'*' => Http::response(['data' => [], 'metadata' => ['rowcount' => 0, 'issuccess' => true], 'error' => null]),
]);
$service = app(LogisticsService::class);
$service->thirdList(['select' => 'custid', 'search' => 'test']);
Http::assertSent(function ($request) {
$body = $request->data();
return $body['select'] === 'custid' && $body['search'] === 'test';
});
});
it('returns fallback data when API returns empty response', function () {
Http::fake([
'*' => Http::response(null),
]);
$service = app(LogisticsService::class);
$result = $service->tablesList();
expect($result)->toHaveKey('error')
->and($result['metadata']['issuccess'])->toBeFalse();
});
it('throws LogisticsApiException on connection timeout', function () {
Http::fake(fn () => throw new ConnectionException('Connection timed out'));
$service = app(LogisticsService::class);
$service->tablesList();
})->throws(LogisticsApiException::class, "L'API Logistics est injoignable");
it('throws LogisticsApiException on general request failure', function () {
Http::fake(fn () => throw new \RuntimeException('Server error'));
$service = app(LogisticsService::class);
$service->tablesList();
})->throws(LogisticsApiException::class, "La requete vers l'API Logistics a echoue");
it('logs failed requests to api_request_logs as valid JSON', function () {
Http::fake(fn () => throw new ConnectionException('Connection timed out'));
$service = app(LogisticsService::class);
try {
$service->tablesList();
} catch (LogisticsApiException) {
// expected
}
$this->assertDatabaseHas('api_request_logs', [
'endpoint' => 'tables_list',
'response_status' => 0,
]);
$log = DB::table('api_request_logs')->where('endpoint', 'tables_list')->first();
$decoded = json_decode($log->response_data, true);
expect($decoded)->toBeArray()
->and($decoded)->toHaveKey('error');
});
it('includes endpoint info in LogisticsApiException', function () {
Http::fake(fn () => throw new ConnectionException('Connection timed out'));
$service = app(LogisticsService::class);
try {
$service->artList(['search' => 'test']);
} catch (LogisticsApiException $e) {
expect($e->endpoint)->toBe('art_list')
->and($e->params)->toBe(['search' => 'test']);
return;
}
$this->fail('Expected LogisticsApiException was not thrown');
});