为 PHPUnit 的模拟对象定义类型

Define type for PHPUnit's mock objects

我想知道是否可以使用 phpdoc 将特定范围内的某些对象(仅在方法内)定义为 PHPUni 的 Mock,因此在该方法中我可以利用类型提示,例如 ->expected、->methods 等等,就像您只是创建模拟而不将其寻址到其真实 class.

这里有一个演示:

class someTest extends PHPUnit
{
    // here, usually we define the real class (SomeClass in this example)
    /** @var SomeClass */
    private $someMock;

    public function setUp()
    {
        $this->someMock = $this->getMock(SomeClass::class);
    }

    public function testSomethingInSomeClass()
    {
        // here i expect the type hint i defined in the beginning of this test class and its fine
        $a = $this->someMock->someMethodFromSomeClass();
    }

    private function setSomeMethodOnMock()
    {
        // but here i would like to have the type-hint for phpunit's mock object
        // e.g. ->expects,  ->method() , ->willReturn() , etc.
        // if i don't define the mock alias the class type i get will be something like Mock_SomeClass_9873432
        $this->someMock->....
    }
}
/**
 * @var SomeClass|\PHPUnit_Framework_MockObject_MockObject
 */
private $someMock;

你可以用方法做同样的事情:

/**
 * @return SomeClass|\PHPUnit_Framework_MockObject_MockObject
 */
private function getSomeMock()
{
    //....
}