我有一个RessortSoapService服务,它使用SOAP调用的响应。
我注入一个类,这使我回到了SoapClient:
public function __construct(ParameterBagInterface $params, SapSoapClientInterface $sapSoapClient)
{
$this->params = $params;
$this->soapClient = $sapSoapClient
->fetchWdslContent($this->params->get('sapwsdlstruktur'))
->getClient();
}
在SapSoapClient类中,方法getClient()为我提供了正确初始化的客户端:
public function getClient(): SoapClient
{
$client = new SoapClient('data://text/plain;base64,' . base64_encode($this->wsdlContent), $this->options);
return $client;
}
现在,我想编写一个单元测试,它当然不应该调用Soap服务器,而只是伪造一个响应。
这是什么?
class RessortSoapServiceTest extends TestCase
{
public function setUp(): void
{
$this->prepareMocks();
$this->ressortService = new RessortSoapService($this->params, $this->sapSoapClient);
}
private function prepareMocks()
{
....
$this->soapClient = $this->createMock(SoapClient::class);
$this->soapClient
->method('__soapCall')
->willReturnCallback(
'<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header/>
<SOAP:Body xmlns:urn="urn:sap-com:document:sap:rfc:functions">
<nm:Z_STA_CUST_GET_RESSORTS.Response xmlns:nm="urn:sap-com:document:sap:rfc:functions" xmlns:prx="urn:sap.com:proxy:PV1:/1SAI/TASC3650D2D2360AAAFB21E:731">
<ET_RESSORTS>
<item>
<GROUP_HIER>0000001</GROUP_HIER>
<GROUP>0000001</GROUP>
<IDENT>0000103</IDENT>
<IS_INACTIVE>0</IS_INACTIVE>
</item>
... some more XML
</ET_RESSORTS>
<ET_RETURN>
<item>
<TYPE>S</TYPE>
<ID>ZSTA</ID>
<NUMBER>401</NUMBER>
<MESSAGE>Daten erfolgreich gelesen</MESSAGE>
</item>
</ET_RETURN>
</nm:Z_STA_CUST_GET_RESSORTS.Response>
</SOAP:Body>
</SOAP:Envelope>'
);
$this->sapSoapClient = $this->createMock(SapSoapClientInterface::class);
$this->sapSoapClient
->method('getClient')
->willReturnCallback($this->soapClient);
}
因此,我创建了一个本机SoapClient的模拟程序,我告诉了它,它应该给我一个包含SOAP XML响应的XML字符串,而不是真正联系SoapServer。
当我运行测试__soapCall时,我返回null,因此测试失败。
如何正确模拟__soapCall响应?这是我第一次使用Soap,所以也许我错过了一些细节或了解?
答案 0 :(得分:0)
您正在使用willReturnCallback
。这期望为实际响应调用一个函数。因此,它尝试查找具有整个xml名称的函数。当然找不到哪个。如果改用willReturn
,则应该得到想要的结果。