89 lines
2.9 KiB
PHP
89 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Domain\Models\ValueObjects\DatePeriod;
|
|
|
|
describe('DatePeriod', function (): void {
|
|
it('creates a period with null dates', function (): void {
|
|
// GIVEN
|
|
$period = new DatePeriod();
|
|
|
|
// WHEN
|
|
$isActive = $period->isActiveAt();
|
|
|
|
// THEN
|
|
expect($period->start)->toBeNull()
|
|
->and($period->end)->toBeNull()
|
|
->and($isActive)->toBeTrue();
|
|
});
|
|
|
|
it('creates a period with start and end dates', function (): void {
|
|
// GIVEN
|
|
$start = new \DateTimeImmutable('2026-01-01 00:00:00');
|
|
$end = new \DateTimeImmutable('2026-12-31 23:59:59');
|
|
|
|
// WHEN
|
|
$period = new DatePeriod($start, $end);
|
|
|
|
// THEN
|
|
expect($period->start)->toEqual($start)
|
|
->and($period->end)->toEqual($end);
|
|
});
|
|
|
|
it('throws when start date is after end date', function (): void {
|
|
// GIVEN
|
|
$start = new \DateTimeImmutable('2026-12-31 23:59:59');
|
|
$end = new \DateTimeImmutable('2026-01-01 00:00:00');
|
|
|
|
// WHEN
|
|
expect(fn () => new DatePeriod($start, $end))
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class, 'The start date must be before or equal to the end date.');
|
|
});
|
|
|
|
it('evaluates isActiveAt correctly', function (): void {
|
|
// GIVEN
|
|
$start = new \DateTimeImmutable('2026-06-01 00:00:00');
|
|
$end = new \DateTimeImmutable('2026-06-30 23:59:59');
|
|
$period = new DatePeriod($start, $end);
|
|
|
|
// WHEN
|
|
$before = $period->isActiveAt(new \DateTimeImmutable('2026-05-31 23:59:59'));
|
|
$during = $period->isActiveAt(new \DateTimeImmutable('2026-06-15 12:00:00'));
|
|
$after = $period->isActiveAt(new \DateTimeImmutable('2026-07-01 00:00:00'));
|
|
|
|
// THEN
|
|
expect($before)->toBeFalse()
|
|
->and($during)->toBeTrue()
|
|
->and($after)->toBeFalse();
|
|
});
|
|
|
|
it('evaluates isActiveAt with only start date', function (): void {
|
|
// GIVEN
|
|
$start = new \DateTimeImmutable('2026-06-01 00:00:00');
|
|
$period = new DatePeriod(start: $start);
|
|
|
|
// WHEN
|
|
$before = $period->isActiveAt(new \DateTimeImmutable('2026-05-31 23:59:59'));
|
|
$during = $period->isActiveAt(new \DateTimeImmutable('2026-06-15 12:00:00'));
|
|
|
|
// THEN
|
|
expect($before)->toBeFalse()
|
|
->and($during)->toBeTrue();
|
|
});
|
|
|
|
it('evaluates isActiveAt with only end date', function (): void {
|
|
// GIVEN
|
|
$end = new \DateTimeImmutable('2026-06-30 23:59:59');
|
|
$period = new DatePeriod(end: $end);
|
|
|
|
// WHEN
|
|
$during = $period->isActiveAt(new \DateTimeImmutable('2026-06-15 12:00:00'));
|
|
$after = $period->isActiveAt(new \DateTimeImmutable('2026-07-01 00:00:00'));
|
|
|
|
// THEN
|
|
expect($during)->toBeTrue()
|
|
->and($after)->toBeFalse();
|
|
});
|
|
});
|