尝试编写 PHP 单元测试时,方法“setMethods”已被弃用

Kha*_*ara 12 php testing phpunit

我想创建一个模拟来替换资源:

$gateway = $this->getMockBuilder('PaymentGateway')
            ->setMethods(['transfer'])
            ->getMock();
Run Code Online (Sandbox Code Playgroud)

我收到这个警告:

方法“setMethods”已弃用

我该如何解决这个弃用问题?

yiv*_*ivi 23

从现在开始,我们应该使用其中一个(这将是与andonlyMethods()最接近的等价物:setMethods()addMethods()

$gateway = $this->getMockBuilder('PaymentGateway')
            ->onlyMethods(['transfer'])
            ->getMock();
Run Code Online (Sandbox Code Playgroud)

PR中对此进行了解释(直接从方法 PHP 文档链接,如此处所示)。