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

55 lines
2 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' => [''],
]);
it('formats calendar dates without shifting day or adding hours in Date::forHumansDate', function (): void {
// GIVEN
app()->instance('request', Request::create('/', 'GET', ['client_timezone' => 'America/New_York']));
$utcMidnight = Date::createFromFormat('!Y-m-d', '2026-05-08', 'UTC');
// WHEN
$formatted = Date::forHumansDate($utcMidnight);
// THEN
expect($formatted)->toBeString()
->and($formatted)->toContain('08/05/2026')
->and($formatted)->not->toContain('à');
});
});