Files
logisticsAPI/tests/Feature/LogisticsServiceTest.php
2026-02-20 08:40:00 +01:00

126 lines
3.8 KiB
PHP

<?php
use App\Services\LogisticsService;
use Illuminate\Support\Facades\Http;
beforeEach(function () {
config([
'logistics.base_url' => 'http://test-server.local',
'logistics.api_key' => 'test-api-key',
'logistics.folder' => 'testfolder',
]);
});
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();
});