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
<?php
declare(strict_types=1);
 
namespace App\Tests\Functional;
 
use App\Tests\DatabaseTestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\Component\HttpClient\Response\MockResponse;
 
final class LookupTest extends DatabaseTestCase {
    public function testCandidatePrefillsTheAddForm(): void {
        $this->mockCatalogue(new JsonMockResponse(['bibs' => [[
            'title' => 'Trzynaście zagadek / Thirteen problems, Seria z Gawronem.',
            'author' => 'Christie, Agatha (1890-1976) Weiss, Maria Wydawnictwo Dolnośląskie',
            'publisher' => 'Da Capo Da Capo,',
            'placeOfPublication' => 'Wrocław : Polska',
            'publicationYear' => '1994',
            'language' => 'polski',
            'isbnIssn' => '83-7023-382-1',
        ]]]));
 
        $crawler = $this->client->request('GET', '/add/lookup', ['id' => '5', 'isn' => '8370233821']);
        self::assertResponseIsSuccessful();
 
        $crawler = $this->client->click($crawler->selectLink('Use')->link());
        $form = $crawler->selectButton('Save')->form();
 
        self::assertSame('5', $form['book[id]']->getValue());
        self::assertSame('Trzynaście zagadek / Thirteen problems, Seria z Gawronem', $form['book[title]']->getValue());
        self::assertSame('Agatha Christie', $form['book[author]']->getValue());
        self::assertSame('Da Capo', $form['book[publisher]']->getValue());
        self::assertSame('Wrocław', $form['book[publicationLoc]']->getValue());
        self::assertSame('1994', $form['book[publicationYear]']->getValue());
        self::assertSame('9788370233822', $form['book[isbn]']->getValue());
    }
 
    public function testNoResultsOffersManualEntry(): void {
        $this->mockCatalogue(new JsonMockResponse(['bibs' => []]));
 
        $this->client->request('GET', '/add/lookup', ['title' => 'Nothing']);
 
        self::assertResponseIsSuccessful();
        self::assertSelectorTextContains('.page_content', 'no matching books');
    }
 
    public function testCatalogueFailureFallsBackToTheAddPage(): void {
        $this->mockCatalogue(new MockResponse('', ['http_code' => 503]));
 
        $this->client->request('GET', '/add/lookup', ['isn' => '8370233821']);
 
        self::assertResponseRedirects('/add');
        $this->client->followRedirect();
        self::assertSelectorTextContains('.flash', 'catalogue is unavailable');
    }
 
    private function mockCatalogue(MockResponse $response): void {
        static::getContainer()->set('bn.client', new MockHttpClient($response, 'https://data.bn.org.pl'));
    }
}