- 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.
318 lines
9.9 KiB
PHP
318 lines
9.9 KiB
PHP
<?php
|
|
|
|
use App\Filament\Pages\Documents;
|
|
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 () {
|
|
Http::fake(['*' => Http::response(['data' => [], 'metadata' => [], 'error' => null])]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->assertSet('mode', 'read');
|
|
});
|
|
|
|
it('can switch between read and write modes', function () {
|
|
Http::fake(['*' => Http::response(['data' => [], 'metadata' => [], 'error' => null])]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('mode', 'write')
|
|
->assertSet('mode', 'write')
|
|
->set('mode', 'read')
|
|
->assertSet('mode', 'read');
|
|
});
|
|
|
|
it('searches documents via document_list', function () {
|
|
Http::fake([
|
|
'*/document_list' => Http::response([
|
|
'data' => [['jnl' => 'VEN', 'number' => '1']],
|
|
'metadata' => ['rowcount' => 1, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('select', 'jnl,number')
|
|
->set('thirdId', 'CUST001')
|
|
->call('searchDocuments')
|
|
->assertSet('hasSearchedDocs', true)
|
|
->assertSet('data', [['jnl' => 'VEN', 'number' => '1']]);
|
|
});
|
|
|
|
it('gets document detail', function () {
|
|
Http::fake([
|
|
'*/document_detail' => Http::response([
|
|
'data' => ['jnl' => 'VEN', 'number' => '1', 'thirdid' => 'CUST001'],
|
|
'metadata' => ['rowcount' => 1, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('detailJnl', 'VEN')
|
|
->set('detailNumber', '1')
|
|
->call('getDocumentDetail')
|
|
->assertSet('detailData', ['jnl' => 'VEN', 'number' => '1', 'thirdid' => 'CUST001']);
|
|
});
|
|
|
|
it('gets status list for a journal', function () {
|
|
Http::fake([
|
|
'*/Document_GetStatusList' => Http::response([
|
|
'data' => [['status' => 'DRAFT'], ['status' => 'VALID']],
|
|
'metadata' => ['rowcount' => 2, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('statusJnl', 'VEN')
|
|
->call('getStatusList')
|
|
->assertSet('statusData', [['status' => 'DRAFT'], ['status' => 'VALID']]);
|
|
});
|
|
|
|
it('gets unit price and vat', function () {
|
|
Http::fake([
|
|
'*/Document_GetUnitPriceAndVat' => Http::response([
|
|
'data' => ['price' => '10.00', 'vat' => '21'],
|
|
'metadata' => ['rowcount' => 1, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('priceArtId', 'ART001')
|
|
->set('priceQty', '2')
|
|
->set('priceJnl', 'VEN')
|
|
->set('priceThirdId', 'CUST001')
|
|
->set('priceDate', '2026-02-20')
|
|
->call('getUnitPriceAndVat')
|
|
->assertSet('priceData', ['price' => '10.00', 'vat' => '21']);
|
|
});
|
|
|
|
it('gets due date', function () {
|
|
Http::fake([
|
|
'*/Document_GetDueDate' => Http::response([
|
|
'data' => ['duedate' => '2026-03-22'],
|
|
'metadata' => ['rowcount' => 1, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('payDelay', '30J')
|
|
->set('dueDateInput', '2026-02-20')
|
|
->call('getDueDate')
|
|
->assertSet('dueDateData', ['duedate' => '2026-03-22']);
|
|
});
|
|
|
|
it('gets attach list thumbnail', function () {
|
|
Http::fake([
|
|
'*/Document_GetAttachListThumbnail' => Http::response([
|
|
'data' => [['filename' => 'photo.jpg']],
|
|
'metadata' => ['rowcount' => 1, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('attachJnl', 'VEN')
|
|
->set('attachNumber', '2026/0001')
|
|
->call('getAttachListThumbnail')
|
|
->assertSet('attachData', [['filename' => 'photo.jpg']]);
|
|
});
|
|
|
|
it('calls Document_GetPDF endpoint', function () {
|
|
Http::fake([
|
|
'*/Document_GetPDF' => Http::response([
|
|
'data' => null,
|
|
'metadata' => ['rowcount' => 0, 'issuccess' => false],
|
|
'error' => 'Layout not found',
|
|
]),
|
|
]);
|
|
|
|
$component = Livewire::test(Documents::class)
|
|
->set('pdfJnl', 'VEN')
|
|
->set('pdfNumber', '2026/0001')
|
|
->set('pdfLayout', 'DEFAULT')
|
|
->call('getPdf');
|
|
|
|
expect($component->get('errorMessage'))->toContain('Layout not found');
|
|
});
|
|
|
|
it('adds a document via document_add', function () {
|
|
Http::fake([
|
|
'*/document_add' => Http::response([
|
|
'data' => ['number' => '2026/0002'],
|
|
'metadata' => ['rowcount' => 1, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('mode', 'write')
|
|
->set('addThirdId', 'CUST001')
|
|
->set('addDate', '2026-02-20')
|
|
->set('addArtIds', 'ART001,ART002')
|
|
->set('addQty', '2,5')
|
|
->set('addSalePrice', '10.00,25.50')
|
|
->set('addJnl', 'VEN')
|
|
->call('addDocument')
|
|
->assertSet('addResult', ['number' => '2026/0002']);
|
|
|
|
Http::assertSent(function ($request) {
|
|
$body = $request->data();
|
|
|
|
return str_contains($request->url(), 'document_add')
|
|
&& $body['Artid'] === ['ART001', 'ART002']
|
|
&& $body['Qty'] === ['2', '5']
|
|
&& $body['Saleprice'] === ['10.00', '25.50'];
|
|
});
|
|
});
|
|
|
|
it('modifies a document via document_mod', function () {
|
|
Http::fake([
|
|
'*/document_mod' => Http::response([
|
|
'data' => ['updated' => true],
|
|
'metadata' => ['rowcount' => 1, 'issuccess' => true],
|
|
'error' => null,
|
|
]),
|
|
]);
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('mode', 'write')
|
|
->set('modNumber', '2026/0001')
|
|
->set('modJnl', 'VEN')
|
|
->set('modArtIds', 'ART001,ART003')
|
|
->set('modQty', '3,1')
|
|
->set('modSalePrice', '10.00,50.00')
|
|
->call('modDocument')
|
|
->assertSet('modResult', ['updated' => true]);
|
|
|
|
Http::assertSent(function ($request) {
|
|
$body = $request->data();
|
|
|
|
return str_contains($request->url(), 'document_mod')
|
|
&& $body['number'] === '2026/0001'
|
|
&& $body['JNL'] === 'VEN';
|
|
});
|
|
});
|
|
|
|
it('shows validation error when statusJnl is empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->call('getStatusList')
|
|
->assertSet('hasStatus', false)
|
|
->assertSet('errorMessage', 'Le champ code journal (jnl) est obligatoire.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('shows validation error when document_detail fields are empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->call('getDocumentDetail')
|
|
->assertSet('hasDetail', false)
|
|
->assertSet('errorMessage', 'Les champs code journal (jnl) et numero de document (number) sont obligatoires.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('shows validation error when getUnitPriceAndVat fields are empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->call('getUnitPriceAndVat')
|
|
->assertSet('hasPrice', false)
|
|
->assertSet('errorMessage', 'Tous les champs sont obligatoires : ARTID, QTY (format string), JNL, THIRDID et DATE.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('shows validation error when getDueDate fields are empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->call('getDueDate')
|
|
->assertSet('hasDueDate', false)
|
|
->assertSet('errorMessage', 'Les champs delai de paiement (paydelay) et date de depart (date) sont obligatoires.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('shows validation error when getAttachListThumbnail fields are empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->call('getAttachListThumbnail')
|
|
->assertSet('hasAttach', false)
|
|
->assertSet('errorMessage', 'Les champs code journal (JNL) et numero de document (NUMBER) sont obligatoires.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('shows validation error when getPdf fields are empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->call('getPdf')
|
|
->assertSet('hasPdf', false)
|
|
->assertSet('errorMessage', 'Les champs code journal (JNL), numero de document (NUMBER) et mise en page (LAYOUT) sont obligatoires.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('shows validation error when addDocument required fields are empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('mode', 'write')
|
|
->call('addDocument')
|
|
->assertSet('hasAdded', false)
|
|
->assertSet('errorMessage', 'Les champs ThirdId, Date, Artid, Qty, Saleprice et JNL sont obligatoires.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('shows validation error when modDocument required fields are empty', function () {
|
|
Http::fake();
|
|
|
|
Livewire::test(Documents::class)
|
|
->set('mode', 'write')
|
|
->call('modDocument')
|
|
->assertSet('hasModified', false)
|
|
->assertSet('errorMessage', 'Les champs numero de document (number) et code journal (JNL) sont obligatoires.');
|
|
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('displays error message on API failure', function () {
|
|
Http::fake([
|
|
'*/document_list' => Http::response([
|
|
'data' => null,
|
|
'metadata' => ['rowcount' => 0, 'issuccess' => false],
|
|
'error' => 'Invalid API key',
|
|
]),
|
|
]);
|
|
|
|
$component = Livewire::test(Documents::class)
|
|
->call('searchDocuments');
|
|
|
|
expect($component->get('errorMessage'))->toContain('Invalid API key');
|
|
});
|