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() ?? []); } }