MySqlPlatformTest.php 7.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

namespace Doctrine\Tests\DBAL\Platforms;

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

require_once __DIR__ . '/../../TestInit.php';
 
class MySqlPlatformTest extends \Doctrine\Tests\DbalTestCase
{
    private $_platform;

    public function setUp()
    {
        $this->_platform = new MySqlPlatform;
    }

19
    public function testGeneratesTableCreationSql()
20 21 22 23 24 25 26 27 28
    {
        $columns = array(
            'id' => array(
                'type' => Type::getType('integer'),
                'autoincrement' => true,
                'primary' => true,
                'notnull' => true
            ),
            'test' => array(
29
                'type' => Type::getType('string'),
30 31 32 33 34 35 36 37 38 39
                'length' => 255,
                'notnull' => true
            )
        );

        $options = array(
            'primary' => array('id')
        );

        $sql = $this->_platform->getCreateTableSql('test', $columns, $options);
40
        $this->assertEquals('CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB', $sql[0]);
41 42
    }

43
    public function testGeneratesTableAlterationSql()
44 45 46 47 48 49 50 51 52
    {
        $changes = array(
            'name' => 'userlist',
            'add' => array(
                'quota' => array(
                'type' => Type::getType('integer'),
                'unsigned' => 1
                )
            ));
53 54

        $sql = $this->_platform->getAlterTableSql('mytable', $changes);
55 56
        $this->assertEquals(
            'ALTER TABLE mytable RENAME TO userlist, ADD quota INT UNSIGNED DEFAULT NULL',
57
            $sql[0]
58 59 60
        );
    }

61
    public function testGeneratesSqlSnippets()
62
    {
63 64 65 66 67
        $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
        $this->assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
        $this->assertEquals('RAND()', $this->_platform->getRandomExpression(), 'Random function is not correct');
        $this->assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
        $this->assertEquals('CHARACTER SET utf8', $this->_platform->getCharsetFieldDeclaration('utf8'), 'Charset declaration is not correct');
68 69
    }

70
    public function testGeneratesTransactionsCommands()
71 72 73
    {
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
74 75
            $this->_platform->getSetTransactionIsolationSql(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
            ''
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
            $this->_platform->getSetTransactionIsolationSql(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
            $this->_platform->getSetTransactionIsolationSql(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
            $this->_platform->getSetTransactionIsolationSql(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
        );
    }

91 92

    public function testGeneratesDDLSnippets()
93 94 95 96 97 98 99
    {
        $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'));
    }

100
    public function testGeneratesTypeDeclarationForIntegers()
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    {
        $this->assertEquals(
            'INT',
            $this->_platform->getIntegerTypeDeclarationSql(array())
        );
        $this->assertEquals(
            'INT AUTO_INCREMENT',
            $this->_platform->getIntegerTypeDeclarationSql(array('autoincrement' => true)
        ));
        $this->assertEquals(
            'INT AUTO_INCREMENT',
            $this->_platform->getIntegerTypeDeclarationSql(
                array('autoincrement' => true, 'primary' => true)
        ));
115 116 117 118
    }

    public function testGeneratesTypeDeclarationForStrings()
    {
119 120 121 122 123 124 125
        $this->assertEquals(
            'CHAR(10)',
            $this->_platform->getVarcharTypeDeclarationSql(
                array('length' => 10, 'fixed' => true)
        ));
        $this->assertEquals(
            'VARCHAR(50)',
126 127
            $this->_platform->getVarcharTypeDeclarationSql(array('length' => 50)),
            'Variable string declaration is not correct'
128 129
        );
        $this->assertEquals(
130
            'VARCHAR(255)',
131 132
            $this->_platform->getVarcharTypeDeclarationSql(array()),
            'Long string declaration is not correct'
133 134 135
        );
    }

136
    public function testPrefersIdentityColumns()
137 138
    {
        $this->assertTrue($this->_platform->prefersIdentityColumns());
139 140 141 142
    }

    public function testSupportsIdentityColumns()
    {
143 144 145
        $this->assertTrue($this->_platform->supportsIdentityColumns());
    }

146 147 148 149 150 151
    public function testDoesNotSupportSavePoints()
    {
        $this->assertFalse($this->_platform->supportsSavepoints());   
    }

    public function testGeneratesConstraintCreationSql()
152
    {
153
        $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('columns' => array('test' => array())));
154 155 156
        $this->assertEquals($sql, 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)');
    }

157 158 159
    public function testGeneratesIndexCreationSql()
    {
        $indexDef = array(
160
            'columns' => array(
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
                'user_name' => array(
                    'sorting' => 'ASC',
                    'length' => 10
                ),
                'last_login' => array()
            )
        );

        $this->assertEquals(
            'CREATE INDEX my_idx ON mytable (user_name(10) ASC, last_login)',
            $this->_platform->getCreateIndexSql('mytable', 'my_idx', $indexDef)
        );
    }

    public function testGeneratesUniqueIndexCreationSql()
176
    {
177
        $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2')));
178 179 180
        $this->assertEquals($sql, 'CREATE UNIQUE INDEX index_name ON test (test, test2)');
    }

181
    public function testGeneratesForeignKeyCreationSql()
182 183 184 185
    {
        $sql = $this->_platform->getCreateForeignKeySql('test', array('foreignTable' => 'other_table', 'local' => 'fk_name_id', 'foreign' => 'id'));
        $this->assertEquals($sql, 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)');
    }
186 187 188 189

    public function testModifyLimitQuery()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
190
        $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
191 192 193 194 195 196 197
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
        $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
    }
198 199 200 201 202 203 204 205 206 207

    /**
     * @group DDC-118
     */
    public function testGetDateTimeTypeDeclarationSql()
    {
        $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()));
    }
208
}