21
21
use function is_array ;
22
22
use function is_numeric ;
23
23
use function is_string ;
24
+ use function preg_replace ;
24
25
use function simplexml_load_string ;
26
+ use function trim ;
25
27
26
28
class DefaultReaderTest extends BaseTestCase
27
29
{
@@ -132,7 +134,7 @@ public function testReadAllFile(): void
132
134
$ node = $ reader ->nextNode ('feed ' );
133
135
134
136
self ::assertSame (
135
- '<feed version="2.0"><updated>2020-08-25T13:53:38+00:00</updated><title>Test feed</title><errors id="1"/><errors2 id="2"/><items><item i="0"><id uuid="12345">1</id><name price="10.1">Test 1</name></item><item i="1"><id uuid="61648">2</id><name price="5">Test 2</name></item><item i="2"><id>3</id><name price="500">Test 3</name></item><item i="3"><id uuid="894654">4</id><name>Test 4</name></item><item i="4"><id uuid="78954">5</id><name price="0.99">Test 5</name></item></items></feed> ' ,
137
+ '<feed version="2.0"><updated>2020-08-25T13:53:38+00:00</updated><title>Test feed</title><errors id="1"/><errors2 id="2"/><items><item i="0"><id uuid="12345">1</id><name price="10.1">Test 1</name></item><item i="1"><id uuid="61648">2</id><name price="5">Test 2</name></item><item i="2"><id>3</id><name price="500"><![CDATA[ Test 3 & 9]]> </name></item><item i="3"><id uuid="894654">4</id><name>Test 4</name></item><item i="4"><id uuid="78954">5</id><name price="0.99">Test 5</name></item></items></feed> ' ,
136
138
$ node ?->toString(),
137
139
);
138
140
}
@@ -172,7 +174,7 @@ public function testNextNodes(): void
172
174
[
173
175
'<item i="0"><id uuid="12345">1</id><name price="10.1">Test 1</name></item> ' ,
174
176
'<item i="1"><id uuid="61648">2</id><name price="5">Test 2</name></item> ' ,
175
- '<item i="2"><id>3</id><name price="500">Test 3</name></item> ' ,
177
+ '<item i="2"><id>3</id><name price="500"><![CDATA[ Test 3 & 9]]> </name></item> ' ,
176
178
'<item i="3"><id uuid="894654">4</id><name>Test 4</name></item> ' ,
177
179
],
178
180
$ output ,
@@ -234,7 +236,7 @@ public function testIterateNodes(): void
234
236
[
235
237
'<item i="0"><id uuid="12345">1</id><name price="10.1">Test 1</name></item> ' ,
236
238
'<item i="1"><id uuid="61648">2</id><name price="5">Test 2</name></item> ' ,
237
- '<item i="2"><id>3</id><name price="500">Test 3</name></item> ' ,
239
+ '<item i="2"><id>3</id><name price="500"><![CDATA[ Test 3 & 9]]> </name></item> ' ,
238
240
'<item i="3"><id uuid="894654">4</id><name>Test 4</name></item> ' ,
239
241
'<item i="4"><id uuid="78954">5</id><name price="0.99">Test 5</name></item> ' ,
240
242
],
@@ -359,14 +361,16 @@ public function testIteratePathMultipleNamespaces(): void
359
361
360
362
/**
361
363
* @param array<list<string>|string> $expected
364
+ * @param array<list<string>|string>|null $expectedOverride
362
365
*/
363
366
#[DataProvider('provideIterateXpath ' )]
364
- public function testIterateWithSimpleLoadString (string $ file , bool $ withNamespaces , string $ path , array $ expected ): void
367
+ public function testIterateWithSimpleLoadString (string $ file , bool $ withNamespaces , string $ path , array $ expected, ? array $ expectedOverride = null ): void
365
368
{
366
369
$ reader = $ this ->newReader (self ::getTestFilePath ($ file ));
367
370
368
371
foreach ($ reader ->iterateNode ('item ' , $ withNamespaces ) as $ i => $ item ) {
369
- $ elements = [];
372
+ $ elements = [];
373
+ $ expectedItem = $ expectedOverride [$ i ] ?? $ expected [$ i ];
370
374
371
375
try {
372
376
self ::withErrorHandler (static function () use ($ item , $ path , &$ elements ): void {
@@ -382,16 +386,16 @@ public function testIterateWithSimpleLoadString(string $file, bool $withNamespac
382
386
throw new Exception ('xpath: error ' );
383
387
}
384
388
} catch (Throwable $ exception ) {
385
- $ expectedMessage = $ expected [ $ i ] ;
389
+ $ expectedMessage = $ expectedItem ;
386
390
if (is_string ($ expectedMessage )) {
387
- self ::assertMatchesRegularExpression ($ expectedMessage , $ exception ->getMessage ());
391
+ self ::assertSame ($ expectedMessage , $ exception ->getMessage ());
388
392
continue ;
389
393
}
390
394
391
395
throw $ exception ;
392
396
}
393
397
394
- self ::assertSame ($ expected [ $ i ] , array_map (static fn (SimpleXMLElement $ element ): string => ( string ) $ element , $ elements ));
398
+ self ::assertSame ($ expectedItem , array_map (static fn (SimpleXMLElement $ element ): string => trim (( string ) preg_replace ( ' /<\?xml[^>]*\?>/ ' , '' , ( string ) $ element-> asXML (), 1 )) , $ elements ));
395
399
}
396
400
}
397
401
@@ -416,14 +420,14 @@ public function testIterateWithXpath(string $file, bool $withNamespaces, string
416
420
} catch (Throwable $ exception ) {
417
421
$ expectedMessage = $ expected [$ i ];
418
422
if (is_string ($ expectedMessage )) {
419
- self ::assertMatchesRegularExpression ($ expectedMessage , $ exception ->getMessage ());
423
+ self ::assertSame ($ expectedMessage , $ exception ->getMessage ());
420
424
continue ;
421
425
}
422
426
423
427
throw $ exception ;
424
428
}
425
429
426
- self ::assertSame ($ expected [$ i ], array_map (static fn (Node $ element ): ? string => $ element ->getTextContent (), $ elements ));
430
+ self ::assertSame ($ expected [$ i ], array_map (static fn (Node $ element ): string => $ element ->toString (), $ elements ));
427
431
}
428
432
}
429
433
@@ -437,11 +441,24 @@ public static function provideIterateXpath(): iterable
437
441
'withNamespaces ' => false ,
438
442
'path ' => '/item/id ' ,
439
443
'expected ' => [
440
- ['1 ' ],
441
- ['2 ' ],
442
- ['3 ' ],
443
- ['4 ' ],
444
- ['5 ' ],
444
+ ['<id uuid="12345">1</id> ' ],
445
+ ['<id uuid="61648">2</id> ' ],
446
+ ['<id>3</id> ' ],
447
+ ['<id uuid="894654">4</id> ' ],
448
+ ['<id uuid="78954">5</id> ' ],
449
+ ],
450
+ ];
451
+
452
+ yield [
453
+ 'file ' => 'sample_04.xml ' ,
454
+ 'withNamespaces ' => false ,
455
+ 'path ' => '/item ' ,
456
+ 'expected ' => [
457
+ ['<item i="0"><id uuid="12345">1</id><name price="10.1">Test 1</name></item> ' ],
458
+ ['<item i="1"><id uuid="61648">2</id><name price="5">Test 2</name></item> ' ],
459
+ ['<item i="2"><id>3</id><name price="500"><![CDATA[Test 3 & 9]]></name></item> ' ],
460
+ ['<item i="3"><id uuid="894654">4</id><name>Test 4</name></item> ' ],
461
+ ['<item i="4"><id uuid="78954">5</id><name price="0.99">Test 5</name></item> ' ],
445
462
],
446
463
];
447
464
@@ -450,9 +467,9 @@ public static function provideIterateXpath(): iterable
450
467
'withNamespaces ' => false ,
451
468
'path ' => '/item/name[@price>10] ' ,
452
469
'expected ' => [
453
- ['Test 1 ' ],
470
+ ['<name price="10.1"> Test 1</name> ' ],
454
471
[],
455
- ['Test 3 ' ],
472
+ ['<name price="500"><![CDATA[ Test 3 & 9]]></name> ' ],
456
473
[],
457
474
[],
458
475
],
@@ -463,9 +480,14 @@ public static function provideIterateXpath(): iterable
463
480
'withNamespaces ' => false ,
464
481
'path ' => '/item/id ' ,
465
482
'expected ' => [
466
- ['1/L1 ' ],
467
- '/(simplexml_load_string\(\): namespace error |DOMDocument::loadXML\(\)): Namespace prefix h for test on id is not defined( in Entity)?/ ' ,
468
- '/(simplexml_load_string\(\): namespace error |DOMDocument::loadXML\(\)): Namespace prefix g on id is not defined/ ' ,
483
+ ['<id>1/L1</id> ' ],
484
+ 'DOMDocument::loadXML(): Namespace prefix h for test on id is not defined in Entity, line: 1 ' ,
485
+ 'DOMDocument::loadXML(): Namespace prefix g on id is not defined in Entity, line: 1 ' ,
486
+ ],
487
+ 'expectedOverride ' => [
488
+ ['<id>1/L1</id> ' ],
489
+ 'simplexml_load_string(): namespace error : Namespace prefix h for test on id is not defined ' ,
490
+ 'simplexml_load_string(): namespace error : Namespace prefix g on id is not defined ' ,
469
491
],
470
492
];
471
493
@@ -474,7 +496,7 @@ public static function provideIterateXpath(): iterable
474
496
'withNamespaces ' => true ,
475
497
'path ' => '/item/id ' ,
476
498
'expected ' => [
477
- ['1/L1 ' ],
499
+ ['<id> 1/L1</id> ' ],
478
500
[],
479
501
[],
480
502
],
@@ -485,9 +507,14 @@ public static function provideIterateXpath(): iterable
485
507
'withNamespaces ' => false ,
486
508
'path ' => '/item/g:id ' ,
487
509
'expected ' => [
488
- '/(SimpleXMLElement::xpath\(\)|DOMXPath::query\(\))\: Undefined namespace prefix/ ' ,
489
- '/(simplexml_load_string\(\): namespace error |DOMDocument::loadXML\(\)): Namespace prefix h for test on id is not defined( in Entity)?/ ' ,
490
- '/(simplexml_load_string\(\): namespace error |DOMDocument::loadXML\(\)): Namespace prefix g on id is not defined/ ' ,
510
+ 'DOMXPath::query(): Undefined namespace prefix ' ,
511
+ 'DOMDocument::loadXML(): Namespace prefix h for test on id is not defined in Entity, line: 1 ' ,
512
+ 'DOMDocument::loadXML(): Namespace prefix g on id is not defined in Entity, line: 1 ' ,
513
+ ],
514
+ 'expectedOverride ' => [
515
+ 'SimpleXMLElement::xpath(): Undefined namespace prefix ' ,
516
+ 'simplexml_load_string(): namespace error : Namespace prefix h for test on id is not defined ' ,
517
+ 'simplexml_load_string(): namespace error : Namespace prefix g on id is not defined ' ,
491
518
],
492
519
];
493
520
@@ -496,9 +523,14 @@ public static function provideIterateXpath(): iterable
496
523
'withNamespaces ' => true ,
497
524
'path ' => '/item/g:id ' ,
498
525
'expected ' => [
499
- '/(SimpleXMLElement::xpath\(\)| DOMXPath::query\(\))\ : Undefined namespace prefix/ ' ,
526
+ 'DOMXPath::query() : Undefined namespace prefix ' ,
500
527
[],
501
- ['1/L3 ' ],
528
+ ['<g:id xmlns:g="http://base.google.com/ns/1.0">1/L3</g:id> ' ],
529
+ ],
530
+ 'expectedOverride ' => [
531
+ 'SimpleXMLElement::xpath(): Undefined namespace prefix ' ,
532
+ [],
533
+ ['<g:id>1/L3</g:id> ' ],
502
534
],
503
535
];
504
536
@@ -507,8 +539,13 @@ public static function provideIterateXpath(): iterable
507
539
'withNamespaces ' => true ,
508
540
'path ' => '/item/data/g:title ' ,
509
541
'expected ' => [
510
- '/(SimpleXMLElement::xpath\(\)|DOMXPath::query\(\))\: Undefined namespace prefix/ ' ,
511
- ['Title 2 ' ],
542
+ 'DOMXPath::query(): Undefined namespace prefix ' ,
543
+ ['<g:title xmlns:g="http://base.google.com/ns/1.0" test="bb">Title 2</g:title> ' ],
544
+ [],
545
+ ],
546
+ 'expectedOverride ' => [
547
+ 'SimpleXMLElement::xpath(): Undefined namespace prefix ' ,
548
+ ['<g:title test="bb">Title 2</g:title> ' ],
512
549
[],
513
550
],
514
551
];
0 commit comments