mivita/tests/Unit/Dhl/DhlProductResolverTest.php

83 lines
3.1 KiB
PHP

<?php
use App\Services\DhlProductResolver;
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('normalizes configurable international countries', function () {
expect(DhlProductResolver::normalizeCountryCodeList([' at ', 'DE', 'XX', 'FR', 'AT']))->toBe(['AT', 'FR']);
});
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.');