在夹具 XML 文件 (PHPunit) 中加载 XML 字段



我正在使用phpunit并通过夹具加载数据,现在在一个场景中,我的表中有一列只接受xml数据,但我无法通过夹具文件中的字段传递任何xml数据。

我尝试过的:

<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="phpunit">
<table_data name="testtable">
<row>
<field name="name">My Test</field>
<field name="active">1</field>
<field name="xml_order">
<Attributes>
<Attribute>                   
<Option Value="A">A1</Option>
<Option Value="B">B1</Option>
<Option Value="C">C1</Option>                               
</Attribute>
</Attributes>
</field>
</row>
</table_data>
</database>

我无法将字段"xml_order"数据加载到 dB 中。尝试寻找解决方案,但无法。谁能帮我解决这个问题?我只想在此 xml 中加载它,而不是在我的测试文件中加载它

由于<Attributes/>是 xml 夹具文件中的常规 xml,因此当 dbunit 读取它时,它会

// a bit simplified
// please visit https://github.com/sebastianbergmann/dbunit/blob/master/src/DataSet/MysqlXmlDataSet.php to get more details
$column = $rowElement->xpath('./field[@name="' . $columnName . '"]');
$columnValue = (string) $column;

它的文本值是一串空白字符。实际上,这对于xml来说本质上是正常的。为了克服这个问题,你可以包装CDATA所有你想在值提取过程中被视为字符串的xml内容:

<field name="xml_order">
<![CDATA[
<Attributes>
<Attribute>                   
<Option Value="A">A1</Option>
<Option Value="B">B1</Option>
<Option Value="C">C1</Option>                               
</Attribute>
</Attributes>
]]>
</field>

另请注意,实际字符串在实际 xml 字符串之前和之后仍然会有相当多的空格和换行符。由于某些原因,我不想要这样做:

<field name="xml_order"><![CDATA[<Attributes>
<Attribute>                   
<Option Value="A">A1</Option>
<Option Value="B">B1</Option>
<Option Value="C">C1</Option>                               
</Attribute>
</Attributes>]]></field>

不过,看起来并不好,但前后不会给您任何空间。

作为替代方案,您可以考虑使用基于数组的数据集:

// or it could be PHPUnit_Extensions_Database_DataSet_ArrayDataSet
// depending on the dbunit version you use
return new PHPUnitDbUnitDataSetArrayDataSet([
'testtable' => [
["name" => "MyTest", "active" => 1, "xml_order" => '<what><ever>xml</ever></what>'],
]
]);

最新更新