feat: GetMangaList endpoint + tests + test db
This commit is contained in:
parent
073439163b
commit
e3d380eadd
73
tests/Domain/Manga/Adapter/InMemoryMangaRepository.php
Normal file
73
tests/Domain/Manga/Adapter/InMemoryMangaRepository.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Manga\Adapter;
|
||||
|
||||
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
|
||||
use App\Domain\Manga\Domain\Model\Manga;
|
||||
|
||||
class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
{
|
||||
/** @var Manga[] */
|
||||
private array $mangas = [];
|
||||
|
||||
public function findAll(int $page = 1, int $limit = 20, string $sortBy = 'title', string $sortOrder = 'asc'): array
|
||||
{
|
||||
$sortedMangas = $this->mangas;
|
||||
|
||||
usort($sortedMangas, function (Manga $a, Manga $b) use ($sortBy, $sortOrder) {
|
||||
$valueA = $this->getPropertyValue($a, $sortBy);
|
||||
$valueB = $this->getPropertyValue($b, $sortBy);
|
||||
|
||||
return $sortOrder === 'asc'
|
||||
? $valueA <=> $valueB
|
||||
: $valueB <=> $valueA;
|
||||
});
|
||||
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
return array_slice($sortedMangas, $offset, $limit);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->mangas);
|
||||
}
|
||||
|
||||
public function findById(string $id): ?Manga
|
||||
{
|
||||
foreach ($this->mangas as $manga) {
|
||||
if ($manga->getId()->getValue() === $id) {
|
||||
return $manga;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function save(Manga $manga): void
|
||||
{
|
||||
$this->mangas[] = $manga;
|
||||
}
|
||||
|
||||
public function delete(Manga $manga): void
|
||||
{
|
||||
$this->mangas = array_filter(
|
||||
$this->mangas,
|
||||
fn(Manga $existingManga) => !$existingManga->getId()->equals($manga->getId())
|
||||
);
|
||||
}
|
||||
|
||||
private function getPropertyValue(Manga $manga, string $property): mixed
|
||||
{
|
||||
return match($property) {
|
||||
'title' => $manga->getTitle()->getValue(),
|
||||
'publicationYear' => $manga->getPublicationYear(),
|
||||
default => throw new \InvalidArgumentException("Unknown sort property: $property")
|
||||
};
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
$this->mangas = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user