include the user when rebuilding Text instances in find and getAll, preserving the rule that lookup methods return new instances rather than stored references.
64 lines
1.3 KiB
PHP
64 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,
|
|
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)
|
|
);
|
|
}
|
|
}
|