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

68 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Providers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Date;
use Webmozart\Assert\Assert;
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
/** @phpstan-ignore staticMethod.notFound */
$localInput = Date::toLocalInput($utcDate);
/** @phpstan-ignore staticMethod.notFound */
$forHumans = Date::forHumans($utcDate);
Assert::string($localInput);
Assert::string($forHumans);
// 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
/** @phpstan-ignore staticMethod.notFound */
$localInput = Date::toLocalInput($utcDate);
/** @phpstan-ignore staticMethod.notFound */
$forHumans = Date::forHumans($utcDate);
Assert::string($localInput);
Assert::string($forHumans);
// 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
/** @phpstan-ignore staticMethod.notFound */
$formatted = Date::forHumansDate($utcMidnight);
Assert::string($formatted);
// THEN
expect($formatted)->toContain('08/05/2026')
->and($formatted)->not->toContain('à');
});
});