| | 74 | |
|---|
| | 75 | /** |
|---|
| | 76 | * test that whitespace handling works as expected |
|---|
| | 77 | */ |
|---|
| | 78 | public function testWhiteSpaceTrim() |
|---|
| | 79 | { |
|---|
| | 80 | $xml = '<xml> |
|---|
| | 81 | <string> |
|---|
| | 82 | |
|---|
| | 83 | This XML |
|---|
| | 84 | document |
|---|
| | 85 | contains |
|---|
| | 86 | line breaks. |
|---|
| | 87 | |
|---|
| | 88 | </string> |
|---|
| | 89 | </xml>'; |
|---|
| | 90 | $options = array(stubXMLUnserializerOption::WHITESPACE => stubXMLUnserializerOption::WHITESPACE_TRIM); |
|---|
| | 91 | $unserializer = new stubXMLUnserializer($options); |
|---|
| | 92 | $this->assertEqual($unserializer->unserialize($xml), array('string' => 'This XML |
|---|
| | 93 | document |
|---|
| | 94 | contains |
|---|
| | 95 | line breaks.') |
|---|
| | 96 | ); |
|---|
| | 97 | } |
|---|
| | 98 | |
|---|
| | 99 | /** |
|---|
| | 100 | * test that whitespace handling works as expected |
|---|
| | 101 | */ |
|---|
| | 102 | public function testWhiteSpaceNormalize() |
|---|
| | 103 | { |
|---|
| | 104 | $xml = '<xml> |
|---|
| | 105 | <string> |
|---|
| | 106 | |
|---|
| | 107 | This XML |
|---|
| | 108 | document |
|---|
| | 109 | contains |
|---|
| | 110 | line breaks. |
|---|
| | 111 | |
|---|
| | 112 | </string> |
|---|
| | 113 | </xml>'; |
|---|
| | 114 | $options = array(stubXMLUnserializerOption::WHITESPACE => stubXMLUnserializerOption::WHITESPACE_NORMALIZE); |
|---|
| | 115 | $unserializer = new stubXMLUnserializer($options); |
|---|
| | 116 | $this->assertEqual($unserializer->unserialize($xml), array('string' => 'This XML document contains line breaks.')); |
|---|
| | 117 | } |
|---|
| | 118 | |
|---|
| | 119 | /** |
|---|
| | 120 | * test that whitespace handling works as expected |
|---|
| | 121 | */ |
|---|
| | 122 | public function testWhiteSpaceKeep() |
|---|
| | 123 | { |
|---|
| | 124 | $xml = '<xml> |
|---|
| | 125 | <string> |
|---|
| | 126 | |
|---|
| | 127 | This XML |
|---|
| | 128 | document |
|---|
| | 129 | contains |
|---|
| | 130 | line breaks. |
|---|
| | 131 | |
|---|
| | 132 | </string> |
|---|
| | 133 | </xml>'; |
|---|
| | 134 | $options = array(stubXMLUnserializerOption::WHITESPACE => stubXMLUnserializerOption::WHITESPACE_KEEP); |
|---|
| | 135 | $unserializer = new stubXMLUnserializer($options); |
|---|
| | 136 | $this->assertEqual($unserializer->unserialize($xml), array('string' => ' |
|---|
| | 137 | |
|---|
| | 138 | This XML |
|---|
| | 139 | document |
|---|
| | 140 | contains |
|---|
| | 141 | line breaks. |
|---|
| | 142 | |
|---|
| | 143 | ')); |
|---|
| | 144 | } |
|---|
| | 145 | |
|---|
| | 146 | /** |
|---|
| | 147 | * test unserializing a list of items |
|---|
| | 148 | */ |
|---|
| | 149 | public function testUnserializeWithAttributes() |
|---|
| | 150 | { |
|---|
| | 151 | $options = array(stubXMLUnserializerOption::ATTRIBUTES_PARSE => true, |
|---|
| | 152 | stubXMLUnserializerOption::ATTRIBUTES_ARRAYKEY => false |
|---|
| | 153 | ); |
|---|
| | 154 | $unserializer = new stubXMLUnserializer($options); |
|---|
| | 155 | $this->assertEqual($unserializer->unserializeFile(TEST_SRC_PATH . '/resources/unserializer.xml'), |
|---|
| | 156 | array('test' => array('foo' => 'bar', |
|---|
| | 157 | 'tag' => 'test', |
|---|
| | 158 | '_content' => 'Test' |
|---|
| | 159 | ) |
|---|
| | 160 | ) |
|---|
| | 161 | ); |
|---|
| | 162 | } |
|---|
| | 163 | |
|---|
| | 164 | /** |
|---|
| | 165 | * test unserializing a list of items |
|---|
| | 166 | */ |
|---|
| | 167 | public function testUnserializeWithTagMap() |
|---|
| | 168 | { |
|---|
| | 169 | $xml1 = '<root>' . |
|---|
| | 170 | ' <foo>FOO</foo>' . |
|---|
| | 171 | ' <bar>BAR</bar>' . |
|---|
| | 172 | '</root>'; |
|---|
| | 173 | $xml2 = '<root>' . |
|---|
| | 174 | ' <foo>'. |
|---|
| | 175 | ' <tomato>45</tomato>'. |
|---|
| | 176 | ' </foo>'. |
|---|
| | 177 | ' <bar>'. |
|---|
| | 178 | ' <tomato>31</tomato>'. |
|---|
| | 179 | ' </bar>'. |
|---|
| | 180 | '</root>'; |
|---|
| | 181 | $options = array(stubXMLUnserializerOption::TAG_MAP => array('foo' => 'bar', |
|---|
| | 182 | 'bar' => 'foo' |
|---|
| | 183 | ) |
|---|
| | 184 | ); |
|---|
| | 185 | $unserializer = new stubXMLUnserializer($options); |
|---|
| | 186 | $this->assertEqual($unserializer->unserialize($xml1), array('bar' => 'FOO', |
|---|
| | 187 | 'foo' => 'BAR' |
|---|
| | 188 | ) |
|---|
| | 189 | ); |
|---|
| | 190 | $this->assertEqual($unserializer->unserialize($xml2), array('bar' => array('tomato' => 45), |
|---|
| | 191 | 'foo' => array('tomato' => 31) |
|---|
| | 192 | ) |
|---|
| | 193 | ); |
|---|
| | 194 | } |
|---|
| | 195 | |
|---|
| | 196 | /** |
|---|
| | 197 | * test unserializing a list of items |
|---|
| | 198 | */ |
|---|
| | 199 | public function testUnserializeWithTypeGuessing() |
|---|
| | 200 | { |
|---|
| | 201 | $xml = '<root>' . |
|---|
| | 202 | ' <string>Just a string...</string>' . |
|---|
| | 203 | ' <booleanValue>true</booleanValue>' . |
|---|
| | 204 | ' <foo>-563</foo>' . |
|---|
| | 205 | ' <bar>4.73736</bar>' . |
|---|
| | 206 | ' <array foo="false" bar="12">true</array>' . |
|---|
| | 207 | '</root>'; |
|---|
| | 208 | $options = array(stubXMLUnserializerOption::ATTRIBUTES_PARSE => true, |
|---|
| | 209 | stubXMLUnserializerOption::GUESS_TYPES => true |
|---|
| | 210 | ); |
|---|
| | 211 | $unserializer = new stubXMLUnserializer($options); |
|---|
| | 212 | $result = $unserializer->unserialize($xml); |
|---|
| | 213 | $this->assertEqual($result, array('string' => 'Just a string...', |
|---|
| | 214 | 'booleanValue' => true, |
|---|
| | 215 | 'foo' => -563, |
|---|
| | 216 | 'bar' => 4.73736, |
|---|
| | 217 | 'array' => array('foo' => false, |
|---|
| | 218 | 'bar' => 12, |
|---|
| | 219 | '_content' => true |
|---|
| | 220 | ) |
|---|
| | 221 | ) |
|---|
| | 222 | ); |
|---|
| | 223 | $this->assertTrue($result['booleanValue']); |
|---|
| | 224 | $this->assertTrue(is_int($result['foo'])); |
|---|
| | 225 | $this->assertTrue(is_float($result['bar'])); |
|---|
| | 226 | $this->assertFalse($result['array']['foo']); |
|---|
| | 227 | $this->assertTrue(is_int($result['array']['bar'])); |
|---|
| | 228 | $this->assertTrue($result['array']['_content']); |
|---|
| | 229 | } |
|---|