Skip to content

Commit 61d093f

Browse files
committed
Updated some tests
1 parent 8f31eaa commit 61d093f

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

src/PHPHtmlParser/StaticDom.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,12 @@ public static function loadFromUrl($url, CurlInterface $curl = null)
9898

9999
return $dom->loadFromUrl($url, $curl);
100100
}
101+
102+
/**
103+
* Sets the $dom variable to null.
104+
*/
105+
public static function unload()
106+
{
107+
self::$dom = null;
108+
}
101109
}

tests/ContentTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function testRewind()
3131
$this->assertEquals('b', $content->char());
3232
}
3333

34+
public function testRewindNegative()
35+
{
36+
$content = new Content('abcde');
37+
$content->fastForward(2)
38+
->rewind(100);
39+
$this->assertEquals('a', $content->char());
40+
}
41+
3442
public function testCopyUntil()
3543
{
3644
$content = new Content('abcdeedcba');
@@ -49,6 +57,12 @@ public function testCopyUntilEscape()
4957
$this->assertEquals('foo\"bar', $content->copyUntil('"', false, true));
5058
}
5159

60+
public function testCopyUntilNotFound()
61+
{
62+
$content = new Content('foo\"bar"bax');
63+
$this->assertEquals('foo\"bar"bax', $content->copyUntil('baz'));
64+
}
65+
5266
public function testCopyByToken()
5367
{
5468
$content = new Content('<a href="google.com">');

tests/DomTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ public function testLoad()
1212
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', $div->outerHtml);
1313
}
1414

15+
/**
16+
* @expectedException PHPHtmlParser\Exceptions\NotLoadedException
17+
*/
18+
public function testNotLoaded()
19+
{
20+
$dom = new Dom;
21+
$div = $dom->find('div', 0);
22+
}
23+
24+
public function testIncorrectAccess()
25+
{
26+
$dom = new Dom;
27+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
28+
$div = $dom->find('div', 0);
29+
$this->assertEquals(null, $div->foo);
30+
}
31+
1532
public function testLoadSelfclosingAttr()
1633
{
1734
$dom = new Dom;
@@ -20,6 +37,14 @@ public function testLoadSelfclosingAttr()
2037
$this->assertEquals('<br foo bar />', $br->outerHtml);
2138
}
2239

40+
public function testLoadSelfclosingAttrToString()
41+
{
42+
$dom = new Dom;
43+
$dom->load("<div class='all'><br foo bar />baz</div>");
44+
$br = $dom->find('br', 0);
45+
$this->assertEquals('<br foo bar />', (string) $br);
46+
}
47+
2348
public function testLoadEscapeQuotes()
2449
{
2550
$dom = new Dom;
@@ -58,6 +83,41 @@ public function testLoadClosingTagOnSelfClosing()
5883
$this->assertEquals('<br /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
5984
}
6085

86+
public function testLoadClosingTagAddSelfClosingTag()
87+
{
88+
$dom = new Dom;
89+
$dom->addSelfClosingTag('mytag');
90+
$dom->load('<div class="all"><mytag><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></mytag></div>');
91+
$this->assertEquals('<mytag /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
92+
}
93+
94+
public function testLoadClosingTagAddSelfClosingTagArray()
95+
{
96+
$dom = new Dom;
97+
$dom->addSelfClosingTag([
98+
'mytag',
99+
'othertag'
100+
]);
101+
$dom->load('<div class="all"><mytag><p>Hey bro, <a href="google.com" data-quote="\"">click here</a><othertag></div>');
102+
$this->assertEquals('<mytag /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a><othertag /></p>', $dom->find('div', 0)->innerHtml);
103+
}
104+
105+
public function testLoadClosingTagRemoveSelfClosingTag()
106+
{
107+
$dom = new Dom;
108+
$dom->removeSelfClosingTag('br');
109+
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
110+
$this->assertEquals('<br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></br>', $dom->find('div', 0)->innerHtml);
111+
}
112+
113+
public function testLoadClosingTagClearSelfClosingTag()
114+
{
115+
$dom = new Dom;
116+
$dom->clearSelfClosingTags();
117+
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
118+
$this->assertEquals('<br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></br>', $dom->find('div', 0)->innerHtml);
119+
}
120+
61121
public function testLoadUpperCase()
62122
{
63123
$dom = new Dom;

tests/Node/HtmlTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ public function testInnerHtml()
3535
$this->assertEquals("<a href='http://google.com'>link</a><br />", $parent->innerHtml());
3636
}
3737

38+
public function testInnerHtmlTwice()
39+
{
40+
$div = new Tag('div');
41+
$div->setAttributes([
42+
'class' => [
43+
'value' => 'all',
44+
'doubleQuote' => true,
45+
],
46+
]);
47+
$a = new Tag('a');
48+
$a->setAttributes([
49+
'href' => [
50+
'value' => 'http://google.com',
51+
'doubleQuote' => false,
52+
],
53+
]);
54+
$br = new Tag('br');
55+
$br->selfClosing();
56+
57+
$parent = new HtmlNode($div);
58+
$childa = new HtmlNode($a);
59+
$childbr = new HtmlNode($br);
60+
$parent->addChild($childa);
61+
$parent->addChild($childbr);
62+
$childa->addChild(new TextNode('link'));
63+
64+
$inner = $parent->innerHtml();
65+
$this->assertEquals($inner, $parent->innerHtml());
66+
}
67+
3868
public function testInnerHtmlMagic()
3969
{
4070
$parent = new HtmlNode('div');
@@ -90,6 +120,36 @@ public function testOuterHtml()
90120
$this->assertEquals('<div class="all"><a href=\'http://google.com\'>link</a><br /></div>', $parent->outerHtml());
91121
}
92122

123+
public function testOuterHtmlTwice()
124+
{
125+
$div = new Tag('div');
126+
$div->setAttributes([
127+
'class' => [
128+
'value' => 'all',
129+
'doubleQuote' => true,
130+
],
131+
]);
132+
$a = new Tag('a');
133+
$a->setAttributes([
134+
'href' => [
135+
'value' => 'http://google.com',
136+
'doubleQuote' => false,
137+
],
138+
]);
139+
$br = new Tag('br');
140+
$br->selfClosing();
141+
142+
$parent = new HtmlNode($div);
143+
$childa = new HtmlNode($a);
144+
$childbr = new HtmlNode($br);
145+
$parent->addChild($childa);
146+
$parent->addChild($childbr);
147+
$childa->addChild(new TextNode('link'));
148+
149+
$outer = $parent->outerHtml();
150+
$this->assertEquals($outer, $parent->outerHtml());
151+
}
152+
93153
public function testOuterHtmlEmpty()
94154
{
95155
$a = new Tag('a');
@@ -139,6 +199,16 @@ public function testText()
139199
$this->assertEquals('link', $node->text());
140200
}
141201

202+
public function testTextTwice()
203+
{
204+
$a = new Tag('a');
205+
$node = new HtmlNode($a);
206+
$node->addChild(new TextNode('link'));
207+
208+
$text = $node->text();
209+
$this->assertEquals($text, $node->text());
210+
}
211+
142212
public function testTextNone()
143213
{
144214
$a = new Tag('a');

tests/Node/TagTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public function testSetAttributes()
2525
$this->assertEquals('http://google.com', $tag->getAttribute('href')['value']);
2626
}
2727

28+
public function testNoise()
29+
{
30+
$tag = new Tag('a');
31+
$this->assertTrue($tag->noise('noise') instanceof Tag);
32+
}
33+
2834
public function testGetAttributeMagic()
2935
{
3036
$attr = [

tests/OptionsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ public function testAddingOver()
3333

3434
$this->assertFalse($options->get('whitespaceTextNode'));
3535
}
36+
37+
public function testGettingNoOption()
38+
{
39+
$options = new Options;
40+
$this->assertEquals(null, $options->get('doesnotexist'));
41+
}
3642
}
3743

tests/StaticDomTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ public function setUp()
99
StaticDom::mount();
1010
}
1111

12+
public function tearDown()
13+
{
14+
StaticDom::unload();
15+
}
16+
17+
public function testMountWithDom()
18+
{
19+
$dom = new PHPHtmlParser\Dom;
20+
StaticDom::unload();
21+
$status = StaticDom::mount('newDom', $dom);
22+
$this->assertTrue($status);
23+
}
24+
1225
public function testLoad()
1326
{
1427
$dom = Dom::load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
@@ -28,6 +41,14 @@ public function testFind()
2841
$this->assertEquals('<input type="submit" tabindex="0" name="submit" value="Информации" />', Dom::find('table input', 1)->outerHtml);
2942
}
3043

44+
/**
45+
* @expectedException PHPHtmlParser\Exceptions\NotLoadedException
46+
*/
47+
public function testFindNoLoad()
48+
{
49+
Dom::find('.post-user font', 0);
50+
}
51+
3152
public function testFindI()
3253
{
3354
Dom::load('tests/horrible.html');

0 commit comments

Comments
 (0)