Phpunit,模拟 SoapClient 有问题(模拟魔术方法) [英] Phpunit, mocking SoapClient is problematic (mock magic methods)

查看:44
本文介绍了Phpunit,模拟 SoapClient 有问题(模拟魔术方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码模拟 SoapClient:

Im trying to mock SoapClient with the following code:

$soapClientMock = $this->getMockBuilder('SoapClient')
                ->disableOriginalConstructor()
                ->getMock();
$soapClientMock->method('getAuthenticateServiceSettings')
        ->willReturn(true);

这将不起作用,因为 Phpunit mockbuilder 找不到函数 getAuthenticateServiceSettings.这是 WSDL 中指定的 Soap 函数.

This will not work since Phpunit mockbuilder does not find the function getAuthenticateServiceSettings. This is a Soap function specified in the WSDL.

但是,如果我扩展 SoapClient 类和 getAuthenticateServiceSettings 方法,它确实可以工作.

However, if i extend the SoapClient class and the getAuthenticateServiceSettings method it does work.

问题是我有 100 次 SOAP 调用,所有调用都有自己的参数等.所以我不想模拟每一个 SOAP 函数,或多或少地重新创建整个 WSDL 文件...

The problem is i have 100s of SOAP calls, all with their own parameters etc. so i dont want to mock every single SOAP function and more or less recreate the whole WSDL file...

有没有办法模拟魔法"方法?

Is there a way to mock "magic" methods?

推荐答案

PHPUnit 允许您基于 wsdl 文件存根 Web 服务.

PHPUnit allows you to stub a web service based on a wsdl file.

$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

这篇关于Phpunit,模拟 SoapClient 有问题(模拟魔术方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆