I have a service RessortSoapService, which uses the response from a SOAP call.
I inject a class, which gives me the SoapClient back:
public function __construct(ParameterBagInterface $params, SapSoapClientInterface $sapSoapClient)
{
$this->params = $params;
$this->soapClient = $sapSoapClient
->fetchWdslContent($this->params->get('sapwsdlstruktur'))
->getClient();
}
In the SapSoapClient class the method getClient() gives me the properly initialised client back:
public function getClient(): SoapClient
{
$client = new SoapClient('data://text/plain;base64,' . base64_encode($this->wsdlContent), $this->options);
return $client;
}
Now I want to write a unittest, which of course should NOT call the Soap server, but just fake a response.
What is did was this:
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);
}
So I created a mock of the native SoapClient, whom I tell, that it should give me back an XML string containing the SOAP XML Response instead of really contacting the SoapServer.
When I run the test __soapCall gives me back null and therefore my test fails.
How can I properly mock my __soapCall response? This is my first use of Soap, so maybe I miss some detail or understanding?
You are using willReturnCallback
. This expects a function to be called for the actual response. So it tries to look for a function with the name of your whole xml. Which of course it isn't going to find. If you use willReturn
instead, then you should get your desired result.