mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-09 01:27:20 +08:00
Mongo connection advanced.
This commit is contained in:
@ -15,6 +15,8 @@ use Yii;
|
|||||||
/**
|
/**
|
||||||
* Class Connection
|
* Class Connection
|
||||||
*
|
*
|
||||||
|
* @property boolean $isActive Whether the Mongo connection is established. This property is read-only.
|
||||||
|
*
|
||||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
@ -49,6 +51,15 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public $dbName;
|
public $dbName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a value indicating whether the Mongo connection is established.
|
||||||
|
* @return boolean whether the Mongo connection is established
|
||||||
|
*/
|
||||||
|
public function getIsActive()
|
||||||
|
{
|
||||||
|
return is_object($this->client) && $this->client->connected;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Establishes a Mongo connection.
|
* Establishes a Mongo connection.
|
||||||
* It does nothing if a Mongo connection has already been established.
|
* It does nothing if a Mongo connection has already been established.
|
||||||
|
|||||||
@ -3,6 +3,8 @@
|
|||||||
namespace yiiunit\extensions\mongo;
|
namespace yiiunit\extensions\mongo;
|
||||||
|
|
||||||
|
|
||||||
|
use yii\mongo\Connection;
|
||||||
|
|
||||||
class ConnectionTest extends MongoTestCase
|
class ConnectionTest extends MongoTestCase
|
||||||
{
|
{
|
||||||
public function testConstruct()
|
public function testConstruct()
|
||||||
@ -13,7 +15,28 @@ class ConnectionTest extends MongoTestCase
|
|||||||
$connection->open();
|
$connection->open();
|
||||||
|
|
||||||
$this->assertEquals($params['dsn'], $connection->dsn);
|
$this->assertEquals($params['dsn'], $connection->dsn);
|
||||||
//$this->assertEquals($params['username'], $connection->username);
|
$this->assertEquals($params['dbName'], $connection->dbName);
|
||||||
//$this->assertEquals($params['password'], $connection->password);
|
$this->assertEquals($params['options'], $connection->options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOpenClose()
|
||||||
|
{
|
||||||
|
$connection = $this->getConnection(false, false);
|
||||||
|
|
||||||
|
$this->assertFalse($connection->isActive);
|
||||||
|
$this->assertEquals(null, $connection->client);
|
||||||
|
|
||||||
|
$connection->open();
|
||||||
|
$this->assertTrue($connection->isActive);
|
||||||
|
$this->assertTrue(is_object($connection->client));
|
||||||
|
|
||||||
|
$connection->close();
|
||||||
|
$this->assertFalse($connection->isActive);
|
||||||
|
$this->assertEquals(null, $connection->client);
|
||||||
|
|
||||||
|
$connection = new Connection;
|
||||||
|
$connection->dsn = 'unknown::memory:';
|
||||||
|
$this->setExpectedException('yii\db\Exception');
|
||||||
|
$connection->open();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user