Goal-Calibration/tests/Fakes/FakeTextRepository.php
Yisroel Baum acdf703d80
scope text endpoints by ownership
TextRepository gains findByUser; JsonTextRepository and the
fake implement filtering by stored userId. TextController
splits the list endpoint into getMyTexts (own) and
getAllTexts (admin), and getText now requires the session
user, returning 403 to non-owners while admins bypass.
2026-05-02 21:42:51 +03:00

90 lines
1.9 KiB
PHP

<?php
namespace Tests\Fakes;
use App\Text\CreateTextDto;
use App\Text\Text;
use App\Text\TextRepository;
use App\User\User;
class FakeTextRepository implements TextRepository
{
/**
* @var Text[]
*/
private array $existingTexts = [];
public function create(CreateTextDto $dto): Text
{
$id = $this->nextId();
$text = new Text(
id: $id,
name: $dto->name,
user: $dto->user,
);
$this->existingTexts[$id] = $text;
return $text;
}
public function find(int $id): ?Text
{
if (!isset($this->existingTexts[$id])) {
return null;
}
$text = $this->existingTexts[$id];
return new Text(
id: $text->getId(),
name: $text->getName(),
user: $text->getUser(),
);
}
private function nextId(): int
{
return count($this->existingTexts);
}
/**
* @return Text[]
*/
public function getAll(): array
{
return array_map(
function (Text $text) {
return new Text(
id: $text->getId(),
name: $text->getName(),
user: $text->getUser(),
);
},
array_values($this->existingTexts)
);
}
/**
* @return Text[]
*/
public function findByUser(User $user): array
{
$userId = $user->getId();
$owned = array_filter(
$this->existingTexts,
function (Text $text) use ($userId) {
return $text->getUser()->getId() === $userId;
}
);
return array_map(
function (Text $text) {
return new Text(
id: $text->getId(),
name: $text->getName(),
user: $text->getUser(),
);
},
array_values($owned)
);
}
}