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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
 
namespace App\Tests\Functional;
 
use App\Entity\OustedBook;
use App\Enum\LabelType;
use App\Tests\DatabaseTestCase;
 
final class BookTest extends DatabaseTestCase {
    public function testEmptyListInvitesToAddABook(): void {
        $this->client->request('GET', '/');
 
        self::assertResponseIsSuccessful();
        self::assertSelectorTextContains('.empty-title', 'There are no books');
    }
 
    public function testListIsPaginated(): void {
        for ($id = 1; $id <= 11; ++$id) {
            $this->createBook($id, 'Book ' . $id);
        }
 
        $crawler = $this->client->request('GET', '/list');
        self::assertCount(10, $crawler->filter('.tile'));
 
        $crawler = $this->client->request('GET', '/list?page=2');
        self::assertCount(1, $crawler->filter('.tile'));
    }
 
    public function testBookCanBeAddedWithPrefilledForm(): void {
        $crawler = $this->client->request('GET', '/add/form?id=7&title=Prefilled');
        self::assertResponseIsSuccessful();
 
        $form = $crawler->selectButton('Save')->form();
        self::assertSame('7', $form['book[id]']->getValue());
        self::assertSame('Prefilled', $form['book[title]']->getValue());
 
        $form['book[author]'] = 'Someone';
        $form['book[isbn]'] = '978-83-7384-345-5';
        $this->client->submit($form);
 
        self::assertResponseRedirects('/book/7');
        self::assertSame('9788373843455', $this->findBook(7)?->getIsbn());
    }
 
    public function testIsbnIsStoredAsATypedLabelAndCanBeClearedInTheForm(): void {
        $this->createBook(1)->setIsbn('9788373843455');
        $this->entityManager()->flush();
 
        $book = $this->findBook(1);
        self::assertSame(LabelType::ISBN, $book?->getLabels()->first()->getType());
 
        $crawler = $this->client->request('GET', '/book/1/edit');
        $form = $crawler->selectButton('Save')->form();
        $form['book[isbn]'] = '';
        $this->client->submit($form);
 
        self::assertCount(0, $this->findBook(1)?->getLabels() ?? []);
    }
 
    public function testIsbn10IsSavedAsIsbn13(): void {
        $crawler = $this->client->request('GET', '/add/form');
        $this->client->submit($crawler->selectButton('Save')->form([
            'book[id]' => '1',
            'book[title]' => 'Title',
            'book[author]' => 'Author',
            'book[isbn]' => '0-306-40615-2',
        ]));
 
        self::assertResponseRedirects('/book/1');
        self::assertSame('9780306406157', $this->findBook(1)?->getIsbn());
    }
 
    public function testMalformedIsbnCannotReachTheDatabase(): void {
        $book = new \App\Entity\Book()->setId(1)->setTitle('Title')->setAuthor('Author')->setIsbn('123');
 
        $this->expectException(\DomainException::class);
        $this->entityManager()->persist($book);
    }
 
    public function testInvalidIsbnIsRejected(): void {
        $crawler = $this->client->request('GET', '/add/form');
        $this->client->submit($crawler->selectButton('Save')->form([
            'book[id]' => '1',
            'book[title]' => 'Title',
            'book[author]' => 'Author',
            'book[isbn]' => 'abc',
        ]));
 
        self::assertResponseStatusCodeSame(422);
    }
 
    public function testIdZeroIsAValidSticker(): void {
        $this->createBook(0, 'Zero');
 
        $this->client->request('GET', '/book/0');
 
        self::assertResponseIsSuccessful();
        self::assertSelectorTextContains('h2', 'Zero');
    }
 
    public function testIdMustBeFreeInBothCollections(): void {
        $this->createBook(1);
        $ousted = (new OustedBook('Lost'))->setId(2)->setTitle('Gone')->setAuthor('Someone');
        $this->entityManager()->persist($ousted);
        $this->entityManager()->flush();
 
        foreach ([1, 2] as $id) {
            $crawler = $this->client->request('GET', '/add/form');
            $this->client->submit($crawler->selectButton('Save')->form([
                'book[id]' => (string)$id,
                'book[title]' => 'Duplicate',
                'book[author]' => 'Someone',
            ]));
 
            self::assertResponseStatusCodeSame(422);
            self::assertSelectorTextContains('body', 'already used by another book');
        }
    }
 
    public function testEditKeepsTheId(): void {
        $this->createBook(3, 'Before');
 
        $crawler = $this->client->request('GET', '/book/3/edit');
        $form = $crawler->selectButton('Save')->form();
        $form['book[title]'] = 'After';
        $this->client->submit($form);
 
        self::assertResponseRedirects('/book/3');
        self::assertSame('After', $this->findBook(3)?->getTitle());
    }
 
    public function testUnknownBookIsNotFound(): void {
        $this->client->request('GET', '/book/999');
 
        self::assertResponseStatusCodeSame(404);
    }
 
    public function testPolishIsServedFromAcceptLanguage(): void {
        $this->client->request('GET', '/list', server: ['HTTP_ACCEPT_LANGUAGE' => 'pl']);
 
        self::assertSelectorTextContains('.navbar-brand', 'Biblioteczka');
    }
}