source: trunk/client/inc/hpdf5/spipu/html2pdf/src/Tests/SvgDrawerTest.php@ 398

Last change on this file since 398 was 347, checked in by roby, 3 years ago

Aggiornamento per compatibilità con php7.4

File size: 6.6 KB
Line 
1<?php
2
3namespace Spipu\Html2Pdf\Tests;
4
5use Phake;
6use Spipu\Html2Pdf\CssConverter;
7use Spipu\Html2Pdf\SvgDrawer;
8
9/**
10 * Class Html2PdfTest
11 */
12class SvgDrawerTest extends \PHPUnit_Framework_TestCase
13{
14 /**
15 * @var SvgDrawer
16 */
17 private $svgDrawer;
18
19 public function setUp()
20 {
21 $myPdf = Phake::mock('Spipu\Html2Pdf\MyPdf');
22
23 $cssConverter = new CssConverter();
24
25 $this->svgDrawer = new SvgDrawer($myPdf, $cssConverter);
26 }
27
28 /**
29 * Test IsDrawing Exception
30 *
31 * @expectedException \Spipu\Html2Pdf\Exception\HtmlParsingException
32 */
33 public function testIsDrawingException()
34 {
35 $properties = [
36 'x' => 0,
37 'y' => 0,
38 'w' => '100mm',
39 'h' => '100mm',
40 ];
41
42 $this->svgDrawer->startDrawing($properties);
43 $this->svgDrawer->startDrawing($properties);
44 }
45
46 /**
47 * Test IsDrawing
48 */
49 public function testIsDrawingOk()
50 {
51 $properties = [
52 'x' => 0,
53 'y' => 0,
54 'w' => 100,
55 'h' => 100,
56 ];
57
58 $this->assertFalse($this->svgDrawer->isDrawing());
59 $this->svgDrawer->startDrawing($properties);
60 $this->assertTrue($this->svgDrawer->isDrawing());
61 $this->svgDrawer->stopDrawing();
62 $this->assertFalse($this->svgDrawer->isDrawing());
63 }
64
65 /**
66 * Test properties
67 */
68 public function testProperties()
69 {
70 $properties = [
71 'x' => 1,
72 'y' => 2,
73 'w' => 3,
74 'h' => 4,
75 ];
76
77 $this->svgDrawer->startDrawing($properties);
78
79 $this->assertSame(1, $this->svgDrawer->getProperty('x'));
80 $this->assertSame(2, $this->svgDrawer->getProperty('y'));
81 $this->assertSame(3, $this->svgDrawer->getProperty('w'));
82 $this->assertSame(4, $this->svgDrawer->getProperty('h'));
83 }
84
85 /**
86 * Test: tokenize
87 *
88 * @param mixed $transform
89 * @param mixed $expected
90 *
91 * @dataProvider transformProvider
92 */
93 public function testTransform($transform, $expected)
94 {
95 $properties = [
96 'x' => 0,
97 'y' => 0,
98 'w' => 100,
99 'h' => 100,
100 ];
101
102 $this->svgDrawer->startDrawing($properties);
103
104 $result = $this->svgDrawer->prepareTransform($transform);
105
106 $this->assertArraySame($expected, $result);
107 }
108
109 /**
110 * @param array $expected
111 * @param array $result
112 */
113 protected function assertArraySame($expected, $result)
114 {
115 if (is_array($expected)) {
116 foreach ($expected as $key => $value) {
117 $expected[$key] = round($value, 5);
118 }
119 }
120
121 if (is_array($result)) {
122 foreach ($result as $key => $value) {
123 $result[$key] = round($value, 5);
124 }
125 }
126
127 $this->assertSame($expected, $result);
128 }
129
130 /**
131 * provider: tokenize
132 *
133 * @return array
134 */
135 public function transformProvider()
136 {
137 return array(
138 array(
139 false,
140 null
141 ),
142 array(
143 'no instruction',
144 null
145 ),
146 array(
147 'foo(1,2)',
148 null
149 ),
150 array(
151 'before scale( 0.1 , 0.2 ) after',
152 [
153 0.1, 0.,
154 0., 0.2,
155 0., 0.
156 ]
157 ),
158 array(
159 'scale(0.1,0.2)',
160 [
161 0.1, 0.,
162 0., 0.2,
163 0., 0.
164 ]
165 ),
166 array(
167 'scale(0.1)',
168 [
169 0.1, 0.,
170 0., 0.1,
171 0., 0.
172 ]
173 ),
174 array(
175 'scale(,)',
176 [
177 1., 0.,
178 0., 1.,
179 0., 0.
180 ]
181 ),
182 array(
183 'scale()',
184 [
185 1., 0.,
186 0., 1.,
187 0., 0.
188 ]
189 ),
190 array(
191 'translate()',
192 [
193 1., 0.,
194 0., 1.,
195 0., 0.
196 ]
197 ),
198 array(
199 'translate(10mm)',
200 [
201 1., 0.,
202 0., 1.,
203 10., 0.
204 ]
205 ),
206 array(
207 'translate(10mm, 20mm)',
208 [
209 1., 0.,
210 0., 1.,
211 10., 20.
212 ]
213 ),
214 array(
215 'rotate()',
216 [
217 1., 0.,
218 0., 1.,
219 0., 0.
220 ]
221 ),
222 array(
223 'rotate(90)',
224 [
225 0., 1.,
226 -1., 0.,
227 0., 0.
228 ]
229 ),
230 array(
231 'rotate(180)',
232 [
233 -1., 0.,
234 0., -1.,
235 0., 0.
236 ]
237 ),
238 array(
239 'rotate(180, 10mm, 10mm)',
240 [
241 -1., 0.,
242 0., -1.,
243 -20., -20.
244 ]
245 ),
246 array(
247 'skewx()',
248 [
249 1., 0.,
250 0., 1.,
251 0., 0.
252 ]
253 ),
254 array(
255 'skewx(45)',
256 [
257 1., 0.,
258 1., 1.,
259 0., 0.
260 ]
261 ),
262 array(
263 'skewy()',
264 [
265 1., 0.,
266 0., 1.,
267 0., 0.
268 ]
269 ),
270 array(
271 'skewy(45)',
272 [
273 1., 1.,
274 0., 1.,
275 0., 0.
276 ]
277 ),
278 array(
279 'matrix()',
280 [
281 0., 0.,
282 0., 0.,
283 0., 0.
284 ]
285 ),
286 array(
287 'matrix(1,2,3,4,5%,6%)',
288 [
289 1., 2.,
290 3., 4.,
291 5., 6.
292 ]
293 ),
294 );
295 }
296}
Note: See TracBrowser for help on using the repository browser.