How to proxy to the original methods and disable the constructor simultaneously with Phpunit?

Using Phpunit 4.5.2, I am trying to make fun of the following class:

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();
    }
}

I want to achieve the following:

  • Fake a call to the original methods.
  • Avoid the constructor (I set the foo property with reflection).

This code:

$mock = $this->getMockBuilder('MyClass')
             ->disableOriginalConstructor()
             ->enableProxyingToOriginalMethods()
             ->getMock()

Error with the following error message:

Argument 1 passed to MyClass :: __ construct () must be an instance of Foo, not one specified

If I remove enableProxyingToOriginalMethods (), the mock is created without errors, so it seems that when I enable proxying, this allows the constructor, despite the call to disableOriginalConstructor ().

How to enable proxy when disabling the constructor?

+4
source share
1

, . , . , disableOriginalConstructor() enableProxyingToOriginalMethods() .

https://github.com/sebastianbergmann/phpunit-mock-objects/issues, PHPUnit , .

+7

All Articles