ConnectionTest.php 3.58 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
4

5 6 7 8
use yii\mongodb\Collection;
use yii\mongodb\file\Collection as FileCollection;
use yii\mongodb\Connection;
use yii\mongodb\Database;
Paul Klimov committed
9

Paul Klimov committed
10
/**
11
 * @group mongodb
Paul Klimov committed
12
 */
13
class ConnectionTest extends MongoDbTestCase
14 15 16 17
{
	public function testConstruct()
	{
		$connection = $this->getConnection(false);
18
		$params = $this->mongoDbConfig;
19 20 21 22

		$connection->open();

		$this->assertEquals($params['dsn'], $connection->dsn);
23
		$this->assertEquals($params['defaultDatabaseName'], $connection->defaultDatabaseName);
Paul Klimov committed
24 25 26 27 28 29 30 31
		$this->assertEquals($params['options'], $connection->options);
	}

	public function testOpenClose()
	{
		$connection = $this->getConnection(false, false);

		$this->assertFalse($connection->isActive);
32
		$this->assertEquals(null, $connection->mongoClient);
Paul Klimov committed
33 34 35

		$connection->open();
		$this->assertTrue($connection->isActive);
36
		$this->assertTrue(is_object($connection->mongoClient));
Paul Klimov committed
37 38 39

		$connection->close();
		$this->assertFalse($connection->isActive);
40
		$this->assertEquals(null, $connection->mongoClient);
Paul Klimov committed
41 42 43

		$connection = new Connection;
		$connection->dsn = 'unknown::memory:';
44
		$this->setExpectedException('yii\mongodb\Exception');
Paul Klimov committed
45
		$connection->open();
46
	}
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	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();
69 70
		$connection->dsn = $this->mongoDbConfig['dsn'];
		$connection->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
71 72 73
		$database = $connection->getDatabase();
		$this->assertTrue($database instanceof Database, 'Unable to get default database!');

74
		$connection = new Connection();
75 76
		$connection->dsn = $this->mongoDbConfig['dsn'];
		$connection->options = ['db' => $this->mongoDbConfig['defaultDatabaseName']];
77 78 79
		$database = $connection->getDatabase();
		$this->assertTrue($database instanceof Database, 'Unable to determine default database from options!');

80
		$connection = new Connection();
81
		$connection->dsn = $this->mongoDbConfig['dsn'] . '/' . $this->mongoDbConfig['defaultDatabaseName'];
82 83 84 85 86 87 88
		$database = $connection->getDatabase();
		$this->assertTrue($database instanceof Database, 'Unable to determine default database from dsn!');
	}

	/**
	 * @depends testGetDefaultDatabase
	 */
89 90
	public function testGetCollection()
	{
91 92
		$connection = $this->getConnection();

93
		$collection = $connection->getCollection('customer');
94 95 96 97 98 99 100
		$this->assertTrue($collection instanceof Collection);

		$collection2 = $connection->getCollection('customer');
		$this->assertTrue($collection === $collection2);

		$collection2 = $connection->getCollection('customer', true);
		$this->assertFalse($collection === $collection2);
101
	}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

	/**
	 * @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);
	}
119
}