Skip to content

Commit

Permalink
test(kobo): Better injection of filesystem manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed Dec 4, 2024
1 parent 9c5826c commit e257f56
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 55 deletions.
13 changes: 12 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ services:
'$bookFolderNamingFormat': '%BOOK_FOLDER_NAMING_FORMAT%'
'$bookFileNamingFormat': '%BOOK_FILE_NAMING_FORMAT%'

App\Service\BookFileSystemManagerInterface: '@App\Service\BookFileSystemManager'

App\Kobo\ParamConverter\SyncTokenParamConverter:
tags:
- { name: request.param_converter, priority: 100 }
Expand Down Expand Up @@ -124,4 +126,13 @@ services:

when@dev:
services:
Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'

when@test:
services:
App\Tests\FileSystemManagerForTests:
autowire: true
bind:
$publicDirectory: '%kernel.project_dir%/tests/Resources'
App\Service\BookFileSystemManagerInterface:
alias: 'App\Tests\FileSystemManagerForTests'
4 changes: 2 additions & 2 deletions src/Kobo/DownloadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Kobo\Kepubify\KepubifyConversionFailed;
use App\Kobo\Kepubify\KepubifyMessage;
use App\Kobo\Response\MetadataResponseService;
use App\Service\BookFileSystemManager;
use App\Service\BookFileSystemManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\HeaderUtils;
Expand All @@ -22,7 +22,7 @@
class DownloadHelper
{
public function __construct(
private readonly BookFileSystemManager $fileSystemManager,
private readonly BookFileSystemManagerInterface $fileSystemManager,
private readonly CoverTransformer $coverTransformer,
protected UrlGeneratorInterface $urlGenerator,
protected LoggerInterface $logger,
Expand Down
2 changes: 1 addition & 1 deletion src/Service/BookFileSystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\String\Slugger\SluggerInterface;

class BookFileSystemManager
class BookFileSystemManager implements BookFileSystemManagerInterface
{
public const ALLOWED_FILE_EXTENSIONS = [
'*.epub', '*.cbr', '*.cbz', '*.pdf', '*.mobi',
Expand Down
28 changes: 28 additions & 0 deletions src/Service/BookFileSystemManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Service;

use App\Entity\Book;

interface BookFileSystemManagerInterface
{
public function getBooksDirectory(): string;

public function getCoverDirectory(): string;

public function getBookFilename(Book $book): string;

public function getBookPublicPath(Book $book): string;

public function getCoverFilename(Book $book): ?string;

public function getBookSize(Book $book): ?int;

public function getCoverSize(Book $book): ?int;

public function fileExist(Book $book): bool;

public function coverExist(Book $book): bool;

public function getBookFile(Book $book): \SplFileInfo;
}
2 changes: 1 addition & 1 deletion src/Service/BookProgressionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class BookProgressionService
{
public function __construct(
private BookFileSystemManager $fileSystemManager,
private BookFileSystemManagerInterface $fileSystemManager,
private LoggerInterface $logger,
private EntityManagerInterface $em,
) {
Expand Down
3 changes: 0 additions & 3 deletions tests/Controller/Kobo/AbstractKoboControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Kobo\Kepubify\KepubifyEnabler;
use App\Kobo\Proxy\KoboProxyConfiguration;
use App\Kobo\Proxy\KoboStoreProxy;
use App\Tests\InjectFakeFileSystemTrait;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Handler\MockHandler;
Expand All @@ -20,8 +19,6 @@

abstract class AbstractKoboControllerTest extends WebTestCase
{
use InjectFakeFileSystemTrait;

protected ?string $accessKey = null;
protected ?KoboDevice $koboDevice = null;

Expand Down
4 changes: 0 additions & 4 deletions tests/Controller/Kobo/KoboDownloadControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class KoboDownloadControllerTest extends AbstractKoboControllerTest
public function testDownload(): void
{
$client = static::getClient();
$this->injectFakeFileSystemManager();

$book = $this->findByIdAndKobo(BookFixture::ID, $this->getKoboDevice());
self::assertNotNull($book, 'The book is not linked to the Kobo');
Expand All @@ -37,7 +36,6 @@ public function testDownload(): void
public function testDownloadKepubFailed(): void
{
$client = static::getClient();
$this->injectFakeFileSystemManager();

// Disable Kepubify conversion
$lastValue = $this->getKepubifyEnabler()->disable();
Expand All @@ -63,8 +61,6 @@ public function testDownloadKepubFailed(): void
public function testDownloadKepub(): void
{
$client = static::getClient();
$this->injectFakeFileSystemManager();


$book = $this->findByIdAndKobo(BookFixture::ID, $this->getKoboDevice());
self::assertNotNull($book, 'The book is not linked to the Kobo');
Expand Down
2 changes: 0 additions & 2 deletions tests/Controller/Kobo/KoboImageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class KoboImageControllerTest extends AbstractKoboControllerTest
public function testDownload(): void
{
$client = static::getClient();
$this->injectFakeFileSystemManager();


$book = $this->findByIdAndKobo(BookFixture::ID, $this->getKoboDevice());
self::assertNotNull($book, 'The book is not linked to the Kobo');
Expand Down
7 changes: 2 additions & 5 deletions tests/Controller/Kobo/KoboSyncControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function assertPreConditions(): void
public function testSyncControllerWithForce() : void
{
$client = static::getClient();
$this->injectFakeFileSystemManager();


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

Expand All @@ -53,7 +53,7 @@ public function testSyncControllerWithForce() : void
public function testSyncControllerWithoutForce() : void
{
$client = static::getClient();
$this->injectFakeFileSystemManager();


$client?->request('GET', '/kobo/'.$this->accessKey.'/v1/library/sync');

Expand Down Expand Up @@ -90,7 +90,6 @@ public function testSyncControllerWithRemote() : void
}
}]'));

$this->injectFakeFileSystemManager();

$client?->request('GET', '/kobo/'.$this->accessKey.'/v1/library/sync');

Expand All @@ -112,7 +111,6 @@ public function testSyncControllerMetadata() : void
{
$uuid = $this->getBook()->getUuid();
$client = static::getClient();
$this->injectFakeFileSystemManager();
$this->getKepubifyEnabler()->disable();

$client?->request('GET', '/kobo/'.$this->accessKey.'/v1/library/'.$uuid."/metadata");
Expand All @@ -128,7 +126,6 @@ public function testSyncControllerMetadataWithConversion() : void
{
$uuid = $this->getBook()->getUuid();
$client = static::getClient();
$this->injectFakeFileSystemManager();
self::assertTrue($this->getKepubifyEnabler()->isEnabled());

$client?->request('GET', '/kobo/'.$this->accessKey.'/v1/library/'.$uuid."/metadata");
Expand Down
26 changes: 26 additions & 0 deletions tests/FileSystemManagerForTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace App\Tests;

use App\Service\BookFileSystemManager;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\String\Slugger\SluggerInterface;

class FileSystemManagerForTests extends BookFileSystemManager
{
public function __construct(
Security $security,
SluggerInterface $slugger,
LoggerInterface $logger,
protected string $publicDirectory,
){
parent::__construct(
$security,
$publicDirectory,
'{authorFirst}/{author}/{serie}/{title}',
'{serie}-{serieIndex}-{title}',
$slugger,
$logger
);
}
}
34 changes: 1 addition & 33 deletions tests/InjectFakeFileSystemTrait.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
<?php
namespace App\Tests;

use App\DataFixtures\BookFixture;
use App\Service\BookFileSystemManager;
use Psr\Log\NullLogger;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\String\Slugger\SluggerInterface;

trait InjectFakeFileSystemTrait
{
public function injectFakeFileSystemManager(): void
{
$resources = __DIR__.'/Resources';
$realPath = realpath($resources);
self::assertNotFalse($realPath);
$fixtureBookPath = $realPath."/";
$mockBuilder = $this->getMockBuilder(BookFileSystemManager::class);

$DEFAULT_BOOK_FOLDER_NAMING_FORMAT = '{authorFirst}/{author}/{title}/{serie}';
$DEFAULT_BOOK_FILE_NAMING_FORMAT = '{serie}-{title}';

$mock = $mockBuilder->setConstructorArgs([
self::getContainer()->get(Security::class),
realpath($resources),
$DEFAULT_BOOK_FOLDER_NAMING_FORMAT,
$DEFAULT_BOOK_FILE_NAMING_FORMAT,
$this->createMock(SluggerInterface::class),
new NullLogger(),
])
->onlyMethods(['getBooksDirectory', 'getCoverDirectory'])
->enableProxyingToOriginalMethods()
->getMock();
$mock->expects(self::any())->method('getBooksDirectory')->willReturn($fixtureBookPath);
$mock->expects(self::any())->method('getCoverDirectory')->willReturn($fixtureBookPath);

self::assertSame(realpath($resources).'/books/'.BookFixture::BOOK_ODYSSEY_FILENAME, $mock->getBookFilename($this->getBook()), "Faking Filesystem failed");
self::getContainer()->set(BookFileSystemManager::class, $mock);
}
{}
}
3 changes: 0 additions & 3 deletions tests/Service/BookProgressionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
use App\Repository\BookRepository;
use App\Repository\UserRepository;
use App\Service\BookProgressionService;
use App\Tests\InjectFakeFileSystemTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class BookProgressionServiceTest extends KernelTestCase
{
use InjectFakeFileSystemTrait;
protected function setUp(): void
{
parent::setUp();

self::bootKernel();
$this->injectFakeFileSystemManager();
}

public function testPageNumber(): void{
Expand Down

0 comments on commit e257f56

Please sign in to comment.