ConnectionTest.php 2.71 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
{
Qiang Xue committed
9 10 11 12 13 14
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

Alexander Makarov committed
15
	public function testConstruct()
w  
Qiang Xue committed
16
	{
Qiang Xue committed
17
		$connection = $this->getConnection(false);
18
		$params = $this->database;
Qiang Xue committed
19

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

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

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

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

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

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

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

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

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

Alexander Makarov committed
71
	public function testQuoteColumnName()
Qiang Xue committed
72
	{
Qiang Xue committed
73
		$connection = $this->getConnection(false);
Qiang Xue committed
74 75 76
		$this->assertEquals('`column`', $connection->quoteColumnName('column'));
		$this->assertEquals('`column`', $connection->quoteColumnName('`column`'));
		$this->assertEquals('`table`.`column`', $connection->quoteColumnName('table.column'));
77 78 79 80
		$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
81
	}
w  
Qiang Xue committed
82
}