Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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'));
    }
}