Skip to content

Commit b0af846

Browse files
authored
Merge pull request #8 from joshtronic/master
Unit Tests!!
2 parents ee664cf + 53538b4 commit b0af846

3 files changed

Lines changed: 376 additions & 0 deletions

File tree

tests/bootstrap.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
require_once 'src/simplexml_dump.php';
4+
require_once 'src/simplexml_tree.php';
5+
6+
class simplexml_dump_bootstrap extends PHPUnit_Framework_TestCase
7+
{
8+
public function setUp()
9+
{
10+
$this->simpleXML = simplexml_load_string('<?xml version="1.0" standalone="yes"?>
11+
<movies>
12+
<movie>
13+
<title>PHP: Behind the Parser</title>
14+
<characters>
15+
<character>
16+
<name>Ms. Coder</name>
17+
<actor>Onlivia Actora</actor>
18+
</character>
19+
<character>
20+
<name>Mr. Coder</name>
21+
<actor>El Act&#211;r</actor>
22+
</character>
23+
</characters>
24+
<plot>
25+
So, this language. It\'s like, a programming language.
26+
Or is it a scripting language? All is revealed in this
27+
thrilling horror spoof of a documentary.
28+
</plot>
29+
<great-lines>
30+
<line>PHP solves all my web problems</line>
31+
</great-lines>
32+
<rating type="thumbs">7</rating>
33+
<rating type="stars">5</rating>
34+
</movie>
35+
</movies>
36+
');
37+
38+
$this->simpleXML_default_NS = simplexml_load_string('<?xml version="1.0" standalone="yes"?>
39+
<movies xmlns="https://github.com/IMSoP/simplexml_debug">
40+
<movie>
41+
<title>PHP: Behind the Parser</title>
42+
<characters>
43+
<character>
44+
<name>Ms. Coder</name>
45+
<actor>Onlivia Actora</actor>
46+
</character>
47+
<character>
48+
<name>Mr. Coder</name>
49+
<actor>El Act&#211;r</actor>
50+
</character>
51+
</characters>
52+
<plot>
53+
So, this language. It\'s like, a programming language.
54+
Or is it a scripting language? All is revealed in this
55+
thrilling horror spoof of a documentary.
56+
</plot>
57+
<great-lines>
58+
<line>PHP solves all my web problems</line>
59+
</great-lines>
60+
<rating type="thumbs">7</rating>
61+
<rating type="stars">5</rating>
62+
</movie>
63+
</movies>
64+
');
65+
66+
$this->simpleXML_named_NS = simplexml_load_string('<?xml version="1.0" standalone="yes"?>
67+
<movies xmlns:test="https://github.com/IMSoP/simplexml_debug">
68+
<test:movie>
69+
<test:title>PHP: Behind the Parser</test:title>
70+
<test:characters>
71+
<test:character>
72+
<test:name>Ms. Coder</test:name>
73+
<test:actor>Onlivia Actora</test:actor>
74+
</test:character>
75+
<test:character>
76+
<test:name>Mr. Coder</test:name>
77+
<test:actor>El Act&#211;r</test:actor>
78+
</test:character>
79+
</test:characters>
80+
<test:plot>
81+
So, this language. It\'s like, a programming language.
82+
Or is it a scripting language? All is revealed in this
83+
thrilling horror spoof of a documentary.
84+
</test:plot>
85+
<test:great-lines>
86+
<test:line>PHP solves all my web problems</test:line>
87+
</test:great-lines>
88+
<test:rating type="thumbs">7</test:rating>
89+
<test:rating type="stars">5</test:rating>
90+
</test:movie>
91+
</movies>
92+
');
93+
}
94+
}
95+
96+
?>

tests/simplexml_dump_Test.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
class simplexml_dump_Test extends simplexml_dump_bootstrap
4+
{
5+
public function setUp()
6+
{
7+
$this->expected = "SimpleXML object (1 item)
8+
[
9+
Element {
10+
Name: 'movies'
11+
String Content: '
12+
13+
'
14+
Content in Default Namespace
15+
Children: 1 - 1 'movie'
16+
Attributes: 0
17+
}
18+
]
19+
";
20+
21+
$this->expected_default_NS = "SimpleXML object (1 item)
22+
[
23+
Element {
24+
Namespace: 'https://github.com/IMSoP/simplexml_debug'
25+
(Default Namespace)
26+
Name: 'movies'
27+
String Content: '
28+
29+
'
30+
Content in Default Namespace
31+
Namespace URI: 'https://github.com/IMSoP/simplexml_debug'
32+
Children: 1 - 1 'movie'
33+
Attributes: 0
34+
}
35+
]
36+
";
37+
38+
$this->expected_named_NS = "SimpleXML object (1 item)
39+
[
40+
Element {
41+
Name: 'movies'
42+
String Content: '
43+
44+
'
45+
Content in Namespace test
46+
Namespace URI: 'https://github.com/IMSoP/simplexml_debug'
47+
Children: 1 - 1 'movie'
48+
Attributes: 0
49+
}
50+
]
51+
";
52+
53+
parent::setUp();
54+
}
55+
56+
public function testDump()
57+
{
58+
ob_start();
59+
simplexml_dump($this->simpleXML);
60+
$return = ob_get_contents();
61+
ob_end_clean();
62+
63+
$this->assertEquals($this->expected, $return);
64+
}
65+
66+
public function testDumpReturn()
67+
{
68+
$return = simplexml_dump($this->simpleXML, true);
69+
$this->assertEquals($this->expected, $return);
70+
}
71+
72+
public function testDumpWithDefaultNS()
73+
{
74+
$return = simplexml_dump($this->simpleXML_default_NS, true);
75+
$this->assertEquals($this->expected_default_NS, $return);
76+
}
77+
78+
public function testDumpWithNamedNS()
79+
{
80+
$return = simplexml_dump($this->simpleXML_named_NS, true);
81+
$this->assertEquals($this->expected_named_NS, $return);
82+
}
83+
84+
public function testDumpAttributeWithNamedNS()
85+
{
86+
$xml = '<parent xmlns:ns="ns"><ns:child ns:foo="bar" /></parent>';
87+
$sxml = simplexml_load_string($xml);
88+
89+
$return = simplexml_dump($sxml->children('ns', true)->child->attributes('ns'), true);
90+
91+
$expected = "SimpleXML object (1 item)
92+
[
93+
Attribute {
94+
Namespace: 'ns'
95+
Namespace Alias: 'ns'
96+
Name: 'foo'
97+
Value: 'bar'
98+
}
99+
]
100+
";
101+
102+
$this->assertEquals($expected, $return);
103+
}
104+
105+
public function testDumpMultipleAttributes()
106+
{
107+
$xml = '<parent xmlns:ns="ns"><child ns:one="1" ns:two="2" ns:three="3" /></parent>';
108+
$sxml = simplexml_load_string($xml);
109+
110+
$return = simplexml_dump($sxml->child, true);
111+
112+
$expected = "SimpleXML object (1 item)
113+
[
114+
Element {
115+
Namespace: 'ns'
116+
Namespace Alias: 'ns'
117+
Name: 'child'
118+
String Content: ''
119+
Content in Namespace ns
120+
Namespace URI: 'ns'
121+
Children: 0
122+
Attributes: 3 - 'one', 'two', 'three'
123+
}
124+
]
125+
";
126+
127+
$this->assertEquals($expected, $return);
128+
}
129+
}
130+
131+
?>

tests/simplexml_tree_Test.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
class simplexml_tree_Test extends simplexml_dump_bootstrap
4+
{
5+
public function setUp()
6+
{
7+
$this->expected = "SimpleXML object (1 item)
8+
[0] // <movies>
9+
->movie[0]
10+
->title[0]
11+
->characters[0]
12+
->character[0]
13+
->name[0]
14+
->actor[0]
15+
->character[1]
16+
->name[0]
17+
->actor[0]
18+
->plot[0]
19+
->great-lines[0]
20+
->line[0]
21+
->rating[0]
22+
['type']
23+
->rating[1]
24+
['type']
25+
";
26+
27+
$this->expected_default_NS = "SimpleXML object (1 item)
28+
[0] // <movies>
29+
->movie[0]
30+
->title[0]
31+
->characters[0]
32+
->character[0]
33+
->name[0]
34+
->actor[0]
35+
->character[1]
36+
->name[0]
37+
->actor[0]
38+
->plot[0]
39+
->great-lines[0]
40+
->line[0]
41+
->rating[0]
42+
['type']
43+
->rating[1]
44+
['type']
45+
";
46+
47+
$this->expected_named_NS = "SimpleXML object (1 item)
48+
[0] // <movies>
49+
->children('test', true)
50+
->movie[0]
51+
->title[0]
52+
->characters[0]
53+
->character[0]
54+
->name[0]
55+
->actor[0]
56+
->character[1]
57+
->name[0]
58+
->actor[0]
59+
->plot[0]
60+
->great-lines[0]
61+
->line[0]
62+
->rating[0]
63+
->attributes('', true)
64+
->type
65+
->rating[1]
66+
->attributes('', true)
67+
->type
68+
";
69+
70+
parent::setUp();
71+
}
72+
73+
public function testTree()
74+
{
75+
ob_start();
76+
simplexml_tree($this->simpleXML);
77+
$return = ob_get_contents();
78+
ob_end_clean();
79+
80+
$this->assertEquals($this->expected, $return);
81+
}
82+
83+
public function testTreeIncludeStringContent()
84+
{
85+
ob_start();
86+
simplexml_tree($this->simpleXML, true);
87+
$return = ob_get_contents();
88+
ob_end_clean();
89+
90+
$expected = "SimpleXML object (1 item)
91+
[0] // <movies>
92+
(string) '' (9 chars)
93+
->movie[0]
94+
(string) '' (41 chars)
95+
->title[0]
96+
(string) 'PHP: Behind the...' (22 chars)
97+
->characters[0]
98+
(string) '' (20 chars)
99+
->character[0]
100+
(string) '' (23 chars)
101+
->name[0]
102+
(string) 'Ms. Coder' (9 chars)
103+
->actor[0]
104+
(string) 'Onlivia Actora' (14 chars)
105+
->character[1]
106+
(string) '' (23 chars)
107+
->name[0]
108+
(string) 'Mr. Coder' (9 chars)
109+
->actor[0]
110+
(string) 'El ActÓr' (9 chars)
111+
->plot[0]
112+
(string) 'So, this langua...' (174 chars)
113+
->great-lines[0]
114+
(string) '' (13 chars)
115+
->line[0]
116+
(string) 'PHP solves all ...' (30 chars)
117+
->rating[0]
118+
(string) '7' (1 chars)
119+
['type']
120+
(string) 'thumbs' (6 chars)
121+
->rating[1]
122+
(string) '5' (1 chars)
123+
['type']
124+
(string) 'stars' (5 chars)
125+
";
126+
127+
$this->assertEquals($expected, $return);
128+
}
129+
130+
public function testTreeReturn()
131+
{
132+
$return = simplexml_tree($this->simpleXML, false, true);
133+
$this->assertEquals($this->expected, $return);
134+
}
135+
136+
public function testTreeWithDefaultNS()
137+
{
138+
$return = simplexml_tree($this->simpleXML_default_NS, false, true);
139+
$this->assertEquals($this->expected_default_NS, $return);
140+
}
141+
142+
public function testTreeWithNamedNS()
143+
{
144+
$return = simplexml_tree($this->simpleXML_named_NS, false, true);
145+
$this->assertEquals($this->expected_named_NS, $return);
146+
}
147+
}
148+
149+
?>

0 commit comments

Comments
 (0)