1 | <?php |
||
2 | |||
3 | namespace Wa72\HtmlPageDom\Tests; |
||
4 | |||
5 | use org\bovigo\vfs\vfsStream; |
||
6 | use PHPUnit\Framework\TestCase; |
||
7 | use Wa72\HtmlPageDom\HtmlPage; |
||
8 | |||
9 | class HtmlPageTest extends TestCase |
||
10 | { |
||
11 | public function setUp(): void |
||
12 | { |
||
13 | $this->root = vfsStream::setup('root'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
14 | } |
||
15 | |||
16 | public function testHtmlPage() |
||
17 | { |
||
18 | $hp = new HtmlPage(); |
||
19 | $this->assertEquals("<!DOCTYPE html>\n<html><head><title></title></head><body></body></html>\n", $hp->__toString()); |
||
20 | |||
21 | $title = 'Erste Testseite'; |
||
22 | $hp->setTitle($title); |
||
23 | $this->assertEquals($title, $hp->getTitle()); |
||
24 | |||
25 | $title = 'Seite "schön & gut" >> so wird\'s, süß'; |
||
26 | $hp->setTitle($title); |
||
27 | $this->assertEquals($title, $hp->getTitle()); |
||
28 | |||
29 | $description = 'Dies ist die erste "Testseite" >> so wird\'s, süß'; |
||
30 | $hp->setMeta('description', $description); |
||
31 | $this->assertEquals($description, $hp->getMeta('description')); |
||
32 | |||
33 | $hp->removeMeta('description'); |
||
34 | $this->assertNull($hp->getMeta('description')); |
||
35 | |||
36 | $bodycontent = '<div id="content">Testcontent1</div>'; |
||
37 | $body = $hp->filter('body'); |
||
38 | $body->setInnerHtml($bodycontent); |
||
39 | $this->assertEquals($bodycontent, $body->html()); |
||
40 | $this->assertEquals($bodycontent, $hp->filter('body')->html()); |
||
41 | |||
42 | $content = "<h1>Überschrift</h1>\n<p>bla bla <br><b>fett</b></p>"; |
||
43 | $hp->setHtmlById('content', $content); |
||
44 | // echo $hp; |
||
45 | $this->assertEquals($content, $hp->getElementById('content')->html()); |
||
46 | |||
47 | $url = 'http://www.tuebingen.de/'; |
||
48 | $hp->setBaseHref($url); |
||
49 | $this->assertEquals($url, $hp->getBaseHref()); |
||
50 | } |
||
51 | |||
52 | |||
53 | public function testClone() |
||
54 | { |
||
55 | $hp = new HtmlPage(); |
||
56 | $this->assertEquals("<!DOCTYPE html>\n<html><head><title></title></head><body></body></html>\n", $hp->__toString()); |
||
57 | |||
58 | $title = 'Erste Testseite'; |
||
59 | $hp->setTitle($title); |
||
60 | $this->assertEquals($title, $hp->getTitle()); |
||
61 | |||
62 | $hp2 = clone $hp; |
||
63 | |||
64 | $newtitle = 'Seitentitel neu'; |
||
65 | $hp->setTitle($newtitle); |
||
66 | |||
67 | $this->assertEquals($title, $hp2->getTitle()); |
||
68 | $this->assertEquals($newtitle, $hp->getTitle()); |
||
69 | } |
||
70 | |||
71 | public function testScript() |
||
72 | { |
||
73 | $html = <<<END |
||
74 | <!DOCTYPE html> |
||
75 | <html> |
||
76 | <head> |
||
77 | <title></title> |
||
78 | <script> |
||
79 | // this will be awesome |
||
80 | alert('Hello world'); |
||
81 | </script> |
||
82 | </head> |
||
83 | <body> |
||
84 | </body> |
||
85 | </html> |
||
86 | |||
87 | END; |
||
88 | $hp = new HtmlPage($html); |
||
89 | $hp->getBody()->append('<h1>Script Test</h1>'); |
||
90 | $newhtml = $hp->save(); |
||
91 | |||
92 | $expected = <<<END |
||
93 | <!DOCTYPE html> |
||
94 | <html><head><title></title><script> |
||
95 | // this will be awesome |
||
96 | alert('Hello world'); |
||
97 | </script></head><body> |
||
98 | <h1>Script Test</h1></body></html> |
||
99 | |||
100 | END; |
||
101 | $this->assertXmlStringEqualsXmlString($expected, $newhtml); |
||
102 | } |
||
103 | |||
104 | public function testMinify() |
||
105 | { |
||
106 | $html = <<<END |
||
107 | <!DOCTYPE html> |
||
108 | <html> |
||
109 | <head> |
||
110 | <title></title> |
||
111 | <script> |
||
112 | // this will be awesome |
||
113 | alert('Hello world'); |
||
114 | </script> |
||
115 | </head> |
||
116 | <body> |
||
117 | <h1>TEST</h1> |
||
118 | <p class=""> |
||
119 | asdf jksdlf ajsfk |
||
120 | <b>jasdf |
||
121 | jaksfd asdf</b> |
||
122 | <a>jasdf jaks</a> |
||
123 | </p> |
||
124 | </body> |
||
125 | </html> |
||
126 | |||
127 | END; |
||
128 | $hp = new HtmlPage($html); |
||
129 | |||
130 | $expected = <<<END |
||
131 | <!DOCTYPE html> |
||
132 | <html><head><title></title><script>alert('Hello world');</script></head><body><h1>TEST</h1><p>asdf jksdlf ajsfk <b>jasdf jaksfd asdf</b> <a>jasdf jaks</a></p></body></html> |
||
133 | |||
134 | END; |
||
135 | $this->assertEquals($expected, $hp->minify()->save()); |
||
136 | } |
||
137 | |||
138 | public function testIndent() |
||
139 | { |
||
140 | $html = <<<END |
||
141 | <!DOCTYPE html> |
||
142 | <html> |
||
143 | <head> |
||
144 | <title></title> |
||
145 | <script> |
||
146 | // this will be awesome |
||
147 | alert('Hello world'); |
||
148 | </script> |
||
149 | </head> |
||
150 | <body> |
||
151 | <h1>TEST</h1> |
||
152 | <p class="something"> |
||
153 | asdf jksdlf ajsfk |
||
154 | <b>jasdf |
||
155 | jaksfd asdf</b> |
||
156 | <a>jasdf jaks</a> |
||
157 | </p> |
||
158 | </body> |
||
159 | </html> |
||
160 | |||
161 | END; |
||
162 | $hp = new HtmlPage($html); |
||
163 | |||
164 | $expected = <<<END |
||
165 | <!DOCTYPE html> |
||
166 | <html> |
||
167 | <head> |
||
168 | <title></title> |
||
169 | <script> |
||
170 | // this will be awesome |
||
171 | alert('Hello world'); |
||
172 | </script> |
||
173 | </head> |
||
174 | <body> |
||
175 | <h1>TEST</h1> |
||
176 | <p class="something">asdf jksdlf ajsfk <b>jasdf jaksfd asdf</b> <a>jasdf jaks</a></p> |
||
177 | </body> |
||
178 | </html> |
||
179 | |||
180 | END; |
||
181 | $this->assertEquals($expected, $hp->indent()->save()); |
||
182 | } |
||
183 | |||
184 | public function testGetCrawler() |
||
185 | { |
||
186 | $html = <<<END |
||
187 | <!DOCTYPE html> |
||
188 | <html> |
||
189 | <head> |
||
190 | <title></title> |
||
191 | <script> |
||
192 | // this will be awesome |
||
193 | alert('Hello world'); |
||
194 | </script> |
||
195 | </head> |
||
196 | <body> |
||
197 | <h1>TEST</h1> |
||
198 | <p class=""> |
||
199 | asdf jksdlf ajsfk |
||
200 | <b>jasdf |
||
201 | jaksfd asdf</b> |
||
202 | <a>jasdf jaks</a> |
||
203 | </p> |
||
204 | </body> |
||
205 | </html> |
||
206 | |||
207 | END; |
||
208 | |||
209 | $hp = new HtmlPage($html); |
||
210 | $this->assertEquals('<h1>TEST</h1>', $hp->getCrawler()->filter('h1')->saveHtml()); |
||
211 | } |
||
212 | |||
213 | public function testGetDOMDocument() |
||
214 | { |
||
215 | $html = <<<END |
||
216 | <!DOCTYPE html> |
||
217 | <html> |
||
218 | <head> |
||
219 | <title></title> |
||
220 | <script> |
||
221 | // this will be awesome |
||
222 | alert('Hello world'); |
||
223 | </script> |
||
224 | </head> |
||
225 | <body> |
||
226 | <h1>TEST</h1> |
||
227 | <p class=""> |
||
228 | asdf jksdlf ajsfk |
||
229 | <b>jasdf |
||
230 | jaksfd asdf</b> |
||
231 | <a>jasdf jaks</a> |
||
232 | </p> |
||
233 | </body> |
||
234 | </html> |
||
235 | |||
236 | END; |
||
237 | |||
238 | $hp = new HtmlPage($html); |
||
239 | $this->assertInstanceOf('\DOMDocument', $hp->getDOMDocument()); |
||
240 | } |
||
241 | |||
242 | public function testSetTitleOnNoTitleElement() |
||
243 | { |
||
244 | $html = <<<END |
||
245 | <!DOCTYPE html> |
||
246 | <html> |
||
247 | <head> |
||
248 | <script> |
||
249 | // this will be awesome |
||
250 | alert('Hello world'); |
||
251 | </script> |
||
252 | </head> |
||
253 | <body> |
||
254 | <h1>TEST</h1> |
||
255 | <p class=""> |
||
256 | asdf jksdlf ajsfk |
||
257 | <b>jasdf |
||
258 | jaksfd asdf</b> |
||
259 | <a>jasdf jaks</a> |
||
260 | </p> |
||
261 | </body> |
||
262 | </html> |
||
263 | |||
264 | END; |
||
265 | |||
266 | $hp = new HtmlPage($html); |
||
267 | $hp->setTitle('TEST'); |
||
268 | $this->assertEquals('TEST', $hp->getTitle()); |
||
269 | } |
||
270 | |||
271 | public function testGetTitleShouldReturnNull() |
||
272 | { |
||
273 | $html = <<<END |
||
274 | <!DOCTYPE html> |
||
275 | <html> |
||
276 | <head> |
||
277 | <script> |
||
278 | // this will be awesome |
||
279 | alert('Hello world'); |
||
280 | </script> |
||
281 | </head> |
||
282 | <body> |
||
283 | <h1>TEST</h1> |
||
284 | <p class=""> |
||
285 | asdf jksdlf ajsfk |
||
286 | <b>jasdf |
||
287 | jaksfd asdf</b> |
||
288 | <a>jasdf jaks</a> |
||
289 | </p> |
||
290 | </body> |
||
291 | </html> |
||
292 | |||
293 | END; |
||
294 | |||
295 | $hp = new HtmlPage($html); |
||
296 | $this->assertNull($hp->getTitle()); |
||
297 | } |
||
298 | |||
299 | public function testGetBaseHrefShouldReturnNull() |
||
300 | { |
||
301 | $hp = new HtmlPage('<!DOCTYPE html><html><head><title>TEST</title></head><body>Hello</body></html>'); |
||
302 | $this->assertNull($hp->getBaseHref()); |
||
303 | } |
||
304 | |||
305 | public function testGetHeadNodeShouldAddTheHeadTag() |
||
306 | { |
||
307 | $hp = new HtmlPage('<!DOCTYPE html><html><body>Hello</body></html>'); |
||
308 | $this->assertInstanceOf('\DOMElement', $hp->getHeadNode()); |
||
309 | $this->assertEquals('<head></head>', (string) $hp->getHead()); |
||
310 | } |
||
311 | |||
312 | public function testGetBodyNodeShouldAddTheBodyTag() |
||
313 | { |
||
314 | $hp = new HtmlPage('<!DOCTYPE html><html></html>'); |
||
315 | $this->assertInstanceOf('\DOMElement', $hp->getBodyNode()); |
||
316 | $this->assertEquals('<body></body>', (string) $hp->getBody()); |
||
317 | } |
||
318 | |||
319 | public function testTrimNewlines() |
||
320 | { |
||
321 | $html = <<<END |
||
322 | <!DOCTYPE html> |
||
323 | <html> |
||
324 | <head> |
||
325 | <title>TEST</title> |
||
326 | </head> |
||
327 | </html> |
||
328 | END; |
||
329 | |||
330 | $this->assertEquals('<!DOCTYPE html> <html> <head> <title>TEST</title> </head> </html>', (string) HtmlPage::trimNewlines($html)); |
||
331 | } |
||
332 | |||
333 | public function testSaveOnFileName() |
||
334 | { |
||
335 | $hp = new HtmlPage('<!DOCTYPE html><html><head><title>TEST</title></head></html>'); |
||
336 | $hp->save(vfsStream::url('root/save.html')); |
||
337 | $this->assertFileExists(vfsStream::url('root/save.html')); |
||
338 | } |
||
339 | } |
||
340 |