2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Integration\WithApp\Infra\Repositories\SessionBasketRepositoryTest;
|
|
|
|
|
|
|
|
|
|
use Domain\Enums\ProductBrand;
|
|
|
|
|
use Domain\Enums\TaxRate;
|
|
|
|
|
use Domain\Enums\Units\OrderUnit;
|
|
|
|
|
use Domain\Models\Entities\Basket;
|
|
|
|
|
use Domain\Models\ValueObjects\BasketProduct;
|
2026-10-03 11:30:52 +02:00
|
|
|
use Domain\Models\ValueObjects\DiscountRate;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Domain\Models\ValueObjects\Entities\BasketId;
|
|
|
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
|
|
|
use Domain\Models\ValueObjects\IncoId;
|
|
|
|
|
use Domain\Ports\Repositories\BasketRepository;
|
|
|
|
|
use Money\Currency;
|
|
|
|
|
use Money\Money;
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
|
|
|
|
|
|
describe('SessionBasketRepository', function () {
|
|
|
|
|
|
|
|
|
|
function getRepository(): BasketRepository
|
|
|
|
|
{
|
|
|
|
|
return app(BasketRepository::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it('returns null when session is empty', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$basketId = new BasketId(Uuid::uuid7());
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$basket = getRepository()->find($basketId);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($basket)->toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can save a basket into the session and retrieve it identically', function (BasketProduct $product) {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$basketId = new BasketId(Uuid::uuid7());
|
|
|
|
|
$basket = new Basket($basketId, [$product->id->value->toString() => $product]);
|
|
|
|
|
$repository = getRepository();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$repository->persist($basket);
|
|
|
|
|
$retrievedBasket = $repository->find($basketId);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($retrievedBasket)->toBeInstanceOf(Basket::class)
|
|
|
|
|
->and($retrievedBasket->id->value->toString())->toBe($basketId->value->toString())
|
|
|
|
|
->and($retrievedBasket->items())->toHaveCount(1);
|
|
|
|
|
|
|
|
|
|
$retrievedProduct = $retrievedBasket->items()[$product->id->value->toString()];
|
|
|
|
|
|
|
|
|
|
expect($retrievedProduct)->toBeInstanceOf(BasketProduct::class)
|
|
|
|
|
->and($retrievedProduct->name)->toBe($product->name)
|
|
|
|
|
->and($retrievedProduct->qty)->toBe($product->qty)
|
|
|
|
|
->and($retrievedProduct->unitPriceHt->getAmount())->toBe($product->unitPriceHt->getAmount())
|
|
|
|
|
->and($retrievedProduct->unitDiscount->getAmount())->toBe($product->unitDiscount->getAmount())
|
|
|
|
|
->and($retrievedProduct->taxRate->value)->toBe($product->taxRate->value)
|
|
|
|
|
->and($retrievedProduct->weightGrams)->toBe($product->weightGrams)
|
|
|
|
|
->and($retrievedProduct->brand?->value)->toBe($product->brand?->value)
|
|
|
|
|
->and($retrievedProduct->incoId?->value)->toBe($product->incoId?->value)
|
|
|
|
|
->and($retrievedProduct->orderUnit->value)->toBe($product->orderUnit->value)
|
|
|
|
|
->and($retrievedProduct->slug)->toBe($product->slug)
|
|
|
|
|
->and($retrievedProduct->picture)->toBe($product->picture)
|
|
|
|
|
->and($retrievedProduct->description)->toBe($product->description)
|
|
|
|
|
->and($retrievedProduct->weightVariable)->toBe($product->weightVariable)
|
2026-10-03 11:30:52 +02:00
|
|
|
->and($retrievedProduct->discountRate->basisPoints())->toBe($product->discountRate->basisPoints())
|
2026-06-14 21:32:07 +02:00
|
|
|
->and($retrievedProduct->priceKgTtc->getAmount())->toBe($product->priceKgTtc->getAmount())
|
|
|
|
|
->and($retrievedProduct->options)->toBe($product->options);
|
|
|
|
|
})->with([
|
|
|
|
|
'Full product' => fn () => new BasketProduct(
|
|
|
|
|
id: new ProductId(Uuid::uuid7()),
|
|
|
|
|
name: 'Poulet Rôti',
|
|
|
|
|
qty: 2,
|
|
|
|
|
unitPriceHt: new Money(1200, new Currency('EUR')),
|
|
|
|
|
unitDiscount: new Money(100, new Currency('EUR')),
|
|
|
|
|
taxRate: TaxRate::from(2), // 5.5%
|
|
|
|
|
weightGrams: 1200,
|
|
|
|
|
brand: ProductBrand::from('loue'),
|
|
|
|
|
incoId: new IncoId(456789),
|
|
|
|
|
orderUnit: OrderUnit::from('KGM'),
|
|
|
|
|
slug: 'poulet-roti',
|
|
|
|
|
picture: 'poulet.jpg',
|
|
|
|
|
description: 'Un bon poulet',
|
|
|
|
|
weightVariable: true,
|
2026-10-03 11:30:52 +02:00
|
|
|
discountRate: DiscountRate::fromRatio(0.05),
|
2026-06-14 21:32:07 +02:00
|
|
|
priceKgTtc: new Money(1500, new Currency('EUR')),
|
|
|
|
|
options: ['cut' => 'halves']
|
|
|
|
|
),
|
|
|
|
|
'Minimal product without optional fields' => fn () => new BasketProduct(
|
|
|
|
|
id: new ProductId(Uuid::uuid7()),
|
|
|
|
|
name: 'Pâtes',
|
|
|
|
|
qty: 1,
|
|
|
|
|
unitPriceHt: new Money(200, new Currency('EUR')),
|
|
|
|
|
unitDiscount: new Money(0, new Currency('EUR')),
|
|
|
|
|
taxRate: TaxRate::from(1), // 20%
|
|
|
|
|
weightGrams: 500,
|
|
|
|
|
brand: null,
|
|
|
|
|
incoId: null,
|
|
|
|
|
orderUnit: OrderUnit::from('PCE'),
|
|
|
|
|
slug: 'pates-500g',
|
|
|
|
|
picture: null,
|
|
|
|
|
description: null,
|
|
|
|
|
weightVariable: false,
|
2026-10-03 11:30:52 +02:00
|
|
|
discountRate: DiscountRate::fromRatio(0.0),
|
2026-06-14 21:32:07 +02:00
|
|
|
priceKgTtc: new Money(400, new Currency('EUR')),
|
|
|
|
|
options: []
|
|
|
|
|
),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
it('can destroy the basket from the session', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$repository = getRepository();
|
|
|
|
|
$basketId = new BasketId(Uuid::uuid7());
|
|
|
|
|
$basket = new Basket($basketId, []);
|
|
|
|
|
$repository->persist($basket);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$repository->delete($basket);
|
|
|
|
|
$retrievedBasket = $repository->find($basketId);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($retrievedBasket)->toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|