Files
logisticsAPI/tests/Feature/TiersPageTest.php
Marvin c84e0c680a Update Tiers functionality and enhance API documentation
- Changed the default value of the `$select` property in Tiers.php from `custid,custname` to `custid,name` to reflect valid column names.
- Updated the tiers.blade.php view to align the placeholder for the select field with the new default value.
- Enhanced the documentation for the `third_list` and `third_GetArtHistory` endpoints, detailing valid search columns, metadata, and response structures.
- Added new tests in TiersPageTest.php to verify the default select value, API parameter handling, and metadata storage.
- Overall, improved the user experience and API interaction for managing tiers.
2026-02-23 11:58:14 +01:00

264 lines
8.4 KiB
PHP

<?php
use App\Filament\Pages\Tiers;
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(Tiers::class)
->assertSet('mode', 'read');
});
it('can switch between read and write modes', function () {
Livewire::test(Tiers::class)
->set('mode', 'write')
->assertSet('mode', 'write')
->set('mode', 'read')
->assertSet('mode', 'read');
});
it('has correct default select value', function () {
Livewire::test(Tiers::class)
->assertSet('select', 'custid,name');
});
it('shows validation error when search is empty', function () {
Http::fake();
Livewire::test(Tiers::class)
->call('searchTiers')
->assertSet('hasSearched', false)
->assertSet('errorMessage', 'Le champ de recherche (search) est obligatoire.');
Http::assertNothingSent();
});
it('searches tiers via third_list', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => [['custid' => '0100002174', 'name' => 'ESI SPRL']],
'metadata' => ['rowCount' => 1, 'source' => 'DBF', 'executionTimeMs' => 112, 'searchColumns' => 'name,groupid,vat', 'selectColumns' => 'custid,name', 'searchTerms' => 'ESI'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('search', 'ESI')
->set('select', 'custid,name')
->call('searchTiers')
->assertSet('hasSearched', true)
->assertSet('data', [['custid' => '0100002174', 'name' => 'ESI SPRL']])
->assertSet('errorMessage', null);
Http::assertSent(function ($request) {
return str_contains($request->url(), 'third_list')
&& $request->data()['search'] === 'ESI';
});
});
it('sends select and results parameters to third_list', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => [['custid' => '0100002174', 'name' => 'ESI SPRL']],
'metadata' => ['rowCount' => 1, 'source' => 'DBF', 'executionTimeMs' => 100, 'searchColumns' => 'name,groupid,vat', 'selectColumns' => 'custid,name', 'searchTerms' => 'test'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('search', 'test')
->set('select', 'custid,name,vat')
->set('results', 20)
->call('searchTiers');
Http::assertSent(function ($request) {
$data = $request->data();
return str_contains($request->url(), 'third_list')
&& $data['search'] === 'test'
&& $data['select'] === 'custid,name,vat'
&& $data['results'] === 20;
});
});
it('sets hasSearched even when no results', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => [],
'metadata' => ['rowCount' => 0, 'source' => 'DBF', 'executionTimeMs' => 100, 'searchColumns' => 'name,groupid,vat', 'selectColumns' => 'custid,name', 'searchTerms' => 'nonexistent'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('search', 'nonexistent')
->call('searchTiers')
->assertSet('hasSearched', true)
->assertSet('data', []);
});
it('stores metadata from third_list response', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => [['custid' => '0100002174', 'name' => 'ESI SPRL']],
'metadata' => ['rowCount' => 1, 'source' => 'DBF', 'executionTimeMs' => 112, 'searchColumns' => 'name,groupid,vat', 'selectColumns' => 'custid,name', 'searchTerms' => 'ESI'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('search', 'ESI')
->call('searchTiers')
->assertSet('metadata.rowCount', 1)
->assertSet('metadata.searchColumns', 'name,groupid,vat');
});
it('shows validation error when historyThirdId is empty', function () {
Http::fake();
Livewire::test(Tiers::class)
->call('getArtHistory')
->assertSet('hasHistory', false)
->assertSet('errorMessage', 'Le champ identifiant tiers (thirdid) est obligatoire.');
Http::assertNothingSent();
});
it('gets art history for a third party', function () {
Http::fake([
'*/third_GetArtHistory' => Http::response([
'data' => [
[
'artid' => 'ART001',
'artname' => 'Article Test',
'jnl' => '03VEN',
'unitprice' => 220,
'qty' => 1,
'vatid' => '21',
'vatpc' => 21,
's_credate' => '2025-06-03',
],
],
'metadata' => ['rowCount' => 1, 'source' => 'DBF'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('historyThirdId', 'CUST001')
->call('getArtHistory')
->assertSet('hasHistory', true)
->assertSet('errorMessage', null);
Http::assertSent(function ($request) {
return str_contains($request->url(), 'third_GetArtHistory')
&& $request->data()['thirdid'] === 'CUST001';
});
});
it('sends thirdid in lowercase key to third_GetArtHistory', function () {
Http::fake([
'*/third_GetArtHistory' => Http::response([
'data' => [],
'metadata' => ['rowCount' => 0, 'source' => 'DBF'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('historyThirdId', '0100002174')
->call('getArtHistory');
Http::assertSent(function ($request) {
return str_contains($request->url(), 'third_GetArtHistory')
&& array_key_exists('thirdid', $request->data())
&& ! array_key_exists('THIRDID', $request->data());
});
});
it('displays error message on third_list API failure', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => 'Search terms are required. Please provide a search query.',
]),
]);
Livewire::test(Tiers::class)
->set('search', 'test')
->call('searchTiers')
->assertSet('hasSearched', true)
->assertSet('data', []);
});
it('displays error message on third_GetArtHistory API failure', function () {
Http::fake([
'*/third_GetArtHistory' => Http::response([
'data' => null,
'metadata' => ['isSuccess' => false],
'error' => ['thirdid parameter is required.'],
], 400),
]);
Livewire::test(Tiers::class)
->set('historyThirdId', 'INVALID')
->call('getArtHistory')
->assertSet('hasHistory', true)
->assertSet('historyData', []);
});
it('handles third_GetArtHistory returning empty data for unknown thirdid', function () {
Http::fake([
'*/third_GetArtHistory' => Http::response([
'data' => [],
'metadata' => ['rowCount' => 0, 'source' => 'DBF'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('historyThirdId', 'XXXXXX')
->call('getArtHistory')
->assertSet('hasHistory', true)
->assertSet('historyData', [])
->assertSet('errorMessage', null);
});
it('filters out empty select parameter', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => [['custid' => '0100002174', 'name' => 'ESI SPRL']],
'metadata' => ['rowCount' => 1, 'source' => 'DBF', 'executionTimeMs' => 100, 'searchColumns' => 'name,groupid,vat', 'selectColumns' => 'custid,name', 'searchTerms' => 'test'],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('search', 'test')
->set('select', '')
->call('searchTiers');
Http::assertSent(function ($request) {
$data = $request->data();
return str_contains($request->url(), 'third_list')
&& ! array_key_exists('select', $data);
});
});