diff --git a/bin/console b/bin/console index be06041a..8fe9d494 100755 --- a/bin/console +++ b/bin/console @@ -30,8 +30,6 @@ if ($input->hasParameterOption('--no-debug', true)) { (new Dotenv())->bootEnv(dirname(__DIR__).'/.env'); -require dirname( __DIR__ ) . '/config/bootstrap.php'; - if ($_SERVER['APP_DEBUG']) { umask(0000); diff --git a/config/bootstrap.php b/config/bootstrap.php deleted file mode 100644 index 85b754f1..00000000 --- a/config/bootstrap.php +++ /dev/null @@ -1,22 +0,0 @@ -boot(); - $container = $kernelTmp->getContainer(); - $wsexportConfig = [ - 'tempPath' => $container->getParameter( 'app.tempPath' ) ?? dirname( __DIR__ ) . '/var/', - ]; -} diff --git a/config/services.yaml b/config/services.yaml index 5c131fdf..612ad64d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -61,5 +61,9 @@ services: arguments: $timeout: '%env(default::int:APP_TIMEOUT)%' + App\FileCache: + arguments: + $projectDir: '%kernel.project_dir%' + # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones diff --git a/config/services_test.yaml b/config/services_test.yaml index 671fe2c2..b6075977 100644 --- a/config/services_test.yaml +++ b/config/services_test.yaml @@ -64,5 +64,9 @@ services: App\Repository\CreditRepository: public: true + App\FileCache: + arguments: + $projectDir: '%kernel.project_dir%' + # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5556abf1..d98570dd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,8 +4,8 @@ - diff --git a/public/index.php b/public/index.php index aced2ae6..9308ad2c 100644 --- a/public/index.php +++ b/public/index.php @@ -28,8 +28,6 @@ Request::setTrustedHosts( [ $trustedHosts ] ); } -require dirname( __DIR__ ) . '/config/bootstrap.php'; - $kernel = new Kernel( $_SERVER[ 'APP_ENV' ], (bool)$_SERVER[ 'APP_DEBUG' ] ); $request = Request::createFromGlobals(); $response = $kernel->handle( $request ); diff --git a/src/BookCreator.php b/src/BookCreator.php index 8e2f1781..50ad9c94 100644 --- a/src/BookCreator.php +++ b/src/BookCreator.php @@ -21,14 +21,14 @@ class BookCreator { /** @var string Full filesystem path to the created book. */ private $filePath; - public static function forApi( Api $api, $format, $options, GeneratorSelector $generatorSelector, CreditRepository $creditRepo ) { + public static function forApi( Api $api, $format, $options, GeneratorSelector $generatorSelector, CreditRepository $creditRepo, FileCache $fileCache ) { if ( !in_array( $format, GeneratorSelector::getAllFormats() ) ) { $list = '"' . implode( '", "', GeneratorSelector::getValidFormats() ) . '"'; throw new WsExportException( 'invalid-format', [ $format, $list ], 400 ); } return new BookCreator( - new BookProvider( $api, $options, $creditRepo ), + new BookProvider( $api, $options, $creditRepo, $fileCache ), $generatorSelector->getGenerator( $format ) ); } diff --git a/src/BookProvider.php b/src/BookProvider.php index 0aecc91b..e7081b35 100644 --- a/src/BookProvider.php +++ b/src/BookProvider.php @@ -19,14 +19,18 @@ class BookProvider { ]; private $creditRepo; + /** @var FileCache */ + private $fileCache; + /** * @param $api Api * @param bool[] $options */ - public function __construct( Api $api, array $options, CreditRepository $creditRepo ) { + public function __construct( Api $api, array $options, CreditRepository $creditRepo, FileCache $fileCache ) { $this->api = $api; $this->options = array_merge( $this->options, $options ); $this->creditRepo = $creditRepo; + $this->fileCache = $fileCache; } /** @@ -208,9 +212,8 @@ protected function getPages( $pages ) { * @return Picture[] */ protected function getPicturesData( array $pictures ) { - $cache = FileCache::singleton(); $client = $this->api->getClient(); - $requests = function () use ( $client, $pictures, $cache ) { + $requests = function () use ( $client, $pictures ) { foreach ( $pictures as $picture ) { $url = $picture->url; yield function () use ( $client, $url ) { @@ -220,11 +223,11 @@ protected function getPicturesData( array $pictures ) { } }; $pool = new Pool( $client, $requests(), [ - 'fulfilled' => function ( Response $response, $index ) use ( $cache, $pictures ) { + 'fulfilled' => function ( Response $response, $index ) use ( $pictures ) { $pictureIndex = array_keys( $pictures )[ $index ]; // Write the temp file and store its path. - $tempFile = $cache->getDirectory() . '/' . uniqid( 'pic-' ); + $tempFile = $this->fileCache->getDirectory() . '/' . uniqid( 'pic-' ); file_put_contents( $tempFile, $response->getBody()->getContents() ); $pictures[$pictureIndex]->tempFile = $tempFile; diff --git a/src/Command/CheckCommand.php b/src/Command/CheckCommand.php index 8a9731c6..e3c34af9 100644 --- a/src/Command/CheckCommand.php +++ b/src/Command/CheckCommand.php @@ -3,6 +3,7 @@ namespace App\Command; use App\BookCreator; +use App\FileCache; use App\GeneratorSelector; use App\Repository\CreditRepository; @@ -30,14 +31,18 @@ class CheckCommand extends Command { /** @var CreditRepository */ private $creditRepo; + /** @var FileCache */ + private $fileCache; + /** @var SymfonyStyle */ private $io; - public function __construct( Api $api, GeneratorSelector $generatorSelector, CreditRepository $creditRepo ) { + public function __construct( Api $api, GeneratorSelector $generatorSelector, CreditRepository $creditRepo, FileCache $fileCache ) { parent::__construct(); $this->api = $api; $this->generatorSelector = $generatorSelector; $this->creditRepo = $creditRepo; + $this->fileCache = $fileCache; } protected function configure() { @@ -125,7 +130,7 @@ private function getPages( InputInterface $input ) { */ private function check( string $page ) { $this->io->section( 'https://' . $this->api->getDomainName() . '/wiki/' . str_replace( ' ', '_', $page ) ); - $creator = BookCreator::forApi( $this->api, 'epub-3', [ 'credits' => false ], $this->generatorSelector, $this->creditRepo ); + $creator = BookCreator::forApi( $this->api, 'epub-3', [ 'credits' => false ], $this->generatorSelector, $this->creditRepo, $this->fileCache ); $creator->create( $page ); $jsonOutput = $creator->getFilePath() . '_epubcheck.json'; $process = new Process( [ 'epubcheck', $creator->getFilePath(), '--json', $jsonOutput ] ); diff --git a/src/Command/ExportCommand.php b/src/Command/ExportCommand.php index 86b2a0a3..e8a9f571 100644 --- a/src/Command/ExportCommand.php +++ b/src/Command/ExportCommand.php @@ -3,6 +3,7 @@ namespace App\Command; use App\BookCreator; +use App\FileCache; use App\GeneratorSelector; use App\Repository\CreditRepository; use App\Util\Api; @@ -29,12 +30,16 @@ class ExportCommand extends Command { /** @var bool */ private $enableCache; - public function __construct( GeneratorSelector $generatorSelector, CreditRepository $creditRepo, Api $api, bool $enableCache ) { + /** @var FileCache */ + private $fileCache; + + public function __construct( GeneratorSelector $generatorSelector, CreditRepository $creditRepo, Api $api, bool $enableCache, FileCache $fileCache ) { parent::__construct(); $this->generatorSelector = $generatorSelector; $this->creditRepo = $creditRepo; $this->api = $api; $this->enableCache = $enableCache; + $this->fileCache = $fileCache; } protected function configure() { @@ -80,7 +85,7 @@ protected function execute( InputInterface $input, OutputInterface $output ): in $io->writeln( 'Caching is disabled.' ); $this->api->disableCache(); } - $creator = BookCreator::forApi( $this->api, $input->getOption( 'format' ), $options, $this->generatorSelector, $this->creditRepo ); + $creator = BookCreator::forApi( $this->api, $input->getOption( 'format' ), $options, $this->generatorSelector, $this->creditRepo, $this->fileCache ); $creator->create( $input->getOption( 'title' ), $input->getOption( 'path' ) ); $io->success( [ diff --git a/src/Command/OpdsCommand.php b/src/Command/OpdsCommand.php index 27425d74..4f3795ba 100644 --- a/src/Command/OpdsCommand.php +++ b/src/Command/OpdsCommand.php @@ -3,6 +3,7 @@ namespace App\Command; use App\BookProvider; +use App\FileCache; use App\OpdsBuilder; use App\Repository\CreditRepository; use App\Util\Api; @@ -22,10 +23,14 @@ class OpdsCommand extends Command { /** @var CreditRepository */ private $creditRepo; - public function __construct( Api $api, CreditRepository $creditRepo ) { + /** @var FileCache */ + private $fileCache; + + public function __construct( Api $api, CreditRepository $creditRepo, FileCache $fileCache ) { parent::__construct(); $this->api = $api; $this->creditRepo = $creditRepo; + $this->fileCache = $fileCache; } protected function configure() { @@ -52,10 +57,10 @@ protected function execute( InputInterface $input, OutputInterface $output ): in date_default_timezone_set( 'UTC' ); $this->api->setLang( $lang ); - $provider = new BookProvider( $this->api, [ 'categories' => false, 'images' => false ], $this->creditRepo ); + $provider = new BookProvider( $this->api, [ 'categories' => false, 'images' => false ], $this->creditRepo, $this->fileCache ); $exportPath = 'https://ws-export.wmcloud.org/'; - $atomGenerator = new OpdsBuilder( $provider, $this->api, $lang, $exportPath ); + $atomGenerator = new OpdsBuilder( $provider, $this->api, $lang, $this->fileCache, $exportPath ); $outputFile = dirname( __DIR__, 2 ) . "/public/opds/$lang/$category.xml"; if ( !is_dir( dirname( $outputFile ) ) ) { mkdir( dirname( $outputFile ), 0755, true ); diff --git a/src/Controller/ExportController.php b/src/Controller/ExportController.php index 3942cf13..8db8a9d8 100644 --- a/src/Controller/ExportController.php +++ b/src/Controller/ExportController.php @@ -5,6 +5,7 @@ use App\BookCreator; use App\Entity\GeneratedBook; use App\Exception\WsExportException; +use App\FileCache; use App\FontProvider; use App\GeneratorSelector; use App\Refresh; @@ -81,7 +82,8 @@ public function home( Api $api, FontProvider $fontProvider, GeneratorSelector $generatorSelector, - CreditRepository $creditRepo + CreditRepository $creditRepo, + FileCache $fileCache ) { // Handle ?refresh=1 for backwards compatibility. if ( $request->get( 'refresh', false ) !== false ) { @@ -100,7 +102,7 @@ public function home( $response = new Response(); if ( $request->get( 'page' ) ) { try { - return $this->export( $request, $api, $fontProvider, $generatorSelector, $creditRepo ); + return $this->export( $request, $api, $fontProvider, $generatorSelector, $creditRepo, $fileCache ); } catch ( WsExportException $ex ) { $exception = $ex; $response->setStatusCode( $ex->getResponseCode() ); @@ -131,7 +133,8 @@ private function export( Api $api, FontProvider $fontProvider, GeneratorSelector $generatorSelector, - CreditRepository $creditRepo + CreditRepository $creditRepo, + FileCache $fileCache ) { // Get params. $page = $request->get( 'page' ); @@ -149,7 +152,7 @@ private function export( // Generate ebook. $options = [ 'images' => $images, 'fonts' => $font, 'credits' => $credits ]; - $creator = BookCreator::forApi( $api, $format, $options, $generatorSelector, $creditRepo ); + $creator = BookCreator::forApi( $api, $format, $options, $generatorSelector, $creditRepo, $fileCache ); $creator->create( $page ); // Send file. diff --git a/src/FileCache.php b/src/FileCache.php index f4cf9009..5c3431da 100644 --- a/src/FileCache.php +++ b/src/FileCache.php @@ -2,6 +2,7 @@ namespace App; +use App\Util\Util; use DirectoryIterator; use Exception; @@ -22,9 +23,10 @@ class FileCache { private static $instance; /** - * @param string $path Directory for temp files + * @param string $projectDir */ - private function __construct( string $path ) { + public function __construct( string $projectDir ) { + $path = $projectDir . '/var/file-cache/'; $this->dir = $this->makePrivateDirectory( $path ); // Randomly clean up the cache @@ -36,17 +38,21 @@ private function __construct( string $path ) { } /** - * Returns a global instance of this class - * @return FileCache + * Builds a unique temporary file name for a given title and extension. + * + * @param string $title + * @param string $extension + * @return string */ - public static function singleton(): FileCache { - global $wsexportConfig; - - if ( !self::$instance ) { - self::$instance = new FileCache( $wsexportConfig['tempPath'] ); + public function buildTemporaryFileName( $title, $extension ) { + for ( $i = 0; $i < 100; $i++ ) { + $path = $this->getDirectory() . '/' . 'ws-' . Util::encodeString( $title ) . '-' . getmypid() . rand() . '.' . $extension; + if ( !file_exists( $path ) ) { + return $path; + } } - return self::$instance; + throw new Exception( 'Unable to create temporary file' ); } /** @@ -71,11 +77,11 @@ private function makePrivateDirectory( string $path ): string { $user = preg_replace( '/^tools\./', '', $user ); $dir = rtrim( $path, '/' ) . '/' . $user; - // Guard against realpth() returning false sometimes + // Guard against realpath() returning false sometimes $dir = realpath( $dir ) ?: $dir; if ( !is_dir( $dir ) ) { - if ( !mkdir( $dir, 0755 ) ) { + if ( !mkdir( $dir, 0755, true ) ) { throw new Exception( "Couldn't create temporary directory $dir" ); } } diff --git a/src/Generator/AtomGenerator.php b/src/Generator/AtomGenerator.php index b738b160..de3a5c7e 100644 --- a/src/Generator/AtomGenerator.php +++ b/src/Generator/AtomGenerator.php @@ -3,6 +3,7 @@ namespace App\Generator; use App\Book; +use App\FileCache; use App\Util\Util; use DOMDocument; use DOMElement; @@ -22,12 +23,16 @@ class AtomGenerator implements FormatGenerator { /** * @var string */ - private $exportBasePath; + private $exportBasePath = ''; - /** - * @param string $exportBasePath - */ - public function __construct( $exportBasePath = '' ) { + /** @var FileCache */ + private $fileCache; + + public function __construct( FileCache $fileCache ) { + $this->fileCache = $fileCache; + } + + public function setExportBasePath( string $exportBasePath ) { $this->exportBasePath = $exportBasePath; } @@ -49,7 +54,7 @@ public function getMimeType() { /** * create the file - * @param $book Book the title of the main page of the book in Wikisource + * @param Book $book The title of the main page of the book in Wikisource * @return string */ public function create( Book $book ) { @@ -58,7 +63,7 @@ public function create( Book $book ) { $this->appendNamespaces( $entry ); $dom->appendChild( $entry ); - $fileName = Util::buildTemporaryFileName( $book->title, 'atom' ); + $fileName = $this->fileCache->buildTemporaryFileName( $book->title, 'atom' ); $dom->save( $fileName ); return $fileName; } diff --git a/src/Generator/ConvertGenerator.php b/src/Generator/ConvertGenerator.php index c8db428c..b8f6e118 100644 --- a/src/Generator/ConvertGenerator.php +++ b/src/Generator/ConvertGenerator.php @@ -4,6 +4,7 @@ use App\Book; use App\Exception\WsExportException; +use App\FileCache; use App\FontProvider; use App\Util\Api; use App\Util\Util; @@ -100,12 +101,16 @@ public static function getSupportedTypes() { /** @var CacheInterface */ private $cache; - public function __construct( FontProvider $fontProvider, Api $api, Intuition $intuition, int $timeout, CacheInterface $cache ) { + /** @var FileCache */ + private $fileCache; + + public function __construct( FontProvider $fontProvider, Api $api, Intuition $intuition, int $timeout, CacheInterface $cache, FileCache $fileCache ) { $this->fontProvider = $fontProvider; $this->api = $api; $this->intuition = $intuition; $this->timeout = $timeout; $this->cache = $cache; + $this->fileCache = $fileCache; } /** @@ -140,11 +145,11 @@ public function getMimeType() { * @return string */ public function create( Book $book ) { - $outputFileName = Util::buildTemporaryFileName( $book->title, $this->getExtension() ); + $outputFileName = $this->fileCache->buildTemporaryFileName( $book->title, $this->getExtension() ); try { $epubFileName = $this->createEpub( $book ); - $persistentEpubFileName = Util::buildTemporaryFileName( $book->title, 'epub' ); + $persistentEpubFileName = $this->fileCache->buildTemporaryFileName( $book->title, 'epub' ); rename( $epubFileName, $persistentEpubFileName ); $this->convert( $persistentEpubFileName, $outputFileName ); } finally { @@ -157,7 +162,7 @@ public function create( Book $book ) { } private function createEpub( Book $book ) { - $epubGenerator = new EpubGenerator( $this->fontProvider, $this->api, $this->intuition, $this->cache ); + $epubGenerator = new EpubGenerator( $this->fontProvider, $this->api, $this->intuition, $this->cache, $this->fileCache ); return $epubGenerator->create( $book ); } diff --git a/src/Generator/EpubGenerator.php b/src/Generator/EpubGenerator.php index 8d1fda93..7b9eaa0a 100644 --- a/src/Generator/EpubGenerator.php +++ b/src/Generator/EpubGenerator.php @@ -4,6 +4,7 @@ use App\Book; use App\Cleaner\BookCleanerEpub; +use App\FileCache; use App\FontProvider; use App\Util\Api; use App\Util\Util; @@ -38,11 +39,15 @@ class EpubGenerator implements FormatGenerator { /** @var CacheInterface */ private $cache; - public function __construct( FontProvider $fontProvider, Api $api, Intuition $intuition, CacheInterface $cache ) { + /** @var FileCache */ + private $fileCache; + + public function __construct( FontProvider $fontProvider, Api $api, Intuition $intuition, CacheInterface $cache, FileCache $fileCache ) { $this->fontProvider = $fontProvider; $this->api = $api; $this->intuition = $intuition; $this->cache = $cache; + $this->fileCache = $fileCache; } /** @@ -73,7 +78,7 @@ public function create( Book $book ) { $wsUrl = Util::wikisourceUrl( $book->lang, $book->title ); $cleaner = new BookCleanerEpub(); $cleaner->clean( $book, Util::wikisourceUrl( $book->lang ) ); - $fileName = Util::buildTemporaryFileName( $book->title, 'epub' ); + $fileName = $this->fileCache->buildTemporaryFileName( $book->title, 'epub' ); $zip = $this->createZipFile( $fileName ); $zip->addFromString( 'META-INF/container.xml', $this->getXmlContainer() ); $zip->addFromString( 'META-INF/com.apple.ibooks.display-options.xml', $this->getAppleIBooksDisplayOptionsXml() ); diff --git a/src/GeneratorSelector.php b/src/GeneratorSelector.php index 13704e21..d7278937 100644 --- a/src/GeneratorSelector.php +++ b/src/GeneratorSelector.php @@ -51,12 +51,16 @@ class GeneratorSelector { /** @var CacheInterface */ private $cache; - public function __construct( FontProvider $fontProvider, Api $api, ConvertGenerator $convertGenerator, Intuition $intuition, CacheInterface $cache ) { + /** @var FileCache */ + private $fileCache; + + public function __construct( FontProvider $fontProvider, Api $api, ConvertGenerator $convertGenerator, Intuition $intuition, CacheInterface $cache, FileCache $fileCache ) { $this->fontProvider = $fontProvider; $this->api = $api; $this->convertGenerator = $convertGenerator; $this->intuition = $intuition; $this->cache = $cache; + $this->fileCache = $fileCache; } /** @@ -79,12 +83,12 @@ public function getGenerator( $format ): FormatGenerator { $format = self::$aliases[$format]; } if ( $format === 'epub-3' ) { - return new EpubGenerator( $this->fontProvider, $this->api, $this->intuition, $this->cache ); + return new EpubGenerator( $this->fontProvider, $this->api, $this->intuition, $this->cache, $this->fileCache ); } elseif ( in_array( $format, ConvertGenerator::getSupportedTypes() ) ) { $this->convertGenerator->setFormat( $format ); return $this->convertGenerator; } elseif ( $format === 'atom' ) { - return new AtomGenerator(); + return new AtomGenerator( $this->fileCache ); } else { throw new Exception( "The file format '$format' is unknown." ); } diff --git a/src/OpdsBuilder.php b/src/OpdsBuilder.php index 7dfdef45..645dbf4e 100644 --- a/src/OpdsBuilder.php +++ b/src/OpdsBuilder.php @@ -20,6 +20,9 @@ */ class OpdsBuilder { + /** @var FileCache */ + private $fileCache; + /** * @var string */ @@ -39,10 +42,11 @@ class OpdsBuilder { * @param string $lang * @param string $exportBasePath */ - public function __construct( BookProvider $bookProvider, Api $api, string $lang, $exportBasePath = '' ) { + public function __construct( BookProvider $bookProvider, Api $api, string $lang, FileCache $fileCache, $exportBasePath = '' ) { $this->bookProvider = $bookProvider; $this->api = $api; $this->lang = $lang; + $this->fileCache = $fileCache; $this->exportBasePath = $exportBasePath; } @@ -74,7 +78,8 @@ public function buildFromCategory( $categoryTitle ) { } private function buildFromTitles( array $titles, $fromPage = '' ) { - $generator = new AtomGenerator( $this->exportBasePath ); + $generator = new AtomGenerator( $this->fileCache ); + $generator->setExportBasePath( $this->exportBasePath ); $dom = new DOMDocument( '1.0', 'UTF-8' ); $feed = $dom->createElement( 'feed' ); diff --git a/src/Refresh.php b/src/Refresh.php index de696a02..8db55911 100644 --- a/src/Refresh.php +++ b/src/Refresh.php @@ -19,20 +19,10 @@ public function __construct( Api $api, CacheItemPoolInterface $cacheItemPool ) { } public function refresh() { - if ( !is_dir( $this->getTempFileName( '' ) ) ) { - mkdir( $this->getTempFileName( '' ) ); - } - $this->cacheItemPool->deleteItems( [ 'namespaces_' . $this->api->getLang(), 'about_' . $this->api->getLang(), 'css_' . $this->api->getLang(), ] ); } - - protected function getTempFileName( $name ) { - $cache = FileCache::singleton(); - - return $cache->getDirectory() . '/' . $this->api->getLang() . '/' . $name; - } } diff --git a/src/Util/Util.php b/src/Util/Util.php index 632bdf7c..89302d47 100644 --- a/src/Util/Util.php +++ b/src/Util/Util.php @@ -2,12 +2,9 @@ namespace App\Util; -use App\FileCache; -use App\Refresh; use DOMDocument; use DOMElement; use DOMXPath; -use Exception; use HtmlFormatter\HtmlFormatter; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -85,17 +82,6 @@ public static function getXhtmlFromContent( $lang, $content, $title = ' ' ) { return $html . '' . $content . ''; } - public static function getTempFile( Api $api, $lang, $name ) { - $cache = FileCache::singleton(); - $path = $cache->getDirectory() . '/' . $lang . '/' . $name; - if ( !file_exists( $path ) ) { - $api->setLang( $lang ); - $refresh = new Refresh( $api, $api->getCache() ); - $refresh->refresh(); - } - return file_get_contents( $path ); - } - public static function encodeString( $string ) { static $map = []; static $num = 0; @@ -141,27 +127,6 @@ public static function getLanguageDirection( $languageCode ) { : 'ltr'; } - /** - * Builds a unique temporary file name for a given title and extension - * - * @param string $title - * @param string $extension - * @return string - */ - public static function buildTemporaryFileName( $title, $extension ) { - $cache = FileCache::singleton(); - $directory = $cache->getDirectory(); - - for ( $i = 0; $i < 100; $i++ ) { - $path = $directory . '/' . 'ws-' . static::encodeString( $title ) . '-' . getmypid() . rand() . '.' . $extension; - if ( !file_exists( $path ) ) { - return $path; - } - } - - throw new Exception( 'Unable to create temporary file' ); - } - public static function removeFile( $fileName ) { $process = new Process( [ 'rm', realpath( $fileName ) ] ); $process->mustRun(); diff --git a/tests/Book/BookProviderTest.php b/tests/Book/BookProviderTest.php index 96ad9b81..235136cc 100644 --- a/tests/Book/BookProviderTest.php +++ b/tests/Book/BookProviderTest.php @@ -3,6 +3,7 @@ namespace App\Tests\Book; use App\BookProvider; +use App\FileCache; use App\Repository\CreditRepository; use App\Util\Api; use DOMDocument; @@ -35,8 +36,8 @@ public function setUp(): void { $api = new Api( new NullLogger(), new NullAdapter(), new NullAdapter(), $client, 0 ); $api->setLang( 'en' ); $creditRepository = $this->getMockBuilder( CreditRepository::class )->disableOriginalConstructor()->getMock(); - - $this->bookProvider = new BookProvider( $api, [ 'categories' => false, 'credits' => true ], $creditRepository ); + $fileCache = new FileCache( dirname( __DIR__, 2 ) ); + $this->bookProvider = new BookProvider( $api, [ 'categories' => false, 'credits' => true ], $creditRepository, $fileCache ); } public function testGetMetadata() { diff --git a/tests/Book/GeneratorSelectorTest.php b/tests/Book/GeneratorSelectorTest.php index 69222e1f..5043d66d 100644 --- a/tests/Book/GeneratorSelectorTest.php +++ b/tests/Book/GeneratorSelectorTest.php @@ -2,6 +2,7 @@ namespace App\Tests\Book; +use App\FileCache; use App\FontProvider; use App\Generator\ConvertGenerator; use App\Generator\EpubGenerator; @@ -37,8 +38,9 @@ public function setUp(): void { $this->fontProvider = new FontProvider( $cache, self::$container->get( OnWikiConfig::class ) ); $this->api = self::$container->get( Api::class ); $this->intuition = self::$container->get( Intuition::class ); - $convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, $this->intuition, 10, $cache ); - $this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator, $this->intuition, $cache ); + $fileCache = self::$container->get( FileCache::class ); + $convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, $this->intuition, 10, $cache, $fileCache ); + $this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator, $this->intuition, $cache, $fileCache ); } public function testGetUnknownGeneratorRaisesException() { diff --git a/tests/Book/RefreshTest.php b/tests/Book/RefreshTest.php index 2afbb6ae..1ead16d0 100644 --- a/tests/Book/RefreshTest.php +++ b/tests/Book/RefreshTest.php @@ -3,6 +3,7 @@ namespace App\Tests\Book; use App\Book; +use App\FileCache; use App\FontProvider; use App\Generator\EpubGenerator; use App\Refresh; @@ -20,6 +21,8 @@ class RefreshTest extends KernelTestCase { public function testRefresh() { + self::bootKernel(); + $cache = new ArrayAdapter(); $api = new Api( new NullLogger(), $cache, $cache, new Client(), 0 ); $api->setLang( 'en' ); @@ -30,7 +33,8 @@ public function testRefresh() { $this->assertFalse( $cache->hasItem( 'css_en' ) ); // Export a book, and test that this fills the cache. - $epubGenerator = new EpubGenerator( new FontProvider( $cache, new OnWikiConfig( $api, $cache, $intuition ) ), $api, $intuition, $cache ); + $fileCache = self::$container->get( FileCache::class ); + $epubGenerator = new EpubGenerator( new FontProvider( $cache, new OnWikiConfig( $api, $cache, $intuition ) ), $api, $intuition, $cache, $fileCache ); $book = new Book(); $book->lang = 'en'; $book->title = 'Emma'; diff --git a/tests/BookCreator/BookCreatorIntegrationTest.php b/tests/BookCreator/BookCreatorIntegrationTest.php index 3f9b4c1c..938fe81b 100644 --- a/tests/BookCreator/BookCreatorIntegrationTest.php +++ b/tests/BookCreator/BookCreatorIntegrationTest.php @@ -4,6 +4,7 @@ use App\BookCreator; use App\EpubCheck\EpubCheck; +use App\FileCache; use App\FontProvider; use App\Generator\ConvertGenerator; use App\GeneratorSelector; @@ -38,14 +39,18 @@ class BookCreatorIntegrationTest extends KernelTestCase { /** @var Intuition */ private $intuition; + /** @var FileCache */ + private $fileCache; + public function setUp(): void { self::bootKernel(); $cache = new ArrayAdapter(); $this->fontProvider = new FontProvider( $cache, self::$container->get( OnWikiConfig::class ) ); $this->api = self::$container->get( Api::class ); $this->intuition = self::$container->get( Intuition::class ); - $convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, $this->intuition, 10, $cache ); - $this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator, $this->intuition, $cache ); + $this->fileCache = self::$container->get( FileCache::class ); + $convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, $this->intuition, 10, $cache, $this->fileCache ); + $this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator, $this->intuition, $cache, $this->fileCache ); $this->creditRepository = self::$container->get( CreditRepository::class ); $this->epubCheck = self::$container->get( EpubCheck::class ); } @@ -83,7 +88,7 @@ public function testCreateBookMobi( $title, $language ) { private function createBook( $title, $language, $format ) { $this->api->setLang( $language ); - $creator = BookCreator::forApi( $this->api, $format, [ 'credits' => false ], $this->generatorSelector, $this->creditRepository ); + $creator = BookCreator::forApi( $this->api, $format, [ 'credits' => false ], $this->generatorSelector, $this->creditRepository, $this->fileCache ); $creator->create( $title ); $this->assertFileExists( $creator->getFilePath() ); $this->assertNotNull( $creator->getBook() ); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 619baa5b..606b22bb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,5 +5,3 @@ require dirname( __DIR__ ) . '/vendor/autoload.php'; ( new Dotenv() )->bootEnv( dirname( __DIR__ ) . '/.env' ); - -require dirname( __DIR__ ) . '/config/bootstrap.php';