PHPUnit extension that enables MongoDB data fixtures for test cases.
PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples Avoiding issues when the fixture is empty Feb 17, 2017
src Avoiding issues when the fixture is empty Feb 17, 2017
tests Using mongodb extension instead of mongo Feb 15, 2017
.gitignore
.travis.yml Dropping PHP 5.5 and updating readme Feb 15, 2017
README.md Dropping PHP 5.5 and updating readme Feb 15, 2017
composer.json Supporting Mongo lib 1.3.x Jul 25, 2018
license.txt Added license info and updated requirements in readme. Feb 10, 2013
phpunit.xml Update test matrix for new version of PHP. May 21, 2015

README.md

Mongounit is a PHPUnit extension for test cases that utilize MongoDB as their data source.

Latest Stable Version Build Status

Requirements

  • PHP 5.6+
  • PHPUnit 4.0+
  • PECL mongodb 1.2+

Testing

  1. Install dependencies composer install -dev
  2. Run ./bin/phpunit

Example use

<?php

class MyMongoTestCase extends \PHPUnit_Framework_TestCase {
	use \Zumba\PHPUnit\Extensions\Mongo\TestTrait;

	/**
	 * Get the mongo connection for this test.
	 *
	 * @return Zumba\PHPUnit\Extensions\Mongo\Client\Connector
	 */
	public function getMongoConnection() {
		// Add your credentials here
		return new \MongoDB\Client();
	}

	/**
	 * Get the dataset to be used for this test.
	 *
	 * @return Zumba\PHPUnit\Extensions\Mongo\DataSet\DataSet
	 */
	public function getMongoDataSet() {
		$dataset = new \Zumba\PHPUnit\Extensions\Mongo\DataSet\DataSet($this->getMongoConnection());
		$dataset->setFixture([
			'some_collection' => [
				['name' => 'Document 1'],
				['name' => 'Document 2']
			]
		]);
		return $dataset;
	}

	public function testRead() {
		$result = $this->getMongoConnection()->test->some_collection->findOne(['name' => 'Document 2']);
		$this->assertEquals('Document 2', $result['name']);
	}

}

See full working example.

Note about PHP Versions

PHP 5.5 and below are no longer actively supported. If you are using these version, stick with previous versions of mongounit, however it is recommended to stop using these versions of PHP.