From 13da7c311aa6f78915f5b1326fb23db8b539f64a Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Sun, 26 Apr 2026 10:32:08 +0300 Subject: [PATCH] return utc from clock --- app/Auth/Clock.php | 3 +++ app/Auth/SystemClock.php | 3 ++- tests/Fakes/FakeClock.php | 15 ++++++++++++++- tests/Unit/Auth/Middleware/AuthMiddlewareTest.php | 10 +++++----- tests/e2e/Controllers/AuthControllerTest.php | 4 ++-- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/Auth/Clock.php b/app/Auth/Clock.php index b96ad32..ad89957 100644 --- a/app/Auth/Clock.php +++ b/app/Auth/Clock.php @@ -6,5 +6,8 @@ use DateTimeImmutable; interface Clock { + /** + * Returns the current time in UTC. + */ public function now(): DateTimeImmutable; } diff --git a/app/Auth/SystemClock.php b/app/Auth/SystemClock.php index 4e845b6..dd77c28 100644 --- a/app/Auth/SystemClock.php +++ b/app/Auth/SystemClock.php @@ -3,11 +3,12 @@ namespace App\Auth; use DateTimeImmutable; +use DateTimeZone; class SystemClock implements Clock { public function now(): DateTimeImmutable { - return new DateTimeImmutable(); + return new DateTimeImmutable('now', new DateTimeZone('UTC')); } } diff --git a/tests/Fakes/FakeClock.php b/tests/Fakes/FakeClock.php index bf7a7b6..31e649b 100644 --- a/tests/Fakes/FakeClock.php +++ b/tests/Fakes/FakeClock.php @@ -4,12 +4,15 @@ namespace Tests\Fakes; use App\Auth\Clock; use DateTimeImmutable; +use InvalidArgumentException; class FakeClock implements Clock { public function __construct( private DateTimeImmutable $currentTime, - ) {} + ) { + $this->assertUtc($currentTime); + } public function now(): DateTimeImmutable { @@ -18,6 +21,16 @@ class FakeClock implements Clock public function setTime(DateTimeImmutable $newTime): void { + $this->assertUtc($newTime); $this->currentTime = $newTime; } + + private function assertUtc(DateTimeImmutable $time): void + { + if ($time->getTimezone()->getOffset($time) !== 0) { + throw new InvalidArgumentException( + 'FakeClock requires a DateTimeImmutable in UTC.' + ); + } + } } diff --git a/tests/Unit/Auth/Middleware/AuthMiddlewareTest.php b/tests/Unit/Auth/Middleware/AuthMiddlewareTest.php index 5953a13..8408384 100644 --- a/tests/Unit/Auth/Middleware/AuthMiddlewareTest.php +++ b/tests/Unit/Auth/Middleware/AuthMiddlewareTest.php @@ -72,7 +72,7 @@ class AuthMiddlewareTest extends TestCase private function makeHandler(): RequestHandlerInterface { - return new class() implements RequestHandlerInterface { + return new class implements RequestHandlerInterface { public ?ServerRequestInterface $capturedRequest = null; public function handle( @@ -113,8 +113,8 @@ class AuthMiddlewareTest extends TestCase $this->sessionRepo->create(new CreateSessionDto( token: 'expired-token', userId: $this->user->getId(), - createdAt: new DateTimeImmutable('2024-12-01'), - expiresAt: new DateTimeImmutable('2024-12-08'), + createdAt: new DateTimeImmutable('2024-12-01T00:00:00+00:00'), + expiresAt: new DateTimeImmutable('2024-12-08T00:00:00+00:00'), )); $response = $this->middleware->process( @@ -130,8 +130,8 @@ class AuthMiddlewareTest extends TestCase $this->sessionRepo->create(new CreateSessionDto( token: 'valid-token', userId: $this->user->getId(), - createdAt: new DateTimeImmutable('2025-01-01'), - expiresAt: new DateTimeImmutable('2025-01-08'), + createdAt: new DateTimeImmutable('2025-01-01T00:00:00+00:00'), + expiresAt: new DateTimeImmutable('2025-01-08T00:00:00+00:00'), )); $handler = $this->makeHandler(); diff --git a/tests/e2e/Controllers/AuthControllerTest.php b/tests/e2e/Controllers/AuthControllerTest.php index 58b209d..19c5770 100644 --- a/tests/e2e/Controllers/AuthControllerTest.php +++ b/tests/e2e/Controllers/AuthControllerTest.php @@ -244,8 +244,8 @@ class AuthControllerTest extends TestCase $this->sessionRepo->create(new CreateSessionDto( token: 'existing-session', userId: 0, - createdAt: new DateTimeImmutable('2025-01-01'), - expiresAt: new DateTimeImmutable('2025-01-08'), + createdAt: new DateTimeImmutable('2025-01-01T00:00:00+00:00'), + expiresAt: new DateTimeImmutable('2025-01-08T00:00:00+00:00'), )); $request = $this->makeJsonRequest(