76 lines
2.5 KiB
PHP
76 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
|
|
|
use Domain\Models\ValueObjects\MoneyAmount;
|
|
use Shared\Enums\Currency;
|
|
use Shared\ValueObjects\Money;
|
|
|
|
covers(MoneyAmount::class);
|
|
|
|
describe('money amount', function (): void {
|
|
$money = new Money(Currency::EUR, 300);
|
|
$secondMoney = new Money(Currency::EUR, 200);
|
|
$minimumMoney = new Money(Currency::EUR, 1);
|
|
$zeroMoney = new Money(Currency::EUR, 0);
|
|
$aboveLimitMoney = new Money(Currency::EUR, 301);
|
|
|
|
it('wraps a Money value', function () use ($money): void {
|
|
// GIVEN / WHEN
|
|
$amount = new MoneyAmount($money);
|
|
|
|
// THEN
|
|
expect($amount)->toBeInstanceOf(MoneyAmount::class)
|
|
->and($amount->value)->toBe($money);
|
|
});
|
|
|
|
it('implements equals by delegating to the wrapped Money', function () use ($money, $secondMoney): void {
|
|
// GIVEN / WHEN
|
|
$amount1 = new MoneyAmount($money);
|
|
$amount2 = new MoneyAmount($money);
|
|
$amount3 = new MoneyAmount($secondMoney);
|
|
|
|
// THEN
|
|
expect($amount1->equals($amount2))->toBeTrue()
|
|
->and($amount1->equals($amount3))->toBeFalse()
|
|
->and($amount1->equals(null))->toBeFalse();
|
|
});
|
|
|
|
it('converts to string by delegating to the wrapped Money', function () use ($money): void {
|
|
// GIVEN / WHEN
|
|
$amount = new MoneyAmount($money);
|
|
|
|
// THEN
|
|
expect((string) $amount)->toBe((string) $amount->value);
|
|
});
|
|
|
|
it('accepts the minimum amount of 0.01€', function () use ($minimumMoney): void {
|
|
// GIVEN / WHEN
|
|
$amount = new MoneyAmount($minimumMoney);
|
|
|
|
// THEN
|
|
expect($amount->value)->toBe($minimumMoney);
|
|
});
|
|
|
|
it('accepts the maximum amount of 3.00€', function () use ($money): void {
|
|
// GIVEN / WHEN
|
|
$amount = new MoneyAmount($money);
|
|
|
|
// THEN
|
|
expect($amount->value)->toBe($money);
|
|
});
|
|
|
|
it('throws when the amount is zero', function () use ($zeroMoney): void {
|
|
// GIVEN / WHEN / THEN
|
|
expect(fn (): MoneyAmount => new MoneyAmount($zeroMoney))
|
|
->toThrow(\DomainException::class, 'Money amount must be between 0.01€ and 3.00€');
|
|
});
|
|
|
|
it('throws when the amount is above 3.00€', function () use ($aboveLimitMoney): void {
|
|
// GIVEN / WHEN / THEN
|
|
expect(fn (): MoneyAmount => new MoneyAmount($aboveLimitMoney))
|
|
->toThrow(\DomainException::class, 'Money amount must be between 0.01€ and 3.00€');
|
|
});
|
|
});
|