Download the PHP package michaelmoussa/doctrine-qbmocker without Composer
On this page you can find all versions of the php package michaelmoussa/doctrine-qbmocker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download michaelmoussa/doctrine-qbmocker
More information about michaelmoussa/doctrine-qbmocker
Files in michaelmoussa/doctrine-qbmocker
Package doctrine-qbmocker
Short Description A helper library for mocking Doctrine query builders
License MIT
Homepage http://github.com/michaelmoussa/doctrine-qbmocker
Informations about the package doctrine-qbmocker
Doctrine QueryBuilderMocker
What's this?
A tool for easy mocking of Doctrine QueryBuilder objects to facilitate unit testing.
Example
Suppose you have a collection of user
accounts with documents looking like this:
You have a User
service class with a method that returns all names and e-mail address of users in a particular country, sorted by lastName, firstName:
Simple enough. Now how do we test it?
We could create a test database, add a few test records, then run the method to see if it retrieves the right data, but that's not ideal.
- Connecting to a database in a test is relatively slow. Too many such tests in a test suite can cause it to really drag.
- It only tests that you're getting the expected data back - not that your method is retrieving it correctly.
- What if someone accidentally deletes the
->field('country')->equals($country)
? Or changes it to->gte($country)
? Or removes the->sort('lastName', 'firstName')
? - Depending on what test records you created and the order in which you created them, you may still get the expected results back even though your method is not retrieving them the way you'd expect. Your test would still pass, but the code would be wrong.
- What if someone accidentally deletes the
The better way to test this is to mock the expected method calls and make sure the Query Builder is being used correctly.
But that ends up being pretty messy... take a look:
Imagine a unit test class filled with dozens of those! Surely we can do better. How about this?
Short and concise! Notice that it looks almost identical to the calls being made in the actual service class: The only difference is that execute()
accepts a parameter, which ends up becoming the value that the mocked call to execute()
will return. Mocking a method's usage of the Query Builder (or vice-versa, for those of us who TDD) is as simple as copying-and-pasting.
Prefer MySQL? This library supports mocking Doctrine ORM query builders as well. Just use MMoussa\Doctrine\Test\ORM\QueryBuilderMocker
instead of MMoussa\Doctrine\Test\ODM\MongoDB\QueryBuilderMocker
!
Installation
The only supported method of installation is Composer: composer require --dev "michaelmoussa/doctrine-qbmocker"
Dependencies
- Using this library for mocking the ORM query builder requires you to
composer require --dev "doctrine/orm"
as well. - Using this library for mocking the ODM query builder requires you to
composer require --dev "doctrine/mongodb-odm" "jmikola/geojson"
as well.
PHPUnit support
- Added support for PHPUnit 6 in version 1.0
- Older PHPUnit versions are supported in versions 0.x
Contributing
I am no longer actively maintaining this project, but I do review and merge Pull Requests, so contributions are welcome. There are several unsupported methods still that would be non-trivial to implement. Please feel free to take care of those and send a PR.
All contributions must conform to PSR2 and include 100% test coverage.