我看到 assertEqualXMLStructure 能够在 PHPUnit 中使用测试结构 XML。但是我遇到了案例问题:当 2 个结构 XML 具有不同的子标签数量时,尽管结构相同,但它失败了。有没有办法通过 PHPUnit 的支持来做到这一点?谢谢
问问题
318 次
1 回答
0
通过编辑 assertEqualXMLStructure 以忽略检查标签子项的数量,我解决了我的问题:
public static function assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement, $checkAttributes = FALSE, $message = '') {
self::assertEquals($expectedElement->tagName, $actualElement->tagName, $message);
if ($checkAttributes) {
self::assertEquals($expectedElement->attributes->length, $actualElement->attributes->length, sprintf('%s%sNumber of attributes on node "%s" does not match', $message, !empty($message) ? "\n" : '', $expectedElement->tagName));
for ($i = 0; $i < $expectedElement->attributes->length; $i++) {
$expectedAttribute = $expectedElement->attributes->item($i);
$actualAttribute = $actualElement->attributes->getNamedItem($expectedAttribute->name);
if (!$actualAttribute) {
self::fail(sprintf('%s%sCould not find attribute "%s" on node "%s"', $message, !empty($message) ? "\n" : '', $expectedAttribute->name, $expectedElement->tagName));
}
}
}
PHPUnit_Util_XML::removeCharacterDataNodes($expectedElement);
PHPUnit_Util_XML::removeCharacterDataNodes($actualElement);
for ($i = 0; $i < $expectedElement->childNodes->length; $i++) {
self::assertEqualXMLStructure($expectedElement->childNodes->item($i), $actualElement->childNodes->item($i), $checkAttributes, $message);
}
}
于 2013-05-23T06:08:35.570 回答