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

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

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

Qiang Xue committed
7
class ConnectionTest extends \yiiunit\MysqlTestCase
w  
Qiang Xue committed
8 9 10
{
	function testConstruct()
	{
Qiang Xue committed
11
		$connection = $this->getConnection(false);
w  
Qiang Xue committed
12
		$params = $this->getParam('mysql');
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()
	{
Qiang Xue committed
21
		$connection = $this->getConnection(false);
Qiang Xue committed
22

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

		$connection->open();
		$this->assertTrue($connection->active);
Qiang Xue committed
28
		$this->assertTrue($connection->pdo instanceof \PDO);
w  
Qiang Xue committed
29 30 31 32 33

		$connection->close();
		$this->assertFalse($connection->active);
		$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()
	{
Qiang Xue committed
42
		$connection = $this->getConnection(false);
Qiang Xue committed
43 44 45 46 47 48
		$this->assertEquals('mysql', $connection->driverName);
		$this->assertFalse($connection->active);
	}

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

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

	function testQuoteColumnName()
	{
Qiang Xue committed
66
		$connection = $this->getConnection(false);
Qiang Xue committed
67 68 69
		$this->assertEquals('`column`', $connection->quoteColumnName('column'));
		$this->assertEquals('`column`', $connection->quoteColumnName('`column`'));
		$this->assertEquals('`table`.`column`', $connection->quoteColumnName('table.column'));
Qiang Xue committed
70
		$this->assertEquals('`table.column`', $connection->quoteColumnName('table.column', true));
Qiang Xue committed
71 72 73 74
	}

	function testGetPdoType()
	{
Qiang Xue committed
75
		$connection = $this->getConnection(false);
Qiang Xue committed
76 77 78 79 80 81 82 83 84 85
		$this->assertEquals(\PDO::PARAM_BOOL, $connection->getPdoType('boolean'));
		$this->assertEquals(\PDO::PARAM_INT, $connection->getPdoType('integer'));
		$this->assertEquals(\PDO::PARAM_STR, $connection->getPdoType('string'));
		$this->assertEquals(\PDO::PARAM_NULL, $connection->getPdoType('NULL'));
	}

	function testAttribute()
	{

	}
w  
Qiang Xue committed
86
}