Goal-Calibration/tests/Fakes/FakeSessionRepository.php

48 lines
1.1 KiB
PHP

<?php
namespace Tests\Fakes;
use App\Auth\CreateSessionDto;
use App\Auth\Session;
use App\Auth\SessionRepository;
class FakeSessionRepository implements SessionRepository
{
/**
* @var Session[]
*/
private array $existingSessions = [];
public function create(CreateSessionDto $dto): Session
{
$session = new Session(
token: $dto->token,
userId: $dto->userId,
createdAt: $dto->createdAt,
expiresAt: $dto->expiresAt,
);
$this->existingSessions[$dto->token] = $session;
return $session;
}
public function findByToken(string $token): ?Session
{
$session = $this->existingSessions[$token] ?? null;
if ($session === null) {
return null;
}
return new Session(
token: $session->getToken(),
userId: $session->getUserId(),
createdAt: $session->getCreatedAt(),
expiresAt: $session->getExpiresAt(),
);
}
public function deleteByToken(string $token): void
{
unset($this->existingSessions[$token]);
}
}