我用phpunit(100%覆盖)为我的PHP应用程序进行单元测试,我有这个:
$xml = simplexml_load_string($soapResponse);
if (false === $xml) {
throw new 'Exception('invalid XML');
}
我找不到simplexml_load_string
返回false
的测试用例。
如果你有解决方案。。。感谢
<?php
$soapResponse = 'invalid_xml';
libxml_use_internal_errors(true);
$xml = simplexml_load_string($soapResponse);
if (false === $xml) {
$errors = libxml_get_errors();
echo 'Errors are '.var_export($errors, true);
throw new 'Exception('invalid XML');
}
所以输出是:
array (
0 =>
LibXMLError::__set_state(array(
'level' => 3,
'code' => 4,
'column' => 1,
'message' => 'Start tag expected, ''<'' not found
',
'file' => '',
'line' => 1,
)),
)
这可能有助于您识别XML中有问题的部分。当XML很大时,这非常有用。
任何无效的XML都将导致simplexml_load_string
返回false
:
$soapResponse = 'invalid';
$xml = simplexml_load_string($soapResponse);
var_dump($xml);
// output: bool(false)
请注意,这也会生成警告。