ldc-mesbr-app/tests/Unit/Domain/Models/ValueObjects/PrintRulesTest.php
2026-10-15 15:39:34 +02:00

98 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects;
use Domain\Models\ValueObjects\PrintRules;
covers(PrintRules::class);
describe('print rules', function (): void {
$maxPerCustomer = 3;
$minMaxPerCustomer = 1;
$zeroMaxPerCustomer = 0;
$cooldownDays = 30;
$minCooldownDays = 1;
$zeroCooldownDays = 0;
it('rejects print rules with both fields set', function () use ($maxPerCustomer, $cooldownDays): void {
// GIVEN / WHEN
$printRules = fn (): PrintRules => new PrintRules(
maxPerCustomer: $maxPerCustomer,
cooldownDays: $cooldownDays
);
// THEN
expect($printRules)
->toThrow(\DomainException::class, 'No more than one print rule.');
});
it('creates print rules with only maxPerCustomer set', function () use ($maxPerCustomer): void {
// GIVEN / WHEN
$printRules = new PrintRules(
maxPerCustomer: $maxPerCustomer
);
// THEN
expect($printRules->maxPerCustomer)->toBe($maxPerCustomer)
->and($printRules->cooldownDays)->toBeNull();
});
it('creates print rules with only cooldownDays set', function () use ($cooldownDays): void {
// GIVEN / WHEN
$printRules = new PrintRules(
cooldownDays: $cooldownDays
);
// THEN
expect($printRules->maxPerCustomer)->toBeNull()
->and($printRules->cooldownDays)->toBe($cooldownDays);
});
it('rejects print rules without any field set', function (): void {
// WHEN / THEN
expect(fn (): PrintRules => new PrintRules())
->toThrow(\DomainException::class, 'PrintRules require at least a max per customer or a cooldown days.');
});
it('rejects a maxPerCustomer equal to zero', function () use ($zeroMaxPerCustomer): void {
// GIVEN / WHEN / THEN
expect(fn (): PrintRules => new PrintRules(
maxPerCustomer: $zeroMaxPerCustomer
))->toThrow(\DomainException::class, 'Max per customer must be greater than 0.');
});
it('rejects a cooldownDays equal to zero', function () use ($zeroCooldownDays): void {
// GIVEN / WHEN / THEN
expect(fn (): PrintRules => new PrintRules(
cooldownDays: $zeroCooldownDays
))->toThrow(\DomainException::class, 'Cooldown days must be greater than 30.');
});
it('rejects print rule when max per customer limit has reached', function () use ($maxPerCustomer, $minMaxPerCustomer): void {
// GIVEN
$printRules = new PrintRules(
maxPerCustomer: $minMaxPerCustomer
);
// WHEN
$action = fn () => $printRules->hasPrintLimitNotReached($maxPerCustomer);
// THEN
expect($action)->toThrow(\DomainException::class, 'Maximum number of prints reached.');
});
it('rejects print rule when cooldown days period has expired', function () use ($cooldownDays, $minCooldownDays): void {
// GIVEN
$printRules = new PrintRules(
cooldownDays: $cooldownDays
);
// WHEN
$action = fn () => $printRules->hasReprintPeriodExpired($minCooldownDays);
// THEN
expect($action)->toThrow(\DomainException::class, 'Reprint period has not expired.');
});
});