@@ -42,6 +42,15 @@ public function testNonExistingFilepath(): void
42
42
$ this ->newReader ('wrong.xml ' );
43
43
}
44
44
45
+ public function testNextNode (): void
46
+ {
47
+ $ reader = $ this ->newReader (self ::getTestFilePath ('sample_04.xml ' ));
48
+
49
+ $ node = $ reader ->nextNode ('updated ' );
50
+
51
+ self ::assertSame ('2020-08-25T13:53:38+00:00 ' , $ node ?->getTextContent());
52
+ }
53
+
45
54
public function testNextInvalidNode (): void
46
55
{
47
56
$ reader = $ this ->newReader (self ::getTestFilePath ('sample_04.xml ' ));
@@ -51,13 +60,36 @@ public function testNextInvalidNode(): void
51
60
self ::assertNull ($ node );
52
61
}
53
62
54
- public function testNextNode (): void
63
+ public function testNextNodeByPath (): void
55
64
{
56
65
$ reader = $ this ->newReader (self ::getTestFilePath ('sample_04.xml ' ));
57
66
58
- $ node = $ reader ->nextNode ('updated ' );
67
+ $ node = $ reader ->nextNode ('/feed/errors2 ' );
59
68
60
- self ::assertSame ('2020-08-25T13:53:38+00:00 ' , $ node ?->getTextContent());
69
+ self ::assertSame ('<errors2 id="2"/> ' , $ node ?->toString());
70
+ }
71
+
72
+ public function testNextInvalidNodeByPath (): void
73
+ {
74
+ $ reader = $ this ->newReader (self ::getTestFilePath ('sample_04.xml ' ));
75
+
76
+ $ node = $ reader ->nextNode ('/feed/items/item1 ' );
77
+
78
+ self ::assertNull ($ node );
79
+ }
80
+
81
+ public function testClose (): void
82
+ {
83
+ self ::expectException (Throwable::class);
84
+ self ::expectExceptionMessage ('Data must be loaded before reading ' );
85
+
86
+ $ reader = $ this ->newReader (self ::getTestFilePath ('sample_04.xml ' ));
87
+
88
+ self ::assertNotNull ($ reader ->nextNode ('item ' ));
89
+
90
+ $ reader ->close ();
91
+
92
+ self ::assertNotNull ($ reader ->nextNode ('item ' ));
61
93
}
62
94
63
95
public function testPreviousNode (): void
@@ -126,6 +158,54 @@ public function testNextEmptyNode(): void
126
158
self ::assertSame ('<errors2 id="2"/> ' , $ node ->toString ());
127
159
}
128
160
161
+ public function testNextNodes (): void
162
+ {
163
+ $ reader = $ this ->newReader (self ::getTestFilePath ('sample_04.xml ' ));
164
+
165
+ $ output = [
166
+ $ reader ->nextNode ('item ' )?->toString(),
167
+ $ reader ->nextNode ('item ' )?->toString(),
168
+ $ reader ->nextNode ('item ' )?->toString(),
169
+ $ reader ->nextNode ('item ' )?->toString(),
170
+ ];
171
+
172
+ self ::assertSame (
173
+ [
174
+ '<item i="0"><id uuid="12345">1</id><name price="10.1">Test 1</name></item> ' ,
175
+ '<item i="1"><id uuid="61648">2</id><name price="5">Test 2</name></item> ' ,
176
+ '<item i="2"><id>3</id><name price="500">Test 3</name></item> ' ,
177
+ '<item i="3"><id uuid="894654">4</id><name>Test 4</name></item> ' ,
178
+ ],
179
+ $ output ,
180
+ );
181
+ }
182
+
183
+ public function testNextNodesByPath (): void
184
+ {
185
+ $ reader = $ this ->newReader (self ::getTestFilePath ('sample_09.xml ' ));
186
+
187
+ $ output = [
188
+ $ reader ->nextNode ('/g:root/data/a ' )?->toString(),
189
+ $ reader ->nextNode ('/g:root/data/a ' )?->toString(),
190
+ $ reader ->nextNode ('/g:root/data/c ' )?->toString(),
191
+ $ reader ->nextNode ('/g:root/a ' )?->toString(),
192
+ $ reader ->nextNode ('/g:root/h:data/a ' )?->toString(),
193
+ $ reader ->nextNode ('/g:root/a ' )?->toString(),
194
+ ];
195
+
196
+ self ::assertSame (
197
+ [
198
+ '<a><id>2</id><prices><price>1</price><priceWithVat>1.21</priceWithVat></prices></a> ' ,
199
+ '<a>data1</a> ' ,
200
+ '<c>5</c> ' ,
201
+ '<a>6</a> ' ,
202
+ '<a>7</a> ' ,
203
+ null ,
204
+ ],
205
+ $ output ,
206
+ );
207
+ }
208
+
129
209
public function testIterateInvalidNodes (): void
130
210
{
131
211
$ reader = $ this ->newReader (self ::getTestFilePath ('sample_04.xml ' ));
@@ -239,6 +319,45 @@ public function testIterateMultipleNamespaces(): void
239
319
);
240
320
}
241
321
322
+ public function testIteratePath (): void
323
+ {
324
+ $ reader = $ this ->newReader (self ::getTestFilePath ('sample_09.xml ' ));
325
+
326
+ $ output = [];
327
+ foreach ($ reader ->iterateNode ('/g:root/data/a ' ) as $ item ) {
328
+ $ output [] = $ item ->toString ();
329
+ }
330
+
331
+ self ::assertSame (
332
+ [
333
+ '<a><id>2</id><prices><price>1</price><priceWithVat>1.21</priceWithVat></prices></a> ' ,
334
+ '<a>data1</a> ' ,
335
+ '<a>4</a> ' ,
336
+ '<a>7</a> ' ,
337
+ ],
338
+ $ output ,
339
+ );
340
+ }
341
+
342
+ public function testIteratePathMultipleNamespaces (): void
343
+ {
344
+ $ reader = $ this ->newReader (self ::getTestFilePath ('sample_08.xml ' ));
345
+
346
+ $ output = [];
347
+ foreach ($ reader ->iterateNode ('/g:rss/channel/item ' , true ) as $ item ) {
348
+ $ output [] = $ item ->toString ();
349
+ }
350
+
351
+ self ::assertSame (
352
+ [
353
+ '<item attr="2"><id>1/L1</id><title>Title 1</title></item> ' ,
354
+ '<item xmlns:g="http://base.google.com/ns/1.0" xmlns:h="http://base.google.com/ns/2.0" attr="1"><data><g:id h:test="asd">1/L2</g:id><g:title test="bb">Title 2</g:title><link>https://www.example.com/v/1</link></data></item> ' ,
355
+ '<item xmlns:g="http://base.google.com/ns/1.0"><g:id>1/L3</g:id><title>Title 3</title></item> ' ,
356
+ ],
357
+ $ output ,
358
+ );
359
+ }
360
+
242
361
/**
243
362
* @param array<array<string>|string> $expected
244
363
*/
@@ -279,7 +398,7 @@ public function testIterateWihSimpleLoadString(string $file, bool $withNamespace
279
398
public static function provideIterateWihSimpleLoadString (): iterable
280
399
{
281
400
yield [
282
- 'file ' => 'sample_04.xml ' ,
401
+ 'file ' => 'sample_04.xml ' ,
283
402
'withNamespaces ' => false ,
284
403
'path ' => '/item/id ' ,
285
404
'expected ' => [
@@ -292,7 +411,7 @@ public static function provideIterateWihSimpleLoadString(): iterable
292
411
];
293
412
294
413
yield [
295
- 'file ' => 'sample_08.xml ' ,
414
+ 'file ' => 'sample_08.xml ' ,
296
415
'withNamespaces ' => false ,
297
416
'path ' => '/item/id ' ,
298
417
'expected ' => [
@@ -303,7 +422,7 @@ public static function provideIterateWihSimpleLoadString(): iterable
303
422
];
304
423
305
424
yield [
306
- 'file ' => 'sample_08.xml ' ,
425
+ 'file ' => 'sample_08.xml ' ,
307
426
'withNamespaces ' => false ,
308
427
'path ' => '/item/g:id ' ,
309
428
'expected ' => [
@@ -314,7 +433,7 @@ public static function provideIterateWihSimpleLoadString(): iterable
314
433
];
315
434
316
435
yield [
317
- 'file ' => 'sample_08.xml ' ,
436
+ 'file ' => 'sample_08.xml ' ,
318
437
'withNamespaces ' => true ,
319
438
'path ' => '/item/id ' ,
320
439
'expected ' => [
@@ -325,7 +444,7 @@ public static function provideIterateWihSimpleLoadString(): iterable
325
444
];
326
445
327
446
yield [
328
- 'file ' => 'sample_08.xml ' ,
447
+ 'file ' => 'sample_08.xml ' ,
329
448
'withNamespaces ' => true ,
330
449
'path ' => '/item/g:id ' ,
331
450
'expected ' => [
@@ -336,7 +455,7 @@ public static function provideIterateWihSimpleLoadString(): iterable
336
455
];
337
456
338
457
yield [
339
- 'file ' => 'sample_08.xml ' ,
458
+ 'file ' => 'sample_08.xml ' ,
340
459
'withNamespaces ' => true ,
341
460
'path ' => '/item/data/g:title ' ,
342
461
'expected ' => [
0 commit comments