Goal-Calibration/tests/Fakes/FakeTextRepository.php

66 lines
1.3 KiB
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);
}
/**
* @return Text[]
*/
public function getAll(): array
{
return array_map(
function (Text $text) {
return new Text(
id: $text->getId(),
name: $text->getName(),
);
},
$this->existingTexts
);
}
}