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.
		
		
		
		
			
				
					119 lines
				
				3.6 KiB
			
		
		
			
		
	
	
					119 lines
				
				3.6 KiB
			| 
											12 years ago
										 | <?php
 | ||
|  | 
 | ||
| 
											12 years ago
										 | namespace yiiunit\extensions\mongodb;
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | use yii\mongodb\Collection;
 | ||
|  | use yii\mongodb\file\Collection as FileCollection;
 | ||
|  | use yii\mongodb\Connection;
 | ||
|  | use yii\mongodb\Database;
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | /**
 | ||
|  |  * @group mongo
 | ||
|  |  */
 | ||
| 
											12 years ago
										 | class ConnectionTest extends MongoDbTestCase
 | ||
| 
											12 years ago
										 | {
 | ||
|  | 	public function testConstruct()
 | ||
|  | 	{
 | ||
|  | 		$connection = $this->getConnection(false);
 | ||
| 
											12 years ago
										 | 		$params = $this->mongoDbConfig;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 		$connection->open();
 | ||
|  | 
 | ||
|  | 		$this->assertEquals($params['dsn'], $connection->dsn);
 | ||
| 
											12 years ago
										 | 		$this->assertEquals($params['defaultDatabaseName'], $connection->defaultDatabaseName);
 | ||
| 
											12 years ago
										 | 		$this->assertEquals($params['options'], $connection->options);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	public function testOpenClose()
 | ||
|  | 	{
 | ||
|  | 		$connection = $this->getConnection(false, false);
 | ||
|  | 
 | ||
|  | 		$this->assertFalse($connection->isActive);
 | ||
| 
											12 years ago
										 | 		$this->assertEquals(null, $connection->mongoClient);
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 		$connection->open();
 | ||
|  | 		$this->assertTrue($connection->isActive);
 | ||
| 
											12 years ago
										 | 		$this->assertTrue(is_object($connection->mongoClient));
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 		$connection->close();
 | ||
|  | 		$this->assertFalse($connection->isActive);
 | ||
| 
											12 years ago
										 | 		$this->assertEquals(null, $connection->mongoClient);
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 		$connection = new Connection;
 | ||
|  | 		$connection->dsn = 'unknown::memory:';
 | ||
| 
											12 years ago
										 | 		$this->setExpectedException('yii\mongodb\Exception');
 | ||
| 
											12 years ago
										 | 		$connection->open();
 | ||
| 
											12 years ago
										 | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | 	public function testGetDatabase()
 | ||
|  | 	{
 | ||
|  | 		$connection = $this->getConnection();
 | ||
|  | 
 | ||
|  | 		$database = $connection->getDatabase($connection->defaultDatabaseName);
 | ||
|  | 		$this->assertTrue($database instanceof Database);
 | ||
|  | 		$this->assertTrue($database->mongoDb instanceof \MongoDB);
 | ||
|  | 
 | ||
|  | 		$database2 = $connection->getDatabase($connection->defaultDatabaseName);
 | ||
|  | 		$this->assertTrue($database === $database2);
 | ||
|  | 
 | ||
|  | 		$databaseRefreshed = $connection->getDatabase($connection->defaultDatabaseName, true);
 | ||
|  | 		$this->assertFalse($database === $databaseRefreshed);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @depends testGetDatabase
 | ||
|  | 	 */
 | ||
|  | 	public function testGetDefaultDatabase()
 | ||
|  | 	{
 | ||
|  | 		$connection = new Connection();
 | ||
| 
											12 years ago
										 | 		$connection->dsn = $this->mongoDbConfig['dsn'];
 | ||
|  | 		$connection->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
 | ||
| 
											12 years ago
										 | 		$database = $connection->getDatabase();
 | ||
|  | 		$this->assertTrue($database instanceof Database, 'Unable to get default database!');
 | ||
|  | 
 | ||
|  | 		$connection = new Connection();
 | ||
| 
											12 years ago
										 | 		$connection->dsn = $this->mongoDbConfig['dsn'];
 | ||
|  | 		$connection->options = ['db' => $this->mongoDbConfig['defaultDatabaseName']];
 | ||
| 
											12 years ago
										 | 		$database = $connection->getDatabase();
 | ||
|  | 		$this->assertTrue($database instanceof Database, 'Unable to determine default database from options!');
 | ||
|  | 
 | ||
|  | 		$connection = new Connection();
 | ||
| 
											12 years ago
										 | 		$connection->dsn = $this->mongoDbConfig['dsn'] . '/' . $this->mongoDbConfig['defaultDatabaseName'];
 | ||
| 
											12 years ago
										 | 		$database = $connection->getDatabase();
 | ||
|  | 		$this->assertTrue($database instanceof Database, 'Unable to determine default database from dsn!');
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @depends testGetDefaultDatabase
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function testGetCollection()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$connection = $this->getConnection();
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 		$collection = $connection->getCollection('customer');
 | ||
| 
											12 years ago
										 | 		$this->assertTrue($collection instanceof Collection);
 | ||
|  | 
 | ||
|  | 		$collection2 = $connection->getCollection('customer');
 | ||
|  | 		$this->assertTrue($collection === $collection2);
 | ||
|  | 
 | ||
|  | 		$collection2 = $connection->getCollection('customer', true);
 | ||
|  | 		$this->assertFalse($collection === $collection2);
 | ||
| 
											12 years ago
										 | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * @depends testGetDefaultDatabase
 | ||
|  | 	 */
 | ||
|  | 	public function testGetFileCollection()
 | ||
|  | 	{
 | ||
|  | 		$connection = $this->getConnection();
 | ||
|  | 
 | ||
|  | 		$collection = $connection->getFileCollection('testfs');
 | ||
|  | 		$this->assertTrue($collection instanceof FileCollection);
 | ||
|  | 
 | ||
|  | 		$collection2 = $connection->getFileCollection('testfs');
 | ||
|  | 		$this->assertTrue($collection === $collection2);
 | ||
|  | 
 | ||
|  | 		$collection2 = $connection->getFileCollection('testfs', true);
 | ||
|  | 		$this->assertFalse($collection === $collection2);
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | }
 |