1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\DompdfBundle\Tests\Wrapper; |
13
|
|
|
|
14
|
|
|
use Core23\DompdfBundle\DompdfEvents; |
15
|
|
|
use Core23\DompdfBundle\Exception\PdfException; |
16
|
|
|
use Core23\DompdfBundle\Factory\DompdfFactoryInterface; |
17
|
|
|
use Core23\DompdfBundle\Wrapper\DompdfWrapper; |
18
|
|
|
use Dompdf\Dompdf; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
|
22
|
|
|
final class DompdfWrapperTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
private $dompdfFactory; |
25
|
|
|
|
26
|
|
|
private $eventDispatcher; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DompdfWrapper |
30
|
|
|
*/ |
31
|
|
|
private $dompdfWrapper; |
32
|
|
|
|
33
|
|
|
private $dompdf; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected function setUp(): void |
39
|
|
|
{ |
40
|
|
|
$this->dompdf = $this->createMock(Dompdf::class); |
41
|
|
|
$this->dompdfFactory = $this->createMock(DompdfFactoryInterface::class); |
42
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
43
|
|
|
$this->dompdfWrapper = new DompdfWrapper($this->dompdfFactory, $this->eventDispatcher); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testStreamHtml(): void |
47
|
|
|
{ |
48
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
49
|
|
|
/** @noinspection HtmlUnknownTarget */ |
50
|
|
|
$input = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
51
|
|
|
|
52
|
|
|
$this->dompdfFactory |
53
|
|
|
->method('create') |
54
|
|
|
->willReturn($this->dompdf) |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
$this->dompdf->expects($this->once()) |
58
|
|
|
->method('loadHtml') |
59
|
|
|
->with($this->equalTo($input)) |
60
|
|
|
; |
61
|
|
|
$this->dompdf->expects($this->once()) |
62
|
|
|
->method('render') |
63
|
|
|
; |
64
|
|
|
$this->dompdf->expects($this->once()) |
65
|
|
|
->method('stream') |
66
|
|
|
->with($this->equalTo('file.pdf')) |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
$this->eventDispatcher->expects($this->once()) |
70
|
|
|
->method('dispatch') |
71
|
|
|
->with($this->equalTo(DompdfEvents::STREAM)) |
72
|
|
|
; |
73
|
|
|
|
74
|
|
|
$this->dompdfWrapper->streamHtml($input, 'file.pdf'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testStreamHtmlWithImg(): void |
78
|
|
|
{ |
79
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
80
|
|
|
/** @noinspection HtmlUnknownTarget */ |
81
|
|
|
$input = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
82
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
83
|
|
|
/** @noinspection HtmlUnknownTarget */ |
84
|
|
|
$output = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
85
|
|
|
|
86
|
|
|
$this->dompdfFactory |
87
|
|
|
->method('create') |
88
|
|
|
->with($this->equalTo(['tempDir' => 'bar'])) |
89
|
|
|
->willReturn($this->dompdf) |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$this->dompdf->expects($this->once()) |
93
|
|
|
->method('loadHtml') |
94
|
|
|
->with($this->equalTo($output)) |
95
|
|
|
; |
96
|
|
|
$this->dompdf->expects($this->once()) |
97
|
|
|
->method('render') |
98
|
|
|
; |
99
|
|
|
$this->dompdf->expects($this->once()) |
100
|
|
|
->method('stream') |
101
|
|
|
->with($this->equalTo('file.pdf')) |
102
|
|
|
; |
103
|
|
|
|
104
|
|
|
$this->eventDispatcher->expects($this->once()) |
105
|
|
|
->method('dispatch') |
106
|
|
|
->with($this->equalTo(DompdfEvents::STREAM)) |
107
|
|
|
; |
108
|
|
|
|
109
|
|
|
$this->dompdfWrapper->streamHtml($input, 'file.pdf', ['tempDir' => 'bar']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGetPdf(): void |
113
|
|
|
{ |
114
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
115
|
|
|
/** @noinspection HtmlUnknownTarget */ |
116
|
|
|
$input = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
117
|
|
|
|
118
|
|
|
$this->prepareOutput($input, 'BINARY_CONTENT'); |
119
|
|
|
|
120
|
|
|
$this->eventDispatcher->expects($this->once()) |
121
|
|
|
->method('dispatch') |
122
|
|
|
->with($this->equalTo(DompdfEvents::OUTPUT)) |
123
|
|
|
; |
124
|
|
|
|
125
|
|
|
$this->dompdfWrapper->getPdf($input, ['tempDir' => 'bar']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetPdfWithError(): void |
129
|
|
|
{ |
130
|
|
|
$this->expectException(PdfException::class); |
131
|
|
|
|
132
|
|
|
$input = '<h1>Foo</h1>Bar <b>baz</b>'; |
133
|
|
|
|
134
|
|
|
$this->prepareOutput($input); |
135
|
|
|
|
136
|
|
|
$this->eventDispatcher->expects($this->once()) |
137
|
|
|
->method('dispatch') |
138
|
|
|
->with($this->equalTo(DompdfEvents::OUTPUT)) |
139
|
|
|
; |
140
|
|
|
|
141
|
|
|
$this->dompdfWrapper->getPdf($input); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testGetStreamResponse(): void |
145
|
|
|
{ |
146
|
|
|
$this->dompdfFactory |
147
|
|
|
->method('create') |
148
|
|
|
->willReturn($this->dompdf) |
149
|
|
|
; |
150
|
|
|
|
151
|
|
|
$this->dompdf->expects($this->once()) |
152
|
|
|
->method('loadHtml') |
153
|
|
|
->with($this->equalTo('<h1>Title</h1>')) |
154
|
|
|
; |
155
|
|
|
$this->dompdf->expects($this->once()) |
156
|
|
|
->method('render') |
157
|
|
|
; |
158
|
|
|
$this->dompdf->expects($this->once()) |
159
|
|
|
->method('stream') |
160
|
|
|
->with($this->equalTo('file.pdf')) |
161
|
|
|
; |
162
|
|
|
|
163
|
|
|
$response = $this->dompdfWrapper->getStreamResponse('<h1>Title</h1>', 'file.pdf'); |
164
|
|
|
$response->sendContent(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $input |
169
|
|
|
* @param null $response |
170
|
|
|
*/ |
171
|
|
|
private function prepareOutput(string $input, $response = null): void |
172
|
|
|
{ |
173
|
|
|
$this->dompdfFactory |
174
|
|
|
->method('create') |
175
|
|
|
->willReturn($this->dompdf) |
176
|
|
|
; |
177
|
|
|
|
178
|
|
|
$this->dompdf->expects($this->once()) |
179
|
|
|
->method('loadHtml') |
180
|
|
|
->with($this->equalTo($input)) |
181
|
|
|
; |
182
|
|
|
$this->dompdf->expects($this->once()) |
183
|
|
|
->method('render') |
184
|
|
|
; |
185
|
|
|
$this->dompdf->expects($this->once()) |
186
|
|
|
->method('output') |
187
|
|
|
->willReturn($response) |
188
|
|
|
; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|