Xml::arrayToXml fails on nested arrays
还没有人认领这个 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
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Codeception/lib-xml 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 45/100
Codeception/lib-xml#6 · 1 条评论 ·
查看 Codeception/lib-xml 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 83/100
taskforcesh/bullmq#5238 ·
维护者通常 1 天内回复
-
[Bug] v-change-database-password reports success but doesn't change the password when MySQL validate_password rejects it可能已有人在做 @bfs2021 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
Automattic/jetpack#55517 ·
维护者通常 1 天内回复
-
Major
难度 2/5 1-3 小时 新手友好度 76/100
PrestaShop/ps_onepagecheckout#245 ·
维护者通常 1 天内回复
-
wpvc
难度 2/5 1-3 小时 新手友好度 72/100
10up/ElasticPress#4420 ·
维护者通常 1 天内回复