Xml::arrayToXml fails on nested arrays
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- php
- Domain
- testing-qa
Research direction
Start at the Xml::arrayToXml helper and reproduce the nested-array example from the issue. Inspect the recursive branch where the nested element is passed before it exists, then verify that the example produces the expected nested XML without PHP errors.
Written by the indexing model from the issue text.
Description
The helper function arrayToXml has code indended to handle nested arrays, but currently always throws PHP errors. This means nested arrays can't be used when testing soap functions with array data either.
Reproduce
Code:
$dom = new \DOMDocument();
$array = [
'foo' => [
'bar' => '1'
]
];
echo \Codeception\Util\Xml::arrayToXml($dom, $dom, $array)->saveXML();
Expected output:
<?xml version="1.0"?>
<foo><bar>1</bar></foo>
Result:
Undefined property: DOMDocument::$foo
Cause
When the function encounters an array, it calls itself to parse the array, with $domNode->$el as the new $domNode parameter. The problem is that that node ->$el will never exist, since that's the node this function was supposed to create in the first place.
Fix
Create a new element first, to pass to the recursive call, and then append that new node to the xml structure.
Current code:
if (is_array($val)) {
self::arrayToXml($xml, $domNode->$el, $val);
} else {
$domNode->appendChild($xml->createElement($el, $val));
}
Fixed code:
if (is_array($val)) {
$elementNode = $xml->createElement($el);
self::arrayToXml($xml, $elementNode, $val);
$domNode->appendChild($elementNode);
} else {
$domNode->appendChild($xml->createElement($el, $val));
}
Alternatively, we could make it a little more consistent by always creating and appending the element in the same way, and only set the value inside the if-condition. That code would look like this:
$elementNode = $xml->createElement($el);
if (is_array($val)) {
self::arrayToXml($xml, $elementNode, $val);
} else {
$elementNode->nodeValue = $val;
}
$domNode->appendChild($elementNode);
Both solutions result in working code for nested arrays.
- Dominant language
- PHP
- Stars
- 17
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Codeception/lib-xml
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
Codeception/lib-xml#6 · 1 comment ·
All issues in Codeception/lib-xml
Similar issues
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·
-
0. Needs triage bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nextcloud/fulltextsearch#1011 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
phpstan/phpstan-doctrine#794 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Automattic/static-site-importer#1767 ·