XMLTest.php
4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace PhpParser\Unserializer;
use PhpParser\Comment;
use PhpParser\Node\Scalar;
class XMLTest extends \PHPUnit_Framework_TestCase
{
public function testNode() {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:attribute="http://nikic.github.com/PHPParser/XML/attribute" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
<node:Scalar_String line="1" docComment="/** doc comment */">
<attribute:startLine>
<scalar:int>1</scalar:int>
</attribute:startLine>
<attribute:comments>
<scalar:array>
<comment isDocComment="false" line="2">// comment
</comment>
<comment isDocComment="true" line="3">/** doc comment */</comment>
</scalar:array>
</attribute:comments>
<subNode:value>
<scalar:string>Test</scalar:string>
</subNode:value>
</node:Scalar_String>
</AST>
XML;
$unserializer = new XML;
$this->assertEquals(
new Scalar\String_('Test', array(
'startLine' => 1,
'comments' => array(
new Comment('// comment' . "\n", 2),
new Comment\Doc('/** doc comment */', 3),
),
)),
$unserializer->unserialize($xml)
);
}
public function testEmptyNode() {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node">
<node:Scalar_MagicConst_Class />
</AST>
XML;
$unserializer = new XML;
$this->assertEquals(
new Scalar\MagicConst\Class_,
$unserializer->unserialize($xml)
);
}
public function testScalars() {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
<scalar:array>
<scalar:array></scalar:array>
<scalar:array/>
<scalar:string>test</scalar:string>
<scalar:string></scalar:string>
<scalar:string/>
<scalar:int>1</scalar:int>
<scalar:float>1</scalar:float>
<scalar:float>1.5</scalar:float>
<scalar:true/>
<scalar:false/>
<scalar:null/>
</scalar:array>
</AST>
XML;
$result = array(
array(), array(),
'test', '', '',
1,
1, 1.5,
true, false, null
);
$unserializer = new XML;
$this->assertEquals($result, $unserializer->unserialize($xml));
}
/**
* @expectedException \DomainException
* @expectedExceptionMessage AST root element not found
*/
public function testWrongRootElementError() {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<notAST/>
XML;
$unserializer = new XML;
$unserializer->unserialize($xml);
}
/**
* @dataProvider provideTestErrors
*/
public function testErrors($xml, $errorMsg) {
$this->setExpectedException('DomainException', $errorMsg);
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar"
xmlns:node="http://nikic.github.com/PHPParser/XML/node"
xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode"
xmlns:foo="http://nikic.github.com/PHPParser/XML/foo">
$xml
</AST>
XML;
$unserializer = new XML;
$unserializer->unserialize($xml);
}
public function provideTestErrors() {
return array(
array('<scalar:true>test</scalar:true>', '"true" scalar must be empty'),
array('<scalar:false>test</scalar:false>', '"false" scalar must be empty'),
array('<scalar:null>test</scalar:null>', '"null" scalar must be empty'),
array('<scalar:foo>bar</scalar:foo>', 'Unknown scalar type "foo"'),
array('<scalar:int>x</scalar:int>', '"x" is not a valid int'),
array('<scalar:float>x</scalar:float>', '"x" is not a valid float'),
array('', 'Expected node or scalar'),
array('<foo:bar>test</foo:bar>', 'Unexpected node of type "foo:bar"'),
array(
'<node:Scalar_String><foo:bar>test</foo:bar></node:Scalar_String>',
'Expected sub node or attribute, got node of type "foo:bar"'
),
array(
'<node:Scalar_String><subNode:value/></node:Scalar_String>',
'Expected node or scalar'
),
array(
'<node:Foo><subNode:value/></node:Foo>',
'Unknown node type "Foo"'
),
);
}
}