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