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
<?php
declare(strict_types=1);
 
namespace App\Tests\Functional;
 
use App\Entity\OustedBook;
use App\Tests\DatabaseTestCase;
 
final class InventoryTest extends DatabaseTestCase {
    public function testPageAndServiceWorkerAreServed(): void {
        $this->client->request('GET', '/inventory/');
        self::assertResponseIsSuccessful();
        self::assertSelectorExists('#scan-input');
 
        $this->client->request('GET', '/inventory/sw.js');
        self::assertResponseIsSuccessful();
        self::assertResponseHeaderSame('Content-Type', 'text/javascript; charset=UTF-8');
        self::assertStringContainsString('"/js/inventory.js"', (string)$this->client->getResponse()->getContent());
    }
 
    public function testBookListContainsStandardNumbersAndPlacement(): void {
        $this->createBook(2, 'Solaris', 'StanisÅ‚aw Lem')->setIsbn('9788373843455')->setIssn('9770867041003')->setPlacement('HOL/4/1');
        $this->createBook(1, 'Plain')->lendTo('A Reader', new \DateTimeImmutable());
        $this->entityManager()->flush();
 
        $this->client->request('GET', '/inventory/books');
 
        self::assertResponseIsSuccessful();
        self::assertSame(['books' => [
            ['id' => 1, 'title' => 'Plain', 'author' => 'Author', 'placement' => null, 'lastSeen' => null, 'borrowed' => true, 'isbn' => [], 'issn' => []],
            ['id' => 2, 'title' => 'Solaris', 'author' => 'StanisÅ‚aw Lem', 'placement' => 'HOL/4/1', 'lastSeen' => null, 'borrowed' => false, 'isbn' => ['9788373843455'], 'issn' => ['9770867041003']],
        ]], $this->jsonResponse());
    }
 
    public function testSyncAssignsLocationsAndMarksBooksAsSeen(): void {
        $this->createBook(1)->setPlacement('OLD/1');
        $this->createBook(2);
        $before = new \DateTimeImmutable('-1 second');
 
        $this->sync(['1' => 'HOL/4/1', '2' => ' Attic ', '99' => 'X', '3' => '']);
 
        self::assertResponseIsSuccessful();
        self::assertSame(['results' => [
            '1' => 'saved',
            '2' => 'saved',
            '99' => 'unknown_book',
            '3' => 'invalid',
        ]], $this->jsonResponse());
 
        $book = $this->findBook(1);
        self::assertSame('HOL/4/1', $book?->getPlacement());
        self::assertGreaterThanOrEqual($before, $book?->getLastSeen());
        self::assertSame('Attic', $this->findBook(2)?->getPlacement());
    }
 
    public function testScanningABorrowedBookMarksItAsReturned(): void {
        $lentAt = new \DateTimeImmutable('2026-10-01 10:00');
        $this->createBook(1)->lendTo('A Reader', $lentAt);
        $this->entityManager()->flush();
        $this->entityManager()->clear();
 
        $this->sync(['1' => 'HOL/4/1']);
 
        self::assertSame(['results' => ['1' => 'returned']], $this->jsonResponse());
        $book = $this->findBook(1);
        self::assertFalse($book?->isBorrowed());
        self::assertSame('HOL/4/1', $book?->getPlacement());
 
        $history = $book?->getBorrowHistory()->first();
        self::assertSame('A Reader', $history ? $history->getWho() : null);
        self::assertEquals($lentAt, $history ? $history->getAt() : null);
        self::assertEquals($book?->getLastSeen(), $history ? $history->getTill() : null);
 
        $this->sync(['1' => 'HOL/4/1']);
        self::assertSame(['results' => ['1' => 'saved']], $this->jsonResponse());
        self::assertCount(1, $this->findBook(1)?->getBorrowHistory() ?? []);
    }
 
    public function testSyncAcceptsBookZero(): void {
        $this->createBook(0);
 
        $this->sync((object)['0' => 'HOL/1/1']);
 
        self::assertResponseIsSuccessful();
        self::assertSame('HOL/1/1', $this->findBook(0)?->getPlacement());
    }
 
    public function testSyncRejectsAnythingButAJsonMap(): void {
        $this->createBook(1);
 
        $this->client->request('POST', '/inventory/sync', ['1' => 'HOL/4/1']);
        self::assertResponseStatusCodeSame(415);
 
        $this->sync([['id' => 1, 'location' => 'HOL/4/1']]);
        self::assertResponseStatusCodeSame(400);
 
        $this->sync(['1' => ['nested']]);
        self::assertResponseStatusCodeSame(400);
 
        self::assertNull($this->findBook(1)?->getPlacement());
    }
 
    public function testDetailsSayWhenBookWasNeverSeen(): void {
        $this->createBook(1);
 
        $this->client->request('GET', '/book/1');
 
        self::assertSelectorTextContains('.book-last-seen td', 'not seen during an inventory yet');
    }
 
    public function testOustingKeepsLastSeen(): void {
        $this->createBook(1);
        $this->sync(['1' => 'HOL/4/1']);
        $seen = $this->findBook(1)?->getLastSeen();
 
        $crawler = $this->client->request('GET', '/book/1');
        self::assertSelectorTextContains('.book-last-seen td', (string)$seen?->format('Y-m-d H:i'));
        $this->client->submit($crawler->selectButton('Take out')->form(['oust[reason]' => 'Lost']));
 
        $this->entityManager()->clear();
        self::assertEquals($seen, $this->entityManager()->find(OustedBook::class, 1)?->getLastSeen());
        $this->client->request('GET', '/ousted/1');
        self::assertSelectorTextContains('.book-last-seen td', (string)$seen?->format('Y-m-d H:i'));
    }
 
    /**
     * @param array<mixed>|object $body
     */
    private function sync(array|object $body): void {
        $this->client->request('POST', '/inventory/sync', server: ['CONTENT_TYPE' => 'application/json'], content: json_encode($body, \JSON_THROW_ON_ERROR));
    }
 
    /**
     * @return array<mixed>
     */
    private function jsonResponse(): array {
        return json_decode((string)$this->client->getResponse()->getContent(), true, flags: \JSON_THROW_ON_ERROR);
    }
}