-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
310 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/Backend/WkHtmlToPdf/ExtraOption/DisableLocalFileAccess.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
124
src/Backend/WkHtmlToPdf/Tests/WkHtmlToPdfAdapterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters