2017 © Pedro Peláez
 

library mongomock

Library containing highly intelligent MongoDB mocks for unit testing

image

helmich/mongomock

Library containing highly intelligent MongoDB mocks for unit testing

  • Thursday, March 29, 2018
  • by helmich
  • Repository
  • 4 Watchers
  • 8 Stars
  • 1,465 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 5 Forks
  • 2 Open issues
  • 9 Versions
  • 36 % Grown

The README.md

MongoDB mock classes

Unit tests, (*1)

Author and license

Martin Helmich
This library is MIT-licenced., (*2)

Synopsis and motivation

This class contains implementations of the MongoDB\Collection and MongoDB\Database classes (not to be confused with the Mongo\Collection class from the deprecated mongo extension) that can store, modify and filter documents in memory, together with a set of (optional) PHPUnit assertions., (*3)

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., (*4)

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., (*5)

Installation

$ composer require --dev helmich/mongomock

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:, (*6)

"Mongomock" version PHPUnit 4 PHPUnit 5 PHPUnit 6 PHPUnit 7 PHPUnit 8 PHPUnit 9
v1 (branch v1), unsupported :no_entry_sign: :white_check_mark: :no_entry_sign: :no_entry_sign: :no_entry_sign: :no_entry_sign:
v2 (branch master) :no_entry_sign: :no_entry_sign: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:

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., (*7)

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):, (*8)

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

In some aspects, the MongoDB\Collection's API was extended to allow for better testability:, (*9)

  1. Filter operands may contain callback functions that are applied to document properties:, (*10)

    $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., (*11)

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

Testing

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

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

The Versions

29/03 2018

dev-master

9999999-dev

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

27/03 2018

v2.1.0

2.1.0.0

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

09/03 2018

v2.0.0

2.0.0.0

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

17/02 2018

v1.x-dev

1.9999999.9999999.9999999-dev

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

17/02 2018

v1.0.0

1.0.0.0

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

09/02 2018

v1.0.0-alpha3

1.0.0.0-alpha3

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

15/09 2017

v1.0.0-alpha2

1.0.0.0-alpha2

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

28/08 2017

v1.0.0-alpha1

1.0.0.0-alpha1

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires

28/08 2017

dev-cleanup

dev-cleanup

Library containing highly intelligent MongoDB mocks for unit testing

  Sources   Download

MIT

The Requires

 

The Development Requires