161 lines
6.3 KiB
PHP
161 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Basket;
|
|
|
|
use App\Cqs\Actions\Basket\AddToBasket;
|
|
use App\Cqs\Actions\Basket\AddToBasketInput;
|
|
use App\Models\Product;
|
|
use App\Models\ProductInco;
|
|
use App\Models\Range;
|
|
use App\Services\ActionExecutorService;
|
|
use Domain\Builders\OrderBuilder;
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\SellUnit;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Exceptions\OrderTunnel\UnorderableProductException;
|
|
use Domain\Models\Entities\Basket;
|
|
use Domain\Models\ValueObjects\Entities\BasketId;
|
|
use Domain\Models\ValueObjects\Entities\ShopperId;
|
|
use Domain\Services\BasketService;
|
|
use Illuminate\Cache\ArrayStore;
|
|
use Illuminate\Cache\Repository;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
|
|
use Infrastructure\Adapters\Locks\LaravelCacheLock;
|
|
use Infrastructure\Adapters\Repositories\EloquentProductRepository;
|
|
use Infrastructure\Adapters\Repositories\SessionBasketRepository;
|
|
use Infrastructure\Adapters\Serializers\BasketSerializer;
|
|
use Infrastructure\Adapters\Services\AppSessionService;
|
|
use Infrastructure\Adapters\Services\SessionOrderBuilderManager;
|
|
use Money\Money;
|
|
use Psr\Log\NullLogger;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
function makeBasketProductFixture(bool $status = true, bool $withInco = true): Product
|
|
{
|
|
$range = new Range();
|
|
$range->name = 'Gamme Panier ' . uniqid();
|
|
$range->status = true;
|
|
/** @var int $maxOrder */
|
|
$maxOrder = Range::query()->max('order') ?? 0;
|
|
$range->order = $maxOrder + 1;
|
|
$range->picture_alt = 'alt';
|
|
$range->picture_title = 'title';
|
|
$range->saveOrFail();
|
|
|
|
$product = new Product([
|
|
'range_id' => $range->id,
|
|
'name' => 'Produit Panier ' . uniqid(),
|
|
'brand' => ProductBrand::loue,
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
'sell_unit' => SellUnit::PCE,
|
|
'order_unit' => OrderUnit::PCE,
|
|
'tax_rate' => TaxRate::TVA_5_5,
|
|
'description' => 'Description panier',
|
|
'status' => $status,
|
|
]);
|
|
$product->price = Money::EUR(1000);
|
|
$product->short_description = 'Court';
|
|
$product->picture_alt = 'alt';
|
|
$product->picture_title = 'title';
|
|
$product->picture_large_alt = 'alt large';
|
|
$product->picture_large_title = 'title large';
|
|
$product->saveOrFail();
|
|
|
|
if ($withInco) {
|
|
$inco = new ProductInco();
|
|
$inco->product_id = $product->id;
|
|
$inco->no_art = random_int(10000, 99999);
|
|
$inco->saveOrFail();
|
|
}
|
|
|
|
return $product;
|
|
}
|
|
|
|
describe('AddToBasket action', function (): void {
|
|
it('adds an orderable product to session basket and increments quantity on repeated additions', function (): void {
|
|
// GIVEN
|
|
$product = makeBasketProductFixture();
|
|
/** @var \Illuminate\Contracts\Session\Session $session */
|
|
$session = Session::driver();
|
|
$repository = new SessionBasketRepository($session, 'basket_', new BasketSerializer());
|
|
$dispatcher = new LaravelDomainEventDispatcher();
|
|
$basketSvc = new BasketService(
|
|
$repository,
|
|
new EloquentProductRepository(),
|
|
new AppSessionService('domain_session_uuid'),
|
|
$dispatcher,
|
|
);
|
|
$action = new AddToBasket($basketSvc);
|
|
|
|
// WHEN
|
|
$action(new AddToBasketInput($product->getId(), 2));
|
|
$action(new AddToBasketInput($product->getId(), 3));
|
|
|
|
// THEN
|
|
$basket = $basketSvc->getBasket();
|
|
expect($basket->countItems())->toBe(5)
|
|
->and($basket->items())->toHaveCount(1)
|
|
->and($basket->items()[$product->id]->qty)->toBe(5)
|
|
->and($basket->items()[$product->id]->unitPriceHt->getAmount())->toBe('1000');
|
|
});
|
|
|
|
it('invalidates existing order builder in session when executed through ActionExecutorService', function (): void {
|
|
// GIVEN
|
|
$product = makeBasketProductFixture();
|
|
/** @var \Illuminate\Contracts\Session\Session $session */
|
|
$session = Session::driver();
|
|
$repository = new SessionBasketRepository($session, 'basket_', new BasketSerializer());
|
|
$builderManager = new SessionOrderBuilderManager($session, 'order_tunnel_builder_session');
|
|
$builderManager->save(OrderBuilder::fromBasket(
|
|
new Basket(new BasketId(UuidHelper::v7()), []),
|
|
new ShopperId(UuidHelper::v7()),
|
|
));
|
|
|
|
$dispatcher = new LaravelDomainEventDispatcher();
|
|
$basketSvc = new BasketService(
|
|
$repository,
|
|
new EloquentProductRepository(),
|
|
new AppSessionService('domain_session_uuid'),
|
|
$dispatcher,
|
|
);
|
|
$executor = new ActionExecutorService(
|
|
new LaravelCacheLock(new Repository(new ArrayStore())),
|
|
new NullLogger(),
|
|
$dispatcher,
|
|
);
|
|
|
|
// WHEN
|
|
$executor->execute(new AddToBasket($basketSvc), new AddToBasketInput($product->getId(), 1));
|
|
|
|
// THEN
|
|
expect($builderManager->get())->toBeNull()
|
|
->and($basketSvc->getBasket()->countItems())->toBe(1);
|
|
});
|
|
|
|
it('throws UnorderableProductException when product is disabled or missing inco', function (): void {
|
|
// GIVEN
|
|
$inactiveProduct = makeBasketProductFixture(status: false, withInco: true);
|
|
$noIncoProduct = makeBasketProductFixture(status: true, withInco: false);
|
|
/** @var \Illuminate\Contracts\Session\Session $session */
|
|
$session = Session::driver();
|
|
$basketSvc = new BasketService(
|
|
new SessionBasketRepository($session, 'basket_', new BasketSerializer()),
|
|
new EloquentProductRepository(),
|
|
new AppSessionService('domain_session_uuid'),
|
|
new LaravelDomainEventDispatcher(),
|
|
);
|
|
$action = new AddToBasket($basketSvc);
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action(new AddToBasketInput($inactiveProduct->getId(), 1)))
|
|
->toThrow(UnorderableProductException::class);
|
|
expect(fn () => $action(new AddToBasketInput($noIncoProduct->getId(), 1)))
|
|
->toThrow(UnorderableProductException::class);
|
|
expect($basketSvc->hasItems())->toBeFalse();
|
|
});
|
|
});
|