173 lines
6.4 KiB
PHP
173 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Queries\Basket;
|
|
|
|
use App\Contracts\QueryInput;
|
|
use App\Contracts\QueryInputEmpty;
|
|
use App\Cqs\Queries\Basket\GetBasketQuery;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\SellUnit;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Models\Entities\Basket;
|
|
use Domain\Models\ValueObjects\BasketProduct;
|
|
use Domain\Models\ValueObjects\DiscountRate;
|
|
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;
|
|
use Shared\DDD\DomainEventDispatcher;
|
|
use Shared\Helpers\UuidHelper;
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
|
|
function makeBasketServiceWithBasket(?Basket $basket): BasketService
|
|
{
|
|
$basketId = $basket !== null ? $basket->id : new BasketId(UuidHelper::v7());
|
|
$sessionId = new SessionId($basketId->value);
|
|
|
|
$sessionService = Mockery::mock(SessionService::class);
|
|
$sessionService->shouldReceive('get')->andReturn($sessionId);
|
|
|
|
$basketRepository = Mockery::mock(BasketRepository::class);
|
|
$basketRepository->shouldReceive('find')->with(Mockery::type(BasketId::class))->andReturn($basket);
|
|
|
|
return new BasketService(
|
|
repository: $basketRepository,
|
|
productRepository: Mockery::mock(ProductRepository::class),
|
|
sessionService: $sessionService,
|
|
eventDispatcher: Mockery::mock(DomainEventDispatcher::class),
|
|
);
|
|
}
|
|
|
|
function makeBasketProductItem(ProductId $id, string $name, int $qty = 1): BasketProduct
|
|
{
|
|
return new BasketProduct(
|
|
id: $id,
|
|
name: $name,
|
|
qty: $qty,
|
|
unitPriceHt: Money::EUR(1000),
|
|
unitDiscount: Money::EUR(0),
|
|
taxRate: TaxRate::TVA_5_5,
|
|
weightGrams: 1000,
|
|
brand: ProductBrand::marie,
|
|
incoId: new IncoId(45),
|
|
orderUnit: OrderUnit::PCE,
|
|
slug: 'poulet-roti',
|
|
picture: new StorageFilePath('images/poulet.jpg'),
|
|
description: 'Description produit',
|
|
weightVariable: false,
|
|
discountRate: DiscountRate::fromRatio(0.0),
|
|
priceKgTtc: Money::EUR(1055),
|
|
);
|
|
}
|
|
|
|
describe('GetBasketQuery', function (): void {
|
|
it('returns an empty BasketDto when the basket is empty', function (): void {
|
|
// GIVEN
|
|
$basketService = makeBasketServiceWithBasket(null);
|
|
$query = new GetBasketQuery($basketService);
|
|
|
|
// WHEN
|
|
$result = $query(QueryInputEmpty::create());
|
|
|
|
// THEN
|
|
expect($result->items)->toBe([])
|
|
->and($result->countItems)->toBe(0)
|
|
->and($result->totalWeight)->toBe(0);
|
|
});
|
|
|
|
it('maps published and unpublished products with accurate isPublished flag', function (): void {
|
|
// GIVEN
|
|
$range = new Range();
|
|
$range->name = 'Gamme Test ' . uniqid();
|
|
$range->status = true;
|
|
$range->order = random_int(1, 9999);
|
|
$range->picture_alt = 'alt';
|
|
$range->picture_title = 'title';
|
|
$range->save();
|
|
|
|
$publishedProduct = new Product([
|
|
'range_id' => $range->id,
|
|
'name' => 'Poulet Publié',
|
|
'brand' => ProductBrand::marie,
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
'sell_unit' => SellUnit::PCE,
|
|
'order_unit' => OrderUnit::PCE,
|
|
'tax_rate' => TaxRate::TVA_5_5,
|
|
'description' => 'Description produit',
|
|
'status' => true,
|
|
'picture_alt' => 'alt',
|
|
'picture_title' => 'title',
|
|
'picture_large_alt' => 'alt',
|
|
'picture_large_title' => 'title',
|
|
]);
|
|
$publishedProduct->order = random_int(1, 999999);
|
|
$publishedProduct->price = Money::EUR(1000);
|
|
$publishedProduct->save();
|
|
|
|
$unpublishedProduct = new Product([
|
|
'range_id' => $range->id,
|
|
'name' => 'Poulet Dépublié',
|
|
'brand' => ProductBrand::marie,
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
'sell_unit' => SellUnit::PCE,
|
|
'order_unit' => OrderUnit::PCE,
|
|
'tax_rate' => TaxRate::TVA_5_5,
|
|
'description' => 'Description produit',
|
|
'status' => false,
|
|
'picture_alt' => 'alt',
|
|
'picture_title' => 'title',
|
|
'picture_large_alt' => 'alt',
|
|
'picture_large_title' => 'title',
|
|
]);
|
|
$unpublishedProduct->order = random_int(1, 999999);
|
|
$unpublishedProduct->price = Money::EUR(1000);
|
|
$unpublishedProduct->save();
|
|
|
|
$publishedId = new ProductId(UuidHelper::fromString($publishedProduct->id));
|
|
$unpublishedId = new ProductId(UuidHelper::fromString($unpublishedProduct->id));
|
|
|
|
$basket = new Basket(new BasketId(UuidHelper::v7()), [
|
|
makeBasketProductItem($publishedId, 'Poulet Publié', 2),
|
|
makeBasketProductItem($unpublishedId, 'Poulet Dépublié', 1),
|
|
]);
|
|
|
|
$query = new GetBasketQuery(makeBasketServiceWithBasket($basket));
|
|
|
|
// WHEN
|
|
$result = $query(QueryInputEmpty::create());
|
|
|
|
// THEN
|
|
expect($result->items)->toHaveCount(2)
|
|
->and($result->countItems)->toBe(3)
|
|
->and($result->items[0]->product->id->equals($publishedId))->toBeTrue()
|
|
->and($result->items[0]->isPublished)->toBeTrue()
|
|
->and($result->items[1]->product->id->equals($unpublishedId))->toBeTrue()
|
|
->and($result->items[1]->isPublished)->toBeFalse();
|
|
});
|
|
|
|
it('throws InvalidArgumentException when invalid input is passed', function (): void {
|
|
// GIVEN
|
|
$query = new GetBasketQuery(makeBasketServiceWithBasket(null));
|
|
$invalidInput = new class () implements QueryInput
|
|
{
|
|
};
|
|
|
|
// WHEN & THEN
|
|
expect(
|
|
/** @phpstan-ignore argument.type */
|
|
fn () => $query($invalidInput)
|
|
)->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
});
|