phpunitHow to mock an interface in PHPUnit?
Mocking an interface in PHPUnit can be done using the getMockForAbstractClass()
method. This method takes the name of the interface as the first argument and an array of constructor arguments as the second argument.
$mock = $this->getMockForAbstractClass('MyInterface', array('arg1', 'arg2'));
The getMockForAbstractClass()
method will create a mock object that implements the interface and can be used to test code that depends on the interface.
getMockForAbstractClass()
: This method takes the name of the interface as the first argument and an array of constructor arguments as the second argument.MyInterface
: This is the name of the interface that is being mocked.array('arg1', 'arg2')
: This is an array of constructor arguments that will be passed to the mock object.
Helpful links
More of Phpunit
- How to show warnings in PHPUnit?
- How to run all PHPUnit tests?
- How to install PHPUnit from Packagist?
- How to use named arguments in PHPUnit?
- How to mock a property in PHPUnit?
- How to skip a PHPUnit test?
- How to stop PHPUnit on failure?
- How to mock a private method in PHPUnit?
- How to run tests in parallel with PHPUnit?
- How to mock a method with different arguments in PHPUnit?
See more codes...