|
| 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 | +} |
0 commit comments