Refactor error handling and enhance API interactions across Filament pages

- Introduced `ApiErrorTranslator` to normalize and translate API error messages, providing clearer feedback in French.
- Updated all Filament pages (Articles, Documents, Divers, Journaux, Tiers, TablesExplorer) to utilize the new error translation mechanism, improving user experience during API interactions.
- Added validation for required fields before API calls, ensuring users receive immediate feedback when mandatory inputs are missing.
- Implemented tracking properties to distinguish between "never searched" and "searched without results," enhancing the user interface.
- Removed the obsolete `$results` property from the Articles page and added a new `$barcode` property to align with API requirements.
- Updated documentation to reflect changes in API behavior and error handling, including new metadata returned by the `art_list` endpoint.
- Added new tests to verify the functionality of the barcode handling and validation logic.
This commit is contained in:
2026-02-23 10:15:17 +01:00
parent 7df94b64fa
commit bb1bbc2904
29 changed files with 1075 additions and 157 deletions

View File

@@ -0,0 +1,171 @@
<?php
use App\Filament\Pages\Articles;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
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,
]);
$this->actingAs(User::factory()->create());
});
it('defaults to read mode', function () {
Livewire::test(Articles::class)
->assertSet('mode', 'read');
});
it('can switch between read and write modes', function () {
Livewire::test(Articles::class)
->set('mode', 'write')
->assertSet('mode', 'write')
->set('mode', 'read')
->assertSet('mode', 'read');
});
it('searches articles via art_list', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [['ARTID' => 'ART001', 'NAME1' => 'Test Article']],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'test')
->set('select', 'artid,name1')
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSet('data', [['ARTID' => 'ART001', 'NAME1' => 'Test Article']])
->assertSet('errorMessage', null);
});
it('sends barcode parameter when provided', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [['ARTID' => 'ART001']],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'test')
->set('barcode', '5411068700323')
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSet('data', [['ARTID' => 'ART001']]);
Http::assertSent(function (\Illuminate\Http\Client\Request $request) {
$body = json_decode($request->body(), true);
return $body['barcode'] === '5411068700323'
&& $body['search'] === 'test';
});
});
it('does not send barcode parameter when empty', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [],
'metadata' => ['rowcount' => 0, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'test')
->set('barcode', '')
->call('searchArticles');
Http::assertSent(function (\Illuminate\Http\Client\Request $request) {
$body = json_decode($request->body(), true);
return ! array_key_exists('barcode', $body);
});
});
it('sets hasSearched even when no results', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [],
'metadata' => ['rowcount' => 0, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'nonexistent')
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSet('data', []);
});
it('gets stock for an article', function () {
Http::fake([
'*/art_getstk' => Http::response([
'data' => ['stock' => 42],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('stockArticleId', 'ART001')
->call('getStock')
->assertSet('hasCheckedStock', true)
->assertSet('stockData', ['stock' => 42])
->assertSet('errorMessage', null);
});
it('shows validation error when stockArticleId is empty', function () {
Http::fake();
Livewire::test(Articles::class)
->call('getStock')
->assertSet('hasCheckedStock', false)
->assertSet('errorMessage', 'Le champ identifiant article (ARTID) est obligatoire pour verifier le stock.');
Http::assertNothingSent();
});
it('translates API error with explanation', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => ['Search terms are required. Please provide search criteria.'],
]),
]);
Livewire::test(Articles::class)
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSee('Explication');
});
it('displays error message on API failure', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => 'Invalid API key',
]),
]);
Livewire::test(Articles::class)
->call('searchArticles')
->assertSet('hasSearched', true);
expect(true)->toBeTrue();
});