/var/www/hkosl.com/b2b2c/webadmin/libraies/paquettg/php-html-parser/tests/DomTest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php

use PHPHtmlParser\Dom;

class 
DomTest extends PHPUnit_Framework_TestCase {

    public function 
tearDown()
    {
        
Mockery::close();
    }

    public function 
testLoad()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
        
$div $dom->find('div'0);
        
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>'$div->outerHtml);
    }

    
/**
     * @expectedException PHPHtmlParser\Exceptions\NotLoadedException
     */
    
public function testNotLoaded()
    {
        
$dom = new Dom;
        
$div $dom->find('div'0);
    }

    public function 
testIncorrectAccess()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
        
$div $dom->find('div'0);
        
$this->assertEquals(null$div->foo);
    }

    public function 
testLoadSelfclosingAttr()
    {
        
$dom = new Dom;
        
$dom->load("<div class='all'><br  foo  bar  />baz</div>");
        
$br $dom->find('br'0);
        
$this->assertEquals('<br foo bar />'$br->outerHtml);
    }

    public function 
testLoadSelfclosingAttrToString()
    {
        
$dom = new Dom;
        
$dom->load("<div class='all'><br  foo  bar  />baz</div>");
        
$br $dom->find('br'0);
        
$this->assertEquals('<br foo bar />', (string) $br);
    }

    public function 
testLoadEscapeQuotes()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div>');
        
$div $dom->find('div'0);
        
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div>'$div->outerHtml);
    }

    public function 
testLoadNoOpeningTag()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><font color="red"><strong>PR Manager</strong></font></b><div class="content">content</div></div>');
        
$this->assertEquals('content'$dom->find('.content'0)->text);
    }

    public function 
testLoadNoClosingTag()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br />');
        
$root $dom->find('div'0)->getParent();
        
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div><br />'$root->outerHtml);
    }

    public function 
testLoadAttributeOnSelfClosing()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br class="both" />');
        
$br $dom->find('br'0);
        
$this->assertEquals('both'$br->getAttribute('class'));
    }

    public function 
testLoadClosingTagOnSelfClosing()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
        
$this->assertEquals('<br /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>'$dom->find('div'0)->innerHtml);
    }

    public function 
testLoadClosingTagAddSelfClosingTag()
    {
        
$dom = new Dom;
        
$dom->addSelfClosingTag('mytag');
        
$dom->load('<div class="all"><mytag><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></mytag></div>');
        
$this->assertEquals('<mytag /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>'$dom->find('div'0)->innerHtml);
    }

    public function 
testLoadClosingTagAddSelfClosingTagArray()
    {
        
$dom = new Dom;
        
$dom->addSelfClosingTag([
            
'mytag',
            
'othertag'
        
]);
        
$dom->load('<div class="all"><mytag><p>Hey bro, <a href="google.com" data-quote="\"">click here</a><othertag></div>');
        
$this->assertEquals('<mytag /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a><othertag /></p>'$dom->find('div'0)->innerHtml);
    }

    public function 
testLoadClosingTagRemoveSelfClosingTag()
    {
        
$dom = new Dom;
        
$dom->removeSelfClosingTag('br');
        
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
        
$this->assertEquals('<br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></br>'$dom->find('div'0)->innerHtml);
    }

    public function 
testLoadClosingTagClearSelfClosingTag()
    {
        
$dom = new Dom;
        
$dom->clearSelfClosingTags();
        
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
        
$this->assertEquals('<br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></br>'$dom->find('div'0)->innerHtml);
    }

    public function 
testLoadNoValueAttribute()
    {
        
$dom = new Dom;
        
$dom->load('<div class="content"><div class="grid-container" ui-view>Main content here</div></div>');
        
$this->assertEquals('<div class="content"><div class="grid-container" ui-view>Main content here</div></div>'$dom->innerHtml);
    }

    public function 
testLoadNoValueAttributeBefore()
    {
        
$dom = new Dom;
        
$dom->load('<div class="content"><div ui-view class="grid-container">Main content here</div></div>');
        
$this->assertEquals('<div class="content"><div ui-view class="grid-container">Main content here</div></div>'$dom->innerHtml);
    }

    public function 
testLoadUpperCase()
    {
        
$dom = new Dom;
        
$dom->load('<DIV CLASS="ALL"><BR><P>hEY BRO, <A HREF="GOOGLE.COM" DATA-QUOTE="\"">CLICK HERE</A></BR></DIV>');
        
$this->assertEquals('<br /><p>hEY BRO, <a href="GOOGLE.COM" data-quote="\"">CLICK HERE</a></p>'$dom->find('div'0)->innerHtml);
    }

    public function 
testLoadWithFile()
    {
        
$dom = new Dom;
        
$dom->loadFromFile('tests/files/small.html');
        
$this->assertEquals('VonBurgermeister'$dom->find('.post-user font'0)->text);
    }

    public function 
testLoadFromFile()
    {
        
$dom = new Dom;
        
$dom->loadFromFile('tests/files/small.html');
        
$this->assertEquals('VonBurgermeister'$dom->find('.post-user font'0)->text);
    }

    public function 
testLoadFromFileFind()
    {
        
$dom = new Dom;
        
$dom->loadFromFile('tests/files/small.html');
        
$this->assertEquals('VonBurgermeister'$dom->find('.post-row div .post-user font'0)->text);
    }

    public function 
testLoadUtf8()
    {
        
$dom = new Dom;
        
$dom->load('<p>Dzień</p>');
        
$this->assertEquals('Dzień'$dom->find('p'0)->text);
    }

    public function 
testLoadFileBig()
    {
        
$dom = new Dom;
        
$dom->loadFromFile('tests/files/big.html');
        
$this->assertEquals(10count($dom->find('.content-border')));
    }

    public function 
testLoadFileBigTwice()
    {
        
$dom = new Dom;
        
$dom->loadFromFile('tests/files/big.html');
        
$post $dom->find('.post-row'0);
        
$this->assertEquals(' <p>Журчанье воды<br /> Черно-белые тени<br /> Вновь на фонтане</p> '$post->find('.post-message'0)->innerHtml);
    }

    public function 
testLoadFileBigTwicePreserveOption()
    {
        
$dom = new Dom;
        
$dom->loadFromFile('tests/files/big.html', ['preserveLineBreaks' => true]);
        
$post $dom->find('.post-row'0);
        
$this->assertEquals('<p>Журчанье воды<br />
Черно-белые тени<br />
Вновь на фонтане</p>'
trim($post->find('.post-message'0)->innerHtml));
    }

    public function 
testLoadFromUrl()
    {
        
$curl Mockery::mock('PHPHtmlParser\CurlInterface');
        
$curl->shouldReceive('get')
             ->
once()
             ->
with('http://google.com')
             ->
andReturn(file_get_contents('tests/files/small.html'));
        
        
$dom = new Dom;
        
$dom->loadFromUrl('http://google.com', [], $curl);
        
$this->assertEquals('VonBurgermeister'$dom->find('.post-row div .post-user font'0)->text);
    }

    public function 
testToStringMagic()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
        
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', (string) $dom);
    }

    public function 
testGetMagic()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
        
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>'$dom->innerHtml);
    }

    public function 
testFirstChild()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br />');
        
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div>'$dom->firstChild()->outerHtml);
    }

    public function 
testLastChild()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br />');
        
$this->assertEquals('<br />'$dom->lastChild()->outerHtml);
    }

    public function 
testGetElementById()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" id="78">click here</a></div><br />');
        
$this->assertEquals('<a href="google.com" id="78">click here</a>'$dom->getElementById('78')->outerHtml);
    }

    public function 
testGetElementsByTag()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" id="78">click here</a></div><br />');
        
$this->assertEquals('<p>Hey bro, <a href="google.com" id="78">click here</a></p>'$dom->getElementsByTag('p')[0]->outerHtml);
    }

    public function 
testGetElementsByClass()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com" id="78">click here</a></div><br />');
        
$this->assertEquals('<p>Hey bro, <a href="google.com" id="78">click here</a></p>'$dom->getElementsByClass('all')[0]->innerHtml);
    }

    public function 
testEnforceEncoding()
    {
        
$dom = new Dom;
        
$dom->load('tests/files/horrible.html', [
            
'enforceEncoding' => 'UTF-8',
        ]);
        
$this->assertNotEquals('<input type="submit" tabindex="0" name="submit" value="Информации" />'$dom->find('table input'1)->outerHtml);
    }

    public function 
testScriptCleanerScriptTag()
    {
        
$dom = new Dom;
        
$dom->load('
        <p>.....</p>
        <script>
        Some code ... 
        document.write("<script src=\'some script\'><\/script>") 
        Some code ... 
        </script>
        <p>....</p>'
);
        
$this->assertEquals('....'$dom->getElementsByTag('p')[1]->innerHtml);
    }

    public function 
testMultipleDoubleQuotes()
    {
        
$dom = new Dom;
        
$dom->load('<a title="This is a "test" of double quotes" href="http://www.example.com">Hello</a>');
        
$this->assertEquals('This is a "test" of double quotes'$dom->getElementsByTag('a')[0]->title);
    }

    public function 
testMultipleSingleQuotes()
    {
        
$dom = new Dom;
        
$dom->load("<a title='Ain't this the best' href=\"http://www.example.com\">Hello</a>");
        
$this->assertEquals("Ain't this the best"$dom->getElementsByTag('a')[0]->title);
    }

    public function 
testBeforeClosingTag()
    {
        
$dom = new Dom;
        
$dom->load("<div class=\"stream-container \"  > <div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\">");
        
$this->assertEquals("<div class=\"stream-container \"> <div class=\"stream-item js-new-items-bar-container\"> </div> <div class=\"stream\"></div></div>", (string) $dom);
    }

    public function 
testCodeTag()
    {
        
$dom = new Dom;
        
$dom->load('<strong>hello</strong><code class="language-php">$foo = "bar";</code>');
        
$this->assertEquals('<strong>hello</strong><code class="language-php">$foo = "bar";</code>', (string) $dom);
    }

    public function 
testDeleteNode()
    {
        
$dom = new Dom;
        
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
        
$a   $dom->find('a')[0];
        
$a->delete();
        unset(
$a);
        
$this->assertEquals('<div class="all"><p>Hey bro, <br /> :)</p></div>', (string) $dom);
    }
}