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\Contracts\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
|
|
|
protected function setUp(): void |
36
|
|
|
{ |
37
|
|
|
$this->dompdf = $this->createMock(Dompdf::class); |
38
|
|
|
$this->dompdfFactory = $this->createMock(DompdfFactoryInterface::class); |
39
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
40
|
|
|
$this->dompdfWrapper = new DompdfWrapper($this->dompdfFactory, $this->eventDispatcher); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testStreamHtml(): void |
44
|
|
|
{ |
45
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
46
|
|
|
/** @noinspection HtmlUnknownTarget */ |
47
|
|
|
$input = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
48
|
|
|
|
49
|
|
|
$this->dompdfFactory |
50
|
|
|
->method('create') |
51
|
|
|
->willReturn($this->dompdf) |
52
|
|
|
; |
53
|
|
|
|
54
|
|
|
$this->dompdf->expects(static::once()) |
55
|
|
|
->method('loadHtml') |
56
|
|
|
->with(static::equalTo($input)) |
57
|
|
|
; |
58
|
|
|
$this->dompdf->expects(static::once()) |
59
|
|
|
->method('render') |
60
|
|
|
; |
61
|
|
|
$this->dompdf->expects(static::once()) |
62
|
|
|
->method('stream') |
63
|
|
|
->with(static::equalTo('file.pdf')) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
$this->eventDispatcher->expects(static::once()) |
67
|
|
|
->method('dispatch') |
68
|
|
|
->with(static::anything(), static::equalTo(DompdfEvents::STREAM)) |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
$this->dompdfWrapper->streamHtml($input, 'file.pdf'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testStreamHtmlWithImg(): void |
75
|
|
|
{ |
76
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
77
|
|
|
/** @noinspection HtmlUnknownTarget */ |
78
|
|
|
$input = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
79
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
80
|
|
|
/** @noinspection HtmlUnknownTarget */ |
81
|
|
|
$output = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
82
|
|
|
|
83
|
|
|
$this->dompdfFactory |
84
|
|
|
->method('create') |
85
|
|
|
->with(static::equalTo(['tempDir' => 'bar'])) |
86
|
|
|
->willReturn($this->dompdf) |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$this->dompdf->expects(static::once()) |
90
|
|
|
->method('loadHtml') |
91
|
|
|
->with(static::equalTo($output)) |
92
|
|
|
; |
93
|
|
|
$this->dompdf->expects(static::once()) |
94
|
|
|
->method('render') |
95
|
|
|
; |
96
|
|
|
$this->dompdf->expects(static::once()) |
97
|
|
|
->method('stream') |
98
|
|
|
->with(static::equalTo('file.pdf')) |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
$this->eventDispatcher->expects(static::once()) |
102
|
|
|
->method('dispatch') |
103
|
|
|
->with(static::anything(), static::equalTo(DompdfEvents::STREAM)) |
104
|
|
|
; |
105
|
|
|
|
106
|
|
|
$this->dompdfWrapper->streamHtml($input, 'file.pdf', ['tempDir' => 'bar']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testGetPdf(): void |
110
|
|
|
{ |
111
|
|
|
/** @noinspection HtmlRequiredAltAttribute */ |
112
|
|
|
/** @noinspection HtmlUnknownTarget */ |
113
|
|
|
$input = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>"; |
114
|
|
|
|
115
|
|
|
$this->prepareOutput($input, 'BINARY_CONTENT'); |
116
|
|
|
|
117
|
|
|
$this->eventDispatcher->expects(static::once()) |
118
|
|
|
->method('dispatch') |
119
|
|
|
->with(static::anything(), static::equalTo(DompdfEvents::OUTPUT)) |
120
|
|
|
; |
121
|
|
|
|
122
|
|
|
$this->dompdfWrapper->getPdf($input, ['tempDir' => 'bar']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testGetPdfWithError(): void |
126
|
|
|
{ |
127
|
|
|
$this->expectException(PdfException::class); |
128
|
|
|
|
129
|
|
|
$input = '<h1>Foo</h1>Bar <b>baz</b>'; |
130
|
|
|
|
131
|
|
|
$this->prepareOutput($input); |
132
|
|
|
|
133
|
|
|
$this->eventDispatcher->expects(static::once()) |
134
|
|
|
->method('dispatch') |
135
|
|
|
->with(static::anything(), static::equalTo(DompdfEvents::OUTPUT)) |
136
|
|
|
; |
137
|
|
|
|
138
|
|
|
$this->dompdfWrapper->getPdf($input); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testGetStreamResponse(): void |
142
|
|
|
{ |
143
|
|
|
$this->dompdfFactory |
144
|
|
|
->method('create') |
145
|
|
|
->willReturn($this->dompdf) |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
$this->dompdf->expects(static::once()) |
149
|
|
|
->method('loadHtml') |
150
|
|
|
->with(static::equalTo('<h1>Title</h1>')) |
151
|
|
|
; |
152
|
|
|
$this->dompdf->expects(static::once()) |
153
|
|
|
->method('render') |
154
|
|
|
; |
155
|
|
|
$this->dompdf->expects(static::once()) |
156
|
|
|
->method('stream') |
157
|
|
|
->with(static::equalTo('file.pdf')) |
158
|
|
|
; |
159
|
|
|
|
160
|
|
|
$response = $this->dompdfWrapper->getStreamResponse('<h1>Title</h1>', 'file.pdf'); |
161
|
|
|
$response->sendContent(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function prepareOutput(string $input, ?string $response = null): void |
165
|
|
|
{ |
166
|
|
|
$this->dompdfFactory |
167
|
|
|
->method('create') |
168
|
|
|
->willReturn($this->dompdf) |
169
|
|
|
; |
170
|
|
|
|
171
|
|
|
$this->dompdf->expects(static::once()) |
172
|
|
|
->method('loadHtml') |
173
|
|
|
->with(static::equalTo($input)) |
174
|
|
|
; |
175
|
|
|
$this->dompdf->expects(static::once()) |
176
|
|
|
->method('render') |
177
|
|
|
; |
178
|
|
|
$this->dompdf->expects(static::once()) |
179
|
|
|
->method('output') |
180
|
|
|
->willReturn($response) |
181
|
|
|
; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|