From 0edcf0bddd0f7fb47b488ad037825fa73ed0bb06 Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Tue, 27 Feb 2024 09:12:21 +0100 Subject: [PATCH] Update AssetBaseDirFormatter --- config/services.php | 8 ++--- docs/configuration.rst | 2 +- docs/customization.rst | 6 ++-- src/Builder/AbstractChromiumPdfBuilder.php | 15 +++++---- src/Builder/AbstractPdfBuilder.php | 16 ++-------- src/Builder/LibreOfficePdfBuilder.php | 2 +- src/Builder/MarkdownPdfBuilder.php | 2 +- src/DependencyInjection/Configuration.php | 6 ++-- .../SensiolabsGotenbergExtension.php | 4 +-- src/Formatter/AssetBaseDirFormatter.php | 17 +++++++--- src/Pdf/Gotenberg.php | 13 ++++---- src/Twig/GotenbergAssetExtension.php | 6 +--- tests/Builder/AbstractBuilderTestCase.php | 4 +-- tests/Builder/HtmlPdfBuilderTest.php | 31 +++++++++++++++---- tests/Builder/LibreOfficePdfBuilderTest.php | 6 +++- tests/Builder/MarkdownPdfBuilderTest.php | 6 +++- tests/Builder/UrlPdfBuilderTest.php | 6 +++- .../DependencyInjection/ConfigurationTest.php | 2 +- tests/Pdf/GotenbergTest.php | 21 ++++++++----- 19 files changed, 98 insertions(+), 75 deletions(-) diff --git a/config/services.php b/config/services.php index 9a31a1f..2b269fb 100644 --- a/config/services.php +++ b/config/services.php @@ -20,8 +20,7 @@ ->args([ service('sensiolabs_gotenberg.client'), abstract_arg('user configuration options'), - param('kernel.project_dir'), - service(Filesystem::class), + service('sensiolabs_gotenberg.asset.base_dir_formatter'), service('twig')->nullOnInvalid(), ]) ->public() @@ -37,17 +36,14 @@ $services->set('sensiolabs_gotenberg.asset.base_dir_formatter', AssetBaseDirFormatter::class) ->args([ - abstract_arg('asset_base_dir to assets'), service(Filesystem::class), param('kernel.project_dir'), + abstract_arg('base_directory to assets'), ]) ->alias(AssetBaseDirFormatter::class, 'sensiolabs_gotenberg.asset.base_dir_formatter') ; $services->set('sensiolabs_gotenberg.twig.asset_extension', GotenbergAssetExtension::class) - ->args([ - service(AssetBaseDirFormatter::class), - ]) ->tag('twig.extension') ; }; diff --git a/docs/configuration.rst b/docs/configuration.rst index faba06e..1c849af 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -9,7 +9,7 @@ The default configuration for the bundle looks like : sensiolabs_gotenberg: base_uri: 'http://localhost:3000' - asset_base_dir: '%kernel.project_dir%/public/' + base_directory: '%kernel.project_dir%' options: paper_width: null # 8.5 paper_height: null # 11 diff --git a/docs/customization.rst b/docs/customization.rst index 296d717..f93ddca 100644 --- a/docs/customization.rst +++ b/docs/customization.rst @@ -21,7 +21,7 @@ If a template needs to link to a static asset (e.g. an image), this bundle provi Twig function to help generate that path. This function work as `asset() Twig function`_ and fetch your assets in the public folder of your application -If your files are in another folder, you can override the default value of ``asset_base_dir`` in your +If your files are in another folder, you can override the default value of ``base_directory`` in your configuration file ``config/sensiolabs_gotenberg.yml``. The path provided can be relative as well as absolute. @@ -34,8 +34,8 @@ The path provided can be relative as well as absolute. PDF body - CEO - Admin + CEO + Admin

Hello world!

diff --git a/src/Builder/AbstractChromiumPdfBuilder.php b/src/Builder/AbstractChromiumPdfBuilder.php index 4d4232a..3f9310a 100644 --- a/src/Builder/AbstractChromiumPdfBuilder.php +++ b/src/Builder/AbstractChromiumPdfBuilder.php @@ -6,7 +6,7 @@ use Sensiolabs\GotenbergBundle\Enum\PdfPart; use Sensiolabs\GotenbergBundle\Exception\ExtraHttpHeadersJsonEncodingException; use Sensiolabs\GotenbergBundle\Exception\PdfPartRenderingException; -use Symfony\Component\Filesystem\Filesystem; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\File as DataPartFile; use Twig\Environment; @@ -15,11 +15,10 @@ abstract class AbstractChromiumPdfBuilder extends AbstractPdfBuilder { public function __construct( GotenbergClientInterface $gotenbergClient, - string $projectDir, - Filesystem $filesystem, + AssetBaseDirFormatter $asset, private readonly ?Environment $twig = null, ) { - parent::__construct($gotenbergClient, $projectDir, $filesystem); + parent::__construct($gotenbergClient, $asset); } /** @@ -234,9 +233,9 @@ public function assets(string ...$paths): static */ public function addAsset(string $path): static { - $dataPart = new DataPart(new DataPartFile($this->resolveFilePath($path))); + $dataPart = new DataPart(new DataPartFile($this->asset->resolve($path))); - $this->formFields['assets'][$path] = $dataPart; + $this->formFields['assets'][$this->asset->resolve($path)] = $dataPart; return $this; } @@ -413,7 +412,7 @@ public function getMultipartFormData(): array protected function withPdfPartFile(PdfPart $pdfPart, string $path): static { $dataPart = new DataPart( - new DataPartFile($this->resolveFilePath($path)), + new DataPartFile($this->asset->resolve($path)), $pdfPart->value, ); @@ -436,7 +435,7 @@ protected function withRenderedPart(PdfPart $pdfPart, string $template, array $c try { $html = $this->twig->render($template, array_merge($context, ['_builder' => $this])); } catch (\Throwable $error) { - throw new PdfPartRenderingException(sprintf('Could not render template "%s" into PDF part "%s".', $template, $pdfPart->value), previous: $error); + throw new PdfPartRenderingException(sprintf('Could not render template "%s" into PDF part "%s". %s', $template, $pdfPart->value, $error->getMessage()), previous: $error); } $this->formFields[$pdfPart->value] = new DataPart($html, $pdfPart->value, 'text/html'); diff --git a/src/Builder/AbstractPdfBuilder.php b/src/Builder/AbstractPdfBuilder.php index 4b75b42..bd4c45b 100644 --- a/src/Builder/AbstractPdfBuilder.php +++ b/src/Builder/AbstractPdfBuilder.php @@ -5,7 +5,7 @@ use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; use Sensiolabs\GotenbergBundle\Client\PdfResponse; use Sensiolabs\GotenbergBundle\Exception\MissingRequiredFieldException; -use Symfony\Component\Filesystem\Filesystem; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\String\UnicodeString; @@ -18,8 +18,7 @@ abstract class AbstractPdfBuilder implements PdfBuilderInterface public function __construct( protected readonly GotenbergClientInterface $gotenbergClient, - protected readonly string $projectDir, - protected readonly Filesystem $filesystem, + protected readonly AssetBaseDirFormatter $asset, ) { } @@ -66,20 +65,11 @@ public function setConfigurations(array $configurations): static */ protected function assertFileExtension(string $path, array $validExtensions): void { - $file = new File($this->resolveFilePath($path)); + $file = new File($this->asset->resolve($path)); $extension = $file->getExtension(); if (!\in_array($extension, $validExtensions, true)) { throw new \InvalidArgumentException(sprintf('The file extension "%s" is not available in Gotenberg.', $extension)); } } - - protected function resolveFilePath(string $path): string - { - if ($this->filesystem->isAbsolutePath($path)) { - return $path; - } - - return "{$this->projectDir}/{$path}"; - } } diff --git a/src/Builder/LibreOfficePdfBuilder.php b/src/Builder/LibreOfficePdfBuilder.php index c73b7a4..e40bf74 100644 --- a/src/Builder/LibreOfficePdfBuilder.php +++ b/src/Builder/LibreOfficePdfBuilder.php @@ -71,7 +71,7 @@ public function files(string ...$paths): self foreach ($paths as $path) { $this->assertFileExtension($path, self::AVAILABLE_EXTENSIONS); - $dataPart = new DataPart(new DataPartFile($this->resolveFilePath($path))); + $dataPart = new DataPart(new DataPartFile($this->asset->resolve($path))); $this->formFields['files'][$path] = $dataPart; } diff --git a/src/Builder/MarkdownPdfBuilder.php b/src/Builder/MarkdownPdfBuilder.php index 810caff..a7a8a1f 100644 --- a/src/Builder/MarkdownPdfBuilder.php +++ b/src/Builder/MarkdownPdfBuilder.php @@ -39,7 +39,7 @@ public function files(string ...$paths): self foreach ($paths as $path) { $this->assertFileExtension($path, ['md']); - $dataPart = new DataPart(new DataPartFile($this->resolveFilePath($path))); + $dataPart = new DataPart(new DataPartFile($this->asset->resolve($path))); $this->formFields['files'][$path] = $dataPart; } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ec54417..3ce90bc 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -27,9 +27,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->thenInvalid('Invalid API Gotenberg host.') ->end() ->end() - ->scalarNode('asset_base_dir') - ->info('Base DIR where assets are located') - ->defaultValue('%kernel.project_dir%/public/') + ->scalarNode('base_directory') + ->info('Base directory will be used for assets, files, markdown') + ->defaultValue('%kernel.project_dir%') ->cannotBeEmpty() ->end() ->arrayNode('default_options') diff --git a/src/DependencyInjection/SensiolabsGotenbergExtension.php b/src/DependencyInjection/SensiolabsGotenbergExtension.php index eef28ae..13acdd4 100644 --- a/src/DependencyInjection/SensiolabsGotenbergExtension.php +++ b/src/DependencyInjection/SensiolabsGotenbergExtension.php @@ -16,7 +16,7 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = new Configuration(); - /** @var array{base_uri: string, asset_base_dir: string, default_options: array} $config */ + /** @var array{base_uri: string, base_directory: string, default_options: array} $config */ $config = $this->processConfiguration($configuration, $configs); $definition = $container->getDefinition('sensiolabs_gotenberg.client'); @@ -26,7 +26,7 @@ public function load(array $configs, ContainerBuilder $container): void $definition->replaceArgument(1, $this->cleanDefaultOptions($config['default_options'])); $definition = $container->getDefinition('sensiolabs_gotenberg.asset.base_dir_formatter'); - $definition->replaceArgument(0, $config['asset_base_dir']); + $definition->replaceArgument(0, $config['base_directory']); } /** diff --git a/src/Formatter/AssetBaseDirFormatter.php b/src/Formatter/AssetBaseDirFormatter.php index 5dc6905..4ae2ae6 100644 --- a/src/Formatter/AssetBaseDirFormatter.php +++ b/src/Formatter/AssetBaseDirFormatter.php @@ -4,18 +4,25 @@ use Symfony\Component\Filesystem\Filesystem; -final readonly class AssetBaseDirFormatter implements \Stringable +final readonly class AssetBaseDirFormatter { - public function __construct(private string $baseDir, private Filesystem $filesystem, private string $projectDir) + private string $baseDir; + + public function __construct(private Filesystem $filesystem, private string $projectDir, string $baseDir) { + $this->baseDir = rtrim($baseDir, '/'); } - public function __toString(): string + public function resolve(string $path): string { + if ($this->filesystem->isAbsolutePath($path)) { + return $path; + } + if ($this->filesystem->isAbsolutePath($this->baseDir)) { - return $this->baseDir; + return "{$this->baseDir}/{$path}"; } - return "{$this->projectDir}/{$this->baseDir}"; + return "{$this->projectDir}/{$this->baseDir}/{$path}"; } } diff --git a/src/Pdf/Gotenberg.php b/src/Pdf/Gotenberg.php index 80dfcf2..b239587 100644 --- a/src/Pdf/Gotenberg.php +++ b/src/Pdf/Gotenberg.php @@ -7,7 +7,7 @@ use Sensiolabs\GotenbergBundle\Builder\MarkdownPdfBuilder; use Sensiolabs\GotenbergBundle\Builder\UrlPdfBuilder; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; -use Symfony\Component\Filesystem\Filesystem; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Twig\Environment; final readonly class Gotenberg implements GotenbergInterface @@ -18,36 +18,35 @@ public function __construct( private GotenbergClientInterface $gotenbergClient, private array $userConfigurations, - private string $projectDir, - private Filesystem $filesystem, + private AssetBaseDirFormatter $asset, private ?Environment $twig = null, ) { } public function html(): HtmlPdfBuilder { - return (new HtmlPdfBuilder($this->gotenbergClient, $this->projectDir, $this->filesystem, $this->twig)) + return (new HtmlPdfBuilder($this->gotenbergClient, $this->asset, $this->twig)) ->setConfigurations($this->userConfigurations) ; } public function url(): UrlPdfBuilder { - return (new UrlPdfBuilder($this->gotenbergClient, $this->projectDir, $this->filesystem, $this->twig)) + return (new UrlPdfBuilder($this->gotenbergClient, $this->asset, $this->twig)) ->setConfigurations($this->userConfigurations) ; } public function markdown(): MarkdownPdfBuilder { - return (new MarkdownPdfBuilder($this->gotenbergClient, $this->projectDir, $this->filesystem, $this->twig)) + return (new MarkdownPdfBuilder($this->gotenbergClient, $this->asset, $this->twig)) ->setConfigurations($this->userConfigurations) ; } public function office(): LibreOfficePdfBuilder { - return (new LibreOfficePdfBuilder($this->gotenbergClient, $this->projectDir, $this->filesystem)) + return (new LibreOfficePdfBuilder($this->gotenbergClient, $this->asset)) ->setConfigurations($this->userConfigurations) ; } diff --git a/src/Twig/GotenbergAssetExtension.php b/src/Twig/GotenbergAssetExtension.php index e2492f8..9d7f8b4 100644 --- a/src/Twig/GotenbergAssetExtension.php +++ b/src/Twig/GotenbergAssetExtension.php @@ -9,10 +9,6 @@ final class GotenbergAssetExtension extends AbstractExtension { - public function __construct(private readonly string $formattedAssetBaseDir) - { - } - public function getFunctions(): array { return [ @@ -31,7 +27,7 @@ public function getAssetUrl(array $context, string $path): string throw new \LogicException('You need to extend from AbstractChromiumPdfBuilder to use gotenberg_asset function.'); } - $builder->assets($this->formattedAssetBaseDir.$path); + $builder->addAsset($path); return (new File($path))->getFilename(); } diff --git a/tests/Builder/AbstractBuilderTestCase.php b/tests/Builder/AbstractBuilderTestCase.php index 5d68f97..6ffa8aa 100644 --- a/tests/Builder/AbstractBuilderTestCase.php +++ b/tests/Builder/AbstractBuilderTestCase.php @@ -8,12 +8,12 @@ abstract class AbstractBuilderTestCase extends TestCase { - protected const FIXTURE_DIR = __DIR__.'/../Fixtures'; + protected const FIXTURE_DIR = '/../Fixtures'; protected static Environment $twig; public static function setUpBeforeClass(): void { - self::$twig = new Environment(new FilesystemLoader(self::FIXTURE_DIR.'/templates')); + self::$twig = new Environment(new FilesystemLoader(__DIR__.'/../Fixtures/templates')); } } diff --git a/tests/Builder/HtmlPdfBuilderTest.php b/tests/Builder/HtmlPdfBuilderTest.php index f4fded5..20a7c07 100644 --- a/tests/Builder/HtmlPdfBuilderTest.php +++ b/tests/Builder/HtmlPdfBuilderTest.php @@ -7,6 +7,7 @@ use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; use Sensiolabs\GotenbergBundle\Exception\ExtraHttpHeadersJsonEncodingException; use Sensiolabs\GotenbergBundle\Exception\PdfPartRenderingException; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Mime\Part\DataPart; @@ -17,7 +18,10 @@ public function testWithConfigurations(): void { $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new HtmlPdfBuilder($client, self::FIXTURE_DIR, $filesystem); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter); $builder->contentFile('content.html'); $builder->setConfigurations(self::getUserConfig()); @@ -57,7 +61,10 @@ public function testWithTemplate(): void { $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new HtmlPdfBuilder($client, self::FIXTURE_DIR, $filesystem, self::$twig); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter, self::$twig); $builder->content('content.html.twig'); $multipartFormData = $builder->getMultipartFormData(); @@ -74,7 +81,10 @@ public function testWithAssets(): void { $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new HtmlPdfBuilder($client, self::FIXTURE_DIR, $filesystem); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter); $builder->contentFile('content.html'); $builder->assets('assets/logo.png'); @@ -93,7 +103,10 @@ public function testWithHeader(): void { $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new HtmlPdfBuilder($client, self::FIXTURE_DIR, $filesystem); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter); $builder->headerFile('header.html'); $builder->contentFile('content.html'); @@ -115,7 +128,10 @@ public function testInvalidTwigTemplate(): void $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new HtmlPdfBuilder($client, self::FIXTURE_DIR, $filesystem, self::$twig); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter, self::$twig); $builder->content('invalid.html.twig'); } @@ -127,7 +143,10 @@ public function testInvalidExtraHttpHeaders(): void $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new HtmlPdfBuilder($client, self::FIXTURE_DIR, $filesystem); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter); $builder->contentFile('content.html'); // @phpstan-ignore-next-line $builder->extraHttpHeaders([ diff --git a/tests/Builder/LibreOfficePdfBuilderTest.php b/tests/Builder/LibreOfficePdfBuilderTest.php index afde88c..791bc38 100644 --- a/tests/Builder/LibreOfficePdfBuilderTest.php +++ b/tests/Builder/LibreOfficePdfBuilderTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use Sensiolabs\GotenbergBundle\Builder\LibreOfficePdfBuilder; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Mime\Part\DataPart; @@ -31,7 +32,10 @@ public function testOfficeFiles(string $filePath, string $contentType): void { $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new LibreOfficePdfBuilder($client, self::FIXTURE_DIR, $filesystem); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new LibreOfficePdfBuilder($client, $assetBaseDirFormatter); $builder->files($filePath); $multipartFormData = $builder->getMultipartFormData(); diff --git a/tests/Builder/MarkdownPdfBuilderTest.php b/tests/Builder/MarkdownPdfBuilderTest.php index c11d606..14225db 100644 --- a/tests/Builder/MarkdownPdfBuilderTest.php +++ b/tests/Builder/MarkdownPdfBuilderTest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use Sensiolabs\GotenbergBundle\Builder\MarkdownPdfBuilder; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Mime\Part\DataPart; @@ -15,7 +16,10 @@ public function testMarkdownFile(): void { $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new MarkdownPdfBuilder($client, self::FIXTURE_DIR, $filesystem); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new MarkdownPdfBuilder($client, $assetBaseDirFormatter); $builder ->wrapperFile('template.html') ->files('assets/file.md') diff --git a/tests/Builder/UrlPdfBuilderTest.php b/tests/Builder/UrlPdfBuilderTest.php index 74247f9..cc430d4 100644 --- a/tests/Builder/UrlPdfBuilderTest.php +++ b/tests/Builder/UrlPdfBuilderTest.php @@ -6,6 +6,7 @@ use Sensiolabs\GotenbergBundle\Builder\HtmlPdfBuilder; use Sensiolabs\GotenbergBundle\Builder\UrlPdfBuilder; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Symfony\Component\Filesystem\Filesystem; #[CoversClass(HtmlPdfBuilder::class)] @@ -15,7 +16,10 @@ public function testUrl(): void { $client = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); - $builder = new UrlPdfBuilder($client, self::FIXTURE_DIR, $filesystem); + + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new UrlPdfBuilder($client, $assetBaseDirFormatter); $builder->url('https://google.com'); $multipartFormData = $builder->getMultipartFormData(); diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 77827bc..bb07eff 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -116,7 +116,7 @@ private static function getBundleDefaultConfig(): array { return [ 'base_uri' => 'http://localhost:3000', - 'asset_base_dir' => '%kernel.project_dir%/public/', + 'base_directory' => '%kernel.project_dir%', 'default_options' => [ 'paper_width' => null, 'paper_height' => null, diff --git a/tests/Pdf/GotenbergTest.php b/tests/Pdf/GotenbergTest.php index 152f387..4ab15bc 100644 --- a/tests/Pdf/GotenbergTest.php +++ b/tests/Pdf/GotenbergTest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; +use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Sensiolabs\GotenbergBundle\Pdf\Gotenberg; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Mime\Part\DataPart; @@ -18,11 +19,12 @@ public function testUrlBuilderFactory(): void $gotenbergClient = $this->createMock(GotenbergClientInterface::class); $filesystem = $this->createMock(Filesystem::class); + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures'); + $gotenberg = new Gotenberg( $gotenbergClient, ['native_page_ranges' => '1-5'], - __DIR__.'/../Fixtures', - $filesystem, + $assetBaseDirFormatter, ); $builder = $gotenberg->url(); $builder->url('https://google.com'); @@ -36,11 +38,12 @@ public function testHtmlBuilderFactory(): void $filesystem = $this->createMock(Filesystem::class); $twig = $this->createMock(Environment::class); + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures'); + $gotenberg = new Gotenberg( $gotenbergClient, ['margin_top' => 3, 'margin_bottom' => 1], - __DIR__.'/../Fixtures', - $filesystem, + $assetBaseDirFormatter, $twig, ); $builder = $gotenberg->html(); @@ -69,11 +72,12 @@ public function testMarkdownBuilderFactory(): void $filesystem = $this->createMock(Filesystem::class); $twig = $this->createMock(Environment::class); + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures'); + $gotenberg = new Gotenberg( $gotenbergClient, [], - __DIR__.'/../Fixtures', - $filesystem, + $assetBaseDirFormatter, $twig, ); $builder = $gotenberg->markdown(); @@ -102,11 +106,12 @@ public function testOfficeBuilderFactory(): void $filesystem = $this->createMock(Filesystem::class); $twig = $this->createMock(Environment::class); + $assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures'); + $gotenberg = new Gotenberg( $gotenbergClient, ['native_page_ranges' => '1-5'], - __DIR__.'/../Fixtures', - $filesystem, + $assetBaseDirFormatter, $twig, ); $builder = $gotenberg->office();