Klimov Paul
11 years ago
9 changed files with 469 additions and 318 deletions
@ -0,0 +1,64 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\mongo; |
||||
|
||||
use yii\base\Object; |
||||
use Yii; |
||||
|
||||
/** |
||||
* Database represents the Mongo database information. |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Database extends Object |
||||
{ |
||||
/** |
||||
* @var \MongoDB Mongo database instance. |
||||
*/ |
||||
public $mongoDb; |
||||
/** |
||||
* @var Collection[] list of collections. |
||||
*/ |
||||
private $_collections = []; |
||||
|
||||
/** |
||||
* Returns the Mongo collection with the given name. |
||||
* @param string $name collection name |
||||
* @param boolean $refresh whether to reload the table schema even if it is found in the cache. |
||||
* @return Collection mongo collection instance. |
||||
*/ |
||||
public function getCollection($name, $refresh = false) |
||||
{ |
||||
if ($refresh || !array_key_exists($name, $this->_collections)) { |
||||
$this->_collections[$name] = $this->selectCollection($name); |
||||
} |
||||
return $this->_collections[$name]; |
||||
} |
||||
|
||||
/** |
||||
* Selects collection with given name. |
||||
* @param string $name collection name. |
||||
* @return Collection collection instance. |
||||
*/ |
||||
protected function selectCollection($name) |
||||
{ |
||||
return Yii::createObject([ |
||||
'class' => 'yii\mongo\Collection', |
||||
'mongoCollection' => $this->mongoDb->selectCollection($name) |
||||
]); |
||||
} |
||||
|
||||
/** |
||||
* Drops this database. |
||||
*/ |
||||
public function drop() |
||||
{ |
||||
$this->mongoDb->drop(); |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
<?php |
||||
|
||||
namespace yiiunit\extensions\mongo; |
||||
|
||||
use yii\mongo\Collection; |
||||
|
||||
/** |
||||
* @group mongo |
||||
*/ |
||||
class DatabaseTest extends MongoTestCase |
||||
{ |
||||
protected function tearDown() |
||||
{ |
||||
$this->dropCollection('customer'); |
||||
parent::tearDown(); |
||||
} |
||||
|
||||
// Tests : |
||||
|
||||
public function testGetCollection() |
||||
{ |
||||
$database = $connection = $this->getConnection()->getDatabase(); |
||||
|
||||
$collection = $database->getCollection('customer'); |
||||
$this->assertTrue($collection instanceof Collection); |
||||
$this->assertTrue($collection->mongoCollection instanceof \MongoCollection); |
||||
|
||||
$collection2 = $database->getCollection('customer'); |
||||
$this->assertTrue($collection === $collection2); |
||||
|
||||
$collectionRefreshed = $database->getCollection('customer', true); |
||||
$this->assertFalse($collection === $collectionRefreshed); |
||||
} |
||||
} |
Loading…
Reference in new issue