Goal-Calibration/tests/Fakes/FakeTextRepository.php

50 lines
972 B
PHP

<?php
namespace Tests\Fakes;
use App\Text\CreateTextDto;
use App\Text\Text;
use App\Text\TextRepository;
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,
);
$this->existingTexts[$id] = $text;
return $text;
}
public function find(int $id): ?Text
{
$text = array_find(
$this->existingTexts,
function (Text $text) use ($id){
return $text->getId() === $id;
}
);
if ($text === null) {
return null;
}
return new Text(
id: $id,
name: $text->getName(),
);
}
private function nextId(): int
{
return count($this->existingTexts);
}
}