我试图用以下代码模拟SoapClient:
$soapClientMock = $this->getMockBuilder('SoapClient') ->disableOriginalConstructor() ->getMock(); $soapClientMock->method('getAuthenticateServiceSettings') ->willReturn(true);
这不起作用,因为PHPunit mockbuilder没有找到函数getAuthenticateServiceSettings.这是WSDL中指定的Soap函数.
但是,如果我扩展SoapClient类和getAuthenticateServiceSettings方法,它确实有效.
问题是我有100多个SOAP调用,都有自己的参数等.所以我不想模拟每个SOAP函数,或多或少地重新创建整个WSDL文件…
有没有办法模仿“魔法”方法?
PHPUnit允许您基于wsdl文件存根Web服务.
$soapClientMock = $this->getMockFromWsdl('soapApiDescription.wsdl'); $soapClientMock ->method('getAuthenticateServiceSettings') ->willReturn(true);
见这里的例子:
https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubbing-and-mocking-web-services.examples.GoogleTest.php