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 skip a PHPUnit test?
- How to run tests in parallel with PHPUnit?
- How to generate a JUnit report in PHPUnit?
- How to show warnings in PHPUnit?
- How to run PHPUnit in a Docker container?
- How to stop PHPUnit on failure?
- How to run all PHPUnit tests?
- What are PHPUnit required extensions
- How to install PHPUnit with a PHAR file?
- How to disable deprecation notices in PHPUnit?
See more codes...