Warm tip: This article is reproduced from serverfault.com, please click

Method 'setMethods' is deprecated when try to write a PHP Unit Test

发布于 2020-11-30 14:14:13

I want to create a mock to replace a resource:

$gateway = $this->getMockBuilder('PaymentGateway')
            ->setMethods(['transfer'])
            ->getMock();

I got this warning:

Method 'setMethods' is deprecated

How can I resolve this deprecation?

Questioner
Khaled Boussoffara
Viewed
0
yivi 2020-11-30 22:26:29

From now on we are supposed to use either onlyMethods() (which would be the closest equivalent to setMethods() and addMethods():

$gateway = $this->getMockBuilder('PaymentGateway')
            ->onlyMethods(['transfer'])
            ->getMock();

This is explained in the PR (linked from the method PHP doc directly, as seen here).