1 | <?php |
||
2 | |||
3 | namespace Wa72\HtmlPageDom\Tests; |
||
4 | |||
5 | use PHPUnit\Framework\TestCase; |
||
6 | use Wa72\HtmlPageDom\HtmlPageCrawler; |
||
7 | |||
8 | class HtmlPageCrawlerTest extends TestCase |
||
9 | { |
||
10 | /** |
||
11 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::__construct |
||
12 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::filter |
||
13 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::nodeName |
||
14 | */ |
||
15 | public function testHtmlPageCrawler() |
||
16 | { |
||
17 | $c = new HtmlPageCrawler(); |
||
18 | $c->addHtmlContent('<!doctype html><html><body><div id="content"><h1>Title</h1></div></body></html>'); |
||
19 | $title = $c->filter('#content > h1'); |
||
20 | |||
21 | $this->assertInstanceOf('\Wa72\HtmlPageDom\HtmlPageCrawler', $title); |
||
22 | $this->assertInstanceOf('\DOMNode', $title->getNode(0)); |
||
23 | $this->assertEquals('h1', $title->nodeName()); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * |
||
28 | * |
||
29 | * @param $string |
||
30 | * @return string |
||
31 | */ |
||
32 | private function _ignoreNewlines($string) |
||
33 | { |
||
34 | return str_replace("\n", '', $string); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::setInnerHtml |
||
39 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::prepend |
||
40 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::makeEmpty |
||
41 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::setAttribute |
||
42 | */ |
||
43 | public function testManipulationFunctions() |
||
44 | { |
||
45 | $c = new HtmlPageCrawler(); |
||
46 | $c->addHtmlContent('<!doctype html><html><body><div id="content"><h1>Title</h1></div></body></html>'); |
||
47 | |||
48 | $content = $c->filter('#content'); |
||
49 | $content->append('<p>Das ist ein Testabsatz'); |
||
50 | $this->assertEquals("<h1>Title</h1><p>Das ist ein Testabsatz</p>", $this->_ignoreNewlines($content->html())); |
||
51 | |||
52 | $content->setInnerHtml('<p>Ein neuer <b>Inhalt</p>'); |
||
53 | $this->assertEquals('<p>Ein neuer <b>Inhalt</b></p>', $content->html()); |
||
54 | |||
55 | $content->prepend('<h1>Neue Überschrift'); |
||
56 | $this->assertEquals('<h1>Neue Überschrift</h1><p>Ein neuer <b>Inhalt</b></p>', $content->html()); |
||
57 | |||
58 | $h1 = $content->filter('h1'); |
||
59 | $this->assertEquals('Neue Überschrift', $h1->text()); |
||
60 | |||
61 | $b = $content->filter('b'); |
||
62 | $this->assertEquals('Inhalt', $b->text()); |
||
63 | |||
64 | $b2 = $c->filter('#content p b'); |
||
65 | $this->assertEquals('Inhalt', $b2->text()); |
||
66 | |||
67 | $content->append('<p class="a2">Zweiter Absatz</p>'); |
||
68 | $content->append('<p class="a3"><b>Dritter Absatz</b> und noch mehr Text</p>'); |
||
69 | |||
70 | $a3 = $content->filter('p.a3'); |
||
71 | $this->assertEquals('<b>Dritter Absatz</b> und noch mehr Text', $a3->html()); |
||
72 | |||
73 | $a3b = $a3->filter('b'); |
||
74 | $this->assertEquals('Dritter Absatz', $a3b->text()); |
||
75 | |||
76 | $body = $c->filter('body'); |
||
77 | $this->assertEquals('<div id="content"><h1>Neue Überschrift</h1><p>Ein neuer <b>Inhalt</b></p><p class="a2">Zweiter Absatz</p><p class="a3"><b>Dritter Absatz</b> und noch mehr Text</p></div>', $this->_ignoreNewlines($body->html())); |
||
78 | |||
79 | $paragraphs = $c->filter('p'); |
||
80 | $this->assertEquals(3, count($paragraphs)); |
||
81 | |||
82 | $paragraphs->append('<span class="appended">.</span>'); |
||
83 | $this->assertEquals('<p>Ein neuer <b>Inhalt</b><span class="appended">.</span></p><p class="a2">Zweiter Absatz<span class="appended">.</span></p><p class="a3"><b>Dritter Absatz</b> und noch mehr Text<span class="appended">.</span></p>', $c->filter('p')->saveHTML()); |
||
84 | |||
85 | $body->makeEmpty(); |
||
86 | $this->assertEmpty($body->html()); |
||
87 | |||
88 | $body->setAttribute('class', 'mybodyclass'); |
||
89 | $this->assertEquals('mybodyclass', $body->attr('class')); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::append |
||
94 | */ |
||
95 | public function testAppend() |
||
96 | { |
||
97 | // Testing append string to several elements |
||
98 | $c = new HtmlPageCrawler('<p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>'); |
||
99 | $c->filter('p')->append('<br>Appended Text'); |
||
100 | $this->assertEquals('<p>Paragraph 1<br>Appended Text</p><p>Paragraph 2<br>Appended Text</p><p>Paragraph 3<br>Appended Text</p>', $c->saveHTML()); |
||
101 | |||
102 | // Testing append HtmlPageCrawler to several elements |
||
103 | $c = new HtmlPageCrawler('<p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>'); |
||
104 | $c->filter('p')->append(new HtmlPageCrawler('<br>Appended Text')); |
||
105 | $this->assertEquals('<p>Paragraph 1<br>Appended Text</p><p>Paragraph 2<br>Appended Text</p><p>Paragraph 3<br>Appended Text</p>', $c->saveHTML()); |
||
106 | |||
107 | // Testing append DOMNode to several elements |
||
108 | $c = new HtmlPageCrawler('<p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>'); |
||
109 | $app = $c->getDOMDocument()->createElement('span', 'Appended Text'); |
||
110 | $c->filter('p')->append($app); |
||
111 | $this->assertEquals('<p>Paragraph 1<span>Appended Text</span></p><p>Paragraph 2<span>Appended Text</span></p><p>Paragraph 3<span>Appended Text</span></p>', $c->saveHTML()); |
||
112 | |||
113 | $c = new HtmlPageCrawler('<div id="content"><span>Append Self</span></div>'); |
||
114 | $c->filter('#content')->append($c->filter('span')); |
||
115 | $this->assertEquals('<div id="content"><span>Append Self</span></div>', $c->saveHTML()); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::appendTo |
||
120 | */ |
||
121 | public function testAppendTo() |
||
122 | { |
||
123 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1><em>Big</em></div>'); |
||
124 | $c->filter('em')->appendTo($c->filter('h1')); |
||
125 | $this->assertEquals('<div id="content"><h1>Title<em>Big</em></h1></div>', $c->saveHTML()); |
||
126 | |||
127 | $c = new HtmlPageCrawler('<div id="content"><h1>Self Title</h1></div>'); |
||
128 | $c->filter('h1')->appendTo($c->filter('h1')); |
||
129 | $this->assertEquals('<div id="content"><h1>Self Title</h1></div>', $c->saveHTML()); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::isHtmlDocument |
||
134 | */ |
||
135 | public function testIsHtmlDocument() |
||
136 | { |
||
137 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
138 | $dom->loadHTML('<!DOCTYPE html><html><body><div id="content"><h1>Title</h1></div></body></html>'); |
||
139 | $c = new HtmlPageCrawler($dom); |
||
140 | |||
141 | $this->assertTrue($c->isHtmlDocument()); |
||
142 | |||
143 | $t = $c->filter('body'); |
||
144 | $this->assertFalse($t->isHtmlDocument()); |
||
145 | |||
146 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
147 | $this->assertFalse($c->isHtmlDocument()); |
||
148 | |||
149 | $c = new HtmlPageCrawler('<html><body><div id="content"><h1>Title</h1></div></body></html>'); |
||
150 | $this->assertTrue($c->isHtmlDocument()); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::saveHTML |
||
155 | */ |
||
156 | public function testSaveHTML() |
||
157 | { |
||
158 | $html = "<!DOCTYPE html><html><body><h1>Title</h1><p>Paragraph 1</p><p>Paragraph 2</p></body></html>"; |
||
159 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
160 | $dom->loadHTML($html); |
||
161 | $c = new HtmlPageCrawler($dom); |
||
162 | $this->assertEquals($html, $this->_ignoreNewlines($c->saveHTML())); |
||
163 | $ps = $c->filter('p'); |
||
164 | $this->assertEquals('<p>Paragraph 1</p><p>Paragraph 2</p>', $ps->saveHTML()); |
||
165 | $t = $c->filter('h1'); |
||
166 | $this->assertEquals('<h1>Title</h1>', $t->saveHTML()); |
||
167 | |||
168 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
169 | $this->assertEquals('<div id="content"><h1>Title</h1></div>', $c->saveHTML()); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::css |
||
174 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::getStyle |
||
175 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::setStyle |
||
176 | */ |
||
177 | public function testCss() |
||
178 | { |
||
179 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
180 | $dom->loadHTML('<!DOCTYPE html><html><body><div id="content"><h1 style=" margin-top: |
||
181 | 10px;border-bottom: 1px solid red">Title</h1></div></body></html>'); |
||
182 | $c = new HtmlPageCrawler($dom); |
||
183 | $t = $c->filter('h1'); |
||
184 | $this->assertEquals('10px', $t->css('margin-top')); |
||
185 | $this->assertEquals('1px solid red', $t->css('border-bottom')); |
||
186 | $t->css('margin-bottom', '20px'); |
||
187 | $this->assertEquals('20px', $t->css('margin-bottom')); |
||
188 | $this->assertEquals('10px', $t->getStyle('margin-top')); |
||
189 | $this->assertEquals('<h1 style="margin-top: 10px;border-bottom: 1px solid red;margin-bottom: 20px;">Title</h1>', $t->saveHTML()); |
||
190 | $t->setStyle('border-bottom', ''); |
||
191 | $this->assertEquals('<h1 style="margin-top: 10px;margin-bottom: 20px;">Title</h1>', $t->saveHTML()); |
||
192 | $t->setStyle('padding-top', '0'); |
||
193 | $this->assertEquals('<h1 style="margin-top: 10px;margin-bottom: 20px;padding-top: 0;">Title</h1>', $t->saveHTML()); |
||
194 | $this->assertEquals('0', $t->getStyle('padding-top')); |
||
195 | $this->assertNull($t->getStyle('border-bottom')); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::addClass |
||
200 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::removeClass |
||
201 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::hasClass |
||
202 | */ |
||
203 | public function testClasses() |
||
204 | { |
||
205 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
206 | $dom->loadHTML('<!DOCTYPE html><html><body><div id="content"><h1 class="style_class">Title</h1></div></body></html>'); |
||
207 | $c = new HtmlPageCrawler($dom); |
||
208 | $t = $c->filter('h1'); |
||
209 | $t->addClass('ueberschrift'); |
||
210 | $t->addClass('nochneklasse'); |
||
211 | $t->addClass('style_class'); |
||
212 | $this->assertEquals('<h1 class="style_class ueberschrift nochneklasse">Title</h1>', $t->saveHTML()); |
||
213 | $this->assertTrue($t->hasClass('ueberschrift')); |
||
214 | $this->assertTrue($t->hasClass('nochneklasse')); |
||
215 | $this->assertTrue($t->hasClass('style_class')); |
||
216 | $t->removeClass('nochneklasse'); |
||
217 | $this->assertTrue($t->hasClass('ueberschrift')); |
||
218 | $this->assertFalse($t->hasClass('nochneklasse')); |
||
219 | $t->addClass('class1 class2'); |
||
220 | $this->assertTrue($t->hasClass('class1')); |
||
221 | $this->assertTrue($t->hasClass('class2')); |
||
222 | |||
223 | $c1 = new HtmlPageCrawler('<p class="a"></p><p class="b"></p><p class="c"></p>'); |
||
224 | $this->assertTrue($c1->hasClass('b')); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::addContent |
||
229 | */ |
||
230 | public function testAddContent() |
||
231 | { |
||
232 | $c = new HtmlPageCrawler(); |
||
233 | $c->addContent('<html><body><div id="content"><h1>Title</h1></div></body>'); |
||
234 | $this->assertEquals( |
||
235 | '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">' |
||
236 | . "" . '<html><body><div id="content"><h1>Title</h1></div></body></html>' . "", |
||
237 | $this->_ignoreNewlines($c->saveHTML()) |
||
238 | ); |
||
239 | |||
240 | $c = new HtmlPageCrawler(); |
||
241 | $c->addContent('<div id="content"><h1>Title'); |
||
242 | $this->assertEquals('<div id="content"><h1>Title</h1></div>', $c->saveHTML()); |
||
243 | |||
244 | $c = new HtmlPageCrawler(); |
||
245 | $c->addContent('<p>asdf<p>asdfaf</p>'); |
||
246 | $this->assertEquals(2, count($c)); |
||
247 | $this->assertEquals('<p>asdf</p><p>asdfaf</p>', $c->saveHTML()); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::before |
||
252 | */ |
||
253 | public function testBefore() |
||
254 | { |
||
255 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
256 | $c->filter('h1')->before('<p>Text before h1</p>'); |
||
257 | $this->assertEquals('<div id="content"><p>Text before h1</p><h1>Title</h1></div>', $c->saveHTML()); |
||
258 | |||
259 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
260 | $c->filter('h1')->before(new HtmlPageCrawler('<p>Text before h1</p><p>and more text before</p>')); |
||
261 | $this->assertEquals('<div id="content"><p>Text before h1</p><p>and more text before</p><h1>Title</h1></div>', $c->saveHTML()); |
||
262 | |||
263 | $c = new HtmlPageCrawler('<div id="content"><h1>Self Before</h1></div>'); |
||
264 | $c->filter('h1')->before($c->filter('h1')); |
||
265 | $this->assertEquals('<div id="content"><h1>Self Before</h1></div>', $c->saveHTML()); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::insertBefore |
||
270 | */ |
||
271 | public function testInsertBefore() |
||
272 | { |
||
273 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1><p>Text before h1</p></div>'); |
||
274 | $c->filter('p')->insertBefore($c->filter('h1')); |
||
275 | $this->assertEquals('<div id="content"><p>Text before h1</p><h1>Title</h1></div>', $c->saveHTML()); |
||
276 | |||
277 | $c = new HtmlPageCrawler('<div id="content"><h1>Self Insert Before Title</h1><p>Text after h1</p></div>'); |
||
278 | $c->filter('h1')->insertBefore($c->filter('h1')); |
||
279 | $this->assertEquals('<div id="content"><h1>Self Insert Before Title</h1><p>Text after h1</p></div>', $c->saveHTML()); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::after |
||
284 | */ |
||
285 | public function testAfter() |
||
286 | { |
||
287 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
288 | $c->filter('h1')->after('<p>Text after h1</p>'); |
||
289 | $this->assertEquals('<div id="content"><h1>Title</h1><p>Text after h1</p></div>', $c->saveHTML()); |
||
290 | |||
291 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1><h1>Title2</h1></div>'); |
||
292 | $c->filter('h1')->after(new HtmlPageCrawler('<p>Text after h1</p><p>and more text after</p>')); |
||
293 | $this->assertEquals('<div id="content"><h1>Title</h1><p>Text after h1</p><p>and more text after</p><h1>Title2</h1><p>Text after h1</p><p>and more text after</p></div>', $c->saveHTML()); |
||
294 | |||
295 | $c = new HtmlPageCrawler('<div id="content"><h1>Self After</h1></div>'); |
||
296 | $c->filter('h1')->after($c->filter('h1')); |
||
297 | $this->assertEquals('<div id="content"><h1>Self After</h1></div>', $c->saveHTML()); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::insertAfter |
||
302 | */ |
||
303 | public function testInsertAfter() |
||
304 | { |
||
305 | $c = new HtmlPageCrawler('<div id="content"><p>Text after h1</p><h1>Title</h1></div>'); |
||
306 | $c->filter('p')->insertAfter($c->filter('h1')); |
||
307 | $this->assertEquals('<div id="content"><h1>Title</h1><p>Text after h1</p></div>', $c->saveHTML()); |
||
308 | |||
309 | $c = new HtmlPageCrawler('<div id="content"><p>Text before h1</p><h1>Self Insert After Title</h1></div>'); |
||
310 | $c->filter('h1')->insertAfter($c->filter('h1')); |
||
311 | $this->assertEquals('<div id="content"><p>Text before h1</p><h1>Self Insert After Title</h1></div>', $c->saveHTML()); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::prepend |
||
316 | */ |
||
317 | public function testPrepend() |
||
318 | { |
||
319 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
320 | $c->filter('#content')->prepend('<p>Text before h1</p>'); |
||
321 | $this->assertEquals('<div id="content"><p>Text before h1</p><h1>Title</h1></div>', $c->saveHTML()); |
||
322 | |||
323 | $c = new HtmlPageCrawler('<div id="content"></div>'); |
||
324 | $c->filter('#content')->prepend(new HtmlPageCrawler('<p>Text before h1</p><p>and more text before</p>')); |
||
325 | $this->assertEquals('<div id="content"><p>Text before h1</p><p>and more text before</p></div>', $c->saveHTML()); |
||
326 | |||
327 | $c = new HtmlPageCrawler('<div id="content"><span>Prepend Self</span></div>'); |
||
328 | $c->filter('#content')->prepend($c->filter('span')); |
||
329 | $this->assertEquals('<div id="content"><span>Prepend Self</span></div>', $c->saveHTML()); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::prependTo |
||
334 | */ |
||
335 | public function testPrependTo() |
||
336 | { |
||
337 | $c = new HtmlPageCrawler('<div id="content"><p>Text before</p></div>'); |
||
338 | $c->filter('p')->prependTo('Text'); |
||
339 | $this->assertEquals('<div id="content"><p>Text before</p></div>', $c->saveHTML()); |
||
340 | |||
341 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
342 | $c->filter('#content')->prependTo(new HtmlPageCrawler('<p>paragraph</p>')); |
||
343 | $this->assertEquals('<div id="content"><h1>Title</h1></div>', $c->saveHTML()); |
||
344 | |||
345 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1><em>Big</em></div>'); |
||
346 | $c->filter('em')->prependTo($c->filter('h1')); |
||
347 | $this->assertEquals('<div id="content"><h1><em>Big</em>Title</h1></div>', $c->saveHTML()); |
||
348 | |||
349 | $c = new HtmlPageCrawler('<div id="content"><h1>Self Title</h1></div>'); |
||
350 | $c->filter('h1')->prependTo($c->filter('h1')); |
||
351 | $this->assertEquals('<div id="content"><h1>Self Title</h1></div>', $c->saveHTML()); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::wrap |
||
356 | */ |
||
357 | public function testWrap() |
||
358 | { |
||
359 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
360 | $c->filter('h1')->wrap('<div class="innercontent">'); |
||
361 | $this->assertEquals('<div id="content"><div class="innercontent"><h1>Title</h1></div></div>', $c->saveHTML()); |
||
362 | |||
363 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
364 | $c->filter('h1')->wrap('<div class="ic">asdf<div class="a1"><div class="a2"></div></div></div></div>'); |
||
365 | $this->assertEquals('<div id="content"><div class="ic">asdf<div class="a1"><div class="a2"><h1>Title</h1></div></div></div></div>', $c->saveHTML()); |
||
366 | |||
367 | $c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>'); |
||
368 | $c->filter('h1')->wrap('<div class="ic">asdf</div><div>jkl</div>'); // wrap has more than 1 root element |
||
369 | $this->assertEquals('<div id="content"><div class="ic">asdf<h1>Title</h1></div></div>', $c->saveHTML()); // only first element is used |
||
370 | |||
371 | // Test for wrapping multiple nodes |
||
372 | $c = new HtmlPageCrawler('<div id="content"><p>p1</p><p>p2</p></div>'); |
||
373 | $c->filter('p')->wrap('<div class="p"></div>'); |
||
374 | $this->assertEquals('<div id="content"><div class="p"><p>p1</p></div><div class="p"><p>p2</p></div></div>', $c->saveHTML()); |
||
375 | |||
376 | $c = new HtmlPageCrawler('plain text node'); |
||
377 | $c->wrap('<div class="ic"></div>'); |
||
378 | $this->assertEquals('<div class="ic">plain text node</div>', $c->parents()->eq(0)->saveHTML()); |
||
379 | |||
380 | $c = HtmlPageCrawler::create('<div>'); |
||
381 | $m = HtmlPageCrawler::create('message 1')->appendTo($c); |
||
382 | $m->wrap('<p>'); |
||
383 | $m = HtmlPageCrawler::create('message 2')->appendTo($c); |
||
384 | $m->wrap('<p>'); |
||
385 | $this->assertEquals('<div><p>message 1</p><p>message 2</p></div>', $c->saveHTML()); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::replaceWith |
||
390 | */ |
||
391 | public function testReplaceWith() |
||
392 | { |
||
393 | $c = HtmlPageCrawler::create('<div id="content"><p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p></div>'); |
||
394 | $oldparagraphs = $c->filter('p')->replaceWith('<div>newtext 1</div><div>newtext 2</div>'); |
||
395 | $this->assertEquals('<div id="content"><div>newtext 1</div><div>newtext 2</div><div>newtext 1</div><div>newtext 2</div><div>newtext 1</div><div>newtext 2</div></div>', $c->saveHTML()); |
||
396 | $this->assertEquals('<p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p>', $oldparagraphs->saveHTML()); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::replaceAll |
||
401 | */ |
||
402 | public function testReplaceAll() |
||
403 | { |
||
404 | $c = HtmlPageCrawler::create('<div id="content"><p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p></div>'); |
||
405 | $new = HtmlPageCrawler::create('<div>newtext 1</div><div>newtext 2</div>'); |
||
406 | $new->replaceAll($c->filter('p')); |
||
407 | $this->assertEquals('<div id="content"><div>newtext 1</div><div>newtext 2</div><div>newtext 1</div><div>newtext 2</div><div>newtext 1</div><div>newtext 2</div></div>', $c->saveHTML()); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::wrapAll |
||
412 | */ |
||
413 | public function testWrapAll() |
||
414 | { |
||
415 | $c = HtmlPageCrawler::create('<div id="content"><div>Before</div><p>Absatz 1</p><div>Inner</div><p>Absatz 2</p><p>Absatz 3</p><div>After</div></div>'); |
||
416 | $c->filter('p')->wrapAll('<div class="a">'); |
||
417 | $this->assertEquals('<div id="content"><div>Before</div><div class="a"><p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p></div><div>Inner</div><div>After</div></div>', $c->saveHTML()); |
||
418 | |||
419 | // Test for wrapping with elements that have children |
||
420 | $c = HtmlPageCrawler::create('<div id="content"><p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p></div>'); |
||
421 | $c->filter('p')->wrapAll('<article><section><div class="a"></div></section></article>'); |
||
422 | $this->assertEquals('<div id="content"><article><section><div class="a"><p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p></div></section></article></div>', $c->saveHTML()); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::wrapInner |
||
427 | */ |
||
428 | public function testWrapInner() |
||
429 | { |
||
430 | $c = HtmlPageCrawler::create('<div id="content"><p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p></div>'); |
||
431 | $c->wrapInner('<div class="a">'); |
||
432 | $this->assertEquals('<div id="content"><div class="a"><p>Absatz 1</p><p>Absatz 2</p><p>Absatz 3</p></div></div>', $c->saveHTML()); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::unwrap |
||
437 | */ |
||
438 | public function testUnwrap() |
||
439 | { |
||
440 | $c = HtmlPageCrawler::create('<div id="content"><div>Before</div><div class="a"><p>Absatz 1</p></div><div>After</div></div>'); |
||
441 | $p = $c->filter('p'); |
||
442 | $p->unwrap(); |
||
443 | $this->assertEquals('<div id="content"><div>Before</div><p>Absatz 1</p><div>After</div></div>', $c->saveHTML()); |
||
444 | } |
||
445 | |||
446 | public function testUnwrapInnerOnDOMElementExeption() |
||
447 | { |
||
448 | $c = HtmlPageCrawler::create('<div id="content"></div>'); |
||
449 | $p = $c->filter('div#content'); |
||
450 | |||
451 | |||
452 | $this->expectException('InvalidArgumentException'); |
||
453 | $this->expectExceptionMessage('DOMElement does not have a parent DOMElement node.'); |
||
454 | |||
455 | $p->unwrapInner(); |
||
456 | $p->unwrapInner(); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::unwrapInner |
||
461 | */ |
||
462 | public function testUnwrapInner() |
||
463 | { |
||
464 | $c = HtmlPageCrawler::create('<div id="content"><div>Before</div><div class="a"><p>Absatz 1</p></div><div>After</div></div>'); |
||
465 | $p = $c->filter('div.a'); |
||
466 | $p->unwrapInner(); |
||
467 | $this->assertEquals('<div id="content"><div>Before</div><p>Absatz 1</p><div>After</div></div>', $c->saveHTML()); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @covers Wa72\HtmlPageDom\HtmlPageCrawler::toggleClass |
||
472 | */ |
||
473 | public function testToggleClass() |
||
474 | { |
||
475 | $c = HtmlPageCrawler::create('<div id="1" class="a c"><div id="2" class="b c"></div></div>'); |
||
476 | $c->filter('div')->toggleClass('a d')->toggleClass('b'); |
||
477 | $this->assertEquals('<div id="1" class="c d b"><div id="2" class="c a d"></div></div>', $c->saveHTML()); |
||
478 | } |
||
479 | |||
480 | public function testRemove() |
||
481 | { |
||
482 | // remove every third td in tbody |
||
483 | $html = <<<END |
||
484 | <table> |
||
485 | <thead> |
||
486 | <tr> |
||
487 | <th>A</th> |
||
488 | <th>B</th> |
||
489 | </tr> |
||
490 | </thead> |
||
491 | <tbody> |
||
492 | <tr class="r1"> |
||
493 | <td class="c11">16.12.2013</td> |
||
494 | <td class="c12">asdf asdf</td> |
||
495 | <td class="c13"> </td> |
||
496 | </tr> |
||
497 | <tr class="r2"> |
||
498 | <td class="c21">02.12.2013 16:30</td> |
||
499 | <td class="c22">asdf asdf</td> |
||
500 | <td class="c23"> </td> |
||
501 | </tr> |
||
502 | <tr class="r3"> |
||
503 | <td class="c31">25.11.2013 16:30</td> |
||
504 | <td class="c32">asdf asdf</td> |
||
505 | <td class="c33"> </td> |
||
506 | </tr> |
||
507 | <tr class="r4"> |
||
508 | <td class="c41">18.11.2013 16:30</td> |
||
509 | <td class="c42">asdf asdf</td> |
||
510 | <td class="c43"> </td> |
||
511 | </tr> |
||
512 | <tr class="r5"> |
||
513 | <td class="c51">24.10.2013 16:30</td> |
||
514 | <td class="c52">asdf asdf</td> |
||
515 | <td class="c53"> </td> |
||
516 | </tr> |
||
517 | <tr class="r6"> |
||
518 | <td class="c61">10.10.2013 16:30</td> |
||
519 | <td class="c62">asdf asdf</td> |
||
520 | <td class="c63"> </td> |
||
521 | </tr> |
||
522 | </table> |
||
523 | END; |
||
524 | $c = HtmlPageCrawler::create($html); |
||
525 | $this->assertEquals(1, count($c->filter('td.c23'))); |
||
526 | $tbd = $c->filter('table > tbody > tr > td') |
||
527 | ->reduce( |
||
528 | function ($c, $j) { |
||
529 | if (($j + 1) % 3 == 0) { |
||
530 | return true; |
||
531 | } |
||
532 | |||
533 | return false; |
||
534 | } |
||
535 | ); |
||
536 | $this->assertEquals(6, count($tbd)); |
||
537 | $tbd->remove(); |
||
538 | $this->assertEquals(0, count($tbd)); |
||
539 | $this->assertEquals(0, count($c->filter('td.c23'))); |
||
540 | } |
||
541 | |||
542 | public function testUTF8Characters() |
||
543 | { |
||
544 | $text = file_get_contents(__DIR__ . '/utf8.html'); |
||
545 | $c = HtmlPageCrawler::create($text); |
||
546 | |||
547 | $expected = <<< END |
||
548 | <p style="margin: 0cm 0cm 0pt;"><span>Die Burse wurde unmittelbar (1478 bis 1482) nach der Universitätsgründung als Studentenwohnhaus und -lehranstalt errichtet. Hier lehrte der Humanist und Reformator Philipp Melanchthon bis zu seiner Berufung nach Wittenberg 1518, an ihn erinnert eine Gedenktafel. 1803 bis 1805 wurde das Gebäude im Stil des Klassizismus zum ersten Tübinger Klinikum umgebaut. Einer der ersten Patienten war Friedrich Hölderlin, der nach einer 231 Tage dauernden Behandlung am 3. Mai 1807 als unheilbar entlassen wurde.</span></p><p style="margin: 0cm 0cm 0pt;"><span>Einst Badeanstalt vor der Stadtmauer. Wer durch das kleine Stadttor geht, hat – rückwärts gewandt – einen guten Blick auf die Stadtbefestigung mit "Pechnasen" und Spuren des alten Wehrgangs.</span></p> |
||
549 | END; |
||
550 | |||
551 | $this->assertEquals($expected, $c->filter('p')->saveHTML()); |
||
552 | } |
||
553 | |||
554 | public function testAttr() |
||
555 | { |
||
556 | $c = HtmlPageCrawler::create('<div>'); |
||
557 | $this->assertNull($c->attr('data-foo')); |
||
558 | $c->setAttribute('data-foo', 'bar'); |
||
559 | $this->assertEquals('bar', $c->attr('data-foo')); |
||
560 | $this->assertEquals('bar', $c->getAttribute('data-foo')); |
||
561 | $c->removeAttribute('data-foo'); |
||
562 | $this->assertNull($c->attr('data-foo')); |
||
563 | $c->setAttribute('data-foo', 'bar'); |
||
564 | $this->assertEquals('bar', $c->attr('data-foo')); |
||
565 | // getAttribute is just an alias to attr() and should provide the same result |
||
566 | $this->assertEquals('bar', $c->getAttribute('data-foo')); |
||
567 | $c->removeAttr('data-foo'); |
||
568 | $this->assertNull($c->attr('data-foo')); |
||
569 | } |
||
570 | |||
571 | public function testAttrOnInvalidNodeList() |
||
572 | { |
||
573 | $this->expectException('InvalidArgumentException'); |
||
574 | |||
575 | $c = HtmlPageCrawler::create(null); |
||
576 | $c->attr('data-foo'); |
||
577 | } |
||
578 | |||
579 | public function testSetInnerHtml() |
||
580 | { |
||
581 | $html = HtmlPageCrawler::create('<h1>Title</h1>'); |
||
582 | $this->assertInstanceOf('Wa72\HtmlPageDom\HtmlPageCrawler', $html->setInnerHtml('<h2>Title</h2>')); |
||
583 | $this->assertEquals('<h2>Title</h2>', $html->html()); |
||
584 | // getInnerHtml is just an alias for html() and should provide the same result |
||
585 | $this->assertEquals('<h2>Title</h2>', $html->getInnerHtml()); |
||
586 | } |
||
587 | |||
588 | public function testToString() |
||
589 | { |
||
590 | $html = HtmlPageCrawler::create('<h2>Title</h2>'); |
||
591 | $this->assertEquals('<h2>Title</h2>', (string) $html); |
||
592 | } |
||
593 | |||
594 | public function testGetDOMDocument() |
||
595 | { |
||
596 | $html = HtmlPageCrawler::create('<h2>Title</h2>'); |
||
597 | $this->assertInstanceOf('\DOMDocument', $html->getDOMDocument()); |
||
598 | } |
||
599 | |||
600 | public function testAddOnCrawlerInstance() |
||
601 | { |
||
602 | $html = HtmlPageCrawler::create('<h1>Title</h1>'); |
||
603 | $html->add($html); |
||
604 | $this->assertEquals('<h1>Title</h1>', (string) $html); |
||
605 | } |
||
606 | |||
607 | public function testReturnValues() |
||
608 | { |
||
609 | // appendTo, insertBefore, insertAfter, replaceAll should always return new Crawler objects |
||
610 | // see http://jquery.com/upgrade-guide/1.9/#appendto-insertbefore-insertafter-and-replaceall |
||
611 | |||
612 | $c1 = HtmlPageCrawler::create('<h1>Headline</h1>'); |
||
613 | $c2 = HtmlPageCrawler::create('<p>1</p><p>2</p><p>3</p>'); |
||
614 | $c3 = HtmlPageCrawler::create('<span>asdf</span>'); |
||
615 | |||
616 | $r1 = $c3->appendTo($c1); |
||
617 | $this->assertNotEquals(spl_object_hash($c3), spl_object_hash($r1)); |
||
618 | |||
619 | $r2 = $c3->insertBefore($c1); |
||
620 | $this->assertNotEquals(spl_object_hash($c3), spl_object_hash($r2)); |
||
621 | |||
622 | $r3 = $c3->insertAfter($c1); |
||
623 | $this->assertNotEquals(spl_object_hash($c3), spl_object_hash($r3)); |
||
624 | |||
625 | $r4 = $c3->replaceAll($c1); |
||
626 | $this->assertNotEquals(spl_object_hash($c3), spl_object_hash($r4)); |
||
627 | |||
628 | |||
629 | $r1 = $c3->appendTo($c2); |
||
630 | $this->assertNotEquals(spl_object_hash($c2), spl_object_hash($r1)); |
||
631 | |||
632 | $r2 = $c3->insertBefore($c2); |
||
633 | $this->assertNotEquals(spl_object_hash($c2), spl_object_hash($r2)); |
||
634 | |||
635 | $r3 = $c3->insertAfter($c2); |
||
636 | $this->assertNotEquals(spl_object_hash($c2), spl_object_hash($r3)); |
||
637 | |||
638 | $r4 = $c3->replaceAll($c2); |
||
639 | $this->assertNotEquals(spl_object_hash($c2), spl_object_hash($r4)); |
||
640 | } |
||
641 | |||
642 | public function testDisconnectedNodes() |
||
643 | { |
||
644 | // if after(), before() or replaceWith() is called on a node without parent, |
||
645 | // the unmodified Crawler object should be returned |
||
646 | // |
||
647 | // see http://jquery.com/upgrade-guide/1.9/#after-before-and-replacewith-with-disconnected-nodes |
||
648 | $c = HtmlPageCrawler::create('<div>abc</div>'); |
||
649 | $r = HtmlPageCrawler::create('<div>def</div>'); |
||
650 | |||
651 | $r1 = $c->after($r); |
||
652 | $this->assertEquals(spl_object_hash($r1), spl_object_hash($c)); |
||
653 | $this->assertEquals(count($r1), count($c)); |
||
654 | |||
655 | $r2 = $c->before($r); |
||
656 | $this->assertEquals(spl_object_hash($r2), spl_object_hash($c)); |
||
657 | $this->assertEquals(count($r2), count($c)); |
||
658 | |||
659 | $r3 = $c->replaceWith($r); |
||
660 | $this->assertEquals(spl_object_hash($r3), spl_object_hash($c)); |
||
661 | $this->assertEquals(count($r3), count($c)); |
||
662 | } |
||
663 | |||
664 | public function testClone() |
||
665 | { |
||
666 | $c = HtmlPageCrawler::create('<div><p class="x">asdf</p></div>'); |
||
667 | $p = $c->filter('p'); |
||
668 | |||
669 | $p1 = $p->makeClone(); |
||
670 | $this->assertNotEquals(spl_object_hash($p), spl_object_hash($p1)); |
||
671 | $this->assertTrue($p1->hasClass('x')); |
||
672 | $p1->removeClass('x'); |
||
673 | $this->assertTrue($p->hasClass('x')); |
||
674 | $this->assertFalse($p1->hasClass('x')); |
||
675 | $p->after($p1); |
||
676 | $this->assertEquals('<div><p class="x">asdf</p><p class="">asdf</p></div>', $c->saveHTML()); |
||
677 | } |
||
678 | |||
679 | public function testGetCombinedText() |
||
680 | { |
||
681 | $c = HtmlPageCrawler::create('<p>abc</p><p>def</p>'); |
||
682 | $this->assertEquals('abcdef', $c->getCombinedText()); |
||
683 | $c->setText('jklo'); |
||
684 | $this->assertEquals('jklojklo', $c->getCombinedText()); |
||
685 | } |
||
686 | |||
687 | public function testSetText() |
||
688 | { |
||
689 | $c = HtmlPageCrawler::create('<div>"</div>'); |
||
690 | $this->assertEquals('"', $c->text()); |
||
691 | $c->setText('&'); |
||
692 | $this->assertEquals('&', $c->text()); |
||
693 | } |
||
694 | |||
695 | public function testMagicGet() |
||
696 | { |
||
697 | // $crawler->length should give us the number of nodes in the crawler |
||
698 | $c = HtmlPageCrawler::create('<p>abc</p><p>def</p>'); |
||
699 | $this->assertEquals(2, $c->length); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
700 | |||
701 | // not existing property throws exception |
||
702 | try { |
||
703 | $c->foo; |
||
0 ignored issues
–
show
The property
foo does not exist on Wa72\HtmlPageDom\HtmlPageCrawler . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
704 | } catch (\Exception $e) { |
||
0 ignored issues
–
show
catch (\Exception $e) is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||
705 | $this->assertEquals('No such property foo', $e->getMessage()); |
||
706 | |||
707 | return; |
||
708 | } |
||
709 | $this->fail(); |
||
710 | } |
||
711 | } |
||
712 |