PhpUnit如何同时使用其他类和自类方法测试double a方法?

PhpUnit如何同时使用其他类和自类方法测试double a方法?,php,phpunit,Php,Phpunit,因此,我想对我的函数进行双重测试,如下所示: class MyClass { protected secondClass; public function __construct( SecondClass $secondClass; ) { $this->secondClass = $secondClass; } public function doEverything() : void { $

因此,我想对我的函数进行双重测试,如下所示:

class MyClass {

    protected secondClass;

    public function __construct(
        SecondClass $secondClass;
    ) {
        $this->secondClass = $secondClass;
    }

    public function doEverything() : void {

        $this->secondClass->doSomethingSecondClass();
        $this->doSomething();

    }

    public function doSomething(): void {
        // do something, return nothing
    }
}
我尝试使用的相应测试如下所示:

class MyClassTest extends TestCase
{
    protected $secondClass;

    protected $myClass;

    public function setUp() : void
    {
                $this->secondClass = $this->getMockBuilder(SecondClass::class)
                                         ->disableOriginalConstructor()
                                         ->setMethods(
                                             [
                                                 'doSomethingSecondClass'
                                             ]
                                         )
                                         ->getMock();

        $this->myClass = $this->getMockBuilder(MyClass::class)
                                   ->setConstructorArgs([$this->secondClass])
                                   ->setMethods(
                                       [
                                           'doEverything',
                                           'doSomething'
                                       ]
                                   )
                                   ->getMock();
    }

    public function testDoEverything(): void
    {
            $this->secondClass->expects($this->once())->method('doSomethingSecondClass');
            $this->myClass->expects($this->once())->method('doSomething');

            $this->myClass->doEverything();
    }

}
上面的结果告诉我doSomethingSecondClass从未在MyClass的doEverything方法中调用。我想,我可以毫无困难地使用$this->myClass->expects

以及以下测试代码:

class MyClassTest extends TestCase
{
    protected $secondClass;

    protected $myClass;

    public function setUp() : void
    {
                $this->secondClass = $this->getMockBuilder(SecondClass::class)
                                         ->disableOriginalConstructor()
                                         ->setMethods(
                                             [
                                                 'doSomethingSecondClass'
                                             ]
                                         )
                                         ->getMock();

                        $this->saveMultiple = new SaveMultiple(
                                     $this->resourceConnection
                                 );
    }

    public function testDoEverything(): void
    {
            $this->secondClass->expects($this->once())->method('doSomethingSecondClass');
            $this->myClass->expects($this->once())->method('doSomething');

            $this->myClass->doEverything();
    }

}
上面的代码告诉我myClass不能与phpUnit中的expects函数一起使用,但是doSomethingSecondClass是按预期调用的

那么,在你看来,我应该如何继续测试这个方法呢


谢谢

永远不会使用
secondClass
,因为您正在为
myClass
使用模拟。在模拟中,您永远不会调用
secondClass
。测试这一点的正确方法是不要对
myClass
使用mock,而是实例化它的真实实例