ivo*_*oba 5 php phpunit soap mocking
我想模拟来自文件的XML的\ SoapClient的响应。
正如SoapClient从文件返回一样,我如何创建stdClass对象。
客户端已经包装了SoapClient,因此可以轻松模拟响应。
我的模拟是这样的:
$soapClient->expects($this->once())
->method('call')
->will(
$this->returnValue(
simplexml_load_string(
file_get_contents(__DIR__ . '/../../../Resources/file.xml')
)
)
);
Run Code Online (Sandbox Code Playgroud)
但这将返回SimpleXml而不是stdClass。
更新:
建议的json_encode / json_decode hack似乎无法处理属性,因为SoapClient返回它们:
SoapClient:
object(stdClass)[4571]
public 'Lang' => string 'de' (length=2)
public 'Success' => boolean true
Run Code Online (Sandbox Code Playgroud)
哈克:
object(stdClass)#27 (3) {
["@attributes"]=>
object(stdClass)#30 (2) {
["Lang"]=>
string(2) "de"
["Success"]=>
string(4) "true"
Run Code Online (Sandbox Code Playgroud)
您可以对 SimpleXml 进行 json 编码/解码,如下所示:
$soapClient->expects($this->once())
->method('call')
->will(
$this->returnValue(
json_decode(json_encode(
simplexml_load_string(
file_get_contents(__DIR__ . '/../../../Resources/file.xml')
)
))
)
);
Run Code Online (Sandbox Code Playgroud)
但我建议将预设响应明确定义为 php 类。