MySqlPlatformTest.php 6.02 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type;

require_once __DIR__ . '/../../TestInit.php';
 
10
class MySqlPlatformTest extends AbstractPlatformTestCase
11
{
12
    public function createPlatform()
13
    {
14
        return new MysqlPlatform;
15 16
    }

17 18 19
    public function testGenerateMixedCaseTableCreate()
    {
        $table = new \Doctrine\DBAL\Schema\Table("Foo");
20
        $table->addColumn("Bar", "integer");
21

22
        $sql = $this->_platform->getCreateTableSQL($table);
23 24 25
        $this->assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) ENGINE = InnoDB', array_shift($sql));
    }

26
    public function getGenerateTableSql()
27
    {
28
        return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB';
29 30
    }

31 32 33
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
    {
        return array(
34
            'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA (foo, bar)) ENGINE = InnoDB'
35 36 37
        );
    }

38
    public function getGenerateAlterTableSql()
39
    {
40
        return array(
41
            "ALTER TABLE mytable RENAME TO userlist, ADD quota INT DEFAULT NULL, DROP foo, CHANGE bar baz VARCHAR(255) DEFAULT 'def' NOT NULL"
42 43 44
        );
    }

45
    public function testGeneratesSqlSnippets()
46
    {
47 48 49
        $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
        $this->assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
        $this->assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
50 51
    }

52
    public function testGeneratesTransactionsCommands()
53 54 55
    {
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
56
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
57
            ''
58 59 60
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
61
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
62 63 64
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
65
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
66 67 68
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
69
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
70 71 72
        );
    }

73 74

    public function testGeneratesDDLSnippets()
75
    {
76 77 78 79
        $this->assertEquals('SHOW DATABASES', $this->_platform->getShowDatabasesSQL());
        $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
        $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
        $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
80 81
    }

82
    public function testGeneratesTypeDeclarationForIntegers()
83 84 85
    {
        $this->assertEquals(
            'INT',
86
            $this->_platform->getIntegerTypeDeclarationSQL(array())
87 88 89
        );
        $this->assertEquals(
            'INT AUTO_INCREMENT',
90
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
91 92 93
        ));
        $this->assertEquals(
            'INT AUTO_INCREMENT',
94
            $this->_platform->getIntegerTypeDeclarationSQL(
95 96
                array('autoincrement' => true, 'primary' => true)
        ));
97 98 99 100
    }

    public function testGeneratesTypeDeclarationForStrings()
    {
101 102
        $this->assertEquals(
            'CHAR(10)',
103
            $this->_platform->getVarcharTypeDeclarationSQL(
104 105 106 107
                array('length' => 10, 'fixed' => true)
        ));
        $this->assertEquals(
            'VARCHAR(50)',
108
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
109
            'Variable string declaration is not correct'
110 111
        );
        $this->assertEquals(
112
            'VARCHAR(255)',
113
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
114
            'Long string declaration is not correct'
115 116 117
        );
    }

118
    public function testPrefersIdentityColumns()
119 120
    {
        $this->assertTrue($this->_platform->prefersIdentityColumns());
121 122 123 124
    }

    public function testSupportsIdentityColumns()
    {
125 126 127
        $this->assertTrue($this->_platform->supportsIdentityColumns());
    }

128
    public function testDoesSupportSavePoints()
129
    {
130
        $this->assertTrue($this->_platform->supportsSavepoints());   
131 132
    }

133
    public function getGenerateIndexSql()
134
    {
135
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
136 137
    }

138
    public function getGenerateUniqueIndexSql()
139
    {
140
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
141 142
    }

143
    public function getGenerateForeignKeySql()
144
    {
145
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
146
    }
147 148 149 150

    public function testModifyLimitQuery()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
151
        $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
152 153 154 155 156 157 158
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
        $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
    }
159 160 161 162 163 164

    /**
     * @group DDC-118
     */
    public function testGetDateTimeTypeDeclarationSql()
    {
165 166 167
        $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => false)));
        $this->assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
        $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
168
    }
169
}