238 lines
7.1 KiB
PHP
238 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Services;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
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\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,
|
|
?CarbonImmutable $orderableStart = null,
|
|
?CarbonImmutable $orderableEnd = null,
|
|
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: $orderableStart,
|
|
orderable_end: $orderableEnd,
|
|
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: 0.0,
|
|
priceKgTtc: Money::EUR(100),
|
|
options: []
|
|
);
|
|
}
|
|
|
|
describe('ProductAvailabilityService', function (): void {
|
|
it('returns true when product is fully orderable', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$now = CarbonImmutable::now();
|
|
$product = createTestProduct(
|
|
status: true,
|
|
orderableStart: $now->subDay(),
|
|
orderableEnd: $now->addDay(),
|
|
hasInco: true
|
|
);
|
|
|
|
// WHEN
|
|
$result = $service->isOrderable($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('returns false when product status is inactive', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$now = CarbonImmutable::now();
|
|
$product = createTestProduct(
|
|
status: false,
|
|
orderableStart: $now->subDay(),
|
|
orderableEnd: $now->addDay(),
|
|
hasInco: true
|
|
);
|
|
|
|
// WHEN
|
|
$result = $service->isOrderable($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('returns false when product has no inco', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$now = CarbonImmutable::now();
|
|
$product = createTestProduct(
|
|
status: true,
|
|
orderableStart: $now->subDay(),
|
|
orderableEnd: $now->addDay(),
|
|
hasInco: false
|
|
);
|
|
|
|
// WHEN
|
|
$result = $service->isOrderable($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('returns false when orderable start is in the future', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$now = CarbonImmutable::now();
|
|
$product = createTestProduct(
|
|
status: true,
|
|
orderableStart: $now->addDay(),
|
|
orderableEnd: $now->addDays(2),
|
|
hasInco: true
|
|
);
|
|
|
|
// WHEN
|
|
$result = $service->isOrderable($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('returns false when orderable end is in the past', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$now = CarbonImmutable::now();
|
|
$product = createTestProduct(
|
|
status: true,
|
|
orderableStart: null,
|
|
orderableEnd: $now->subDay(),
|
|
hasInco: true
|
|
);
|
|
|
|
// WHEN
|
|
$result = $service->isOrderable($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('determines if product is published', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$product = createTestProduct(status: true);
|
|
|
|
// WHEN
|
|
$result = $service->isPublished($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('determines if product is not published when status is false', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$product = createTestProduct(status: false);
|
|
|
|
// WHEN
|
|
$result = $service->isPublished($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
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);
|
|
|
|
$repository->shouldReceive('find')
|
|
->once()
|
|
->with($productId)
|
|
->andReturn($product);
|
|
|
|
// WHEN
|
|
$result = $service->isBasketProductPublished($basketProduct);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('returns true when product has inco via isAssociatedWithInco directly', function (): void {
|
|
// GIVEN
|
|
$repository = Mockery::mock(ProductRepository::class);
|
|
$service = new ProductAvailabilityService($repository);
|
|
$product = createTestProduct(hasInco: true);
|
|
|
|
// WHEN
|
|
$result = $service->isAssociatedWithInco($product);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
});
|