104 lines
3.4 KiB
PHP
104 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Policies;
|
|
|
|
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
|
|
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
|
|
use Domain\Policies\CustomerOrderLimitPolicy;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
|
|
describe('CustomerOrderLimitPolicy', function () {
|
|
it('asserts can place order successfully when below limit', function () {
|
|
// GIVEN
|
|
$policy = new CustomerOrderLimitPolicy(20000);
|
|
$basketTotal = new Money(19999, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$policy->assertCanPlaceOrder($basketTotal);
|
|
|
|
// THEN
|
|
expect(true)->toBeTrue();
|
|
});
|
|
|
|
it('asserts can place order successfully when exactly at limit', function () {
|
|
// GIVEN
|
|
$policy = new CustomerOrderLimitPolicy(20000);
|
|
$basketTotal = new Money(20000, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$policy->assertCanPlaceOrder($basketTotal);
|
|
|
|
// THEN
|
|
expect(true)->toBeTrue();
|
|
});
|
|
|
|
it('throws OrderLimitExceededException when above limit', function () {
|
|
// GIVEN
|
|
$policy = new CustomerOrderLimitPolicy(20000);
|
|
$basketTotal = new Money(20001, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$action = fn () => $policy->assertCanPlaceOrder($basketTotal);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(OrderLimitExceededException::class);
|
|
});
|
|
|
|
it('asserts can place weekly order successfully when below limit', function () {
|
|
// GIVEN
|
|
$policy = new CustomerOrderLimitPolicy(20000, 30000);
|
|
$basketTotal = new Money(10000, new Currency('EUR'));
|
|
$weeklyTotal = new Money(19999, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$policy->assertCanPlaceWeeklyOrder($basketTotal, $weeklyTotal);
|
|
|
|
// THEN
|
|
expect(true)->toBeTrue();
|
|
});
|
|
|
|
it('asserts can place weekly order successfully when exactly at limit', function () {
|
|
// GIVEN
|
|
$policy = new CustomerOrderLimitPolicy(20000, 30000);
|
|
$basketTotal = new Money(10000, new Currency('EUR'));
|
|
$weeklyTotal = new Money(20000, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$policy->assertCanPlaceWeeklyOrder($basketTotal, $weeklyTotal);
|
|
|
|
// THEN
|
|
expect(true)->toBeTrue();
|
|
});
|
|
|
|
it('throws WeeklyLimitExceededException when above limit', function () {
|
|
// GIVEN
|
|
$policy = new CustomerOrderLimitPolicy(20000, 30000);
|
|
$basketTotal = new Money(10000, new Currency('EUR'));
|
|
$weeklyTotal = new Money(20001, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$action = fn () => $policy->assertCanPlaceWeeklyOrder($basketTotal, $weeklyTotal);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(WeeklyLimitExceededException::class);
|
|
});
|
|
|
|
it('uses custom currency when provided', function () {
|
|
// GIVEN
|
|
$currency = new Currency('USD');
|
|
$policy = new CustomerOrderLimitPolicy(20000, 30000, $currency);
|
|
$basketTotal = new Money(20001, $currency);
|
|
$weeklyTotal = new Money(10000, $currency);
|
|
|
|
// WHEN
|
|
$actionOrder = fn () => $policy->assertCanPlaceOrder($basketTotal);
|
|
$actionWeekly = fn () => $policy->assertCanPlaceWeeklyOrder($basketTotal, $weeklyTotal);
|
|
|
|
// THEN
|
|
expect($actionOrder)->toThrow(OrderLimitExceededException::class)
|
|
->and($actionWeekly)->toThrow(WeeklyLimitExceededException::class);
|
|
});
|
|
});
|