zumba/mongounit

支持MongoDB的PHPUnit扩展

4.0.0 2019-11-04 22:45 UTC

This package is auto-updated.

Last update: 2024-09-05 08:47:25 UTC


README

Mongounit是一个PHPUnit扩展,用于测试使用MongoDB作为数据源的测试用例。

Latest Stable Version Build Status

需求

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

测试

  1. 安装依赖项 composer install -dev
  2. 运行 ./bin/phpunit

示例使用

<?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']);
	}

}

查看完整的示例。

关于PHP版本说明

PHP 5.5及以下版本不再提供活跃支持。如果您正在使用这些版本,请继续使用mongounit的旧版本,但建议停止使用这些PHP版本。