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

namespace Doctrine\Tests\DBAL\Platforms;

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

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

17
    public function getGenerateTableSql()
18
    {
19
        return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
20 21
    }

22 23 24 25 26 27 28
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
    {
        return array(
            'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX test_foo_bar_uniq (foo, bar))'
        );
    }

29
    public function getGenerateAlterTableSql()
30
    {
31
        return array(
32
            "ALTER TABLE mytable RENAME TO userlist, ADD quota INT DEFAULT NULL, DROP foo, CHANGE bar baz VARCHAR(255) DEFAULT 'def' NOT NULL"
33 34 35
        );
    }

36
    public function testGeneratesSqlSnippets()
37
    {
38
        $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
39
        $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
40
        $this->assertEquals('(column1 + column2 + column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
41 42
    }

43
    public function testGeneratesTransactionsCommands()
44 45 46
    {
        $this->assertEquals(
            'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
47
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_UNCOMMITTED)
48 49 50
        );
        $this->assertEquals(
            'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
51
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_COMMITTED)
52 53 54
        );
        $this->assertEquals(
            'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ',
55
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::REPEATABLE_READ)
56 57 58
        );
        $this->assertEquals(
            'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
59
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::SERIALIZABLE)
60 61 62
        );
    }

63
    public function testGeneratesDDLSnippets()
64
    {
65 66 67 68
        $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'));
69 70
    }

71
    public function testGeneratesTypeDeclarationForIntegers()
72 73 74
    {
        $this->assertEquals(
            'INT',
75
            $this->_platform->getIntegerTypeDeclarationSQL(array())
76 77 78
        );
        $this->assertEquals(
            'INT AUTO_INCREMENT',
79
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
80 81 82
        ));
        $this->assertEquals(
            'INT AUTO_INCREMENT',
83
            $this->_platform->getIntegerTypeDeclarationSQL(
84 85
                array('autoincrement' => true, 'primary' => true)
        ));
86 87 88 89
    }

    public function testGeneratesTypeDeclarationsForStrings()
    {
90 91
        $this->assertEquals(
            'CHAR(10)',
92
            $this->_platform->getVarcharTypeDeclarationSQL(
93 94 95 96
                array('length' => 10, 'fixed' => true)
        ));
        $this->assertEquals(
            'VARCHAR(50)',
97
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
98
            'Variable string declaration is not correct'
99 100 101
        );
        $this->assertEquals(
            'TEXT',
102
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
103
            'Long string declaration is not correct'
104 105 106
        );
    }

107
    public function testPrefersIdentityColumns()
108 109
    {
        $this->assertTrue($this->_platform->prefersIdentityColumns());
110 111 112 113
    }

    public function testSupportsIdentityColumns()
    {
114 115 116
        $this->assertTrue($this->_platform->supportsIdentityColumns());
    }

117 118 119 120 121
    public function testDoesNotSupportSavePoints()
    {
        $this->assertFalse($this->_platform->supportsSavepoints());   
    }

122
    public function getGenerateIndexSql()
123
    {
124
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
125 126
    }

127
    public function getGenerateUniqueIndexSql()
128
    {
129
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
130 131
    }

132
    public function getGenerateForeignKeySql()
133
    {
134
        return  'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
135
    }
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

    public function testModifyLimitQuery()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
        $this->assertEquals('SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 10 * FROM user) AS inner_tbl) AS outer_tbl', $sql);
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
        $this->assertEquals('SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 10 * FROM user) AS inner_tbl) AS outer_tbl', $sql);
    }

    public function testModifyLimitQueryWithAscOrderBy()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
        $this->assertEquals('SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 10 * FROM user ORDER BY username ASC) AS inner_tbl ORDER BY inner_tbl.u DESC) AS outer_tbl ORDER BY outer_tbl.u ASC', $sql);
    }

    public function testModifyLimitQueryWithDescOrderBy()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10);
        $this->assertEquals('SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 10 * FROM user ORDER BY username DESC) AS inner_tbl ORDER BY inner_tbl.u ASC) AS outer_tbl ORDER BY outer_tbl.u DESC', $sql);
    }
}