Skip to content

Commit

Permalink
test(kobo): Add more books
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed Dec 4, 2024
1 parent e41a5df commit 8f7ffd9
Show file tree
Hide file tree
Showing 26 changed files with 281 additions and 32 deletions.
88 changes: 63 additions & 25 deletions src/DataFixtures/BookFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,81 @@
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Yaml\Yaml;

/**
* @phpstan-type BookData array{
* title: string,
* uuid?: string,
* authors: list<string>,
* publishDate: string,
* language: string,
* publisher: string,
* extension: string,
* imageExtension: string,
* imageFilename: string,
* bookFilename: string,
* pageNumber: int
* }
*/
class BookFixture extends Fixture implements DependentFixtureInterface
{
public const BOOK_REFERENCE = 'book-odyssey';
public const BOOK_REFERENCE = 'book-the_odyssey';

public const BOOK_ODYSSEY_FILENAME = 'real-TheOdysses.epub';
public const BOOK_PAGE_REFERENCE = '7557680347007504212_1727-h-21.htm.xhtml';
public const ID = 1;
public const UUID = '54c8fb05-cf05-4cb6-9482-bc25fa49fa80';

public function load(ObjectManager $manager): void
{
// https://www.gutenberg.org/ebooks/1727
$book = new Book();
$book->setUuid(self::UUID);
$book->setTitle('The Odyssey');
$book->setAuthors(['Homer']);
$book->setPublishDate(new \DateTimeImmutable('1999-04-01'));
$book->setLanguage('en');
$book->setPublisher('Public domain');
$book->setExtension('epub');
$book->setImageExtension('jpg');
$book->setImageFilename('TheOdysses.jpg');
$book->setImagePath('');
$book->setBookFilename('TheOdysses.epub');
$book->setChecksum(md5($book->getBookFilename()));
$book->setBookPath('');
$book->setPageNumber(30);

$manager->persist($book);
$manager->flush();

$this->addReference(self::BOOK_REFERENCE, $book);
}
public const NUMBER_OF_YAML_BOOKS = 20;

public function getDependencies(): array
{
return [
UserFixture::class,
];
}

/**
* @throws \DateMalformedStringException
*/
public function load(ObjectManager $manager): void
{
$yamlFile = __DIR__.'/books.yaml';

/** @var array{books: list<BookData>} $data */
$data = Yaml::parseFile($yamlFile);

/** @var list<BookData> $books */
$books = $data['books'];

foreach ($books as $index => $bookData) {
$book = new Book();
if (array_key_exists('uuid', $bookData)) {
$book->setUuid($bookData['uuid']);
}
$book->setTitle($bookData['title']);
$book->setAuthors($bookData['authors']);
$book->setPublishDate(new \DateTimeImmutable($bookData['publishDate']));
$book->setLanguage($bookData['language']);
$book->setPublisher($bookData['publisher']);
$book->setExtension($bookData['extension']);
$book->setImageExtension($bookData['imageExtension']);
$book->setImageFilename($bookData['imageFilename']);
$book->setImagePath('');
$book->setBookFilename($bookData['bookFilename']);
$book->setChecksum(md5($book->getBookFilename()));
$book->setBookPath('');
$book->setPageNumber($bookData['pageNumber']);

$manager->persist($book);

$reference = strtolower((string) preg_replace('/[^A-Za-z0-9-_]/', '_', $bookData['title']));

$this->addReference('book-'.$reference, $book);
$this->addReference('book-'.$index, $book);
}

$manager->flush();
}
}
5 changes: 5 additions & 0 deletions src/DataFixtures/ShelfFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function load(ObjectManager $manager): void
$shelf->setUser($this->getUser());
$this->getBook()->addShelf($shelf);

foreach (range(0, BookFixture::NUMBER_OF_YAML_BOOKS - 1) as $index) {
$book = $this->getReference('book-'.$index, Book::class);
$book->addShelf($shelf);
}

$manager->persist($shelf);
$manager->flush();

Expand Down
202 changes: 202 additions & 0 deletions src/DataFixtures/books.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
books:
- title: "The Odyssey"
authors: ["Homer"]
uuid: "54c8fb05-cf05-4cb6-9482-bc25fa49fa80"
publishDate: "1999-04-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "TheOdysses.jpg"
bookFilename: "real-TheOdysses.epub"
pageNumber: 30
- title: "Pride and Prejudice"
authors: ["Jane Austen"]
publishDate: "1813-01-28"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "PrideAndPrejudice.jpg"
bookFilename: "fake-PrideAndPrejudice.epub"
pageNumber: 60
- title: "Moby Dick"
authors: ["Herman Melville"]
publishDate: "1851-10-18"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "MobyDick.jpg"
bookFilename: "fake-MobyDick.epub"
pageNumber: 70
- title: "Great Expectations"
authors: ["Charles Dickens"]
publishDate: "1861-08-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "GreatExpectations.jpg"
bookFilename: "fake-GreatExpectations.epub"
pageNumber: 80
- title: "Dracula"
authors: ["Bram Stoker"]
publishDate: "1897-05-26"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "Dracula.jpg"
bookFilename: "fake-Dracula.epub"
pageNumber: 90
- title: "Frankenstein"
authors: ["Mary Shelley"]
publishDate: "1818-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "Frankenstein.jpg"
bookFilename: "fake-Frankenstein.epub"
pageNumber: 75
- title: "The Adventures of Sherlock Holmes"
authors: ["Arthur Conan Doyle"]
publishDate: "1892-10-14"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "SherlockHolmes.jpg"
bookFilename: "fake-SherlockHolmes.epub"
pageNumber: 65
- title: "Jane Eyre"
authors: ["Charlotte Brontë"]
publishDate: "1847-10-16"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "JaneEyre.jpg"
bookFilename: "fake-JaneEyre.epub"
pageNumber: 85
- title: "Les Misérables"
authors: ["Victor Hugo"]
publishDate: "1862-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "LesMiserables.jpg"
bookFilename: "fake-LesMiserables.epub"
pageNumber: 120
- title: "The Count of Monte Cristo"
authors: ["Alexandre Dumas"]
publishDate: "1844-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "CountOfMonteCristo.jpg"
bookFilename: "fake-CountOfMonteCristo.epub"
pageNumber: 105
- title: "A Tale of Two Cities"
authors: ["Charles Dickens"]
publishDate: "1859-11-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "TaleOfTwoCities.jpg"
bookFilename: "fake-TaleOfTwoCities.epub"
pageNumber: 95
- title: "The Picture of Dorian Gray"
authors: ["Oscar Wilde"]
publishDate: "1890-07-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "DorianGray.jpg"
bookFilename: "fake-DorianGray.epub"
pageNumber: 68
- title: "The Brothers Karamazov"
authors: ["Fyodor Dostoevsky"]
publishDate: "1880-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "BrothersKaramazov.jpg"
bookFilename: "fake-BrothersKaramazov.epub"
pageNumber: 135
- title: "Crime and Punishment"
authors: ["Fyodor Dostoevsky"]
publishDate: "1866-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "CrimeAndPunishment.jpg"
bookFilename: "fake-CrimeAndPunishment.epub"
pageNumber: 120
- title: "War and Peace"
authors: ["Leo Tolstoy"]
publishDate: "1869-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "WarAndPeace.jpg"
bookFilename: "fake-WarAndPeace.epub"
pageNumber: 145
- title: "Anna Karenina"
authors: ["Leo Tolstoy"]
publishDate: "1877-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "AnnaKarenina.jpg"
bookFilename: "fake-AnnaKarenina.epub"
pageNumber: 130
- title: "The Divine Comedy"
authors: ["Dante Alighieri"]
publishDate: "1320-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "DivineComedy.jpg"
bookFilename: "fake-DivineComedy.epub"
pageNumber: 120
- title: "Don Quixote"
authors: ["Miguel de Cervantes"]
publishDate: "1605-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "DonQuixote.jpg"
bookFilename: "fake-DonQuixote.epub"
pageNumber: 150
- title: "Wuthering Heights"
authors: ["Emily Brontë"]
publishDate: "1847-01-01"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "WutheringHeights.jpg"
bookFilename: "fake-WutheringHeights.epub"
pageNumber: 90
- title: "A Christmas Carol"
authors: ["Charles Dickens"]
publishDate: "1843-12-19"
language: "en"
publisher: "Public domain"
extension: "epub"
imageExtension: "jpg"
imageFilename: "AChristmasCarol.jpg"
bookFilename: "fake-AChristmasCarol.epub"
pageNumber: 50
6 changes: 4 additions & 2 deletions tests/Controller/Kobo/KoboDownloadControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function testDownload(): void
self::assertResponseIsSuccessful();
self::assertResponseHeaderSame('Content-Type', 'application/epub+zip');
self::assertResponseHasHeader('Content-Length');
$expectedDisposition = "attachment; filename=book-1-TheOdysses.epub; filename*=utf-8''TheOdysses.epub";

$expectedDisposition = "attachment; filename=book-1-".BookFixture::BOOK_ODYSSEY_FILENAME."; filename*=utf-8''".BookFixture::BOOK_ODYSSEY_FILENAME;
self::assertResponseHeaderSame('Content-Disposition', $expectedDisposition, 'The Content-Disposition header is not as expected');

}
Expand Down Expand Up @@ -79,7 +80,8 @@ public function testDownloadKepub(): void
self::assertResponseHeaderSame('Content-Type', 'application/epub+zip');
self::assertResponseHasHeader('Content-Length');

$expectedDisposition = "attachment; filename=book-1-TheOdysses.kepub; filename*=utf-8''TheOdysses.kepub";
$expectedDisposition = "attachment; filename=book-1-".BookFixture::BOOK_ODYSSEY_FILENAME."; filename*=utf-8''".BookFixture::BOOK_ODYSSEY_FILENAME;
$expectedDisposition = str_replace('.epub', '.kepub', $expectedDisposition);
self::assertResponseHeaderSame('Content-Disposition', $expectedDisposition, 'The Content-Disposition header is not as expected');

}
Expand Down
9 changes: 5 additions & 4 deletions tests/Controller/Kobo/KoboSyncControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Tests\Controller\Kobo;

use App\DataFixtures\BookFixture;
use App\Entity\KoboSyncedBook;
use App\Kobo\Response\MetadataResponseService;
use App\Tests\Contraints\AssertHasDownloadWithFormat;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function testSyncControllerWithForce() : void
$response = self::getJsonResponse();
self::assertResponseIsSuccessful();
self::assertThat($response, new JSONIsValidSyncResponse([
'NewEntitlement' => 1,
'NewEntitlement' => BookFixture::NUMBER_OF_YAML_BOOKS,
'NewTag' => 1
]), 'Response is not a valid sync response');

Expand All @@ -59,12 +60,12 @@ public function testSyncControllerWithoutForce() : void
$response = self::getJsonResponse();
self::assertResponseIsSuccessful();
self::assertThat($response, new JSONIsValidSyncResponse([
'NewEntitlement' => 1,
'NewEntitlement' => BookFixture::NUMBER_OF_YAML_BOOKS,
'NewTag' => 1
]), 'Response is not a valid sync response');

$count = $this->getEntityManager()->getRepository(KoboSyncedBook::class)->count(['koboDevice' => 1]);
self::assertSame(1, $count, 'There should be 1 synced books');
self::assertSame(BookFixture::NUMBER_OF_YAML_BOOKS, $count, 'Number of synced book is invalid');

$this->getEntityManager()->getRepository(KoboSyncedBook::class)->deleteAllSyncedBooks(1);

Expand Down Expand Up @@ -96,7 +97,7 @@ public function testSyncControllerWithRemote() : void
$response = self::getJsonResponse();
self::assertResponseIsSuccessful();
self::assertThat($response, new JSONIsValidSyncResponse([
'NewEntitlement' => 1,
'NewEntitlement' => BookFixture::NUMBER_OF_YAML_BOOKS,
'NewTag' => 1,
'DeletedTag' => 1
]), 'Response is not a valid sync response');
Expand Down
Loading

0 comments on commit 8f7ffd9

Please sign in to comment.