return utc from clock

This commit is contained in:
Yisroel Baum 2026-04-26 10:32:08 +03:00
parent 2fe41a5fe7
commit 13da7c311a
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
5 changed files with 26 additions and 9 deletions

View file

@ -6,5 +6,8 @@ use DateTimeImmutable;
interface Clock
{
/**
* Returns the current time in UTC.
*/
public function now(): DateTimeImmutable;
}

View file

@ -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'));
}
}

View file

@ -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.'
);
}
}
}

View file

@ -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();

View file

@ -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(