phpunit avoid constructor arguments for mock
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.
Here you go:
// Get a Mock Soap Client object to work with. $classToMock = 'SoapClient'; $methodsToMock = array('__getFunctions'); $mockConstructorParams = array('fake wsdl url', array()); $mockClassName = 'MyMockSoapClient'; $callMockConstructor = false; $mockSoapClient = $this->getMock($classToMock, $methodsToMock, $mockConstructorParams, $mockClassName, $callMockConstructor);
This question is a little old, but for new visitors, you can do it using the createMock
method (previously called createTestDouble
and introduced in v5.4.0).
$mock = $this->createMock($className);
As you can see in the code below extracted from the PHPUnit\Framework\TestCase
class (in phpunit/src/framework/TestCase.php
), it will basically create a mock object without calling the original constructor.
/** PHPUnit\Framework\TestCase::createMock method */protected function createMock(string $originalClassName): MockObject{ return $this->getMockBuilder($originalClassName) ->disableOriginalConstructor() ->disableOriginalClone() ->disableArgumentCloning() ->disallowMockingUnknownTypes() ->getMock();}