MongoDbTestCase.php 3.34 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
4 5

use yii\helpers\FileHelper;
6
use yii\mongodb\Connection;
7
use Yii;
8
use yii\mongodb\Exception;
9 10
use yiiunit\TestCase;

11
class MongoDbTestCase extends TestCase
12 13 14 15
{
	/**
	 * @var array Mongo connection configuration.
	 */
16
	protected $mongoDbConfig = [
17
		'dsn' => 'mongodb://localhost:27017',
18
		'defaultDatabaseName' => 'yii2test',
Klimov Paul committed
19
		'options' => [],
20 21 22 23
	];
	/**
	 * @var Connection Mongo connection instance.
	 */
24
	protected $mongodb;
25 26 27 28 29 30 31 32 33 34 35 36

	public static function setUpBeforeClass()
	{
		static::loadClassMap();
	}

	protected function setUp()
	{
		parent::setUp();
		if (!extension_loaded('mongo')) {
			$this->markTestSkipped('mongo extension required.');
		}
37
		$config = $this->getParam('mongodb');
38
		if (!empty($config)) {
39
			$this->mongoDbConfig = $config;
40 41 42 43 44 45 46
		}
		$this->mockApplication();
		static::loadClassMap();
	}

	protected function tearDown()
	{
47 48
		if ($this->mongodb) {
			$this->mongodb->close();
49 50 51 52 53 54 55 56 57 58
		}
		$this->destroyApplication();
	}

	/**
	 * Adds sphinx extension files to [[Yii::$classPath]],
	 * avoiding the necessity of usage Composer autoloader.
	 */
	protected static function loadClassMap()
	{
59
		$baseNameSpace = 'yii/mongodb';
Qiang Xue committed
60
		$basePath = realpath(__DIR__. '/../../../../extensions/yii/mongodb');
61 62 63 64 65 66 67 68 69 70 71
		$files = FileHelper::findFiles($basePath);
		foreach ($files as $file) {
			$classRelativePath = str_replace($basePath, '', $file);
			$classFullName = str_replace(['/', '.php'], ['\\', ''], $baseNameSpace . $classRelativePath);
			Yii::$classMap[$classFullName] = $file;
		}
	}

	/**
	 * @param boolean $reset whether to clean up the test database
	 * @param boolean $open whether to open test database
72
	 * @return \yii\mongodb\Connection
73 74 75
	 */
	public function getConnection($reset = false, $open = true)
	{
76 77
		if (!$reset && $this->mongodb) {
			return $this->mongodb;
78 79
		}
		$db = new Connection;
80 81 82 83
		$db->dsn = $this->mongoDbConfig['dsn'];
		$db->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
		if (isset($this->mongoDbConfig['options'])) {
			$db->options = $this->mongoDbConfig['options'];
84 85 86 87
		}
		if ($open) {
			$db->open();
		}
88
		$this->mongodb = $db;
89 90
		return $db;
	}
Paul Klimov committed
91 92 93 94 95 96 97

	/**
	 * Drops the specified collection.
	 * @param string $name collection name.
	 */
	protected function dropCollection($name)
	{
98
		if ($this->mongodb) {
99
			try {
100
				$this->mongodb->getCollection($name)->drop();
101 102 103
			} catch (Exception $e) {
				// shut down exception
			}
Paul Klimov committed
104 105
		}
	}
106

107 108 109 110
	/**
	 * Drops the specified file collection.
	 * @param string $name file collection name.
	 */
Paul Klimov committed
111
	protected function dropFileCollection($name = 'fs')
112
	{
113
		if ($this->mongodb) {
114
			try {
115
				$this->mongodb->getFileCollection($name)->drop();
116 117 118 119 120 121
			} catch (Exception $e) {
				// shut down exception
			}
		}
	}

122 123
	/**
	 * Finds all records in collection.
124
	 * @param \yii\mongodb\Collection $collection
125 126 127 128 129 130 131 132 133 134 135 136 137
	 * @param array $condition
	 * @param array $fields
	 * @return array rows
	 */
	protected function findAll($collection, $condition = [], $fields = [])
	{
		$cursor = $collection->find($condition, $fields);
		$result = [];
		foreach ($cursor as $data) {
			$result[] = $data;
		}
		return $result;
	}
138 139 140 141 142 143 144 145 146 147 148

	/**
	 * Returns the Mongo server version.
	 * @return string Mongo server version.
	 */
	protected function getServerVersion()
	{
		$connection = $this->getConnection();
		$buildInfo = $connection->getDatabase()->executeCommand(['buildinfo' => true]);
		return $buildInfo['version'];
	}
Qiang Xue committed
149
}