Skip to content

Commit 3f1f6d6

Browse files
committed
Cleaned up tests
1 parent a78054f commit 3f1f6d6

File tree

6 files changed

+170
-138
lines changed

6 files changed

+170
-138
lines changed

Diff for: .travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 7.1
54
- 7.2
65
- 7.3
76
- 7.4

Diff for: phpunit.xml

+24-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="phpunit.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
3+
backupStaticAttributes="false"
4+
bootstrap="phpunit.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
beStrictAboutChangesToGlobalState="true"
12+
beStrictAboutTestsThatDoNotTestAnything="true"
13+
executionOrder="random"
1114
>
12-
<testsuites>
13-
<testsuite name="Repository Test Suite">
14-
<directory>./tests/</directory>
15-
</testsuite>
16-
</testsuites>
15+
<testsuites>
16+
<testsuite name="Repository Test Suite">
17+
<directory>./tests/</directory>
18+
</testsuite>
19+
</testsuites>
1720

18-
<filter>
19-
<whitelist addUncoveredFilesFromWhitelist="false">
20-
<directory suffix=".php">src</directory>
21-
<exclude>
22-
<directory suffix=".php">vendor</directory>
23-
</exclude>
24-
</whitelist>
25-
</filter>
21+
<filter>
22+
<whitelist addUncoveredFilesFromWhitelist="false">
23+
<directory suffix=".php">src</directory>
24+
<exclude>
25+
<directory suffix=".php">vendor</directory>
26+
</exclude>
27+
</whitelist>
28+
</filter>
2629
</phpunit>

Diff for: src/PHPHtmlParser/Dom.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private function isLoaded(): void
349349
*/
350350
private function clean(string $str): string
351351
{
352-
if ($this->options->isCleanupInput() != true) {
352+
if (!$this->options->isCleanupInput()) {
353353
// skip entire cleanup step
354354
return $str;
355355
}

Diff for: tests/Dom/LoadTest.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPHtmlParser\Dom;
6+
use PHPHtmlParser\Exceptions\NotLoadedException;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class LoadTest extends TestCase
10+
{
11+
/**
12+
* @var Dom
13+
*/
14+
private $dom;
15+
16+
public function setUp()
17+
{
18+
$dom = new Dom();
19+
$dom->loadStr('<div class="all"><br><p>Hey bro, <a href="google.com" id="78" data-quote="\"">click here</a></br></div><br class="both" />');
20+
$this->dom = $dom;
21+
}
22+
23+
public function tearDown()
24+
{
25+
Mockery::close();
26+
}
27+
28+
public function testLoadEscapeQuotes()
29+
{
30+
$a = $this->dom->find('a', 0);
31+
$this->assertEquals('<a href="google.com" id="78" data-quote="\"">click here</a>', $a->outerHtml);
32+
}
33+
34+
public function testLoadNoClosingTag()
35+
{
36+
$p = $this->dom->find('p', 0);
37+
$this->assertEquals('Hey bro, <a href="google.com" id="78" data-quote="\"">click here</a>', $p->innerHtml);
38+
}
39+
40+
public function testLoadClosingTagOnSelfClosing()
41+
{
42+
$this->assertCount(2, $this->dom->find('br'));
43+
}
44+
45+
public function testIncorrectAccess()
46+
{
47+
$div = $this->dom->find('div', 0);
48+
$this->assertEquals(null, $div->foo);
49+
}
50+
51+
public function testLoadAttributeOnSelfClosing()
52+
{
53+
$br = $this->dom->find('br', 1);
54+
$this->assertEquals('both', $br->getAttribute('class'));
55+
}
56+
57+
public function testToStringMagic()
58+
{
59+
$this->assertEquals('<div class="all"><br /><p>Hey bro, <a href="google.com" id="78" data-quote="\"">click here</a></p></div><br class="both" />', (string) $this->dom);
60+
}
61+
62+
public function testGetMagic()
63+
{
64+
$this->assertEquals('<div class="all"><br /><p>Hey bro, <a href="google.com" id="78" data-quote="\"">click here</a></p></div><br class="both" />', $this->dom->innerHtml);
65+
}
66+
67+
public function testFirstChild()
68+
{
69+
$this->assertEquals('<div class="all"><br /><p>Hey bro, <a href="google.com" id="78" data-quote="\"">click here</a></p></div>', $this->dom->firstChild()->outerHtml);
70+
}
71+
72+
public function testLastChild()
73+
{
74+
$this->assertEquals('<br class="both" />', $this->dom->lastChild()->outerHtml);
75+
}
76+
77+
public function testGetElementById()
78+
{
79+
$this->assertEquals('<a href="google.com" id="78" data-quote="\"">click here</a>', $this->dom->getElementById('78')->outerHtml);
80+
}
81+
82+
public function testGetElementsByTag()
83+
{
84+
$this->assertEquals('<p>Hey bro, <a href="google.com" id="78" data-quote="\"">click here</a></p>', $this->dom->getElementsByTag('p')[0]->outerHtml);
85+
}
86+
87+
public function testGetElementsByClass()
88+
{
89+
$this->assertEquals('<br /><p>Hey bro, <a href="google.com" id="78" data-quote="\"">click here</a></p>', $this->dom->getElementsByClass('all')[0]->innerHtml);
90+
}
91+
92+
public function testDeleteNode()
93+
{
94+
$a = $this->dom->find('a')[0];
95+
$a->delete();
96+
unset($a);
97+
$this->assertEquals('<div class="all"><br /><p>Hey bro, </p></div><br class="both" />', (string) $this->dom);
98+
}
99+
}

Diff for: tests/Dom/NotLoadedTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPHtmlParser\Dom;
6+
use PHPHtmlParser\Exceptions\NotLoadedException;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class NotLoadedTest extends TestCase
10+
{
11+
/**
12+
* @var Dom
13+
*/
14+
private $dom;
15+
16+
public function setUp()
17+
{
18+
$dom = new Dom();
19+
$this->dom = $dom;
20+
}
21+
22+
public function tearDown()
23+
{
24+
Mockery::close();
25+
}
26+
27+
public function testNotLoaded()
28+
{
29+
$this->expectException(NotLoadedException::class);
30+
$div = $this->dom->find('div', 0);
31+
}
32+
}
33+
34+

Diff for: tests/DomTest.php

+12-115
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use PHPHtmlParser\Dom;
6+
use PHPHtmlParser\Exceptions\NotLoadedException;
67
use PHPHtmlParser\Options;
78
use PHPUnit\Framework\TestCase;
89

@@ -25,31 +26,6 @@ public function testParsingCData()
2526
$this->assertSame($html, $dom->root->outerHtml());
2627
}
2728

28-
public function testloadStr()
29-
{
30-
$dom = new Dom();
31-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
32-
$div = $dom->find('div', 0);
33-
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', $div->outerHtml);
34-
}
35-
36-
/**
37-
* @expectedException \PHPHtmlParser\Exceptions\NotLoadedException
38-
*/
39-
public function testNotLoaded()
40-
{
41-
$dom = new Dom();
42-
$div = $dom->find('div', 0);
43-
}
44-
45-
public function testIncorrectAccess()
46-
{
47-
$dom = new Dom();
48-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
49-
$div = $dom->find('div', 0);
50-
$this->assertEquals(null, $div->foo);
51-
}
52-
5329
public function testLoadSelfclosingAttr()
5430
{
5531
$dom = new Dom();
@@ -66,44 +42,13 @@ public function testLoadSelfclosingAttrToString()
6642
$this->assertEquals('<br foo bar />', (string) $br);
6743
}
6844

69-
public function testLoadEscapeQuotes()
70-
{
71-
$dom = new Dom();
72-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div>');
73-
$div = $dom->find('div', 0);
74-
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div>', $div->outerHtml);
75-
}
76-
7745
public function testLoadNoOpeningTag()
7846
{
7947
$dom = new Dom();
8048
$dom->loadStr('<div class="all"><font color="red"><strong>PR Manager</strong></font></b><div class="content">content</div></div>');
8149
$this->assertEquals('content', $dom->find('.content', 0)->text);
8250
}
8351

84-
public function testLoadNoClosingTag()
85-
{
86-
$dom = new Dom();
87-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br />');
88-
$root = $dom->find('div', 0)->getParent();
89-
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div><br />', $root->outerHtml);
90-
}
91-
92-
public function testLoadAttributeOnSelfClosing()
93-
{
94-
$dom = new Dom();
95-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br class="both" />');
96-
$br = $dom->find('br', 0);
97-
$this->assertEquals('both', $br->getAttribute('class'));
98-
}
99-
100-
public function testLoadClosingTagOnSelfClosing()
101-
{
102-
$dom = new Dom();
103-
$dom->loadStr('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
104-
$this->assertEquals('<br /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
105-
}
106-
10752
public function testLoadNoValueAttribute()
10853
{
10954
$dom = new Dom();
@@ -223,55 +168,6 @@ public function testLoadFromUrl()
223168
$this->assertEquals('VonBurgermeister', $dom->find('.post-row div .post-user font', 0)->text);
224169
}
225170

226-
public function testToStringMagic()
227-
{
228-
$dom = new Dom();
229-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
230-
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', (string) $dom);
231-
}
232-
233-
public function testGetMagic()
234-
{
235-
$dom = new Dom();
236-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
237-
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', $dom->innerHtml);
238-
}
239-
240-
public function testFirstChild()
241-
{
242-
$dom = new Dom();
243-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br />');
244-
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></div>', $dom->firstChild()->outerHtml);
245-
}
246-
247-
public function testLastChild()
248-
{
249-
$dom = new Dom();
250-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></div><br />');
251-
$this->assertEquals('<br />', $dom->lastChild()->outerHtml);
252-
}
253-
254-
public function testGetElementById()
255-
{
256-
$dom = new Dom();
257-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" id="78">click here</a></div><br />');
258-
$this->assertEquals('<a href="google.com" id="78">click here</a>', $dom->getElementById('78')->outerHtml);
259-
}
260-
261-
public function testGetElementsByTag()
262-
{
263-
$dom = new Dom();
264-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" id="78">click here</a></div><br />');
265-
$this->assertEquals('<p>Hey bro, <a href="google.com" id="78">click here</a></p>', $dom->getElementsByTag('p')[0]->outerHtml);
266-
}
267-
268-
public function testGetElementsByClass()
269-
{
270-
$dom = new Dom();
271-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com" id="78">click here</a></div><br />');
272-
$this->assertEquals('<p>Hey bro, <a href="google.com" id="78">click here</a></p>', $dom->getElementsByClass('all')[0]->innerHtml);
273-
}
274-
275171
public function testScriptCleanerScriptTag()
276172
{
277173
$dom = new Dom();
@@ -321,16 +217,6 @@ public function testCodeTag()
321217
$this->assertEquals('<strong>hello</strong><code class="language-php">$foo = "bar";</code>', (string) $dom);
322218
}
323219

324-
public function testDeleteNode()
325-
{
326-
$dom = new Dom();
327-
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
328-
$a = $dom->find('a')[0];
329-
$a->delete();
330-
unset($a);
331-
$this->assertEquals('<div class="all"><p>Hey bro, <br /> :)</p></div>', (string) $dom);
332-
}
333-
334220
public function testCountChildren()
335221
{
336222
$dom = new Dom();
@@ -619,4 +505,15 @@ public function testXMLOpeningToken()
619505

620506
$this->assertEquals('<?xml version="1.0" encoding="UTF-8" ?><p>fun time</p>', $dom->outerHtml);
621507
}
508+
509+
/**
510+
* Test to cover issue found in ticket #221
511+
*/
512+
public function testRandomTagInMiddleOfText()
513+
{
514+
$dom = new Dom();
515+
$dom->loadStr('<p>Hello, this is just a test in which <55 names with some other text > should be interpreted as text</p>');
516+
517+
$this->assertEquals('<p>Hello, this is just a test in which <55 names with some other text> should be interpreted as text</55></p>', $dom->outerHtml);
518+
}
622519
}

0 commit comments

Comments
 (0)