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 a static method with PHPUnit?
- How to use the PHPUnit Framework TestCase?
- How to install PHPUnit with a PHAR file?
- How to run PHPUnit in a Docker container?
- How to mock with PHPUnit?
- How to assert that key exists in an array using PHPUnit?
- How to install PHPUnit?
- How to use the PHPUnit Framework ExceptionWrapper?
- How to expect an error with PHPUnit?
See more codes...