ldc-cocotte/tests/Feature/App/Providers/DateMacrosTest.php

41 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Date;
describe('AppServiceProvider Date macros', function (): void {
it('uses valid client_timezone from request', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->app->instance('request', Request::create('/', 'GET', ['client_timezone' => 'America/New_York']));
$utcDate = new \DateTimeImmutable('2026-06-15 14:00:00', new \DateTimeZone('UTC'));
// WHEN
$localInput = Date::toLocalInput($utcDate);
$forHumans = Date::forHumans($utcDate);
// THEN
expect($localInput)->toBe('2026-06-15T10:00')
->and($forHumans)->toContain('10:00');
});
it('falls back to Europe/Paris when client_timezone is invalid or empty', function (string $invalidTimezone): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->app->instance('request', Request::create('/', 'GET', ['client_timezone' => $invalidTimezone]));
$utcDate = new \DateTimeImmutable('2026-06-15 14:00:00', new \DateTimeZone('UTC'));
// WHEN
$localInput = Date::toLocalInput($utcDate);
$forHumans = Date::forHumans($utcDate);
// THEN
expect($localInput)->toBe('2026-06-15T16:00')
->and($forHumans)->toContain('16:00');
})->with([
'invalid string' => ['invalid'],
'empty string' => [''],
]);
});