ldc-cocotte/tests/Unit/Shared/Helpers/DatesHelperTest.php

112 lines
4 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Unit\Shared\Helpers;
use Carbon\CarbonImmutable;
use Shared\Helpers\DatesHelper;
covers(DatesHelper::class);
beforeEach(function (): void {
CarbonImmutable::setTestNow(CarbonImmutable::parse('2026-06-15 10:00:00', 'UTC'));
});
afterEach(function (): void {
CarbonImmutable::setTestNow();
});
it('throws LogicException when instantiated', function (): void {
// GIVEN / WHEN / THEN
expect(static fn (): DatesHelper => new DatesHelper())
->toThrow(\LogicException::class, 'Static class');
});
it('returns true for isIn when both bounds are null', function (): void {
// GIVEN
$from = null;
$to = null;
// WHEN
$result = DatesHelper::isIn($from, $to);
// THEN
expect($result)->toBeTrue();
});
it('checks bounded isIn intervals at before, inside, and after edges', function (): void {
// GIVEN
$from = CarbonImmutable::parse('2026-06-15 09:00:00', 'UTC');
$to = CarbonImmutable::parse('2026-06-15 11:00:00', 'UTC');
// WHEN / THEN (inside)
expect(DatesHelper::isIn($from, $to))->toBeTrue();
// WHEN / THEN (at exact lower bound)
expect(DatesHelper::isIn(CarbonImmutable::parse('2026-06-15 10:00:00', 'UTC'), $to))->toBeTrue();
// WHEN / THEN (at exact upper bound)
expect(DatesHelper::isIn($from, CarbonImmutable::parse('2026-06-15 10:00:00', 'UTC')))->toBeTrue();
// WHEN / THEN (before interval)
$futureFrom = CarbonImmutable::parse('2026-06-15 10:00:01', 'UTC');
$futureTo = CarbonImmutable::parse('2026-06-15 12:00:00', 'UTC');
expect(DatesHelper::isIn($futureFrom, $futureTo))->toBeFalse();
// WHEN / THEN (after interval)
$pastFrom = CarbonImmutable::parse('2026-06-15 08:00:00', 'UTC');
$pastTo = CarbonImmutable::parse('2026-06-15 09:59:59', 'UTC');
expect(DatesHelper::isIn($pastFrom, $pastTo))->toBeFalse();
});
it('checks open-ended isIn intervals with only from or only to', function (): void {
// GIVEN
$past = CarbonImmutable::parse('2026-06-15 09:00:00', 'UTC');
$future = CarbonImmutable::parse('2026-06-15 11:00:00', 'UTC');
// WHEN / THEN
expect(DatesHelper::isIn($past, null))->toBeTrue()
->and(DatesHelper::isIn($future, null))->toBeFalse()
->and(DatesHelper::isIn(null, $future))->toBeTrue()
->and(DatesHelper::isIn(null, $past))->toBeFalse();
});
it('parses trimmed Y-m-d, RFC3339, fractional RFC3339, ISO8601, and fallback strings', function (): void {
// GIVEN / WHEN
$ymd = DatesHelper::parseDateTime(' 2026-06-15 ');
$rfc3339 = DatesHelper::parseDateTime('2026-06-15T10:30:00+02:00');
$fractional = DatesHelper::parseDateTime('2026-06-15T10:30:00.123456+02:00');
$iso8601 = DatesHelper::parseDateTime('2026-06-15T10:30:00Z');
$fallback = DatesHelper::parseDateTime('2026-06-15 14:00:00');
// THEN
expect($ymd->format('Y-m-d H:i:s'))->toBe('2026-06-15 00:00:00')
->and($rfc3339->format(\DATE_RFC3339))->toBe('2026-06-15T10:30:00+02:00')
->and($fractional->format('Y-m-d\TH:i:s.uP'))->toBe('2026-06-15T10:30:00.123456+02:00')
->and($iso8601->format('Y-m-d\TH:i:s\Z'))->toBe('2026-06-15T10:30:00Z')
->and($fallback->format('Y-m-d H:i:s'))->toBe('2026-06-15 14:00:00');
});
it('throws InvalidArgumentException when parsing an invalid datetime string', function (): void {
// GIVEN
$invalid = 'not-a-valid-date';
// WHEN / THEN
expect(static fn (): \DateTimeInterface => DatesHelper::parseDateTime($invalid))
->toThrow(\InvalidArgumentException::class, 'Unrecognized date/time format: not-a-valid-date');
});
it('formats both string and DateTimeImmutable to RFC3339', function (): void {
// GIVEN
$dateTime = new \DateTimeImmutable('2026-06-15T10:30:00+02:00');
// WHEN
$fromString = DatesHelper::toRfc3339('2026-06-15T10:30:00+02:00');
$fromInstance = DatesHelper::toRfc3339($dateTime);
// THEN
expect($fromString)->toBe('2026-06-15T10:30:00+02:00')
->and($fromInstance)->toBe('2026-06-15T10:30:00+02:00');
});