You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					149 lines
				
				3.3 KiB
			
		
		
			
		
	
	
					149 lines
				
				3.3 KiB
			| 
											12 years ago
										 | <?php
 | ||
|  | 
 | ||
| 
											12 years ago
										 | namespace yiiunit\extensions\mongodb;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | use yii\helpers\FileHelper;
 | ||
| 
											12 years ago
										 | use yii\mongodb\Connection;
 | ||
| 
											12 years ago
										 | use Yii;
 | ||
| 
											12 years ago
										 | use yii\mongodb\Exception;
 | ||
| 
											12 years ago
										 | use yiiunit\TestCase;
 | ||
|  | 
 | ||
| 
											12 years ago
										 | class MongoDbTestCase extends TestCase
 | ||
| 
											12 years ago
										 | {
 | ||
|  | 	/**
 | ||
|  | 	 * @var array Mongo connection configuration.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	protected $mongoDbConfig = [
 | ||
| 
											12 years ago
										 | 		'dsn' => 'mongodb://localhost:27017',
 | ||
| 
											12 years ago
										 | 		'defaultDatabaseName' => 'yii2test',
 | ||
| 
											12 years ago
										 | 		'options' => [],
 | ||
| 
											12 years ago
										 | 	];
 | ||
|  | 	/**
 | ||
|  | 	 * @var Connection Mongo connection instance.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	protected $mongodb;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	public static function setUpBeforeClass()
 | ||
|  | 	{
 | ||
|  | 		static::loadClassMap();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	protected function setUp()
 | ||
|  | 	{
 | ||
|  | 		parent::setUp();
 | ||
|  | 		if (!extension_loaded('mongo')) {
 | ||
|  | 			$this->markTestSkipped('mongo extension required.');
 | ||
|  | 		}
 | ||
| 
											12 years ago
										 | 		$config = $this->getParam('mongodb');
 | ||
| 
											12 years ago
										 | 		if (!empty($config)) {
 | ||
| 
											12 years ago
										 | 			$this->mongoDbConfig = $config;
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 		$this->mockApplication();
 | ||
|  | 		static::loadClassMap();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	protected function tearDown()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		if ($this->mongodb) {
 | ||
|  | 			$this->mongodb->close();
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 		$this->destroyApplication();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Adds sphinx extension files to [[Yii::$classPath]],
 | ||
|  | 	 * avoiding the necessity of usage Composer autoloader.
 | ||
|  | 	 */
 | ||
|  | 	protected static function loadClassMap()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$baseNameSpace = 'yii/mongodb';
 | ||
|  | 		$basePath = realpath(__DIR__. '/../../../../extensions/mongodb');
 | ||
| 
											12 years ago
										 | 		$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
 | ||
| 
											12 years ago
										 | 	 * @return \yii\mongodb\Connection
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	public function getConnection($reset = false, $open = true)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		if (!$reset && $this->mongodb) {
 | ||
|  | 			return $this->mongodb;
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 		$db = new Connection;
 | ||
| 
											12 years ago
										 | 		$db->dsn = $this->mongoDbConfig['dsn'];
 | ||
|  | 		$db->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
 | ||
|  | 		if (isset($this->mongoDbConfig['options'])) {
 | ||
|  | 			$db->options = $this->mongoDbConfig['options'];
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 		if ($open) {
 | ||
|  | 			$db->open();
 | ||
|  | 		}
 | ||
| 
											12 years ago
										 | 		$this->mongodb = $db;
 | ||
| 
											12 years ago
										 | 		return $db;
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * Drops the specified collection.
 | ||
|  | 	 * @param string $name collection name.
 | ||
|  | 	 */
 | ||
|  | 	protected function dropCollection($name)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		if ($this->mongodb) {
 | ||
| 
											12 years ago
										 | 			try {
 | ||
| 
											12 years ago
										 | 				$this->mongodb->getCollection($name)->drop();
 | ||
| 
											12 years ago
										 | 			} catch (Exception $e) {
 | ||
|  | 				// shut down exception
 | ||
|  | 			}
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Drops the specified file collection.
 | ||
|  | 	 * @param string $name file collection name.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	protected function dropFileCollection($name = 'fs')
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		if ($this->mongodb) {
 | ||
| 
											12 years ago
										 | 			try {
 | ||
| 
											12 years ago
										 | 				$this->mongodb->getFileCollection($name)->drop();
 | ||
| 
											12 years ago
										 | 			} catch (Exception $e) {
 | ||
|  | 				// shut down exception
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Finds all records in collection.
 | ||
| 
											12 years ago
										 | 	 * @param \yii\mongodb\Collection $collection
 | ||
| 
											12 years ago
										 | 	 * @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;
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * 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'];
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | }
 |