ldc-cocotte/tests/Integration/WithApp/App/Dto/OrderLineDtoTest.php

139 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Dto;
use App\Dto\OrderLineDto;
use App\Models\Order;
use App\Models\OrderLine;
use App\Models\Shop;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Domain\Enums\OrderStatus;
use Domain\Enums\ProductBrand;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Models\ValueObjects\IncoId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderEmployee;
use Domain\Models\ValueObjects\OrderLineProduct;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Models\ValueObjects\ProductWeight;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Support\Facades\Date;
use Money\Money;
use Shared\Helpers\UuidHelper;
beforeEach(function (): void {
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
Date::setTestNow($now);
CarbonImmutable::setTestNow($now);
Carbon::setTestNow($now);
});
afterEach(function (): void {
Date::setTestNow();
CarbonImmutable::setTestNow();
Carbon::setTestNow();
});
describe('OrderLineDto Integration', function (): void {
test('it maps from OrderLine Eloquent model with correct unit price TTC and flattened product properties', function (): void {
// GIVEN
$shop = Shop::factory()->create();
$order = Order::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(UuidHelper::v7()),
'status' => OrderStatus::VALIDATED->value,
'total' => Money::EUR(4154),
'employee' => new OrderEmployee(
new Matricule('8888001'),
'Jean',
'Dupont',
new ShopperEstablishmentCode('FAC01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101'),
),
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
'pickup_day' => Date::now()->toDateString(),
]);
$orderLineProduct = new OrderLineProduct(
incoId: new IncoId(999),
originalId: new ProductId(UuidHelper::v7()),
name: 'Terrine de Foie Gras de Canard mi-cuit 500g',
weight: new ProductWeight(500, false),
brand: ProductBrand::loue,
orderUnit: OrderUnit::PCE,
price: Money::EUR(1969),
discountedPrice: Money::EUR(1969),
priceKgTtc: Money::EUR(3938),
taxRate: TaxRate::TVA_5_5,
picture: null,
);
$line = new OrderLine();
$line->order()->associate($order);
$line->product = $orderLineProduct;
$line->quantity = 2;
$line->unit_price = Money::EUR(1969);
$line->unit_tax = Money::EUR(108);
$line->unit_discount = Money::EUR(0);
$line->tax_rate = TaxRate::TVA_5_5;
$line->total_ttc = Money::EUR(4154);
$line->total_ht = Money::EUR(3938);
$line->total_tax = Money::EUR(216);
$line->total_discount = Money::EUR(0);
$line->save();
// WHEN
$dto = OrderLineDto::fromModel($line);
// THEN
expect($line->unitPriceTtc)->toEqual(Money::EUR(2077))
->and($dto->id)->toBe((int) $line->id)
->and($dto->order_id)->toBe((string) $line->order_id)
->and($dto->product_name)->toBe('Terrine de Foie Gras de Canard mi-cuit 500g')
->and($dto->product_inco_id)->toBe('999')
->and($dto->product_brand_label)->toBe(ProductBrand::loue->label())
->and($dto->is_variable_weight)->toBeFalse()
->and($dto->price_kg_ttc)->toEqual(Money::EUR(3938))
->and($dto->quantity)->toBe(2)
->and($dto->unit_price)->toEqual(Money::EUR(1969))
->and($dto->unit_tax)->toEqual(Money::EUR(108))
->and($dto->unit_discount)->toEqual(Money::EUR(0))
->and($dto->tax_rate)->toBe(TaxRate::TVA_5_5)
->and($dto->unit_price_ttc)->toEqual(Money::EUR(2077))
->and($dto->unitPriceTtc)->toEqual(Money::EUR(2077))
->and($dto->total_ht)->toEqual(Money::EUR(3938))
->and($dto->total_tax)->toEqual(Money::EUR(216))
->and($dto->total_ttc)->toEqual(Money::EUR(4154))
->and($dto->toArray())->toBe([
'id' => (int) $line->id,
'order_id' => (string) $line->order_id,
'product_name' => 'Terrine de Foie Gras de Canard mi-cuit 500g',
'product_inco_id' => '999',
'product_brand_label' => ProductBrand::loue->label(),
'is_variable_weight' => false,
'price_kg_ttc' => '3938',
'quantity' => 2,
'unit_price' => '1969',
'unit_tax' => '108',
'unit_discount' => '0',
'tax_rate' => TaxRate::TVA_5_5->value,
'unit_price_ttc' => '2077',
'unit_price_discounted_ht' => '1969',
'total_ttc' => '4154',
'total_ht' => '3938',
'total_tax' => '216',
'total_discount' => '0',
'total_gross_ht' => '3938',
]);
});
});