ConnectionTest.php 2.66 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php

Qiang Xue committed
3
namespace yiiunit\framework\db;
Qiang Xue committed
4

Qiang Xue committed
5
use yii\db\Connection;
w  
Qiang Xue committed
6

7 8 9 10
/**
 * @group db
 * @group mysql
 */
Alexander Makarov committed
11
class ConnectionTest extends DatabaseTestCase
w  
Qiang Xue committed
12
{
Alexander Makarov committed
13
	public function testConstruct()
w  
Qiang Xue committed
14
	{
Qiang Xue committed
15
		$connection = $this->getConnection(false);
16
		$params = $this->database;
Qiang Xue committed
17

w  
Qiang Xue committed
18 19 20 21 22
		$this->assertEquals($params['dsn'], $connection->dsn);
		$this->assertEquals($params['username'], $connection->username);
		$this->assertEquals($params['password'], $connection->password);
	}

Alexander Makarov committed
23
	public function testOpenClose()
w  
Qiang Xue committed
24
	{
25
		$connection = $this->getConnection(false, false);
Qiang Xue committed
26

27
		$this->assertFalse($connection->isActive);
w  
Qiang Xue committed
28 29 30
		$this->assertEquals(null, $connection->pdo);

		$connection->open();
31
		$this->assertTrue($connection->isActive);
Qiang Xue committed
32
		$this->assertTrue($connection->pdo instanceof \PDO);
w  
Qiang Xue committed
33 34

		$connection->close();
35
		$this->assertFalse($connection->isActive);
w  
Qiang Xue committed
36 37
		$this->assertEquals(null, $connection->pdo);

Qiang Xue committed
38 39
		$connection = new Connection;
		$connection->dsn = 'unknown::memory:';
w  
Qiang Xue committed
40 41 42 43
		$this->setExpectedException('yii\db\Exception');
		$connection->open();
	}

Alexander Makarov committed
44
	public function testGetDriverName()
Qiang Xue committed
45
	{
46 47
		$connection = $this->getConnection(false, false);
		$this->assertEquals($this->driverName, $connection->driverName);
Qiang Xue committed
48 49
	}

Alexander Makarov committed
50
	public function testQuoteValue()
Qiang Xue committed
51
	{
Qiang Xue committed
52
		$connection = $this->getConnection(false);
Qiang Xue committed
53 54
		$this->assertEquals(123, $connection->quoteValue(123));
		$this->assertEquals("'string'", $connection->quoteValue('string'));
55
		$this->assertEquals("'It\\'s interesting'", $connection->quoteValue("It's interesting"));
Qiang Xue committed
56 57
	}

Alexander Makarov committed
58
	public function testQuoteTableName()
Qiang Xue committed
59
	{
Qiang Xue committed
60
		$connection = $this->getConnection(false);
Qiang Xue committed
61 62 63
		$this->assertEquals('`table`', $connection->quoteTableName('table'));
		$this->assertEquals('`table`', $connection->quoteTableName('`table`'));
		$this->assertEquals('`schema`.`table`', $connection->quoteTableName('schema.table'));
64 65 66
		$this->assertEquals('`schema`.`table`', $connection->quoteTableName('schema.`table`'));
		$this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}'));
		$this->assertEquals('(table)', $connection->quoteTableName('(table)'));
Qiang Xue committed
67 68
	}

Alexander Makarov committed
69
	public function testQuoteColumnName()
Qiang Xue committed
70
	{
Qiang Xue committed
71
		$connection = $this->getConnection(false);
Qiang Xue committed
72 73 74
		$this->assertEquals('`column`', $connection->quoteColumnName('column'));
		$this->assertEquals('`column`', $connection->quoteColumnName('`column`'));
		$this->assertEquals('`table`.`column`', $connection->quoteColumnName('table.column'));
75 76 77 78
		$this->assertEquals('`table`.`column`', $connection->quoteColumnName('table.`column`'));
		$this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]'));
		$this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}'));
		$this->assertEquals('(column)', $connection->quoteColumnName('(column)'));
Qiang Xue committed
79
	}
w  
Qiang Xue committed
80
}