2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Domain\Services;
|
|
|
|
|
|
|
|
|
|
use Domain\Enums\ProductBrand;
|
|
|
|
|
use Domain\Enums\TaxRate;
|
|
|
|
|
use Domain\Enums\Units\OrderUnit;
|
2026-11-15 16:25:19 +01:00
|
|
|
use Domain\Events\BasketHasBeenModified;
|
|
|
|
|
use Domain\Events\BasketRemoved;
|
2026-06-14 21:32:07 +02:00
|
|
|
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\Models\ValueObjects\SessionId;
|
|
|
|
|
use Domain\Ports\Repositories\BasketRepository;
|
|
|
|
|
use Domain\Ports\Repositories\ProductRepository;
|
|
|
|
|
use Domain\Ports\Services\SessionService;
|
|
|
|
|
use Domain\Services\BasketService;
|
|
|
|
|
use Mockery;
|
|
|
|
|
use Money\Money;
|
2026-11-15 16:25:19 +01:00
|
|
|
use Shared\DDD\DomainEventDispatcher;
|
2026-11-17 12:15:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-16 16:13:14 +01:00
|
|
|
/** @param array{id?: ProductId, qty?: int, unitPriceHt?: Money, unitDiscount?: Money} $overrides */
|
2026-06-14 21:32:07 +02:00
|
|
|
function makeBasketProduct(array $overrides = []): BasketProduct
|
|
|
|
|
{
|
2026-11-16 16:13:14 +01:00
|
|
|
return new BasketProduct(
|
2026-11-17 12:15:39 +01:00
|
|
|
id: $overrides['id'] ?? new ProductId(UuidHelper::fromString('019e9e37-f2a1-74ef-b9c2-66d222470128')),
|
2026-11-16 16:13:14 +01:00
|
|
|
name: 'Poulet Rôti',
|
|
|
|
|
qty: $overrides['qty'] ?? 1,
|
|
|
|
|
unitPriceHt: $overrides['unitPriceHt'] ?? Money::EUR(1000),
|
|
|
|
|
unitDiscount: $overrides['unitDiscount'] ?? Money::EUR(0),
|
|
|
|
|
taxRate: TaxRate::TVA_5_5,
|
|
|
|
|
weightGrams: 1200,
|
|
|
|
|
brand: ProductBrand::marie,
|
|
|
|
|
incoId: new IncoId(45),
|
|
|
|
|
orderUnit: OrderUnit::PCE,
|
|
|
|
|
slug: 'poulet-roti',
|
|
|
|
|
picture: new StorageFilePath('images/poulet.jpg'),
|
|
|
|
|
description: 'Un bon poulet',
|
|
|
|
|
weightVariable: false,
|
|
|
|
|
discountRate: DiscountRate::fromRatio(0.0),
|
|
|
|
|
priceKgTtc: Money::EUR(1500),
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('BasketService', function (): void {
|
2026-11-15 16:25:19 +01:00
|
|
|
it('adds a new product to the basket and dispatches BasketHasBeenModified', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$productId = new ProductId(UuidHelper::v7());
|
2026-06-14 21:32:07 +02:00
|
|
|
$qty = 2;
|
2026-11-17 12:15:39 +01:00
|
|
|
$basket = new Basket(new BasketId(UuidHelper::v7()));
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketProduct = makeBasketProduct(['id' => $productId, 'qty' => $qty]);
|
|
|
|
|
|
|
|
|
|
$basketRepository = Mockery::mock(BasketRepository::class);
|
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionService->shouldReceive('get')->andReturn(new SessionId(UuidHelper::v7()))->byDefault();
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketRepository->shouldReceive('find')->once()->andReturn($basket);
|
|
|
|
|
$basketRepository->shouldReceive('persist')->once()->with(Mockery::on(function (Basket $b) use ($productId) {
|
|
|
|
|
return $b->has($productId) && $b->countItems() === 2;
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
$productRepository = Mockery::mock(ProductRepository::class);
|
|
|
|
|
$productRepository->shouldReceive('getBasketProduct')->once()->with($productId, $qty)->andReturn($basketProduct);
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
$eventDispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
|
$eventDispatcher->shouldReceive('dispatch')
|
|
|
|
|
->once()
|
|
|
|
|
->with(Mockery::on(fn (BasketHasBeenModified $event): bool => $event->basketId->equals($basket->id)));
|
|
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepository, $productRepository, $sessionService, $eventDispatcher);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->add($productId, $qty);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($basket->has($productId))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
it('removes a quantity from the basket and dispatches BasketHasBeenModified', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$productId = new ProductId(UuidHelper::v7());
|
|
|
|
|
$basket = new Basket(new BasketId(UuidHelper::v7()));
|
2026-06-14 21:32:07 +02:00
|
|
|
$basket->addOrUpdate(makeBasketProduct(['id' => $productId, 'qty' => 3]));
|
|
|
|
|
|
|
|
|
|
$basketRepository = Mockery::mock(BasketRepository::class);
|
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionService->shouldReceive('get')->andReturn(new SessionId(UuidHelper::v7()))->byDefault();
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketRepository->shouldReceive('find')->once()->andReturn($basket);
|
|
|
|
|
$basketRepository->shouldReceive('persist')->once()->with(Mockery::on(function (Basket $b) use ($productId) {
|
|
|
|
|
$items = $b->items();
|
|
|
|
|
|
|
|
|
|
return $items[(string) $productId->value]->qty === 1;
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
$productRepository = Mockery::mock(ProductRepository::class);
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
$eventDispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
|
$eventDispatcher->shouldReceive('dispatch')
|
|
|
|
|
->once()
|
|
|
|
|
->with(Mockery::on(fn (BasketHasBeenModified $event): bool => $event->basketId->equals($basket->id)));
|
|
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepository, $productRepository, $sessionService, $eventDispatcher);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->remove($productId, 2);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$items = $basket->items();
|
|
|
|
|
expect($items[(string) $productId->value]->qty)->toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('removes the entire line when quantity to remove equals current quantity', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$productId = new ProductId(UuidHelper::v7());
|
|
|
|
|
$basket = new Basket(new BasketId(UuidHelper::v7()));
|
2026-06-14 21:32:07 +02:00
|
|
|
$basket->addOrUpdate(makeBasketProduct(['id' => $productId, 'qty' => 3]));
|
|
|
|
|
|
|
|
|
|
$basketRepository = Mockery::mock(BasketRepository::class);
|
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionService->shouldReceive('get')->andReturn(new SessionId(UuidHelper::v7()))->byDefault();
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketRepository->shouldReceive('find')->once()->andReturn($basket);
|
|
|
|
|
$basketRepository->shouldReceive('persist')->once()->with(Mockery::on(function (Basket $b) use ($productId) {
|
|
|
|
|
return ! $b->has($productId);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
$productRepository = Mockery::mock(ProductRepository::class);
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
$eventDispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
|
$eventDispatcher->shouldReceive('dispatch')
|
|
|
|
|
->once()
|
|
|
|
|
->with(Mockery::on(fn (BasketHasBeenModified $event): bool => $event->basketId->equals($basket->id)));
|
|
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepository, $productRepository, $sessionService, $eventDispatcher);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->remove($productId, 3);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($basket->has($productId))->toBeFalse();
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
it('removes an entire line directly via removeLine and dispatches BasketHasBeenModified', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$productId = new ProductId(UuidHelper::v7());
|
|
|
|
|
$basket = new Basket(new BasketId(UuidHelper::v7()));
|
2026-06-14 21:32:07 +02:00
|
|
|
$basket->addOrUpdate(makeBasketProduct(['id' => $productId, 'qty' => 5]));
|
|
|
|
|
|
|
|
|
|
$basketRepository = Mockery::mock(BasketRepository::class);
|
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionService->shouldReceive('get')->andReturn(new SessionId(UuidHelper::v7()))->byDefault();
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketRepository->shouldReceive('find')->once()->andReturn($basket);
|
|
|
|
|
$basketRepository->shouldReceive('persist')->once()->with(Mockery::on(function (Basket $b) use ($productId) {
|
|
|
|
|
return ! $b->has($productId);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
$productRepository = Mockery::mock(ProductRepository::class);
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
$eventDispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
|
$eventDispatcher->shouldReceive('dispatch')
|
|
|
|
|
->once()
|
|
|
|
|
->with(Mockery::on(fn (BasketHasBeenModified $event): bool => $event->basketId->equals($basket->id)));
|
|
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepository, $productRepository, $sessionService, $eventDispatcher);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->removeLine($productId);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($basket->has($productId))->toBeFalse();
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
it('empties the basket by destroying it from repository and dispatches BasketRemoved', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$basket = new Basket(new BasketId(UuidHelper::v7()));
|
2026-11-16 15:12:42 +01:00
|
|
|
/** @var BasketRepository&\Mockery\MockInterface $basketRepository */
|
|
|
|
|
$basketRepository = Mockery::spy(BasketRepository::class);
|
|
|
|
|
$basketRepository->allows('find')->andReturn($basket);
|
|
|
|
|
|
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionService->shouldReceive('get')->andReturn(new SessionId(UuidHelper::v7()))->byDefault();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
$productRepository = Mockery::mock(ProductRepository::class);
|
|
|
|
|
|
2026-11-16 15:12:42 +01:00
|
|
|
/** @var DomainEventDispatcher&\Mockery\MockInterface $eventDispatcher */
|
|
|
|
|
$eventDispatcher = Mockery::spy(DomainEventDispatcher::class);
|
2026-11-15 16:25:19 +01:00
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepository, $productRepository, $sessionService, $eventDispatcher);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->empty();
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:12:42 +01:00
|
|
|
$basketRepository->shouldHaveReceived('delete')->once()->with($basket);
|
|
|
|
|
$eventDispatcher->shouldHaveReceived('dispatch')
|
|
|
|
|
->once()
|
|
|
|
|
->with(Mockery::on(static fn (BasketRemoved $event): bool => $event->basketId->equals($basket->id)));
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
it('gets the basket from repository without dispatching events', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$basket = new Basket(new BasketId(UuidHelper::v7()));
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketRepository = Mockery::mock(BasketRepository::class);
|
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionService->shouldReceive('get')->andReturn(new SessionId(UuidHelper::v7()))->byDefault();
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketRepository->shouldReceive('find')->once()->andReturn($basket);
|
|
|
|
|
|
|
|
|
|
$productRepository = Mockery::mock(ProductRepository::class);
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
$eventDispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepository, $productRepository, $sessionService, $eventDispatcher);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$returnedBasket = $service->getBasket();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($returnedBasket)->toBe($basket);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns a new basket if not found in repository', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionId = new SessionId(UuidHelper::v7());
|
2026-06-14 21:32:07 +02:00
|
|
|
$basketRepository = Mockery::mock(BasketRepository::class);
|
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
|
|
|
|
$sessionService->shouldReceive('get')->andReturn($sessionId)->byDefault();
|
|
|
|
|
$basketRepository->shouldReceive('find')->once()->andReturn(null);
|
|
|
|
|
|
|
|
|
|
$productRepository = Mockery::mock(ProductRepository::class);
|
|
|
|
|
|
2026-11-15 16:25:19 +01:00
|
|
|
$eventDispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepository, $productRepository, $sessionService, $eventDispatcher);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$returnedBasket = $service->getBasket();
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($returnedBasket->id->value)->toBe($sessionId->value)
|
2026-06-14 21:32:07 +02:00
|
|
|
->and($returnedBasket->countItems())->toBe(0);
|
|
|
|
|
});
|
2026-11-15 16:25:19 +01:00
|
|
|
|
|
|
|
|
it('reports whether the basket has items', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$sessionId = new SessionId(UuidHelper::v7());
|
2026-11-15 16:25:19 +01:00
|
|
|
$nonEmpty = new Basket(new BasketId($sessionId->value), [makeBasketProduct()]);
|
|
|
|
|
$empty = new Basket(new BasketId($sessionId->value), []);
|
|
|
|
|
$basketRepo = Mockery::mock(BasketRepository::class);
|
|
|
|
|
$productRepo = Mockery::mock(ProductRepository::class);
|
|
|
|
|
$sessionSvc = Mockery::mock(SessionService::class);
|
|
|
|
|
$eventDispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
|
|
|
|
|
|
$sessionSvc->shouldReceive('get')->andReturn($sessionId);
|
|
|
|
|
$basketRepo->shouldReceive('find')->twice()->andReturn($nonEmpty, $empty);
|
|
|
|
|
|
|
|
|
|
$service = new BasketService($basketRepo, $productRepo, $sessionSvc, $eventDispatcher);
|
|
|
|
|
|
|
|
|
|
// WHEN / THEN
|
|
|
|
|
expect($service->hasItems())->toBeTrue()
|
|
|
|
|
->and($service->hasItems())->toBeFalse();
|
|
|
|
|
});
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|