phpunitHow to mock a query builder with PHPUnit?
Mocking a query builder with PHPUnit can be done using the getMockBuilder
method. This method allows you to create a mock object of a given class.
$mockQueryBuilder = $this->getMockBuilder('QueryBuilder')
->setMethods(['select', 'from', 'where', 'orderBy', 'limit'])
->getMock();
The code above creates a mock object of the QueryBuilder
class with the methods select
, from
, where
, orderBy
and limit
.
Code explanation
getMockBuilder
: This method creates a mock object of a given class.setMethods
: This method sets the methods that should be mocked.getMock
: This method returns the mock object.
Helpful links
More of Phpunit
- How to mock an interface in PHPUnit?
- How to install PHPUnit with a PHAR file?
- How to install PHPUnit from Packagist?
- How to order tests with PHPUnit?
- How to stop PHPUnit on failure?
- How to use a listener with PHPUnit?
- How to install PHPUnit?
- How to use hooks in PHPUnit?
- How to increase memory limit in PHPUnit?
- How to use an inline dataprovider in PHPUnit?
See more codes...