mivita/tests/Unit/Dhl/DhlProductResolverTest.php

150 lines
5.1 KiB
PHP

<?php
use App\Models\Setting;
use App\Services\DhlProductResolver;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
beforeEach(function () {
config([
'dhl.config_source' => 'env',
'dhl.international_countries' => ['AT', 'ES'],
]);
});
it('resolves domestic DHL products for Germany', function () {
$resolver = new DhlProductResolver;
expect($resolver->resolveForShipment('DE', 'V62KP'))->toMatchArray([
'country_code' => 'DE',
'dhl_country_code' => 'DEU',
'product_code' => 'V62KP',
]);
});
it('suggests international parcel for Austria and Spain', function (string $countryCode) {
$resolver = new DhlProductResolver;
expect($resolver->resolveProductCode($countryCode, null, 'V01PAK'))->toBe('V53PAK');
})->with([
'austria' => 'AT',
'spain' => 'ES',
]);
it('rejects unsupported destination countries', function () {
(new DhlProductResolver)->resolveProductCode('FR', null, 'V01PAK');
})->throws(InvalidArgumentException::class, 'DHL-Versand in das Zielland FR ist aktuell nicht freigegeben.');
it('uses configured international destination countries', function () {
$previousConfigSource = config('dhl.config_source');
$previousCountries = config('dhl.international_countries');
config([
'dhl.config_source' => 'env',
'dhl.international_countries' => ['AT', 'FR'],
]);
try {
$resolver = new DhlProductResolver;
expect($resolver->resolveProductCode('FR', null, 'V01PAK'))->toBe('V53PAK')
->and($resolver->getProductSuggestionsByCountry())->toMatchArray([
'DE' => 'V01PAK',
'FR' => 'V53PAK',
]);
} finally {
config([
'dhl.config_source' => $previousConfigSource,
'dhl.international_countries' => $previousCountries,
]);
}
});
it('uses saved DHL international countries even with env config priority', function () {
$createdSettingsTable = ensureDhlSettingsTableForResolverTest();
try {
config([
'dhl.config_source' => 'env',
'dhl.international_countries' => ['AT'],
]);
Setting::whereSlug('dhl_international_countries')->delete();
Setting::setContentBySlug('dhl_international_countries', ['FR'], 'object');
expect((new DhlProductResolver)->getSupportedInternationalCountries())->toBe(['FR']);
} finally {
Setting::whereSlug('dhl_international_countries')->delete();
if ($createdSettingsTable) {
Schema::drop('settings');
}
}
});
it('keeps an intentionally empty saved DHL international country list', function () {
$createdSettingsTable = ensureDhlSettingsTableForResolverTest();
try {
config([
'dhl.config_source' => 'env',
'dhl.international_countries' => ['AT'],
]);
Setting::whereSlug('dhl_international_countries')->delete();
Setting::setContentBySlug('dhl_international_countries', [], 'object');
expect((new DhlProductResolver)->getSupportedInternationalCountries())->toBe([]);
} finally {
Setting::whereSlug('dhl_international_countries')->delete();
if ($createdSettingsTable) {
Schema::drop('settings');
}
}
});
it('normalizes configurable international countries', function () {
expect(DhlProductResolver::normalizeCountryCodeList([' at ', 'DE', 'XX', 'FR', 'AT']))->toBe(['AT', 'FR']);
});
function ensureDhlSettingsTableForResolverTest(): bool
{
if (Schema::hasTable('settings')) {
return false;
}
Schema::create('settings', function (Blueprint $table): void {
$table->increments('id');
$table->string('slug')->index();
$table->string('type')->nullable();
$table->json('object')->nullable();
$table->text('full_text')->nullable();
$table->text('text')->nullable();
$table->integer('int')->nullable();
$table->timestamps();
});
return true;
}
it('describes DHL product scope for preflight checks', function () {
$resolver = new DhlProductResolver;
expect($resolver->getProductScope('V01PAK'))->toBe('national')
->and($resolver->getProductScopeLabel('V01PAK'))->toBe('DHL Paket National')
->and($resolver->getProductScope('V53PAK'))->toBe('international')
->and($resolver->getProductScopeLabel('V53PAK'))->toBe('DHL Paket International');
});
it('rejects domestic products for international shipments when explicitly selected', function () {
(new DhlProductResolver)->resolveProductCode('AT', 'V01PAK');
})->throws(InvalidArgumentException::class, 'Produkt V01PAK ist fuer DHL-Versand in das Zielland AT nicht erlaubt.');
it('does not fallback unknown countries to Germany', function () {
(new DhlProductResolver)->toDhlCountryCode('XX');
})->throws(InvalidArgumentException::class, 'DHL-Laendercode XX wird nicht unterstuetzt.');
it('requires a billing number for the resolved product', function () {
(new DhlProductResolver)->assertBillingNumber('V53PAK', null);
})->throws(InvalidArgumentException::class, 'Keine DHL-Abrechnungsnummer fuer Produkt V53PAK konfiguriert.');