ConnectionTest.php 2.94 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
{
13 14 15 16
    public function testConstruct()
    {
        $connection = $this->getConnection(false);
        $params = $this->database;
Qiang Xue committed
17

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

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

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

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

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

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

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

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

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

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