- Removed fully qualified names for better readability. - Streamlined code by using direct class references.
177 lines
5.2 KiB
PHP
177 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\User;
|
|
use App\Policies\Frontend\BasketPolicy;
|
|
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 Money\Money;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
covers(BasketPolicy::class);
|
|
|
|
function createDummyBasketProduct(): BasketProduct
|
|
{
|
|
return new BasketProduct(
|
|
new ProductId(Uuid::uuid7()),
|
|
'Dummy',
|
|
1,
|
|
Money::EUR(1000),
|
|
Money::EUR(0),
|
|
TaxRate::TVA_20,
|
|
1000,
|
|
null,
|
|
null,
|
|
OrderUnit::PCE,
|
|
'dummy',
|
|
null,
|
|
null,
|
|
false,
|
|
DiscountRate::zero(),
|
|
Money::EUR(1000)
|
|
);
|
|
}
|
|
|
|
function createTestDomainProduct(bool $status = true): Product
|
|
{
|
|
return new Product(
|
|
new ProductId(Uuid::uuid7()),
|
|
Money::EUR(1000),
|
|
TaxRate::TVA_20,
|
|
null,
|
|
InvoicingUnit::PCE,
|
|
SellUnit::PCE,
|
|
null,
|
|
$status,
|
|
null,
|
|
null,
|
|
true
|
|
);
|
|
}
|
|
|
|
describe('BasketPolicy', function () {
|
|
describe('viewTotal', function () {
|
|
it('returns true when user is a Customer instance', function () {
|
|
// GIVEN
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$policy = new BasketPolicy($productRepository);
|
|
$customer = \Mockery::mock(Customer::class);
|
|
|
|
// WHEN
|
|
$result = $policy->viewTotal($customer);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('returns true when user is a User instance', function () {
|
|
// GIVEN
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$policy = new BasketPolicy($productRepository);
|
|
$user = \Mockery::mock(User::class);
|
|
|
|
// WHEN
|
|
$result = $policy->viewTotal($user);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('returns false when user is null', function () {
|
|
// GIVEN
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$policy = new BasketPolicy($productRepository);
|
|
|
|
// WHEN
|
|
$result = $policy->viewTotal(null);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('viewPrices', function () {
|
|
it('returns false when user is null', function () {
|
|
// GIVEN
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$policy = new BasketPolicy($productRepository);
|
|
$product = createDummyBasketProduct();
|
|
|
|
// WHEN
|
|
$result = $policy->viewPrices(null, $product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('returns true when user is not null and product is published', function () {
|
|
// GIVEN
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$policy = new BasketPolicy($productRepository);
|
|
$customer = \Mockery::mock(Customer::class);
|
|
$product = createDummyBasketProduct();
|
|
|
|
$domainProduct = createTestDomainProduct(status: true);
|
|
|
|
$productRepository->shouldReceive('find')
|
|
->once()
|
|
->with($product->id)
|
|
->andReturn($domainProduct);
|
|
|
|
// WHEN
|
|
$result = $policy->viewPrices($customer, $product);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('returns false when user is not null and product is not published', function () {
|
|
// GIVEN
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$policy = new BasketPolicy($productRepository);
|
|
$user = \Mockery::mock(User::class);
|
|
$product = createDummyBasketProduct();
|
|
|
|
$domainProduct = createTestDomainProduct(status: false);
|
|
|
|
$productRepository->shouldReceive('find')
|
|
->once()
|
|
->with($product->id)
|
|
->andReturn($domainProduct);
|
|
|
|
// WHEN
|
|
$result = $policy->viewPrices($user, $product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('returns false when user is not null and product is not found', function () {
|
|
// GIVEN
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$policy = new BasketPolicy($productRepository);
|
|
$user = \Mockery::mock(User::class);
|
|
$product = createDummyBasketProduct();
|
|
|
|
$productRepository->shouldReceive('find')
|
|
->once()
|
|
->with($product->id)
|
|
->andReturn(null);
|
|
|
|
// WHEN
|
|
$result = $policy->viewPrices($user, $product);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
});
|
|
});
|