Using Phpunit 4.5.2, I'm trying to mock 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 wish to achieve the following:
This code:
$mock = $this->getMockBuilder('MyClass')
->disableOriginalConstructor()
->enableProxyingToOriginalMethods()
->getMock()
Will fail with the following error message:
Argument 1 passed to MyClass::__construct() must be an instance of Foo, none given
If I remove the enableProxyingToOriginalMethods(), the mock is created without errors, so it seems that when I enable proxying, this enables the constructor despite the disableOriginalConstructor() call.
How can I enable proxying while keeping the constructor disabled?
If you proxy to the original class then an object of the original class must be instantiated. If the original class has a constructor then that constructor must be executed. Hence disableOriginalConstructor() and enableProxyingToOriginalMethods() are mutually exclusive.
Feel free to open a ticket at https://github.com/sebastianbergmann/phpunit-mock-objects/issues to ask for PHPUnit to issue an error when these two are used together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With