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(); }); });