Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xml::arrayToXml fails on nested arrays #7

Open
BBoom opened this issue Nov 20, 2024 · 0 comments
Open

Xml::arrayToXml fails on nested arrays #7

BBoom opened this issue Nov 20, 2024 · 0 comments

Comments

@BBoom
Copy link

BBoom commented Nov 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant