Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Sep 19, 2023
1 parent 5dc12ef commit d8cfda8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
57 changes: 35 additions & 22 deletions src/Formats/Fb2/Parser/Fb2Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,7 @@ public static function make(string $path): self

if (array_key_exists('title-info', $self->metadata)) {
$self->titleInfo = $self->metadata['title-info'];

$coverPage = $self->titleInfo['coverpage'] ?? null;
if ($coverPage) {
$cover = $coverPage['image'] ?? null;
if ($cover) {
$id = $cover['@attributes']['href'] ?? null;
$id = str_replace('#', '', $id);

$binary = $xml->getContent()['binary'] ?? null;
if ($binary) {
foreach ($binary as $item) {
$attributes = $item['@attributes'] ?? null;
$binaryId = $attributes['id'] ?? null;
if ($binaryId === $id) {
$cover = $item['@content'] ?? null;
$cover = str_replace("\n", '', $cover);
$self->cover = $cover;
}
}
}
}
}
$self->cover = $self->findCover();
}

if (array_key_exists('document-info', $self->metadata)) {
Expand All @@ -72,6 +51,40 @@ public static function make(string $path): self
return $self;
}

private function findCover(): ?string
{
$coverPage = $this->titleInfo['coverpage'] ?? null;
if (! $coverPage) {
return null;
}

$cover = $coverPage['image'] ?? null;
if (! $cover) {
return null;
}

$id = $cover['@attributes']['href'] ?? null;
$id = str_replace('#', '', $id);

$binary = $this->xml->getContent()['binary'] ?? null;
if (! $binary) {
return null;
}

foreach ($binary as $item) {
$attributes = $item['@attributes'] ?? null;
$binaryId = $attributes['id'] ?? null;
if ($binaryId === $id) {
$cover = $item['@content'] ?? null;
$cover = str_replace("\n", '', $cover);

return $cover;
}
}

return null;
}

public function getRoot(): ?string
{
return $this->root;
Expand Down
21 changes: 12 additions & 9 deletions tests/FormatTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
<?php

use Kiwilan\Ebook\Ebook;
use Kiwilan\Ebook\EbookCover;

it('can parse format', function (string $path) {
$ebook = Ebook::read($path);
$basename = pathinfo($path, PATHINFO_BASENAME);

expect($ebook->getBasename())->toBe($basename);

$slug = $ebook->getMetaTitle()->getUniqueFilename();
// $path = "tests/output/{$slug}-cover.jpg";
// if (file_exists($path)) {
// unlink($path);
// }
// file_put_contents($path, $ebook->getCover()?->getContent());
// expect($ebook->getCover())->toBeInstanceOf(EbookCover::class);
// expect($path)->toBeReadableFile();

expect($ebook->getTitle())->toBe("Alice's Adventures in Wonderland");
expect($ebook->getDescription())->toBe('With the curious, quick-witted Alice at its heart, readers will not only rediscover characters such as the charming White Rabbit, the formidable Queen of Hearts, the Mad Hatter and the grinning Cheshire Cat but will find fresh and wonderful creations of these characters by a true master of his art,; images that will live in our hearts and minds for generations to come.');
expect($ebook->getDescriptionHtml())->toBeString();
Expand All @@ -29,6 +21,17 @@

$tags = $ebook->getTags();
expect($tags)->toBeArray();

if ($path === FORMAT_FB2 || $path === FORMAT_EPUB) {
$slug = $ebook->getMetaTitle()->getUniqueFilename();
$path = "tests/output/{$slug}-cover.jpg";
if (file_exists($path)) {
unlink($path);
}
file_put_contents($path, $ebook->getCover()?->getContent());
expect($ebook->getCover())->toBeInstanceOf(EbookCover::class);
expect($path)->toBeReadableFile();
}
})->with([
FORMAT_AZW3,
FORMAT_EPUB,
Expand Down

0 comments on commit d8cfda8

Please sign in to comment.