PHPackages                             kevinkl3/mongomock - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. kevinkl3/mongomock

ActiveLibrary[Testing &amp; Quality](/categories/testing)

kevinkl3/mongomock
==================

Library containing highly intelligent MongoDB mocks for unit testing

v2.7.1(4mo ago)072MITPHPPHP ^8.0CI failing

Since Aug 28Pushed 4mo agoCompare

[ Source](https://github.com/kevinkl3/php-mongomock)[ Packagist](https://packagist.org/packages/kevinkl3/mongomock)[ RSS](/packages/kevinkl3-mongomock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (30)Used By (0)

MongoDB mock classes
====================

[](#mongodb-mock-classes)

[![Unit tests](https://github.com/martin-helmich/php-mongomock/workflows/Unit%20tests/badge.svg)](https://github.com/martin-helmich/php-mongomock/workflows/Unit%20tests/badge.svg)

Author and license
------------------

[](#author-and-license)

Martin Helmich
This library is [MIT-licenced](LICENSE.txt).

Synopsis and motivation
-----------------------

[](#synopsis-and-motivation)

This class contains implementations of the [MongoDB\\Collection](https://docs.mongodb.com/php-library/master/reference/class/MongoDBCollection/) and [MongoDB\\Database](https://docs.mongodb.com/php-library/master/reference/class/MongoDBDatabase/) classes (not to be confused with the [Mongo\\Collection](http://php.net/manual/en/class.mongocollection.php)class from the deprecated *mongo* extension) that can store, modify and filter documents in memory, together with a set of (optional) PHPUnit assertions.

I wrote this library because I wanted to unit-test a library that used MongoDB collections intensively and felt that mocking the `MongoDB\Collection` class using PHPUnit's built-in mock builders was too restrictive.

**Note**: Currently, this implementation contains only a subset of the actual MongoDB collection API. I've only implemented the parts of the API that I needed for my use case. If you need additional functionality, feel free to open an issue, or (better yet) a pull request.

Installation
------------

[](#installation)

```
$ composer require --dev helmich/mongomock

```

Compatibility
-------------

[](#compatibility)

There are several release branches of this library, each of these being compatible with different releases of PHPUnit and PHP. The following table should give an easy overview:

"Mongomock" versionPHPUnit 4PHPUnit 5PHPUnit 6PHPUnit 7PHPUnit 8PHPUnit 9v1 (branch `v1`), **unsupported**🚫✅🚫🚫🚫🚫v2 (branch `master`)🚫🚫✅✅✅✅When you are using `composer require` and have already declared a dependency to `phpunit/phpunit` in your `composer.json` file, Composer should pick latest compatible version automatically.

Usage
-----

[](#usage)

You can use this library exactly as you'd use the `MongoDB\Collection` or `MongoDB\Database` classes (in theory, at least -- remember, this package is not API-complete):

```
use Helmich\MongoMock\MockCollection;

$collection = new MockCollection();
$collection->createIndex(['foo' => 1]);

$documentId = $collection->insertOne(['foo' => 'bar'])->insertedId();
$collection->updateOne(['_id' => $documentId], ['$set' => ['foo' => 'baz']]);
```

Differences
-----------

[](#differences)

In some aspects, the `MongoDB\Collection`'s API was extended to allow for better testability:

1. Filter operands may contain callback functions that are applied to document properties:

    ```
    $r = $collection->find([
        'someProperty' => function($p) {
            return $p == 'bar';
        }
    ]);
    ```
2. Filter operands may contain PHPUnit constraints (meaning instances of the `PHPUnit_Framework_Constraint` class). You can easily build these using the factory functions in the `PHPUnit_Framework_Assert` class.

    ```
    $r = $collection->find([
        'someProperty' => \PHPUnit_Framework_Assert::isInstanceOf(\MongoDB\BSON\Binary::class)
    ]);
    ```

Testing
-------

[](#testing)

To run the tests (anywhere with a running Docker installation):

```
$ docker-compose run php7phpunit
$ docker-compose run php8phpunit

```

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance77

Regular maintenance activity

Popularity8

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 55.9% 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 ~190 days

Recently: every ~397 days

Total

17

Last Release

126d ago

Major Versions

v1.x-dev → v2.0.02018-03-09

PHP version history (5 changes)v1.0.0-alpha1PHP &gt;=7.0.0

v2.0.0PHP &gt;=7.0.0,&lt;8.0

v2.2.0PHP &gt;=7.1.0,&lt;8.0

v2.5.0PHP ^7.1.0 || ^8.0

v2.7.1PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a4d635b31fdcc426ef3bdc093be9f5a8f5feca36814cd1c7ab4adde7a7b1e261?d=identicon)[kevinkl3](/maintainers/kevinkl3)

---

Top Contributors

[![martin-helmich](https://avatars.githubusercontent.com/u/2538958?v=4)](https://github.com/martin-helmich "martin-helmich (81 commits)")[![kevinl4a](https://avatars.githubusercontent.com/u/143346086?v=4)](https://github.com/kevinl4a "kevinl4a (23 commits)")[![filecage](https://avatars.githubusercontent.com/u/698854?v=4)](https://github.com/filecage "filecage (13 commits)")[![raffis](https://avatars.githubusercontent.com/u/2376735?v=4)](https://github.com/raffis "raffis (11 commits)")[![shadiakiki1986](https://avatars.githubusercontent.com/u/8392324?v=4)](https://github.com/shadiakiki1986 "shadiakiki1986 (6 commits)")[![jrattue](https://avatars.githubusercontent.com/u/5196521?v=4)](https://github.com/jrattue "jrattue (2 commits)")[![kevinkl3](https://avatars.githubusercontent.com/u/3175119?v=4)](https://github.com/kevinkl3 "kevinkl3 (2 commits)")[![tdimdimich](https://avatars.githubusercontent.com/u/6346930?v=4)](https://github.com/tdimdimich "tdimdimich (1 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (1 commits)")[![hpaknia](https://avatars.githubusercontent.com/u/6986272?v=4)](https://github.com/hpaknia "hpaknia (1 commits)")[![prilka](https://avatars.githubusercontent.com/u/1280103?v=4)](https://github.com/prilka "prilka (1 commits)")[![Severens](https://avatars.githubusercontent.com/u/6905787?v=4)](https://github.com/Severens "Severens (1 commits)")[![tarikhajk](https://avatars.githubusercontent.com/u/28071110?v=4)](https://github.com/tarikhajk "tarikhajk (1 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kevinkl3-mongomock/health.svg)

```
[![Health](https://phpackages.com/badges/kevinkl3-mongomock/health.svg)](https://phpackages.com/packages/kevinkl3-mongomock)
```

###  Alternatives

[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[timacdonald/log-fake

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

4235.9M55](/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.0M774](/packages/typo3-testing-framework)[robiningelbrecht/phpunit-pretty-print

Prettify PHPUnit output

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

PHPackages © 2026

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