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
|
|
|
|