What is the way to avoid phpunit having to call the constructor for a mock object? Otherwise I would need a mock object as constructor argument, another one for that etc. The ap
You can use getMockBuilder
instead of just getMock
:
$mock = $this->getMockBuilder('class_name')
->disableOriginalConstructor()
->getMock();
See the section on "Test Doubles" in PHPUnit's documentation for details.
Although you can do this, it's much better to not need to. You can refactor your code so instead of a concrete class (with a constructor) needing to be injected, you only depend upon an interface. This means you can mock or stub the interface without having to tell PHPUnit to modify the constructor behaviour.