Skip to content

Commit

Permalink
Fix basic collection with namespace (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavvanik committed Mar 3, 2022
1 parent 418f1d4 commit dd6f7de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected function addSequentialNode(DOMElement $element, $value): void
return;
}

$child = new DOMElement($element->tagName);
$child = $this->document->createElement($element->tagName);
$child->nodeValue = htmlspecialchars($value);
$element->parentNode->appendChild($child);
}
Expand Down
19 changes: 17 additions & 2 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ public function it_can_handle_zero_values_in_beginning_of_basic_collection()
]));
}

/** @test */
public function it_can_handle_values_with_namespace_as_basic_collection()
{
$root = [
'rootElementName' => 'root',
'_attributes' => [
'xmlns:ns1' => 'https://github.com/spatie/array-to-xml',
],
];

$this->assertMatchesXmlSnapshot(ArrayToXml::convert([
'ns1:user' => ['one', 'two', 'three'],
], $root));
}

/** @test */
public function it_accepts_an_xml_encoding_type()
{
Expand Down Expand Up @@ -515,7 +530,7 @@ public function it_can_drop_xml_declaration()

$this->assertMatchesSnapshot($arrayToXml->dropXmlDeclaration()->toXml());
}

/** @test */
public function it_can_convert_an_array_with_null_value_to_xml()
{
Expand All @@ -525,7 +540,7 @@ public function it_can_convert_an_array_with_null_value_to_xml()

$this->assertMatchesXmlSnapshot(ArrayToXml::convert($arr));
}

/** @test */
public function it_can_add_processing_instructions()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<root xmlns:ns1="https://github.com/spatie/array-to-xml">
<ns1:user>one</ns1:user>
<ns1:user>two</ns1:user>
<ns1:user>three</ns1:user>
</root>

0 comments on commit dd6f7de

Please sign in to comment.