|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace VeeWee\Tests\Xml\Dom\Configurator; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use VeeWee\Xml\Dom\Document; |
| 10 | +use function VeeWee\Xml\Dom\Configurator\promote_namespaces; |
| 11 | +use function VeeWee\Xml\Dom\Locator\document_element; |
| 12 | +use function VeeWee\Xml\Dom\Mapper\xml_string; |
| 13 | + |
| 14 | +final class PromoteNamespacesTest extends TestCase |
| 15 | +{ |
| 16 | + #[DataProvider('provideXmls')] |
| 17 | + public function test_it_can_promote_namespaces(string $input, string $expected): void |
| 18 | + { |
| 19 | + $doc = Document::fromXmlString($input, promote_namespaces()); |
| 20 | + $actual = xml_string()($doc->map(document_element())); |
| 21 | + |
| 22 | + static::assertSame($expected, $actual); |
| 23 | + } |
| 24 | + |
| 25 | + public static function provideXmls(): iterable |
| 26 | + { |
| 27 | + yield 'no-action' => [ |
| 28 | + '<hello/>', |
| 29 | + '<hello/>', |
| 30 | + ]; |
| 31 | + |
| 32 | + yield 'child-to-root' => [ |
| 33 | + <<<EOXML |
| 34 | + <foo> |
| 35 | + <bar xmlns:a="http://a"><a:baz/></bar> |
| 36 | + </foo> |
| 37 | + EOXML, |
| 38 | + <<<EOXML |
| 39 | + <foo xmlns:a="http://a"> |
| 40 | + <bar><a:baz/></bar> |
| 41 | + </foo> |
| 42 | + EOXML, |
| 43 | + ]; |
| 44 | + |
| 45 | + yield 'mixed-namespaces' => [ |
| 46 | + <<<EOXML |
| 47 | + <foo> |
| 48 | + <bar xmlns:a="http://a"><a:x/></bar> |
| 49 | + <baz xmlns:b="http://b"><b:y/></baz> |
| 50 | + </foo> |
| 51 | + EOXML, |
| 52 | + <<<EOXML |
| 53 | + <foo xmlns:a="http://a" xmlns:b="http://b"> |
| 54 | + <bar><a:x/></bar> |
| 55 | + <baz><b:y/></baz> |
| 56 | + </foo> |
| 57 | + EOXML, |
| 58 | + ]; |
| 59 | + |
| 60 | + yield 'soap-like' => [ |
| 61 | + <<<EOXML |
| 62 | + <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> |
| 63 | + <SOAP-ENV:Body> |
| 64 | + <tns:getUser xmlns:tns="https://example.com"> |
| 65 | + <tns:id xmlns:tns="https://example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:int" xmlns:xsd="http://www.w3.org/2001/XMLSchema">1</tns:id> |
| 66 | + </tns:getUser> |
| 67 | + </SOAP-ENV:Body> |
| 68 | + </SOAP-ENV:Envelope> |
| 69 | + EOXML, |
| 70 | + <<<EOXML |
| 71 | + <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="https://example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
| 72 | + <SOAP-ENV:Body> |
| 73 | + <tns:getUser> |
| 74 | + <tns:id xsi:type="xsd:int">1</tns:id> |
| 75 | + </tns:getUser> |
| 76 | + </SOAP-ENV:Body> |
| 77 | + </SOAP-ENV:Envelope> |
| 78 | + EOXML, |
| 79 | + ]; |
| 80 | + } |
| 81 | +} |
0 commit comments