PHPackages                             michaelmoussa/doctrine-qbmocker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. michaelmoussa/doctrine-qbmocker

AbandonedArchivedLibrary

michaelmoussa/doctrine-qbmocker
===============================

A helper library for mocking Doctrine query builders

1.0.0(9y ago)2384.9k13[5 issues](https://github.com/michaelmoussa/doctrine-qbmocker/issues)MITPHPPHP ^7.0.0

Since Jan 27Pushed 6y ago1 watchersCompare

[ Source](https://github.com/michaelmoussa/doctrine-qbmocker)[ Packagist](https://packagist.org/packages/michaelmoussa/doctrine-qbmocker)[ Docs](http://github.com/michaelmoussa/doctrine-qbmocker)[ RSS](/packages/michaelmoussa-doctrine-qbmocker/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (8)Versions (16)Used By (0)

[![Build Status](https://camo.githubusercontent.com/2d67a69351eeaed486caa06dc0551f1cd1662b531d445953f2a399142ce9facb/68747470733a2f2f7472617669732d63692e6f72672f6d69636861656c6d6f757373612f646f637472696e652d71626d6f636b65722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/michaelmoussa/doctrine-qbmocker)[![Coverage Status](https://camo.githubusercontent.com/6f168f4cc17e8229f6f792ee6f614c8dbde5dd3598f5adcc45f6ed006e899cc5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d69636861656c6d6f757373612f646f637472696e652d71626d6f636b65722f62616467652e706e67)](https://coveralls.io/r/michaelmoussa/doctrine-qbmocker)[![Latest Stable Version](https://camo.githubusercontent.com/494d09a3a26735a19e96734256364c5d6a21088015eddd46c66f5048c5b777eb/68747470733a2f2f706f7365722e707567782e6f72672f6d69636861656c6d6f757373612f646f637472696e652d71626d6f636b65722f762f737461626c652e706e67)](https://packagist.org/packages/michaelmoussa/doctrine-qbmocker)

Doctrine QueryBuilderMocker
===========================

[](#doctrine-querybuildermocker)

### What's this?

[](#whats-this)

A tool for easy mocking of Doctrine QueryBuilder objects to facilitate unit testing.

### Example

[](#example)

Suppose you have a collection of `user` accounts with documents looking like this:

```
{
    firstName: "John",
    lastName: "Doe",
    email: "JohnDoe@example.com",
    country: "USA"
}

```

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:

```
public function getUsersByCountry($country)
{
    return $this->documentManager->createQueryBuilder('My\Document\User')
                                 ->select('firstName', 'lastName', 'email')
                                 ->field('country')->equals($country)
                                 ->sort('lastName', 'firstName')
                                 ->getQuery()
                                 ->execute();
}

```

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.

1. Connecting to a database in a test is relatively slow. Too many such tests in a test suite can cause it to really drag.
2. 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.

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:

```
public function testCanGetSortedUsersByCountry()
{
    $mockQuery = $this->getMockBuilder('Doctrine\ODM\MongoDB\Query\Builder')
                      ->disableOriginalConstructor()
                      ->getMock();
    $mockQuery->expects($this->once())
              ->method('execute')
              ->will($this->returnValue('it works!'));

    $mockQueryBuilder = $this->getMockBuilder('Doctrine\ODM\MongoDB\Query\Builder')
                             ->disableOriginalConstructor()
                             ->getMock();
    $mockQueryBuilder->expects($this->at(0))
                     ->method('select')
                     ->with('firstName', 'lastName', 'email')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(1))
                     ->method('field')
                     ->with('country')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(2))
                     ->method('equals')
                     ->with('USA')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(3))
                     ->method('sort')
                     ->with('lastName', 'firstName')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(4))
                     ->method('getQuery')
                     ->will($this->returnValue($mockQuery));

    $mockDocumentManager = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentManager')
                                ->disableOriginalConstructor()
                                ->getMock();
    $mockDocumentManager->expects($this->once())
                        ->method('createQueryBuilder')
                        ->with('My\Document\User')
                        ->will($this->returnValue($mockQueryBuilder));

    $service->setDocumentManager($mockDocumentManager);

    $this->assertSame('it works!', $this->service->getUsersByCountry('USA'));

```

Imagine a unit test class filled with dozens of those! Surely we can do better. How about this?

```
public function testCanGetSortedUsersByCountry()
{
    $qbm = new QueryBuilderMocker($this);
    $qbm->select('firstName', 'lastName', 'email')
        ->field('country')->equals($country)
        ->sort('lastName', 'firstName')
        ->getQuery()
        ->execute('it works!');
    $qb = $qbm->getQueryBuilderMock();

    $this->assertSame('it works!', $this->service->getUsersByCountry('USA'));
}

```

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

[](#installation)

The only supported method of installation is Composer: `composer require --dev "michaelmoussa/doctrine-qbmocker"`

### Dependencies

[](#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

[](#phpunit-support)

- Added support for PHPUnit 6 in version 1.0
- Older PHPUnit versions are supported in versions 0.x

### Contributing

[](#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.

###  Health Score

37

↑

LowBetter than 83% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 70.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~83 days

Recently: every ~176 days

Total

15

Last Release

3320d ago

Major Versions

0.12.0 → 1.0.02017-04-08

PHP version history (3 changes)0.10.0PHP ^5.4

0.11.0PHP ^5.4 | ^7.0.0

1.0.0PHP ^7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/91d3f2058c69f2d3e5396f0d6ecf1bf048e7a83905731530ab6dd4659bb62789?d=identicon)[michaelmoussa](/maintainers/michaelmoussa)

---

Top Contributors

[![michaelmoussa](https://avatars.githubusercontent.com/u/183833?v=4)](https://github.com/michaelmoussa "michaelmoussa (50 commits)")[![kingjerod](https://avatars.githubusercontent.com/u/2287252?v=4)](https://github.com/kingjerod "kingjerod (8 commits)")[![spdionis](https://avatars.githubusercontent.com/u/4737101?v=4)](https://github.com/spdionis "spdionis (4 commits)")[![theorx](https://avatars.githubusercontent.com/u/5157406?v=4)](https://github.com/theorx "theorx (4 commits)")[![hickeroar](https://avatars.githubusercontent.com/u/2441838?v=4)](https://github.com/hickeroar "hickeroar (3 commits)")[![spiffyjr](https://avatars.githubusercontent.com/u/2760734?v=4)](https://github.com/spiffyjr "spiffyjr (2 commits)")

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/michaelmoussa-doctrine-qbmocker/health.svg)

```
[![Health](https://phpackages.com/badges/michaelmoussa-doctrine-qbmocker/health.svg)](https://phpackages.com/packages/michaelmoussa-doctrine-qbmocker)
```

###  Alternatives

[timacdonald/log-fake

A drop in fake logger for testing with the Laravel framework.

4235.9M56](/packages/timacdonald-log-fake)[jasonmccreary/laravel-test-assertions

A set of helpful assertions when testing Laravel applications.

3513.9M32](/packages/jasonmccreary-laravel-test-assertions)[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)[typo3/testing-framework

The TYPO3 testing framework provides base classes for unit, functional and acceptance testing.

675.0M775](/packages/typo3-testing-framework)[robiningelbrecht/phpunit-pretty-print

Prettify PHPUnit output

76460.0k15](/packages/robiningelbrecht-phpunit-pretty-print)[webmozarts/strict-phpunit

Enables type-safe comparisons of objects in PHPUnit

31252.7k5](/packages/webmozarts-strict-phpunit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
