mivita/tests/Unit/Dhl/DhlShipmentWeightCalculatorTest.php

60 lines
1.9 KiB
PHP

<?php
use App\Models\Product;
use App\Models\ShoppingOrder;
use App\Models\ShoppingOrderItem;
use App\Services\DhlShipmentWeightCalculator;
use Illuminate\Database\Eloquent\Collection;
function dhlWeightOrder(int $baseWeightGrams, array $items = []): ShoppingOrder
{
$order = new ShoppingOrder;
$order->weight = $baseWeightGrams;
$order->setRelation('shopping_order_items', new Collection($items));
return $order;
}
function dhlWeightItem(int $comp, int $qty, int $productWeightGrams): ShoppingOrderItem
{
$product = new Product;
$product->weight = $productWeightGrams;
$item = new ShoppingOrderItem;
$item->comp = $comp;
$item->qty = $qty;
$item->setRelation('product', $product);
return $item;
}
it('uses the shopping order weight as DHL base weight', function () {
$weight = (new DhlShipmentWeightCalculator)->calculate(dhlWeightOrder(1250));
expect($weight)->toBe(1.25);
});
it('adds compensation product weights to the DHL weight', function () {
$weight = (new DhlShipmentWeightCalculator)->calculate(dhlWeightOrder(500, [
dhlWeightItem(comp: 1, qty: 2, productWeightGrams: 250),
dhlWeightItem(comp: 0, qty: 10, productWeightGrams: 1000),
]));
expect($weight)->toBe(1.0);
});
it('falls back to a safe default DHL weight when no weight exists', function () {
$weight = (new DhlShipmentWeightCalculator)->calculate(dhlWeightOrder(0));
expect($weight)->toBe(1.0);
});
it('validates product specific DHL weight limits', function () {
(new DhlShipmentWeightCalculator)->assertWithinProductLimit(1.001, 'V62KP');
})->throws(InvalidArgumentException::class, 'Gewicht 1.001 kg ueberschreitet das DHL-Maximalgewicht fuer V62KP (1.0 kg).');
it('allows regular parcel products up to the DHL parcel limit', function () {
(new DhlShipmentWeightCalculator)->assertWithinProductLimit(31.5, 'V01PAK');
expect(true)->toBeTrue();
});