Xml::arrayToXml fails on nested arrays

未關閉 適合新手
#7 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
65/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
php
領域
testing-qa

研究方向

從 Xml::arrayToXml helper 開始,重現 issue 中的巢狀陣列範例。檢查巢狀元素在存在之前就被傳入的遞迴分支,然後驗證該範例是否能在沒有 PHP 錯誤的情況下產生預期的巢狀 XML。

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
PHP
星號
17
分支
1
PR 合併指標
30 天內沒有已合併 PR

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

Codeception/lib-xml 的其他 Issue

查看 Codeception/lib-xml 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。