64 lines
2.6 KiB
PHP
64 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Casts;
|
|
|
|
use App\Casts\UtcDatetimeCast;
|
|
use App\Models\Discount;
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Enums\DiscountType;
|
|
use Domain\Models\ValueObjects\DiscountValue;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Money\Money;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
describe('UtcDatetimeCast', function (): void {
|
|
it('converts a non-UTC DateTimeInterface to UTC when persisted and reads it back in UTC', function (): void {
|
|
// GIVEN
|
|
$parisStart = Date::parse('2026-05-04 16:00:00', 'Europe/Paris');
|
|
$parisEnd = Date::parse('2026-11-02 15:30:00', 'Europe/Paris');
|
|
|
|
$discount = new Discount();
|
|
$discount->type = DiscountType::AMOUNT;
|
|
$discount->value = DiscountValue::fromAmount(Money::EUR(100));
|
|
$discount->is_old_price_displayed = true;
|
|
$discount->date_start = $parisStart;
|
|
$discount->date_end = $parisEnd;
|
|
|
|
// WHEN
|
|
$discount->saveOrFail();
|
|
|
|
$rawRow = DB::table('discounts')->where('id', $discount->id)->first();
|
|
$reloaded = Discount::query()->findOrFail($discount->id);
|
|
|
|
// THEN
|
|
expect($rawRow?->date_start)->toBe('2026-05-04 14:00:00')
|
|
->and($rawRow?->date_end)->toBe('2026-11-02 14:30:00')
|
|
->and($reloaded->date_start?->timezoneName)->toBe('UTC')
|
|
->and($reloaded->date_start?->format('Y-m-d H:i:s'))->toBe('2026-05-04 14:00:00')
|
|
->and($reloaded->date_end?->format('Y-m-d H:i:s'))->toBe('2026-11-02 14:30:00');
|
|
});
|
|
|
|
it('normalizes offset-aware ISO strings and CarbonImmutable to UTC and preserves null values', function (): void {
|
|
// GIVEN
|
|
$cast = new UtcDatetimeCast();
|
|
$model = new Discount();
|
|
$parisImmutable = CarbonImmutable::parse('2026-05-04 16:00:00', 'Europe/Paris');
|
|
|
|
// WHEN
|
|
$storedImmutable = $cast->set($model, 'date_start', $parisImmutable, []);
|
|
$storedIso = $cast->set($model, 'date_start', '2026-07-10T10:15:00+02:00', []);
|
|
$storedNull = $cast->set($model, 'date_start', null, []);
|
|
$readNull = $cast->get($model, 'date_start', null, []);
|
|
|
|
// THEN
|
|
expect($storedImmutable)->toBe('2026-05-04 14:00:00')
|
|
->and($storedIso)->toBe('2026-07-10 08:15:00')
|
|
->and($storedNull)->toBeNull()
|
|
->and($readNull)->toBeNull();
|
|
});
|
|
});
|