<?php
|
declare(strict_types=1);
|
|
namespace App\Tests\Functional;
|
|
use App\Tests\DatabaseTestCase;
|
|
final class SearchTest extends DatabaseTestCase {
|
protected function setUp(): void {
|
parent::setUp();
|
|
$this->createBook(1, 'Łódź nocą', 'Agatha Christie');
|
$this->createBook(2, 'Rabat 100%', 'Stanisław Lem');
|
$this->createBook(3, 'Solaris', 'Stanisław Lem')->setIsbn('9788373843455')->setPublisher('Wydawnictwo Literackie');
|
$this->entityManager()->flush();
|
}
|
|
/**
|
* @return iterable<string, array{string, list<int>}>
|
*/
|
public static function quickQueries(): iterable {
|
yield 'non-ASCII title' => ['Łódź', [1]];
|
yield 'several books' => ['Stanisław', [2, 3]];
|
yield 'words may hit different columns' => ['lem solaris', [3]];
|
yield 'publisher' => ['literackie', [3]];
|
yield 'ISBN with hyphens' => ['978-83-7384-345-5', [3]];
|
yield 'ISBN-10 form of a stored ISBN-13' => ['83-7384-345-0', [3]];
|
yield 'ID' => ['2', [2]];
|
yield 'percent sign is literal' => ['100%', [2]];
|
yield 'underscore is literal' => ['S_laris', []];
|
yield 'no match' => ['nothing', []];
|
}
|
|
/**
|
* @param list<int> $expectedIds
|
*/
|
#[\PHPUnit\Framework\Attributes\DataProvider('quickQueries')]
|
public function testQuickSearch(string $query, array $expectedIds): void {
|
$crawler = $this->client->request('GET', '/search', ['q' => $query]);
|
|
self::assertResponseIsSuccessful();
|
$ids = $crawler->filter('.tile .label-primary')->each(static fn($node): int => (int)$node->text());
|
self::assertSame($expectedIds, $ids);
|
}
|
|
public function testFieldsAreCombined(): void {
|
$crawler = $this->client->request('GET', '/search', ['author' => 'lem', 'title' => 'SOLARIS']);
|
|
self::assertCount(1, $crawler->filter('.tile'));
|
}
|
|
public function testFieldsDoNotMatchOtherColumns(): void {
|
$crawler = $this->client->request('GET', '/search', ['title' => 'lem']);
|
|
self::assertCount(0, $crawler->filter('.tile'));
|
self::assertSelectorExists('.empty');
|
}
|
|
public function testSearchPageWithoutCriteriaShowsNoResults(): void {
|
$crawler = $this->client->request('GET', '/search');
|
|
self::assertResponseIsSuccessful();
|
self::assertCount(0, $crawler->filter('.tile'));
|
}
|
}
|