如何在PHP中用phpunit测试抽象类的静态方法

I need to mock static method of class. But in phpunit:4 and later versions mocking static method is impossible (I get exception with message 'Static method "findBy" cannot be invoked on mock object'). How i need configure mock to get successful test?

I tried create mock class without phphunit manually. Somethig like tests/Mocks/MyClass.php that extends abstract class and implement my static method/

interface IdentityInterface
{
     // this method calling first in other component
    public static function findBy(string $number, ?string $trunk): ?IdentityInterface;
}

abstract class Identity implements IdentityInterface
{
    // this method and other calling after findBy method
    public function getName(): string
    {
        return $this->name; // need to cover
    }
}

My static method must return instance of itself to use other internal methods that i need to cover.

dqba94619
dqba94619 我的方法不是抽象的,我不能创建扩展Identity的SomeIdentity类,因为它不是由包结构提供的。而且phpunit:3.5已被弃用,我不能将它用于测试静态方法
12 个月之前 回复
duanpan7011
duanpan7011 PHPunitmockobject抽象和静态方法的可能重复
12 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问