Skip to content

Commit

Permalink
test: add test on adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthurlbc committed Nov 5, 2024
1 parent fd68228 commit 8124b96
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Backend/Dompdf/DompdfAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function generateFromDOMDocument(DOMDocument $DOMDocument): StreamInterfa
public function generateFromHtmlFile(SplFileInfo $file): StreamInterface
{
$dompdf = $this->buildDompdf();
$dompdf->loadHtmlFile($file->getPath());
$dompdf->loadHtmlFile($file->getPathname());

return $this->createStream($dompdf);
}
Expand Down
150 changes: 150 additions & 0 deletions src/Backend/Dompdf/Tests/DompdfAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\Dompdf\Tests;

use DOMDocument;
use KNPLabs\Snappy\Backend\Dompdf\DompdfAdapter;
use KNPLabs\Snappy\Backend\Dompdf\DompdfFactory;
use KNPLabs\Snappy\Core\Backend\Options;
use KNPLabs\Snappy\Core\Backend\Options\PageOrientation;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

final class DompdfAdapterTest extends TestCase
{
private DompdfAdapter $adapter;
private Options $options;
private StreamFactoryInterface $streamFactoryMock;
private string $tempDir;

protected function setUp(): void
{
$this->tempDir = sys_get_temp_dir();
$this->options = new Options(null, [
'output' => __DIR__,
'construct' => [
'chroot' => $this->tempDir,
]
]);

$this->streamFactoryMock = $this->createMock(StreamFactoryInterface::class);

$factory = new DompdfFactory($this->streamFactoryMock);
$this->adapter = $factory->create($this->options);
}

public function testGenerateFromDOMDocument(): void
{
$domDocument = new DOMDocument();
$domDocument->loadHTML('<html><body>Hello World</body></html>');

$expectedStreamMock = $this->createMock(StreamInterface::class);
$this->streamFactoryMock
->expects($this->once())
->method('createStream')
->with($this->isType('string'))
->willReturn($expectedStreamMock);

$output = $this->adapter->generateFromDOMDocument($domDocument);

$this->assertSame($expectedStreamMock, $output);
}

public function testGenerateFromHtmlFile(): void
{
$tempFilePath = $this->tempDir . '/test.html';
file_put_contents($tempFilePath, '<html><body>Temporary Test File</body></html>');
$fileMock = new \SplFileInfo($tempFilePath);

$expectedStreamMock = $this->createMock(StreamInterface::class);
$this->streamFactoryMock
->expects($this->once())
->method('createStream')
->with($this->isType('string'))
->willReturn($expectedStreamMock);

$output = $this->adapter->generateFromHtmlFile($fileMock);

$this->assertSame($expectedStreamMock, $output);

if (file_exists($tempFilePath)) {
unlink($tempFilePath);
}
}

public function testGenerateFromHtml(): void
{
$htmlContent = '<html><body>Test HTML content</body></html>';

$expectedStreamMock = $this->createMock(StreamInterface::class);
$this->streamFactoryMock
->expects($this->once())
->method('createStream')
->willReturn($expectedStreamMock);

$output = $this->adapter->generateFromHtml($htmlContent);

$this->assertSame($expectedStreamMock, $output);
}

public function testGenerateFromInvalidHtml(): void
{
$invalidHtmlContent = '<html><body><h1>Unclosed Header';

$expectedStreamMock = $this->createMock(StreamInterface::class);
$this->streamFactoryMock
->expects($this->once())
->method('createStream')
->willReturn($expectedStreamMock);

$output = $this->adapter->generateFromHtml($invalidHtmlContent);

$this->assertSame($expectedStreamMock, $output);
}

public function testGenerateFromEmptyHtml(): void
{
$htmlContent = '';

$expectedStreamMock = $this->createMock(StreamInterface::class);
$this->streamFactoryMock
->expects($this->once())
->method('createStream')
->willReturn($expectedStreamMock);

$output = $this->adapter->generateFromHtml($htmlContent);

$this->assertSame($expectedStreamMock, $output);
}

public function testStreamContentFromHtml(): void
{
$htmlContent = '<html><body>Test Content</body></html>';
$expectedOutput = 'PDF content for Test Content';

$this->streamFactoryMock
->method('createStream')
->willReturn($this->createStreamWithContent($expectedOutput));

$output = $this->adapter->generateFromHtml($htmlContent);
$this->assertSame($expectedOutput, $output->getContents());
}

private function createStreamWithContent(string $content): StreamInterface
{
$streamMock = $this->createMock(StreamInterface::class);
$streamMock->method('getContents')->willReturn($content);
return $streamMock;
}

public function testOptionsHandling(): void
{
$this->options = new Options(PageOrientation::LANDSCAPE, []);
$this->adapter = (new DompdfFactory($this->streamFactoryMock))->create($this->options);

$this->assertTrue(true);
}
}
20 changes: 20 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/DisableLocalFileAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class DisableLocalFileAccess implements ExtraOption
{
public function isRepeatable(): bool
{
return false;
}

public function compile(): array
{
return ['--disable-local-file-access'];
}
}
124 changes: 124 additions & 0 deletions src/Backend/WkHtmlToPdf/Tests/WkHtmlToPdfAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\Tests;

use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption\Title;
use KNPLabs\Snappy\Backend\WkHtmlToPdf\WkHtmlToPdfAdapter;
use KNPLabs\Snappy\Backend\WkHtmlToPdf\WkHtmlToPdfFactory;
use KNPLabs\Snappy\Core\Backend\Options;
use Nyholm\Psr7\Uri;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriFactoryInterface;
use SplFileInfo;
use Symfony\Component\Process\Exception\ProcessFailedException;

final class WkHtmlToPdfAdapterTest extends TestCase
{
private WkHtmlToPdfFactory $factory;
private WkHtmlToPdfAdapter $wkHtmlToPdfAdapter;
private StreamFactoryInterface $streamFactory;
private UriFactoryInterface $uriFactory;
private string $tempDir;

protected function setUp(): void
{
$this->tempDir = sys_get_temp_dir();
$this->streamFactory = $this->createMock(StreamFactoryInterface::class);
$this->uriFactory = $this->createMock(UriFactoryInterface::class);

$this->factory = new WkHtmlToPdfFactory(
'wkhtmltopdf',
60,
$this->streamFactory,
$this->uriFactory
);

$this->wkHtmlToPdfAdapter = $this->factory->create(new Options(null, []));
}

public function testGenerateFromHtmlFile(): void
{
$htmlContent = '<html><body><h1>Test PDF</h1></body></html>';
$testFilePath = $this->tempDir . '/test.html';
file_put_contents($testFilePath, $htmlContent);

$this->uriFactory
->method('createUri')
->with($testFilePath)
->willReturn(new Uri($testFilePath));

$stream = $this->createMock(StreamInterface::class);
$stream->method('getContents')->willReturn('%PDF-1.4 content');

$this->streamFactory
->expects($this->once())
->method('createStreamFromResource')
->willReturn($stream);


try {
$resultStream = $this->wkHtmlToPdfAdapter->generateFromHtmlFile(new \SplFileInfo($testFilePath));
} catch (\Exception $e) {
$this->fail("Erreur lors de la génération du PDF : " . $e->getMessage());
}

$this->assertNotNull($resultStream);
$this->assertInstanceOf(StreamInterface::class, $resultStream);
$this->assertNotEmpty($resultStream->getContents());

unlink($testFilePath);
}

public function testGenerateFromInvalidHtmlFile(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('File not found:');

$this->wkHtmlToPdfAdapter->generateFromHtmlFile(new SplFileInfo($this->tempDir . '/nonexistent.html'));
}

public function testGenerateWithAdditionalOptions(): void
{
$htmlContent = '<html><head><title>Test PDF</title></head><body><h1>Test PDF</h1></body></html>';
$testFilePath = $this->tempDir . '/test_with_options.html';
file_put_contents($testFilePath, $htmlContent);

$options = new Options(null, [
new Title('Test PDF Title')
]);

$this->factory = new WkHtmlToPdfFactory(
'wkhtmltopdf',
60,
$this->streamFactory,
$this->uriFactory
);

$this->wkHtmlToPdfAdapter = $this->factory->create($options);

$this->uriFactory
->method('createUri')
->with($testFilePath)
->willReturn(new Uri(realpath($testFilePath)));

$stream = $this->createMock(StreamInterface::class);
$stream->method('getContents')->willReturn('%PDF-1.4 content');

$this->streamFactory
->method('createStreamFromResource')
->willReturn($stream);


$resultStream = $this->wkHtmlToPdfAdapter->generateFromHtmlFile(new SplFileInfo($testFilePath));

$this->assertNotNull($resultStream);
$this->assertInstanceOf(StreamInterface::class, $resultStream);
$this->assertNotEmpty($resultStream->getContents());

unlink($testFilePath);
}
}
30 changes: 15 additions & 15 deletions src/Backend/WkHtmlToPdf/WkHtmlToPdfAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function generateFromHtmlFile(SplFileInfo $file): StreamInterface
}

return $this->generateFromUri(
$this->uriFactory->createUri($filepath)->withScheme('file')
$this->uriFactory->createUri($filepath)
);
}

Expand All @@ -64,10 +64,11 @@ public function generateFromUri(UriInterface $uri): StreamInterface
$process = new Process(
command: [
$this->binary,
'--log-level', 'none',
'--log-level',
'none',
'--quiet',
...$this->compileOptions(),
$uri->toString(),
(string) $uri,
$outputFile->getPathname(),
],
timeout: $this->timeout,
Expand All @@ -79,7 +80,7 @@ public function generateFromUri(UriInterface $uri): StreamInterface
throw new ProcessFailedException($process);
}

return $this->streamFactory->createFromResource($outputFile->resource);
return $this->streamFactory->createStreamFromResource($outputFile->resource);
}

private static function validateOptions(Options $options): void
Expand Down Expand Up @@ -113,17 +114,16 @@ private function compileOptions(): array
{
return array_reduce(
$this->options->extraOptions,
fn (array $carry, ExtraOption $extraOption) =>
$extraOption instanceof ExtraOption\Orientation && $this->options->pageOrientation !== null
? [
...$carry,
...ExtraOption\Orientation::fromPageOrientation($this->options->pageOrientation)->compile(),
]
: [
...$carry,
...$extraOption->compile(),
]
,
fn(array $carry, ExtraOption $extraOption) =>
$extraOption instanceof ExtraOption\Orientation && $this->options->pageOrientation !== null
? [
...$carry,
...ExtraOption\Orientation::fromPageOrientation($this->options->pageOrientation)->compile(),
]
: [
...$carry,
...$extraOption->compile(),
],
[],
);
}
Expand Down

0 comments on commit 8124b96

Please sign in to comment.