2525/**
2626 * @covers \GlHtml\GlHtmlNode
2727 */
28- class GlHtmlNodeTest extends \PHPUnit_Framework_TestCase {
28+ class GlHtmlNodeTest extends \PHPUnit_Framework_TestCase
29+ {
2930
30- public function testReplaceMe () {
31- $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
32- $ htmlresult = "<!DOCTYPE html><html><head></head><body><div><h1></h1></div></body></html> " ;
31+ private function removeCR ($ html )
32+ {
33+ return str_replace (["\n" , "\r" ], '' , $ html );
34+ }
35+
36+ public function testSetValue ()
37+ {
38+ $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
39+ $ htmlresult = "<!DOCTYPE html><html><head></head><body><div><span>bonjour</span></div></body></html> " ;
40+
41+ $ dom = new GlHtml ($ html );
42+ $ node = $ dom ->get ("span " )[0 ];
43+
44+ $ node ->setValue ("bonjour " );
45+
46+ $ result = $ dom ->html ();
47+
48+ $ htmlresult = $ this ->removeCR ($ htmlresult );
49+ $ result = $ this ->removeCR ($ result );
50+
51+ $ this ->assertEquals ($ htmlresult , $ result );
52+ }
53+
54+ public function testAdd ()
55+ {
56+ $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
57+ $ htmlresult = '<!DOCTYPE html><html><head></head><body><div><span><div></div><div class="super">test</div></span></div></body></html> ' ;
58+
59+ $ dom = new GlHtml ($ html );
60+ $ node = $ dom ->get ("span " )[0 ];
61+
62+ $ node ->add ('<div class="super">test</div> ' );
63+
64+ $ result = $ dom ->html ();
65+
66+ $ htmlresult = $ this ->removeCR ($ htmlresult );
67+ $ result = $ this ->removeCR ($ result );
68+
69+ $ this ->assertEquals ($ htmlresult , $ result );
70+ }
71+
72+ public function testReplaceInner ()
73+ {
74+ $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
75+ $ htmlresult = '<!DOCTYPE html><html><head></head><body><div><span><hr></span></div></body></html> ' ;
76+
77+ $ dom = new GlHtml ($ html );
78+ $ node = $ dom ->get ("span " )[0 ];
79+
80+ $ node ->replaceInner ('<hr/> ' );
81+
82+ $ result = $ dom ->html ();
83+
84+ $ htmlresult = $ this ->removeCR ($ htmlresult );
85+ $ result = $ this ->removeCR ($ result );
86+
87+ $ this ->assertEquals ($ htmlresult , $ result );
88+ }
89+
90+ public function testGetDOMNode ()
91+ {
92+
93+ }
94+
95+ public function testGetName ()
96+ {
97+ $ html = '<!DOCTYPE html><html><head></head><body><div><span id="test"><div></div></span></div></body></html> ' ;
98+
99+ $ dom = new GlHtml ($ html );
100+ $ node = $ dom ->get ("#test " )[0 ];
101+
102+ $ this ->assertEquals ('span ' , $ node ->getName ());
103+ }
104+
105+ public function testGetAttribute ()
106+ {
107+ $ html = '<!DOCTYPE html><html><head></head><body><div><span id="testid" class="testclass"><div></div></span></div></body></html> ' ;
108+
109+ $ dom = new GlHtml ($ html );
110+ $ node = $ dom ->get ("#testid " )[0 ];
111+
112+ $ this ->assertEquals ('testclass ' , $ node ->getAttribute ('class ' ));
113+ }
114+
115+ public function testGetText ()
116+ {
117+ $ html = '<!DOCTYPE html><html><head></head><body><div><span>texte<div></div></span></div></body></html> ' ;
118+
119+ $ dom = new GlHtml ($ html );
120+ $ node = $ dom ->get ("span " )[0 ];
121+
122+ $ this ->assertEquals ('texte ' , $ node ->getText ());
123+ }
124+
125+ public function testGetSentences ()
126+ {
127+ $ html = '<!DOCTYPE html><html><head></head><body><div>texte1<div id="test">texte2<h1>texte3</h1>texte4</div></div></body></html> ' ;
33128
34- $ dom = new GlHtml ($ html );
129+ $ dom = new GlHtml ($ html );
130+ $ node = $ dom ->get ("#test " )[0 ];
131+
132+ $ this ->assertEquals (['texte2 ' ,'texte3 ' ,'texte4 ' ], $ node ->getSentences ());
133+ }
134+
135+ public function testCallChild ()
136+ {
137+ $ html = '<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> ' ;
138+
139+ $ dom = new GlHtml ($ html );
35140 $ node = $ dom ->get ("span " )[0 ];
36-
141+
142+ $ node ->callChild (
143+ function (GlHtmlNode $ childnode ) {
144+ $ this ->assertEquals ('div ' , $ childnode ->getName ());
145+ }
146+ );
147+ }
148+
149+ public function testReplaceMe ()
150+ {
151+ $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
152+ $ htmlresult = "<!DOCTYPE html><html><head></head><body><div><h1></h1></div></body></html> " ;
153+
154+ $ dom = new GlHtml ($ html );
155+ $ node = $ dom ->get ("span " )[0 ];
156+
37157 $ node ->replaceMe ("<h1></h1> " );
38-
158+
39159 $ result = $ dom ->html ();
40-
41- $ htmlresult = str_replace (["\n" ,"\r" ],'' ,$ htmlresult );
42- $ result = str_replace (["\n" ,"\r" ],'' ,$ result );
43-
44- $ this ->assertEquals ($ htmlresult ,$ result );
160+
161+ $ htmlresult = str_replace (["\n" , "\r" ], '' , $ htmlresult );
162+ $ result = str_replace (["\n" , "\r" ], '' , $ result );
163+
164+ $ this ->assertEquals ($ htmlresult , $ result );
45165 }
46-
47- public function testRemplaceMeParent () {
48- $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
166+
167+
168+ public function testRemplaceMeParent ()
169+ {
170+ $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
49171 $ htmlresult = "<!DOCTYPE html><html><head></head><body><h1></h1></body></html> " ;
50172
51- $ dom = new GlHtml ($ html );
173+ $ dom = new GlHtml ($ html );
52174 $ node = $ dom ->get ("span " )[0 ];
53175
54176 $ node ->getParent ()->replaceMe ("<h1></h1> " );
55177
56178 $ result = $ dom ->html ();
57179
58- $ htmlresult = str_replace (["\n" ,"\r" ],'' ,$ htmlresult );
59- $ result = str_replace (["\n" ,"\r" ],'' ,$ result );
180+ $ htmlresult = str_replace (["\n" , "\r" ], '' , $ htmlresult );
181+ $ result = str_replace (["\n" , "\r" ], '' , $ result );
60182
61- $ this ->assertEquals ($ htmlresult ,$ result );
183+ $ this ->assertEquals ($ htmlresult , $ result );
62184 }
63-
64- public function testsetAttributes () {
65- $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
185+
186+ public function testsetAttributes ()
187+ {
188+ $ html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html> " ;
66189 $ htmlresult = '<!DOCTYPE html><html><head></head><body><div><span id="test"><div></div></span></div></body></html> ' ;
67190
68- $ dom = new GlHtml ($ html );
191+ $ dom = new GlHtml ($ html );
69192 $ node = $ dom ->get ("span " )[0 ];
70193
71194 $ node ->setAttributes (['id ' => 'test ' ]);
72-
195+
73196 $ result = $ dom ->html ();
74197
75- $ htmlresult = str_replace (["\n" ,"\r" ],'' ,$ htmlresult );
76- $ result = str_replace (["\n" ,"\r" ],'' ,$ result );
198+ $ htmlresult = str_replace (["\n" , "\r" ], '' , $ htmlresult );
199+ $ result = str_replace (["\n" , "\r" ], '' , $ result );
77200
78- $ this ->assertEquals ($ htmlresult ,$ result );
201+ $ this ->assertEquals ($ htmlresult , $ result );
79202 }
80-
81- public function testSelfClose () {
203+
204+ public function testSelfClose ()
205+ {
82206 $ html = <<<EOD
83207<!DOCTYPE html>
84208<html>
@@ -90,16 +214,17 @@ public function testSelfClose() {
90214</html>
91215EOD ;
92216
93- $ dom = new GlHtml ($ html );
217+ $ dom = new GlHtml ($ html );
94218 $ node = $ dom ->get ("div " )[0 ];
95-
96- $ result = $ node ->getHtml ();
219+
220+ $ result = $ node ->getHtml ();
97221 $ htmlresult = "<span></span> " ;
98222
99- $ this ->assertEquals ($ htmlresult ,$ result );
223+ $ this ->assertEquals ($ htmlresult , $ result );
100224 }
101-
102- public function testDelete () {
225+
226+ public function testDelete ()
227+ {
103228 $ html = <<<EOD
104229<!DOCTYPE html>
105230<html>
@@ -111,7 +236,7 @@ public function testDelete() {
111236</html>
112237EOD ;
113238
114- $ dom = new GlHtml ($ html );
239+ $ dom = new GlHtml ($ html );
115240 $ node = $ dom ->get ("p " )[0 ];
116241 $ node ->delete ();
117242
@@ -128,8 +253,8 @@ public function testDelete() {
128253
129254 $ result = $ dom ->html ();
130255
131- $ result = str_replace ([' ' , "\n" , "\t" , "\r" ], '' , $ result );
256+ $ result = str_replace ([' ' , "\n" , "\t" , "\r" ], '' , $ result );
132257 $ htmlresult = str_replace ([' ' , "\n" , "\t" , "\r" ], '' , $ htmlresult );
133- $ this ->assertEquals ($ htmlresult ,$ result );
258+ $ this ->assertEquals ($ htmlresult , $ result );
134259 }
135260}
0 commit comments