-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlTest.php
More file actions
343 lines (299 loc) · 9.72 KB
/
XmlTest.php
File metadata and controls
343 lines (299 loc) · 9.72 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Http Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file if a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Http;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Http\Xml;
/**
* Xml class tests.
*/
final class XmlTest extends TestCase
{
/**
* Test XML parse method exists.
*/
public function testXmlParseMethodExists(): void
{
$this->assertTrue(method_exists(Xml::class, 'parse'));
}
/**
* Test simple XML parsing.
*/
public function testSimpleXmlParsing(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>Test</name>
<value>123</value>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML parsing with attributes.
*/
public function testXmlParsingWithAttributes(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<root>
<item id="1" type="test">
<name>Test Item</name>
<value>100</value>
</item>
<item id="2" type="sample">
<name>Sample Item</name>
<value>200</value>
</item>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML parsing with CDATA.
*/
public function testXmlParsingWithCdata(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<root>
<description><![CDATA[This is a test description with <special> characters & symbols.]]></description>
<content><![CDATA[
<h1>HTML Content</h1>
<p>This is a paragraph with <strong>bold</strong> text.</p>
]]></content>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML parsing with namespaces.
*/
public function testXmlParsingWithNamespaces(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:test="http://example.com/test">
<test:item>
<test:name>Namespaced Item</test:name>
<test:value>500</test:value>
</test:item>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML parsing with different options.
*/
public function testXmlParsingWithDifferentOptions(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>Test Value</item>
</root>';
// Test with default options
$result1 = Xml::parse($xmlString);
$this->assertNotFalse($result1);
// Test with custom options
$result2 = Xml::parse($xmlString, LIBXML_NOCDATA);
$this->assertNotFalse($result2);
}
/**
* Test invalid XML parsing.
*/
public function testInvalidXmlParsing(): void
{
$invalidXml = '<root><item>Unclosed tag</root>';
$result = Xml::parse($invalidXml);
$this->assertFalse($result);
}
/**
* Test empty XML parsing.
*/
public function testEmptyXmlParsing(): void
{
$result = Xml::parse('');
$this->assertFalse($result);
}
/**
* Test malformed XML parsing.
*/
public function testMalformedXmlParsing(): void
{
$malformedXml = '<?xml version="1.0"?><root><item><name>Test</name><value>123</item></root>';
$result = Xml::parse($malformedXml);
$this->assertFalse($result);
}
/**
* Test XML with special characters.
*/
public function testXmlWithSpecialCharacters(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<root>
<text>Special chars: < > & " '</text>
<unicode>Unicode: ñ ü ß € 中文 العربية</unicode>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test large XML parsing.
*/
public function testLargeXmlParsing(): void
{
$largeXml = '<?xml version="1.0" encoding="UTF-8"?><root>';
// Generate large XML with many items
for ($i = 1; $i <= 100; $i++) {
$largeXml .= "<item id=\"{$i}\"><name>Item {$i}</name><value>" . ($i * 10) . "</value></item>";
}
$largeXml .= '</root>';
$result = Xml::parse($largeXml);
$this->assertNotFalse($result);
}
/**
* Test XML parsing constants.
*/
public function testXmlParsingConstants(): void
{
// Test that XML parsing constants are available
$this->assertTrue(defined('LIBXML_NOCDATA'));
$this->assertTrue(defined('LIBXML_VERSION'));
$this->assertIsInt(LIBXML_NOCDATA);
$this->assertIsInt(LIBXML_VERSION);
}
/**
* Test complex nested XML.
*/
public function testComplexNestedXml(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="1">
<title>PHP Programming</title>
<author>
<name>John Doe</name>
<email>john@example.com</email>
</author>
<publisher>
<name>Tech Books</name>
<address>
<street>123 Main St</street>
<city>Anytown</city>
<country>USA</country>
</address>
</publisher>
<chapters>
<chapter number="1">Introduction</chapter>
<chapter number="2">Variables</chapter>
<chapter number="3">Functions</chapter>
</chapters>
</book>
</catalog>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML with mixed content.
*/
public function testXmlWithMixedContent(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<document>
<paragraph>
This is text with <emphasis>emphasized</emphasis> content
and <link href="http://example.com">a link</link>.
</paragraph>
</document>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML with processing instructions.
*/
public function testXmlWithProcessingInstructions(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<root>
<item>Test</item>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML with comments.
*/
public function testXmlWithComments(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a comment -->
<root>
<!-- Another comment -->
<item>Test</item>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML validation functionality.
*/
public function testXmlValidationFunctionality(): void
{
// Test if SimpleXML extension is loaded
$this->assertTrue(extension_loaded('simplexml'), 'SimpleXML extension should be loaded');
// Test if libxml extension is loaded
$this->assertTrue(extension_loaded('libxml'), 'libxml extension should be loaded');
}
/**
* Test XML encoding handling.
*/
public function testXmlEncodingHandling(): void
{
$xmlString = '<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<text>Text with special chars</text>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test XML parse without version declaration.
*/
public function testXmlParseWithoutVersionDeclaration(): void
{
$xmlString = '<root>
<item>Test</item>
</root>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
/**
* Test RSS-like XML structure.
*/
public function testRssLikeXmlStructure(): void
{
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<description>A test RSS feed</description>
<item>
<title>First Post</title>
<description>Description of first post</description>
<link>http://example.com/post1</link>
<pubDate>Mon, 15 Aug 2025 12:00:00 GMT</pubDate>
</item>
</channel>
</rss>';
$result = Xml::parse($xmlString);
$this->assertNotFalse($result);
}
}