如何使用Phpunit代理原始方法并同时禁用构造函数?

如何使用Phpunit代理原始方法并同时禁用构造函数?,php,phpunit,Php,Phpunit,使用Phpunit 4.5.2,我试图模拟以下类: class Foo { public function bar() {} } class MyClass { private $foo; public function __construct(Foo $foo) { $this->foo = $foo; //some other stuff that I want to suppress during the unit test

使用Phpunit 4.5.2,我试图模拟以下类:

class Foo {
    public function bar() {}
}

class MyClass
{
    private $foo;

    public function __construct(Foo $foo) {
        $this->foo = $foo;
        //some other stuff that I want to suppress during the unit tests.
    }

    public function doSomething() {
        $this->foo->bar();
    }
}
我希望实现以下目标:

  • 让模拟调用与原始方法相同
  • 避免使用构造函数(我正在使用反射设置foo属性)
  • 此代码:

    $mock = $this->getMockBuilder('MyClass')
                 ->disableOriginalConstructor()
                 ->enableProxyingToOriginalMethods()
                 ->getMock()
    
    将失败,并显示以下错误消息:

    传递给MyClass::_construct()的参数1必须是Foo的实例,未给定任何实例

    如果我删除enableProxyingToOriginalMethods(),则创建的模拟不会出现错误,因此当我启用代理时,这似乎会启用构造函数,尽管调用了disableOriginalConstructor()


    如何在禁用构造函数的同时启用代理?

    如果代理到原始类,则必须实例化原始类的对象。如果原始类具有构造函数,则必须执行该构造函数。因此,disableOriginalConstructor()和enableProxyingToOriginalMethods()是互斥的

    请随时在打开一张票证,要求PHPUnit在这两个选项一起使用时发出错误