我有一个服务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,所以也许我错过了一些细节或理解?
您正在使用willReturnCallback
。这需要为实际响应调用一个函数。因此,它试图寻找一个具有整个xml名称的函数。它当然找不到。如果你使用willReturn
,那么你应该得到你想要的结果。