feat: ajout d'une route GetMangaByIdHandler.php et fix de la SearchBar.vue
This commit is contained in:
parent
ed0a075a6c
commit
d9e935f7de
@@ -148,4 +148,35 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
$this->chapters = [];
|
||||
$this->savedChapters = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function search(string $query, int $page = 1, int $limit = 20): array
|
||||
{
|
||||
$filteredMangas = array_filter($this->mangas, function (Manga $manga) use ($query) {
|
||||
$searchableFields = [
|
||||
$manga->getTitle()->getValue(),
|
||||
$manga->getSlug()->getValue(),
|
||||
$manga->getAuthor(),
|
||||
$manga->getDescription()
|
||||
];
|
||||
|
||||
foreach ($searchableFields as $field) {
|
||||
if (str_contains(strtolower($field), strtolower($query))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
$sortedMangas = array_values($filteredMangas);
|
||||
usort($sortedMangas, fn (Manga $a, Manga $b) => $a->getTitle()->getValue() <=> $b->getTitle()->getValue());
|
||||
|
||||
$offset = ($page - 1) * $limit;
|
||||
return array_slice($sortedMangas, $offset, $limit);
|
||||
}
|
||||
|
||||
public function countSearch(string $query): int
|
||||
{
|
||||
return count($this->search($query, 1, PHP_INT_MAX));
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ class GetMangaTest extends AbstractApiTestCase
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('GET', '/api/mangas/999');
|
||||
$response = $client->request('GET', '/api/mangas/by-id/999');
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(404);
|
||||
@@ -42,7 +42,7 @@ class GetMangaTest extends AbstractApiTestCase
|
||||
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('GET', '/api/mangas/' . $manga->getId());
|
||||
$response = $client->request('GET', '/api/mangas/by-id/' . $manga->getId());
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
139
tests/Feature/Manga/SearchMangaTest.php
Normal file
139
tests/Feature/Manga/SearchMangaTest.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature\Manga;
|
||||
|
||||
use App\Entity\Manga;
|
||||
use App\Tests\Feature\AbstractApiTestCase;
|
||||
use Zenstruck\Foundry\Test\ResetDatabase;
|
||||
|
||||
class SearchMangaTest extends AbstractApiTestCase
|
||||
{
|
||||
use ResetDatabase;
|
||||
|
||||
public function testSearchMangaWithEmptyQuery(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('GET', '/api/mangas/search', [
|
||||
'query' => [
|
||||
'q' => ''
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(400);
|
||||
$this->assertJsonContains([
|
||||
'hydra:title' => 'An error occurred',
|
||||
'hydra:description' => 'Le terme de recherche doit contenir au moins 3 caractères'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testSearchMangaWithShortQuery(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('GET', '/api/mangas/search', [
|
||||
'query' => [
|
||||
'q' => 'on'
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(400);
|
||||
$this->assertJsonContains([
|
||||
'hydra:title' => 'An error occurred',
|
||||
'hydra:description' => 'Le terme de recherche doit contenir au moins 3 caractères'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testSearchMangaByTitle(): void
|
||||
{
|
||||
// Given
|
||||
$this->createManga('One Piece', 'one-piece');
|
||||
$this->createManga('Dragon Ball', 'dragon-ball');
|
||||
$this->createManga('One Punch Man', 'one-punch-man');
|
||||
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('GET', '/api/mangas/search', [
|
||||
'query' => [
|
||||
'q' => 'one'
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
$data = $response->toArray();
|
||||
|
||||
$this->assertCount(2, $data['items']);
|
||||
$titles = array_map(fn($item) => $item['title'], $data['items']);
|
||||
$this->assertContains('One Piece', $titles);
|
||||
$this->assertContains('One Punch Man', $titles);
|
||||
}
|
||||
|
||||
public function testSearchMangaBySlug(): void
|
||||
{
|
||||
// Given
|
||||
$this->createManga('One Piece', 'one-piece');
|
||||
$this->createManga('Dragon Ball', 'dragon-ball');
|
||||
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('GET', '/api/mangas/search', [
|
||||
'query' => [
|
||||
'q' => 'dragon'
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
$data = $response->toArray();
|
||||
|
||||
$this->assertCount(1, $data['items']);
|
||||
$this->assertEquals('Dragon Ball', $data['items'][0]['title']);
|
||||
$this->assertEquals('dragon-ball', $data['items'][0]['slug']);
|
||||
}
|
||||
|
||||
// public function testSearchMangaWithPagination(): void
|
||||
// {
|
||||
// // Given
|
||||
// $this->createManga('One Piece', 'one-piece');
|
||||
// $this->createManga('One Punch Man', 'one-punch-man');
|
||||
// $this->createManga('One Outs', 'one-outs');
|
||||
|
||||
// // When
|
||||
// $client = static::createClient();
|
||||
// $response = $client->request('GET', '/api/mangas/search', [
|
||||
// 'query' => [
|
||||
// 'q' => 'one',
|
||||
// 'page' => 2,
|
||||
// 'limit' => 1
|
||||
// ]
|
||||
// ]);
|
||||
|
||||
// // Then
|
||||
// $this->assertResponseIsSuccessful();
|
||||
// $data = $response->toArray();
|
||||
|
||||
// $this->assertCount(1, $data['items']);
|
||||
// $this->assertTrue($data['hasNextPage']);
|
||||
// $this->assertTrue($data['hasPreviousPage']);
|
||||
// }
|
||||
|
||||
private function createManga(string $title, string $slug): void
|
||||
{
|
||||
$manga = new Manga();
|
||||
$manga->setTitle($title)
|
||||
->setSlug($slug)
|
||||
->setDescription('Description test')
|
||||
->setAuthor('Author test')
|
||||
->setPublicationYear(2020)
|
||||
->setGenres(['action'])
|
||||
->setStatus('ongoing')
|
||||
->setRating(4.5)
|
||||
->setMonitored(false);
|
||||
|
||||
$this->entityManager->persist($manga);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user