Files
logisticsAPI/tests/Feature/LogisticsServiceTest.php
Marvin 7df94b64fa Implement toggle for read/write mode across entity pages and enhance Documents and Divers functionality
- Added a toggle for switching between read and write modes on the Articles, Documents, Journaux, Tiers, and Divers pages, allowing users to access both data retrieval and data submission endpoints.
- Updated the Documents page to cover all 9 documented endpoints, including 7 for reading and 2 for writing, with appropriate error handling.
- Created a new Divers page to handle three endpoints: getserialnumber, codes_list, and custom_geninv_updatestock (the latter being non-functional).
- Introduced new methods in LogisticsService for handling PDF generation and stock updates, with corresponding updates in the API documentation.
- Improved form field components for better visual spacing in input fields.
2026-02-20 15:51:58 +01:00

224 lines
7.1 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('sends correct parameters for Document_GetPDF', function () {
Http::fake([
'*' => Http::response(['data' => null, 'metadata' => ['rowcount' => 0, 'issuccess' => false], 'error' => 'Layout not found']),
]);
$service = app(LogisticsService::class);
$service->documentGetPdf('VEN', '2026/0001', 'DEFAULT');
Http::assertSent(function ($request) {
$body = $request->data();
return str_contains($request->url(), 'Document_GetPDF')
&& $body['JNL'] === 'VEN'
&& $body['NUMBER'] === '2026/0001'
&& $body['LAYOUT'] === 'DEFAULT';
});
});
it('sends correct parameters for custom_geninv_updatestock', function () {
Http::fake([
'*' => Http::response(['data' => null, 'metadata' => ['rowcount' => 0, 'issuccess' => false], 'error' => 'Unknown STKID']),
]);
$service = app(LogisticsService::class);
$params = ['ARTID' => 'ART001', 'STKID' => 'STK1', 'QTY' => '10', 'TOCHECK' => '5', 'TOCHECKDETAIL' => 'test', 'MODE' => '1'];
$service->customGeninvUpdatestock($params);
Http::assertSent(function ($request) {
$body = $request->data();
return str_contains($request->url(), 'custom_geninv_updatestock')
&& $body['ARTID'] === 'ART001'
&& $body['STKID'] === 'STK1'
&& $body['QTY'] === '10';
});
});
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');
});