comparison vendor/nikic/php-parser/test/PhpParser/Unserializer/XMLTest.php @ 0:4c8ae668cc8c

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