I make unit test with phpunit (100% cover) for my PHP application and I have this :
$xml = simplexml_load_string($soapResponse);
if (false === $xml) {
throw new \Exception('invalid XML');
}
I don't find a test case with simplexml_load_string return false.
If you have a solution... Thanks
as John C noted, invalid xml will cause simplexml_load_string to return false and generate warnings. Additionally you may want to disable those warnings and store them. To do so you can use libxml_use_internal_errors and libxml_get_errors.
<?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');
}
So the output is:
array (
0 =>
LibXMLError::__set_state(array(
'level' => 3,
'code' => 4,
'column' => 1,
'message' => 'Start tag expected, \'<\' not found
',
'file' => '',
'line' => 1,
)),
)
which may help you to identify problematic part in XML. This is extremely useful when XML is large.
Any invalid XML will cause simplexml_load_string to return false:
$soapResponse = 'invalid';
$xml = simplexml_load_string($soapResponse);
var_dump($xml);
// output: bool(false)
Note that this will also generate warnings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With