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