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
<?php
declare(strict_types=1);
 
namespace App\Tests\Functional;
 
use App\Entity\OustedBook;
use App\Tests\DatabaseTestCase;
 
final class LoanTest extends DatabaseTestCase {
    public function testBookCanBeLentAndReturned(): void {
        $this->createBook(1);
 
        $crawler = $this->client->request('GET', '/book/1');
        $this->client->submit($crawler->selectButton('Lend')->form(['borrow[who]' => 'A Reader']));
        self::assertResponseRedirects('/book/1');
        self::assertSame('A Reader', $this->findBook(1)?->getBorrow()?->getWho());
 
        $crawler = $this->client->request('GET', '/borrowed');
        self::assertSelectorTextContains('.results .tile', 'A Reader');
        self::assertSelectorExists('.results .tile-title a[href="/book/1"]');
 
        $crawler = $this->client->request('GET', '/book/1');
        self::assertCount(0, $crawler->selectButton('Take out'));
        $this->client->submit($crawler->selectButton('Mark as returned')->form());
 
        $book = $this->findBook(1);
        self::assertFalse($book?->isBorrowed());
        self::assertCount(1, $book?->getBorrowHistory() ?? []);
    }
 
    public function testActionsRequireAValidCsrfToken(): void {
        $this->createBook(1)->lendTo('A Reader', new \DateTimeImmutable());
        $this->entityManager()->flush();
 
        $this->client->request('POST', '/book/1/return', ['_token' => 'forged']);
 
        self::assertTrue($this->findBook(1)?->isBorrowed());
    }
 
    public function testOustedBookKeepsHistoryAndCanBeRestored(): void {
        $book = $this->createBook(1, 'Moved');
        $book->addBorrowHistory('A Reader', new \DateTimeImmutable('2020-01-01'), new \DateTimeImmutable('2020-02-01'));
        $this->entityManager()->flush();
 
        $crawler = $this->client->request('GET', '/book/1');
        $this->client->submit($crawler->selectButton('Take out')->form(['oust[reason]' => 'Lost']));
        self::assertResponseRedirects('/ousted/1');
 
        self::assertNull($this->findBook(1));
        $ousted = $this->entityManager()->find(OustedBook::class, 1);
        self::assertSame('Lost', $ousted?->getReason());
        self::assertCount(1, $ousted?->getBorrowHistory() ?? []);
 
        $this->client->request('GET', '/ousted');
        self::assertSelectorTextContains('.results .tile', 'Lost');
        self::assertSelectorExists('.results .tile-title a[href="/ousted/1"]');
        self::assertSelectorNotExists('.results a[href="/book/1/edit"]');
 
        $crawler = $this->client->request('GET', '/ousted/1');
        $this->client->submit($crawler->selectButton('Return to the collection')->form());
        self::assertResponseRedirects('/book/1');
 
        self::assertCount(1, $this->findBook(1)?->getBorrowHistory() ?? []);
        self::assertNull($this->entityManager()->find(OustedBook::class, 1));
    }
 
    public function testBorrowedBookCannotBeOusted(): void {
        $this->createBook(1);
        $crawler = $this->client->request('GET', '/book/1');
        $form = $crawler->selectButton('Take out')->form(['oust[reason]' => 'Lost']);
 
        $this->findBook(1)?->lendTo('A Reader', new \DateTimeImmutable());
        $this->entityManager()->flush();
        $this->client->submit($form);
 
        self::assertNotNull($this->findBook(1));
    }
 
    public function testTypedLabelsCanBeAddedAndAreValidated(): void {
        $this->createBook(1);
 
        $crawler = $this->client->request('GET', '/book/1');
        $this->client->submit($crawler->selectButton('Add label')->form([
            'add_label[type]' => 'isbn',
            'add_label[label]' => '978-83-7384-345-5',
        ]));
        self::assertSame('9788373843455', $this->findBook(1)?->getIsbn());
 
        $crawler = $this->client->request('GET', '/book/1');
        $this->client->submit($crawler->selectButton('Add label')->form([
            'add_label[type]' => 'issn',
            'add_label[label]' => 'not a number',
        ]));
        self::assertNull($this->findBook(1)?->getIssn());
    }
 
    public function testOustingKeepsLabels(): void {
        $book = $this->createBook(1)->setIsbn('9788373843455');
        $book->addLabel('crime');
        $this->entityManager()->flush();
 
        $crawler = $this->client->request('GET', '/book/1');
        $this->client->submit($crawler->selectButton('Take out')->form(['oust[reason]' => 'Lost']));
 
        $this->entityManager()->clear();
        $ousted = $this->entityManager()->find(OustedBook::class, 1);
        self::assertSame('9788373843455', $ousted?->getIsbn());
        self::assertCount(2, $ousted?->getLabels() ?? []);
 
        $crawler = $this->client->request('GET', '/ousted/1');
        $this->client->submit($crawler->selectButton('Return to the collection')->form());
 
        self::assertSame('9788373843455', $this->findBook(1)?->getIsbn());
        self::assertCount(2, $this->findBook(1)?->getLabels() ?? []);
    }
}