- Implement tests for product publication status. - Add tests for product orderability based on various conditions. - Enhance pricing calculation tests for different discount scenarios. - Remove outdated PricingServiceTest and refactor ProductAvailabilityServiceTest.
122 lines
3.6 KiB
PHP
122 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Services;
|
|
|
|
use Domain\Enums\SellUnit;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Models\Entities\Product;
|
|
use Domain\Models\ValueObjects\BasketProduct;
|
|
use Domain\Models\ValueObjects\DiscountRate;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Domain\Ports\Repositories\ProductRepository;
|
|
use Domain\Services\ProductAvailabilityService;
|
|
use Mockery;
|
|
use Money\Money;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
function createTestProduct(
|
|
bool $status = true,
|
|
bool $hasInco = true
|
|
): Product {
|
|
return new Product(
|
|
id: new ProductId(Uuid::uuid7()),
|
|
price: Money::EUR(100),
|
|
tax_rate: TaxRate::TVA_20,
|
|
product_weight: null,
|
|
invoicing_unit: InvoicingUnit::PCE,
|
|
sell_unit: SellUnit::PCE,
|
|
validDiscount: null,
|
|
status: $status,
|
|
orderable_start: null,
|
|
orderable_end: null,
|
|
hasInco: $hasInco
|
|
);
|
|
}
|
|
|
|
function createTestBasketProduct(ProductId $id): BasketProduct
|
|
{
|
|
return new BasketProduct(
|
|
id: $id,
|
|
name: 'Test Product',
|
|
qty: 1,
|
|
unitPriceHt: Money::EUR(100),
|
|
unitDiscount: Money::EUR(0),
|
|
taxRate: TaxRate::TVA_20,
|
|
weightGrams: 1000,
|
|
brand: null,
|
|
incoId: null,
|
|
orderUnit: OrderUnit::PCE,
|
|
slug: 'test-product',
|
|
picture: null,
|
|
description: null,
|
|
weightVariable: false,
|
|
discountRate: DiscountRate::fromRatio(0.0),
|
|
priceKgTtc: Money::EUR(100),
|
|
options: []
|
|
);
|
|
}
|
|
|
|
describe('ProductAvailabilityService', function (): void {
|
|
it('returns false when basket product is not found in repository', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$productId = new ProductId(Uuid::uuid7());
|
|
$basketProduct = createTestBasketProduct($productId);
|
|
|
|
$repository->shouldReceive('find')
|
|
->once()
|
|
->with($productId)
|
|
->andReturn(null);
|
|
|
|
// WHEN
|
|
$result = $service->isBasketProductPublished($basketProduct);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('returns true when basket product is found and published', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$productId = new ProductId(Uuid::uuid7());
|
|
$basketProduct = createTestBasketProduct($productId);
|
|
$product = createTestProduct(status: true, hasInco: true);
|
|
|
|
$repository->shouldReceive('find')
|
|
->once()
|
|
->with($productId)
|
|
->andReturn($product);
|
|
|
|
// WHEN
|
|
$result = $service->isBasketProductPublished($basketProduct);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('returns false when basket product is found but not published', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$productId = new ProductId(Uuid::uuid7());
|
|
$basketProduct = createTestBasketProduct($productId);
|
|
$product = createTestProduct(status: false, hasInco: true);
|
|
|
|
$repository->shouldReceive('find')
|
|
->once()
|
|
->with($productId)
|
|
->andReturn($product);
|
|
|
|
// WHEN
|
|
$result = $service->isBasketProductPublished($basketProduct);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
});
|