ldc-cocotte/tests/Integration/WithApp/App/Casts/CalendarDateCastTest.php

54 lines
1.9 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Casts;
use App\Casts\CalendarDateCast;
use App\Models\PickupException;
use Carbon\CarbonImmutable;
use Domain\Enums\RepeatFrequency;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
describe('CalendarDateCast', function (): void {
it('stores a calendar date without shifting Paris midnight back to the previous UTC day', function (): void {
// GIVEN
$parisMidnight = Date::parse('2026-05-08 00:00:00', 'Europe/Paris');
$exception = new PickupException();
$exception->date = $parisMidnight;
$exception->label = 'Victoire 1945';
$exception->repeatable = true;
$exception->repeat_frequency = RepeatFrequency::YEARLY;
$exception->setAttribute('auto_updated', true);
// WHEN
$exception->saveOrFail();
$rawDate = DB::table('pickup_exceptions')->where('id', $exception->id)->value('date');
$reloaded = PickupException::query()->findOrFail($exception->id);
// THEN
expect($rawDate)->toBe('2026-05-08')
->and($reloaded->date->format('Y-m-d'))->toBe('2026-05-08')
->and($reloaded->date->timezoneName)->toBe('UTC');
});
it('preserves calendar date for CarbonImmutable midnight and returns startOfDay UTC', function (): void {
// GIVEN
$cast = new CalendarDateCast();
$model = new PickupException();
$parisImmutable = CarbonImmutable::parse('2026-05-08 00:00:00', 'Europe/Paris');
// WHEN
$storedDate = $cast->set($model, 'date', $parisImmutable, []);
$readDate = $cast->get($model, 'date', '2026-05-08', []);
// THEN
expect($storedDate)->toBe('2026-05-08')
->and($readDate?->format('Y-m-d H:i:s'))->toBe('2026-05-08 00:00:00')
->and($readDate?->timezoneName)->toBe('UTC');
});
});