phpunitHow to create a mock object in PHPUnit?
Mock objects are used in unit testing to replace real objects. PHPUnit provides a framework for creating mock objects using the getMock()
method.
Example code
$mock = $this->getMock('MyClass', array('methodName'));
This code creates a mock object of the class MyClass
and allows the methodName
to be called.
Code explanation
getMock()
: This is the method used to create a mock object.MyClass
: This is the class that the mock object is based on.methodName
: This is the name of the method that can be called on the mock object.
Helpful links
More of Phpunit
- How to skip a PHPUnit test?
- How to run all PHPUnit tests?
- How to run PHPUnit in quiet mode?
- How to run tests in parallel with PHPUnit?
- How to mock a method with different arguments in PHPUnit?
- How to mock a static method with PHPUnit?
- How to show warnings in PHPUnit?
- How to mock a query builder with PHPUnit?
- How to use named arguments in PHPUnit?
- How to mock a property in PHPUnit?
See more codes...