source: trunk/client/inc/hpdf5/spipu/html2pdf/src/Tests/Parsing/TagParserTest.php@ 347

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

Aggiornamento per compatibilità con php7.4

File size: 7.3 KB
Line 
1<?php
2/**
3 * Html2Pdf Library - Tests
4 *
5 * HTML => PDF converter
6 * distributed under the OSL-3.0 License
7 *
8 * @package Html2pdf
9 * @author Laurent MINGUET <webmaster@html2pdf.fr>
10 * @copyright 2017 Laurent MINGUET
11 */
12
13namespace Spipu\Html2Pdf\Tests\Parsing;
14
15use Spipu\Html2Pdf\Parsing\Node;
16use Spipu\Html2Pdf\Parsing\TagParser;
17
18/**
19 * Class TagParserTest
20 */
21class TagParserTest extends \PHPUnit_Framework_TestCase
22{
23 /**
24 * @var TagParser
25 */
26 private $parser;
27
28 protected function setUp()
29 {
30 $textParser = $this->getMockBuilder('Spipu\Html2Pdf\Parsing\TextParser')
31 ->disableOriginalConstructor()
32 ->setMethods(['prepareTxt'])
33 ->getMock();
34
35 $textParser
36 ->expects($this->any())
37 ->method('prepareTxt')
38 ->will($this->returnCallback([$this, 'mockPrepareTxt']));
39
40 $this->parser = new TagParser($textParser);
41 }
42
43 /**
44 * mock of prepareTxt method
45 *
46 * @param $txt
47 * @param bool $spaces
48 * @return mixed
49 */
50 public function mockPrepareTxt($txt, $spaces = true)
51 {
52 return $txt;
53 }
54
55 /**
56 * @param string $code
57 * @param array $expected
58 *
59 * @dataProvider tagAttributesProvider
60 */
61 public function testExtractTagAttributes($code, $expected)
62 {
63 $result = $this->parser->extractTagAttributes($code);
64
65 $this->assertEquals($expected, $result);
66 }
67
68 /**
69 * @return array
70 */
71 public function tagAttributesProvider()
72 {
73 return array(
74 array('attr=value', array('attr' => 'value')),
75 array('attr="value"', array('attr' => 'value')),
76 array('attr=\'value\'', array('attr' => 'value')),
77 array('attr="value with spaces"', array('attr' => 'value with spaces')),
78 array('attr="value with \'quotes"', array('attr' => 'value with \'quotes')),
79 array('my attr="value"', array('attr' => 'value')),
80 array('attr1=val1 attr2="value2"', array('attr1' => 'val1', 'attr2' => 'value2')),
81 );
82 }
83
84 /**
85 * Test if a bad tag is detected
86 *
87 * @expectedException \Spipu\Html2Pdf\Exception\HtmlParsingException
88 */
89 public function testAnalyzeTagBadTag()
90 {
91 $this->parser->analyzeTag('test');
92 }
93
94 /**
95 * Test basic open, close, autoclose tags
96 */
97 public function testBasicTags()
98 {
99 $result = $this->parser->analyzeTag('<my_tag/>');
100
101 $this->assertTrue($result instanceof Node);
102 $this->assertSame('my_tag', $result->getName());
103 $this->assertSame(true, $result->isAutoClose());
104 $this->assertSame(false, $result->isClose());
105
106 $result->setLine(10);
107 $this->assertSame(10, $result->getLine());
108
109 $result->setParam('attr', 'value');
110 $this->assertSame('value', $result->getParam('attr'));
111
112
113 $result = $this->parser->analyzeTag('<my_tag>');
114
115 $this->assertSame('my_tag', $result->getName());
116 $this->assertSame(false, $result->isAutoClose());
117 $this->assertSame(false, $result->isClose());
118 $this->assertSame(['style' => [], 'num' => 0], $result->getParams());
119 $this->assertSame('default', $result->getParam('attr', 'default'));
120
121 $result = $this->parser->analyzeTag('</my_tag>');
122
123 $this->assertSame('my_tag', $result->getName());
124 $this->assertSame(false, $result->isAutoClose());
125 $this->assertSame(true, $result->isClose());
126
127 }
128
129 /**
130 * Test styles
131 */
132 public function testAnalyzeAttributes()
133 {
134 $result = $this->parser->analyzeTag('<img src="my_src" alt="my alt"/>');
135 $this->assertSame('my_src', $result->getParam('src'));
136 $this->assertSame('my alt', $result->getParam('alt'));
137
138 $result = $this->parser->analyzeTag('<a href="my_src" title="my title"/>');
139 $this->assertSame('my_src', $result->getParam('href'));
140 $this->assertSame('my title', $result->getParam('title'));
141
142
143 $result = $this->parser->analyzeTag('<input type="text" value="my value" class="my_class" />');
144 $this->assertSame('text', $result->getParam('type'));
145 $this->assertSame('my value', $result->getParam('value'));
146 $this->assertSame('my_class', $result->getParam('class'));
147
148 $result = $this->parser->analyzeTag('<my_tag width="10" height="20" align="center" valign="bottom" bgcolor="red">');
149 $this->assertSame('10px', $result->getStyle('width'));
150 $this->assertSame('20px', $result->getStyle('height'));
151 $this->assertSame('center', $result->getStyle('text-align'));
152 $this->assertSame('bottom', $result->getStyle('vertical-align'));
153 $this->assertSame('red', $result->getStyle('background'));
154
155 $result = $this->parser->analyzeTag('<img align="right">');
156 $this->assertSame('right', $result->getStyle('float'));
157
158 $result = $this->parser->analyzeTag('<table cellpadding="1" cellspacing="2">');
159 $this->assertSame('1px', $result->getParam('cellpadding'));
160 $this->assertSame('2px', $result->getParam('cellspacing'));
161
162 $result = $this->parser->analyzeTag('<td rowspan="0" colspan="2px">');
163 $this->assertSame(1, $result->getParam('rowspan'));
164 $this->assertSame(2, $result->getParam('colspan'));
165
166 $result = $this->parser->analyzeTag('<my_tag color="red">');
167 $this->assertSame('red', $result->getParam('color'));
168 $this->assertSame(null, $result->getStyle('color'));
169
170 $result = $this->parser->analyzeTag('<font color="red">');
171 $this->assertSame(null, $result->getParam('color'));
172 $this->assertSame('red', $result->getStyle('color'));
173 }
174
175 /**
176 * Test borders
177 */
178 public function testBorders()
179 {
180 $result = $this->parser->analyzeTag('<div border="1" bordercolor="red" />');
181
182 $this->assertSame('div', $result->getName());
183 $this->assertSame('solid 1px red', $result->getParam('border'));
184 $this->assertSame('solid 1px red', $result->getStyle('border'));
185
186 $result = $this->parser->analyzeTag('<div border="0" bordercolor="red" />');
187
188 $this->assertSame('div', $result->getName());
189 $this->assertSame('none', $result->getParam('border'));
190 $this->assertSame('none', $result->getStyle('border'));
191 }
192
193 /**
194 * Test levels
195 */
196 public function testLevels()
197 {
198 $result = $this->parser->analyzeTag('<basic_tag>');
199 $this->assertSame(0, $result->getParam('num'));
200
201 $result = $this->parser->analyzeTag('<table>');
202 $this->assertSame(1, $result->getParam('num'));
203
204 $result = $this->parser->analyzeTag('<ol>');
205 $this->assertSame(2, $result->getParam('num'));
206
207 $result = $this->parser->analyzeTag('<ul>');
208 $this->assertSame(3, $result->getParam('num'));
209
210 $result = $this->parser->analyzeTag('</ul>');
211 $this->assertSame('ul', $result->getName());
212 $this->assertSame(3, $result->getParam('num'));
213
214 $result = $this->parser->analyzeTag('</ol>');
215 $this->assertSame(2, $result->getParam('num'));
216
217 $result = $this->parser->analyzeTag('</table>');
218 $this->assertSame(1, $result->getParam('num'));
219
220 $result = $this->parser->analyzeTag('<basic_tag>');
221 $this->assertSame(0, $result->getParam('num'));
222 }
223}
Note: See TracBrowser for help on using the repository browser.