Skip to content

Commit

Permalink
Update tests + add formatter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenRenaux committed Feb 27, 2024
1 parent 0edcf0b commit dd17304
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 46 deletions.
6 changes: 4 additions & 2 deletions src/Builder/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@ public function assets(string ...$paths): static
*/
public function addAsset(string $path): static
{
$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path)));
$resolvedPath = $this->asset->resolve($path);

$this->formFields['assets'][$this->asset->resolve($path)] = $dataPart;
$dataPart = new DataPart(new DataPartFile($resolvedPath));

$this->formFields['assets'][$resolvedPath] = $dataPart;

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Formatter/AssetBaseDirFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Symfony\Component\Filesystem\Filesystem;

final readonly class AssetBaseDirFormatter
class AssetBaseDirFormatter
{
private string $baseDir;

public function __construct(private Filesystem $filesystem, private string $projectDir, string $baseDir)
public function __construct(private readonly Filesystem $filesystem, private readonly string $projectDir, string $baseDir)
{
$this->baseDir = rtrim($baseDir, '/');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Builder/AbstractBuilderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

abstract class AbstractBuilderTestCase extends TestCase
{
protected const FIXTURE_DIR = '/../Fixtures';
protected const FIXTURE_DIR = __DIR__.'/../Fixtures';

protected static Environment $twig;

public static function setUpBeforeClass(): void
{
self::$twig = new Environment(new FilesystemLoader(__DIR__.'/../Fixtures/templates'));
self::$twig = new Environment(new FilesystemLoader(self::FIXTURE_DIR.'/templates'));
}
}
39 changes: 24 additions & 15 deletions tests/Builder/HtmlPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ final class HtmlPdfBuilderTest extends AbstractBuilderTestCase
public function testWithConfigurations(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->any())
->method('resolve')
->willReturn(self::FIXTURE_DIR.'/templates/content.html')
;

$builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter);
$builder->contentFile('content.html');
Expand Down Expand Up @@ -60,9 +62,11 @@ public function testWithConfigurations(): void
public function testWithTemplate(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->any())
->method('resolve')
->willReturn(self::FIXTURE_DIR.'/templates/content.html')
;

$builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter, self::$twig);
$builder->content('content.html.twig');
Expand All @@ -80,9 +84,11 @@ public function testWithTemplate(): void
public function testWithAssets(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->exactly(2))
->method('resolve')
->willReturnOnConsecutiveCalls(self::FIXTURE_DIR.'/templates/content.html', self::FIXTURE_DIR.'/assets/logo.png')
;

$builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter);
$builder->contentFile('content.html');
Expand All @@ -102,9 +108,11 @@ public function testWithAssets(): void
public function testWithHeader(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->exactly(2))
->method('resolve')
->willReturnOnConsecutiveCalls(self::FIXTURE_DIR.'/templates/header.html', self::FIXTURE_DIR.'/templates/content.html')
;

$builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter);
$builder->headerFile('header.html');
Expand All @@ -127,9 +135,10 @@ public function testInvalidTwigTemplate(): void
$this->expectExceptionMessage('Could not render template "invalid.html.twig" into PDF part "index.html".');

$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->never())
->method('resolve')
;

$builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter, self::$twig);

Expand Down
9 changes: 5 additions & 4 deletions tests/Builder/LibreOfficePdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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;

#[CoversClass(LibreOfficePdfBuilder::class)]
Expand All @@ -31,9 +30,11 @@ public static function provideValidOfficeFiles(): iterable
public function testOfficeFiles(string $filePath, string $contentType): void
{
$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->any())
->method('resolve')
->willReturn(self::FIXTURE_DIR.'/'.$filePath)
;

$builder = new LibreOfficePdfBuilder($client, $assetBaseDirFormatter);
$builder->files($filePath);
Expand Down
9 changes: 5 additions & 4 deletions tests/Builder/MarkdownPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;

#[CoversClass(MarkdownPdfBuilder::class)]
Expand All @@ -15,9 +14,11 @@ final class MarkdownPdfBuilderTest extends AbstractBuilderTestCase
public function testMarkdownFile(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->any())
->method('resolve')
->willReturnOnConsecutiveCalls(self::FIXTURE_DIR.'/templates/template.html', self::FIXTURE_DIR.'/assets/file.md', self::FIXTURE_DIR.'/assets/file.md')
;

$builder = new MarkdownPdfBuilder($client, $assetBaseDirFormatter);
$builder
Expand Down
8 changes: 4 additions & 4 deletions tests/Builder/UrlPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Sensiolabs\GotenbergBundle\Builder\UrlPdfBuilder;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Symfony\Component\Filesystem\Filesystem;

#[CoversClass(HtmlPdfBuilder::class)]
final class UrlPdfBuilderTest extends AbstractBuilderTestCase
{
public function testUrl(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, self::FIXTURE_DIR, self::FIXTURE_DIR);
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->never())
->method('resolve')
;

$builder = new UrlPdfBuilder($client, $assetBaseDirFormatter);
$builder->url('https://google.com');
Expand Down
33 changes: 33 additions & 0 deletions tests/Formatter/AssetBaseDirFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Formatter;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Symfony\Component\Filesystem\Filesystem;

#[CoversClass(AssetBaseDirFormatter::class)]
final class AssetBaseDirFormatterTest extends TestCase
{
/**
* @return iterable<string, array<int, string>>
*/
public static function generateBaseDirectoryAndPath(): iterable
{
yield 'absolute path and relative base dir' => [__DIR__.'/../Fixtures/assets/logo.png', '/assets'];
yield 'relative path and absolute base dir' => ['logo.png', __DIR__.'/../Fixtures/assets'];
yield 'relative path and relative base dir' => ['logo.png', 'assets'];
yield 'relative path and relative base dir with end slash' => ['logo.png', 'assets/'];
}

#[DataProvider('generateBaseDirectoryAndPath')]
public function testResolveWithAbsolutePath(string $path, string $baseDirectory): void
{
$filesystem = new Filesystem();
$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', $baseDirectory);
$resolvedPath = $assetBaseDirFormatter->resolve($path);
self::assertSame(__DIR__.'/../Fixtures/assets/logo.png', $resolvedPath);
}
}
32 changes: 19 additions & 13 deletions tests/Pdf/GotenbergTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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;
use Twig\Environment;

Expand All @@ -17,9 +16,10 @@ final class GotenbergTest extends TestCase
public function testUrlBuilderFactory(): void
{
$gotenbergClient = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures');
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->never())
->method('resolve')
;

$gotenberg = new Gotenberg(
$gotenbergClient,
Expand All @@ -35,10 +35,12 @@ public function testUrlBuilderFactory(): void
public function testHtmlBuilderFactory(): void
{
$gotenbergClient = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);
$twig = $this->createMock(Environment::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures');
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->exactly(1))
->method('resolve')
->willReturn(__DIR__.'/..Fixtures/templates/content.html')
;

$gotenberg = new Gotenberg(
$gotenbergClient,
Expand Down Expand Up @@ -69,10 +71,12 @@ public function testHtmlBuilderFactory(): void
public function testMarkdownBuilderFactory(): void
{
$gotenbergClient = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);
$twig = $this->createMock(Environment::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures');
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->any())
->method('resolve')
->willReturnOnConsecutiveCalls(__DIR__.'/../Fixtures/assets/file.md', __DIR__.'/../Fixtures/assets/file.md', __DIR__.'/../Fixtures/templates/wrapper.html')
;

$gotenberg = new Gotenberg(
$gotenbergClient,
Expand Down Expand Up @@ -103,10 +107,12 @@ public function testMarkdownBuilderFactory(): void
public function testOfficeBuilderFactory(): void
{
$gotenbergClient = $this->createMock(GotenbergClientInterface::class);
$filesystem = $this->createMock(Filesystem::class);
$twig = $this->createMock(Environment::class);

$assetBaseDirFormatter = new AssetBaseDirFormatter($filesystem, __DIR__.'/../Fixtures', '/../Fixtures');
$assetBaseDirFormatter = $this->createMock(AssetBaseDirFormatter::class);
$assetBaseDirFormatter->expects($this->any())
->method('resolve')
->willReturn(__DIR__.'/../Fixtures/assets/office/document.odt')
;

$gotenberg = new Gotenberg(
$gotenbergClient,
Expand Down

0 comments on commit dd17304

Please sign in to comment.