ConnectionTest.php 2.58 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

Alexander Makarov committed
7
class ConnectionTest extends DatabaseTestCase
w  
Qiang Xue committed
8 9 10
{
	function testConstruct()
	{
Qiang Xue committed
11
		$connection = $this->getConnection(false);
12
		$params = $this->database;
Qiang Xue committed
13

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

	function testOpenClose()
	{
21
		$connection = $this->getConnection(false, false);
Qiang Xue committed
22

23
		$this->assertFalse($connection->isActive);
w  
Qiang Xue committed
24 25 26
		$this->assertEquals(null, $connection->pdo);

		$connection->open();
27
		$this->assertTrue($connection->isActive);
Qiang Xue committed
28
		$this->assertTrue($connection->pdo instanceof \PDO);
w  
Qiang Xue committed
29 30

		$connection->close();
31
		$this->assertFalse($connection->isActive);
w  
Qiang Xue committed
32 33
		$this->assertEquals(null, $connection->pdo);

Qiang Xue committed
34 35
		$connection = new Connection;
		$connection->dsn = 'unknown::memory:';
w  
Qiang Xue committed
36 37 38 39
		$this->setExpectedException('yii\db\Exception');
		$connection->open();
	}

Qiang Xue committed
40 41
	function testGetDriverName()
	{
42 43
		$connection = $this->getConnection(false, false);
		$this->assertEquals($this->driverName, $connection->driverName);
Qiang Xue committed
44 45 46 47
	}

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

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

	function testQuoteColumnName()
	{
Qiang Xue committed
67
		$connection = $this->getConnection(false);
Qiang Xue committed
68 69 70
		$this->assertEquals('`column`', $connection->quoteColumnName('column'));
		$this->assertEquals('`column`', $connection->quoteColumnName('`column`'));
		$this->assertEquals('`table`.`column`', $connection->quoteColumnName('table.column'));
71 72 73 74
		$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
75
	}
w  
Qiang Xue committed
76
}