Skip to content

Commit 633ff13

Browse files
committed
test: add test on adapters
1 parent c94f35b commit 633ff13

File tree

5 files changed

+311
-17
lines changed

5 files changed

+311
-17
lines changed

src/Backend/Dompdf/DompdfAdapter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function generateFromDOMDocument(DOMDocument $DOMDocument): StreamInterfa
4343
public function generateFromHtmlFile(SplFileInfo $file): StreamInterface
4444
{
4545
$dompdf = $this->buildDompdf();
46-
$dompdf->loadHtmlFile($file->getPath());
46+
$dompdf->loadHtmlFile($file->getPathname());
4747

4848
return $this->createStream($dompdf);
4949
}
@@ -58,7 +58,7 @@ public function generateFromHtml(string $html): StreamInterface
5858

5959
private function buildDompdf(): Dompdf\Dompdf
6060
{
61-
return new Dompdf\Dompdf( $this->compileConstructOptions());
61+
return new Dompdf\Dompdf($this->compileConstructOptions());
6262
}
6363

6464
private function compileConstructOptions(): Dompdf\Options
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Backend\Dompdf\Tests;
6+
7+
use DOMDocument;
8+
use KNPLabs\Snappy\Backend\Dompdf\DompdfAdapter;
9+
use KNPLabs\Snappy\Backend\Dompdf\DompdfFactory;
10+
use KNPLabs\Snappy\Core\Backend\Options;
11+
use KNPLabs\Snappy\Core\Backend\Options\PageOrientation;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Http\Message\StreamFactoryInterface;
14+
use Psr\Http\Message\StreamInterface;
15+
16+
final class DompdfAdapterTest extends TestCase
17+
{
18+
private DompdfAdapter $adapter;
19+
private Options $options;
20+
private StreamFactoryInterface $streamFactoryMock;
21+
private string $tempDir;
22+
23+
protected function setUp(): void
24+
{
25+
$this->tempDir = sys_get_temp_dir();
26+
$this->options = new Options(null, [
27+
'output' => __DIR__,
28+
'construct' => [
29+
'chroot' => $this->tempDir,
30+
]
31+
]);
32+
33+
$this->streamFactoryMock = $this->createMock(StreamFactoryInterface::class);
34+
35+
$factory = new DompdfFactory($this->streamFactoryMock);
36+
$this->adapter = $factory->create($this->options);
37+
}
38+
39+
public function testGenerateFromDOMDocument(): void
40+
{
41+
$domDocument = new DOMDocument();
42+
$domDocument->loadHTML('<html><body>Hello World</body></html>');
43+
44+
$expectedStreamMock = $this->createMock(StreamInterface::class);
45+
$this->streamFactoryMock
46+
->expects($this->once())
47+
->method('createStream')
48+
->with($this->isType('string'))
49+
->willReturn($expectedStreamMock);
50+
51+
$output = $this->adapter->generateFromDOMDocument($domDocument);
52+
53+
$this->assertSame($expectedStreamMock, $output);
54+
}
55+
56+
public function testGenerateFromHtmlFile(): void
57+
{
58+
$tempFilePath = $this->tempDir . '/test.html';
59+
file_put_contents($tempFilePath, '<html><body>Temporary Test File</body></html>');
60+
$fileMock = new \SplFileInfo($tempFilePath);
61+
62+
$expectedStreamMock = $this->createMock(StreamInterface::class);
63+
$this->streamFactoryMock
64+
->expects($this->once())
65+
->method('createStream')
66+
->with($this->isType('string'))
67+
->willReturn($expectedStreamMock);
68+
69+
$output = $this->adapter->generateFromHtmlFile($fileMock);
70+
71+
$this->assertSame($expectedStreamMock, $output);
72+
73+
if (file_exists($tempFilePath)) {
74+
unlink($tempFilePath);
75+
}
76+
}
77+
78+
public function testGenerateFromHtml(): void
79+
{
80+
$htmlContent = '<html><body>Test HTML content</body></html>';
81+
82+
$expectedStreamMock = $this->createMock(StreamInterface::class);
83+
$this->streamFactoryMock
84+
->expects($this->once())
85+
->method('createStream')
86+
->willReturn($expectedStreamMock);
87+
88+
$output = $this->adapter->generateFromHtml($htmlContent);
89+
90+
$this->assertSame($expectedStreamMock, $output);
91+
}
92+
93+
public function testGenerateFromInvalidHtml(): void
94+
{
95+
$invalidHtmlContent = '<html><body><h1>Unclosed Header';
96+
97+
$expectedStreamMock = $this->createMock(StreamInterface::class);
98+
$this->streamFactoryMock
99+
->expects($this->once())
100+
->method('createStream')
101+
->willReturn($expectedStreamMock);
102+
103+
$output = $this->adapter->generateFromHtml($invalidHtmlContent);
104+
105+
$this->assertSame($expectedStreamMock, $output);
106+
}
107+
108+
public function testGenerateFromEmptyHtml(): void
109+
{
110+
$htmlContent = '';
111+
112+
$expectedStreamMock = $this->createMock(StreamInterface::class);
113+
$this->streamFactoryMock
114+
->expects($this->once())
115+
->method('createStream')
116+
->willReturn($expectedStreamMock);
117+
118+
$output = $this->adapter->generateFromHtml($htmlContent);
119+
120+
$this->assertSame($expectedStreamMock, $output);
121+
}
122+
123+
public function testStreamContentFromHtml(): void
124+
{
125+
$htmlContent = '<html><body>Test Content</body></html>';
126+
$expectedOutput = 'PDF content for Test Content';
127+
128+
$this->streamFactoryMock
129+
->method('createStream')
130+
->willReturn($this->createStreamWithContent($expectedOutput));
131+
132+
$output = $this->adapter->generateFromHtml($htmlContent);
133+
$this->assertSame($expectedOutput, $output->getContents());
134+
}
135+
136+
private function createStreamWithContent(string $content): StreamInterface
137+
{
138+
$streamMock = $this->createMock(StreamInterface::class);
139+
$streamMock->method('getContents')->willReturn($content);
140+
return $streamMock;
141+
}
142+
143+
public function testOptionsHandling(): void
144+
{
145+
$this->options = new Options(PageOrientation::LANDSCAPE, []);
146+
$this->adapter = (new DompdfFactory($this->streamFactoryMock))->create($this->options);
147+
148+
$this->assertTrue(true);
149+
}
150+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
6+
7+
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
8+
9+
final class DisableLocalFileAccess implements ExtraOption
10+
{
11+
public function isRepeatable(): bool
12+
{
13+
return false;
14+
}
15+
16+
public function compile(): array
17+
{
18+
return ['--disable-local-file-access'];
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\Tests;
6+
7+
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption\Title;
8+
use KNPLabs\Snappy\Backend\WkHtmlToPdf\WkHtmlToPdfAdapter;
9+
use KNPLabs\Snappy\Backend\WkHtmlToPdf\WkHtmlToPdfFactory;
10+
use KNPLabs\Snappy\Core\Backend\Options;
11+
use Nyholm\Psr7\Uri;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Http\Message\StreamFactoryInterface;
14+
use Psr\Http\Message\StreamInterface;
15+
use Psr\Http\Message\UriFactoryInterface;
16+
use SplFileInfo;
17+
use Symfony\Component\Process\Exception\ProcessFailedException;
18+
19+
final class WkHtmlToPdfAdapterTest extends TestCase
20+
{
21+
private WkHtmlToPdfFactory $factory;
22+
private WkHtmlToPdfAdapter $wkHtmlToPdfAdapter;
23+
private StreamFactoryInterface $streamFactory;
24+
private UriFactoryInterface $uriFactory;
25+
private string $tempDir;
26+
27+
protected function setUp(): void
28+
{
29+
$this->tempDir = sys_get_temp_dir();
30+
$this->streamFactory = $this->createMock(StreamFactoryInterface::class);
31+
$this->uriFactory = $this->createMock(UriFactoryInterface::class);
32+
33+
$this->factory = new WkHtmlToPdfFactory(
34+
'wkhtmltopdf',
35+
60,
36+
$this->streamFactory,
37+
$this->uriFactory
38+
);
39+
40+
$this->wkHtmlToPdfAdapter = $this->factory->create(new Options(null, []));
41+
}
42+
43+
public function testGenerateFromHtmlFile(): void
44+
{
45+
$htmlContent = '<html><body><h1>Test PDF</h1></body></html>';
46+
$testFilePath = $this->tempDir . '/test.html';
47+
file_put_contents($testFilePath, $htmlContent);
48+
49+
$this->uriFactory
50+
->method('createUri')
51+
->with($testFilePath)
52+
->willReturn(new Uri($testFilePath));
53+
54+
$stream = $this->createMock(StreamInterface::class);
55+
$stream->method('getContents')->willReturn('%PDF-1.4 content');
56+
57+
$this->streamFactory
58+
->expects($this->once())
59+
->method('createStreamFromResource')
60+
->willReturn($stream);
61+
62+
63+
try {
64+
$resultStream = $this->wkHtmlToPdfAdapter->generateFromHtmlFile(new \SplFileInfo($testFilePath));
65+
} catch (\Exception $e) {
66+
$this->fail("Erreur lors de la génération du PDF : " . $e->getMessage());
67+
}
68+
69+
$this->assertNotNull($resultStream);
70+
$this->assertInstanceOf(StreamInterface::class, $resultStream);
71+
$this->assertNotEmpty($resultStream->getContents());
72+
73+
unlink($testFilePath);
74+
}
75+
76+
public function testGenerateFromInvalidHtmlFile(): void
77+
{
78+
$this->expectException(\RuntimeException::class);
79+
$this->expectExceptionMessage('File not found:');
80+
81+
$this->wkHtmlToPdfAdapter->generateFromHtmlFile(new SplFileInfo($this->tempDir . '/nonexistent.html'));
82+
}
83+
84+
public function testGenerateWithAdditionalOptions(): void
85+
{
86+
$htmlContent = '<html><head><title>Test PDF</title></head><body><h1>Test PDF</h1></body></html>';
87+
$testFilePath = $this->tempDir . '/test_with_options.html';
88+
file_put_contents($testFilePath, $htmlContent);
89+
90+
$options = new Options(null, [
91+
new Title('Test PDF Title')
92+
]);
93+
94+
$this->factory = new WkHtmlToPdfFactory(
95+
'wkhtmltopdf',
96+
60,
97+
$this->streamFactory,
98+
$this->uriFactory
99+
);
100+
101+
$this->wkHtmlToPdfAdapter = $this->factory->create($options);
102+
103+
$this->uriFactory
104+
->method('createUri')
105+
->with($testFilePath)
106+
->willReturn(new Uri(realpath($testFilePath)));
107+
108+
$stream = $this->createMock(StreamInterface::class);
109+
$stream->method('getContents')->willReturn('%PDF-1.4 content');
110+
111+
$this->streamFactory
112+
->method('createStreamFromResource')
113+
->willReturn($stream);
114+
115+
116+
$resultStream = $this->wkHtmlToPdfAdapter->generateFromHtmlFile(new SplFileInfo($testFilePath));
117+
118+
$this->assertNotNull($resultStream);
119+
$this->assertInstanceOf(StreamInterface::class, $resultStream);
120+
$this->assertNotEmpty($resultStream->getContents());
121+
122+
unlink($testFilePath);
123+
}
124+
}

src/Backend/WkHtmlToPdf/WkHtmlToPdfAdapter.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function generateFromHtmlFile(SplFileInfo $file): StreamInterface
5252
}
5353

5454
return $this->generateFromUri(
55-
$this->uriFactory->createUri($filepath)->withScheme('file')
55+
$this->uriFactory->createUri($filepath)
5656
);
5757
}
5858

@@ -63,10 +63,11 @@ public function generateFromUri(UriInterface $uri): StreamInterface
6363
$process = new Process(
6464
command: [
6565
$this->binary,
66-
'--log-level', 'none',
66+
'--log-level',
67+
'none',
6768
'--quiet',
6869
...$this->compileOptions(),
69-
$uri->toString(),
70+
(string) $uri,
7071
$outputFile->getPathname(),
7172
],
7273
timeout: $this->timeout,
@@ -78,7 +79,7 @@ public function generateFromUri(UriInterface $uri): StreamInterface
7879
throw new ProcessFailedException($process);
7980
}
8081

81-
return $this->streamFactory->createFromResource($outputFile->resource);
82+
return $this->streamFactory->createStreamFromResource($outputFile->resource);
8283
}
8384

8485
private static function validateOptions(Options $options): void
@@ -112,17 +113,16 @@ private function compileOptions(): array
112113
{
113114
return array_reduce(
114115
$this->options->extraOptions,
115-
fn (array $carry, ExtraOption $extraOption) =>
116-
$extraOption instanceof ExtraOption\Orientation && $this->options->pageOrientation !== null
117-
? [
118-
...$carry,
119-
...ExtraOption\Orientation::fromPageOrientation($this->options->pageOrientation)->compile(),
120-
]
121-
: [
122-
...$carry,
123-
...$extraOption->compile(),
124-
]
125-
,
116+
fn(array $carry, ExtraOption $extraOption) =>
117+
$extraOption instanceof ExtraOption\Orientation && $this->options->pageOrientation !== null
118+
? [
119+
...$carry,
120+
...ExtraOption\Orientation::fromPageOrientation($this->options->pageOrientation)->compile(),
121+
]
122+
: [
123+
...$carry,
124+
...$extraOption->compile(),
125+
],
126126
[],
127127
);
128128
}

0 commit comments

Comments
 (0)