1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleXmlReader; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
|
7
|
|
|
class PathIteratorTest extends PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** @test */ |
10
|
|
|
public function testBasicFunctionality() |
11
|
|
|
{ |
12
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/simpletest.xml'); |
13
|
|
|
$matches = iterator_to_array($xml->path('root/matchparent/match')); |
14
|
|
|
$this->assertEquals('match1', (string) $matches[0]); |
15
|
|
|
$this->assertEquals('match2', (string) $matches[1]); |
16
|
|
|
$this->assertEquals('match3', (string) $matches[2]->child); |
17
|
|
|
$this->assertEquals(3, count($matches)); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** @test */ |
21
|
|
|
public function testStart() |
22
|
|
|
{ |
23
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/simpletest.xml'); |
24
|
|
|
$matches = iterator_to_array($xml->path('root/*/match')); |
25
|
|
|
$this->assertEquals(3, count($matches)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** @test */ |
29
|
|
|
public function testPathOuterXml() |
30
|
|
|
{ |
31
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/simpletest.xml'); |
32
|
|
|
$matches = iterator_to_array($xml->path('root/matchparent/match', SimpleXmlReader::RETURN_OUTER_XML_STRING)); |
33
|
|
|
$this->assertEquals('<match><child>match3</child></match>', preg_replace('/\s/', '', (string) $matches[2])); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @test */ |
37
|
|
|
public function testPathInnerXml() |
38
|
|
|
{ |
39
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/simpletest.xml'); |
40
|
|
|
$matches = iterator_to_array($xml->path('root/matchparent/match', SimpleXmlReader::RETURN_INNER_XML_STRING)); |
41
|
|
|
$this->assertEquals('<child>match3</child>', preg_replace('/\s/', '', (string) $matches[2])); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @test */ |
45
|
|
|
public function testEmptyXmlShortTagNotiation() |
46
|
|
|
{ |
47
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/empty1.xml'); |
48
|
|
|
$matches = iterator_to_array($xml->path('root/matchparent/match')); |
49
|
|
|
$this->assertCount(0, $matches); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @test */ |
53
|
|
|
public function testEmptyXmlOpenCloseTagNotiation() |
54
|
|
|
{ |
55
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/empty2.xml'); |
56
|
|
|
$matches = iterator_to_array($xml->path('root/matchparent/match')); |
57
|
|
|
$this->assertCount(0, $matches); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** @test */ |
61
|
|
|
public function testEmptyXmlWithHeader() |
62
|
|
|
{ |
63
|
|
|
$xml = SimpleXmlReader::openGzippedXML(__DIR__ . '/testdata/empty3.xml.gz'); |
64
|
|
|
$matches = iterator_to_array($xml->path('root/matchparent/match')); |
65
|
|
|
$this->assertCount(0, $matches); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @test */ |
69
|
|
|
public function testAutoOpen() |
70
|
|
|
{ |
71
|
|
|
$xml = SimpleXmlReader::autoOpenXML(__DIR__ . '/testdata/empty3.xml.gz'); |
72
|
|
|
$this->assertCount(0, iterator_to_array($xml->path('root/matchparent/match'))); |
73
|
|
|
$xml = SimpleXmlReader::autoOpenXML(__DIR__ . '/testdata/empty2.xml'); |
74
|
|
|
$this->assertCount(0, iterator_to_array($xml->path('root/matchparent/match'))); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @test */ |
78
|
|
|
public function testInvalidXml1() |
79
|
|
|
{ |
80
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/invalid1.xml'); |
81
|
|
|
// No PHP errors should be raised |
82
|
|
|
$iterator = $xml->path('root/foo/bar'); |
83
|
|
|
$result = iterator_to_array($iterator); |
84
|
|
|
$this->assertCount(2, $result); |
85
|
|
|
foreach ($result as $bar) { |
86
|
|
|
$this->assertEquals('foobarbaz', (string) $bar->baz); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @test */ |
91
|
|
|
public function testInvalidXml2() |
92
|
|
|
{ |
93
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/invalid2.xml'); |
94
|
|
|
// No PHP errors should be raised, but result is empty |
95
|
|
|
$iterator = $xml->path('root/foo/bar'); |
96
|
|
|
$result = iterator_to_array($iterator); |
97
|
|
|
$this->assertCount(0, $result); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @test */ |
101
|
|
|
public function testInvalidXml3() |
102
|
|
|
{ |
103
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/invalid3.xml'); |
104
|
|
|
// No PHP errors should be raised, but an exception must be thrown |
105
|
|
|
$iterator = $xml->path('response/result/log/logs/entry'); |
106
|
|
|
$this->setExpectedException('SimpleXmlReader\XmlException'); |
107
|
|
|
iterator_to_array($iterator); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** @test */ |
112
|
|
|
public function testPathCbAttr1OuterXml() |
113
|
|
|
{ |
114
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/cb.xml'); |
115
|
|
|
$res = implode('', iterator_to_array($xml->path('root/zoo/animal', SimpleXmlReader::RETURN_OUTER_XML_STRING, function ($xr, $crumbs) { |
116
|
|
|
$path = implode("/", $crumbs); |
117
|
|
|
if ($path == "root/zoo") { |
118
|
|
|
if ($xr->getAttribute('city') != "Banghok") { |
119
|
|
|
return PathIterator::ELEMENT_IS_INVALID; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
return PathIterator::ELEMENT_IS_VALID; |
123
|
|
|
}))); |
124
|
|
|
$this->assertEquals('<animal>kakariki</animal>', preg_replace('/\s/', '', (string) $res)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** @test */ |
128
|
|
|
public function testPathCbAttr2OuterXml() |
129
|
|
|
{ |
130
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/cb.xml'); |
131
|
|
|
$res = implode('', iterator_to_array($xml->path('root/zoo/animal', SimpleXmlReader::RETURN_OUTER_XML_STRING, function ($xr, $crumbs) { |
132
|
|
|
$path = implode("/", $crumbs); |
133
|
|
|
if ($path == "root/zoo") { |
134
|
|
|
if ($xr->getAttribute('contenent') != "Europe") { |
135
|
|
|
return PathIterator::ELEMENT_IS_INVALID; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return PathIterator::ELEMENT_IS_VALID; |
139
|
|
|
}))); |
140
|
|
|
$this->assertEquals('<animal>cat</animal><animal>bear</animal>', preg_replace('/\s/', '', (string) $res)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** @test */ |
144
|
|
|
public function testPathCbElemOuterXml() |
145
|
|
|
{ |
146
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/cb.xml'); |
147
|
|
|
$res = implode('', iterator_to_array($xml->path('root/zoo/animal', SimpleXmlReader::RETURN_OUTER_XML_STRING, function ($xr, $crumbs) { |
148
|
|
|
$path = implode("/", $crumbs); |
149
|
|
|
if ($path == "root/zoo/work") { |
150
|
|
|
if ($xr->readString() != "yes") { |
151
|
|
|
return PathIterator::SIBLINGS_ARE_INVALID; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
return PathIterator::ELEMENT_IS_VALID; |
155
|
|
|
}))); |
156
|
|
|
$this->assertEquals('<animal>kakariki</animal><animal>bear</animal>', preg_replace('/\s/', '', (string) $res)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** @test */ |
160
|
|
|
public function testPathCbElemAttrOuterXml() |
161
|
|
|
{ |
162
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/cb.xml'); |
163
|
|
|
$res = implode('', iterator_to_array($xml->path('root/zoo/animal', SimpleXmlReader::RETURN_OUTER_XML_STRING, function ($xr, $crumbs) { |
164
|
|
|
$path = implode("/", $crumbs); |
165
|
|
|
if ($path == "root/zoo") { |
166
|
|
|
if ($xr->getAttribute('contenent') != "Europe") { |
167
|
|
|
return PathIterator::ELEMENT_IS_INVALID; |
168
|
|
|
} |
169
|
|
|
} elseif ($path == "root/zoo/work") { |
170
|
|
|
if ($xr->readString() != "yes") { |
171
|
|
|
return PathIterator::SIBLINGS_ARE_INVALID; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
return PathIterator::ELEMENT_IS_VALID; |
175
|
|
|
}))); |
176
|
|
|
$this->assertEquals('<animal>bear</animal>', preg_replace('/\s/', '', (string) $res)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** @test */ |
180
|
|
|
public function testPathCb2ElemOuterXml() |
181
|
|
|
{ |
182
|
|
|
$xml = SimpleXmlReader::openXML(__DIR__ . '/testdata/cb2.xml'); |
183
|
|
|
$res = implode('', iterator_to_array($xml->path('root/group/zoos/zoo/animal', SimpleXmlReader::RETURN_OUTER_XML_STRING, function ($xr, $crumbs) { |
184
|
|
|
$path = implode("/", $crumbs); |
185
|
|
|
if ($path == "root/group/work") { |
186
|
|
|
if ($xr->readString() != "yes") { |
187
|
|
|
return PathIterator::SIBLINGS_ARE_INVALID; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
return PathIterator::ELEMENT_IS_VALID; |
191
|
|
|
}))); |
192
|
|
|
$this->assertEquals('<animal>kakariki</animal><animal>bear</animal>', preg_replace('/\s/', '', (string) $res)); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|