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

namespace Doctrine\Tests\DBAL\Platforms;

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

require_once __DIR__ . '/../../TestInit.php';
9

10
class MsSqlPlatformTest extends AbstractPlatformTestCase
11
{
12

13
    public function createPlatform()
14
    {
15
        return new MsSqlPlatform;
16 17
    }

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

23 24 25
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
    {
        return array(
26
            'CREATE TABLE test (foo NVARCHAR(255) DEFAULT NULL, bar NVARCHAR(255) DEFAULT NULL)',
27
            'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar) WHERE foo IS NOT NULL AND bar IS NOT NULL'
28 29 30
        );
    }

31
    public function getGenerateAlterTableSql()
32
    {
33
        return array(
34
            'ALTER TABLE mytable RENAME TO userlist',
35 36
            'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
            'ALTER TABLE mytable DROP COLUMN foo',
37
            'ALTER TABLE mytable CHANGE bar baz NVARCHAR(255) DEFAULT \'def\' NOT NULL',
38 39 40
        );
    }

41
    public function testGeneratesSqlSnippets()
42
    {
43
        $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
44
        $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
45
        $this->assertEquals('(column1 + column2 + column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
46 47
    }

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

68
    public function testGeneratesDDLSnippets()
69
    {
70
        $dropDatabaseExpectation = 'DROP DATABASE foobar';
71

72 73
        $this->assertEquals('SHOW DATABASES', $this->_platform->getShowDatabasesSQL());
        $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
74
        $this->assertEquals($dropDatabaseExpectation, $this->_platform->getDropDatabaseSQL('foobar'));
75
        $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
76 77
    }

78
    public function testGeneratesTypeDeclarationForIntegers()
79 80
    {
        $this->assertEquals(
81 82
                'INT',
                $this->_platform->getIntegerTypeDeclarationSQL(array())
83 84
        );
        $this->assertEquals(
85 86
                'INT IDENTITY',
                $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
87 88
        ));
        $this->assertEquals(
89 90 91
                'INT IDENTITY',
                $this->_platform->getIntegerTypeDeclarationSQL(
                        array('autoincrement' => true, 'primary' => true)
92
        ));
93 94 95 96
    }

    public function testGeneratesTypeDeclarationsForStrings()
    {
97
        $this->assertEquals(
98 99 100
                'NCHAR(10)',
                $this->_platform->getVarcharTypeDeclarationSQL(
                        array('length' => 10, 'fixed' => true)
101 102
        ));
        $this->assertEquals(
103 104 105
                'NVARCHAR(50)',
                $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
                'Variable string declaration is not correct'
106 107
        );
        $this->assertEquals(
108
                'NVARCHAR(255)',
109 110
                $this->_platform->getVarcharTypeDeclarationSQL(array()),
                'Long string declaration is not correct'
111 112 113
        );
    }

114
    public function testPrefersIdentityColumns()
115 116
    {
        $this->assertTrue($this->_platform->prefersIdentityColumns());
117 118 119 120
    }

    public function testSupportsIdentityColumns()
    {
121 122 123
        $this->assertTrue($this->_platform->supportsIdentityColumns());
    }

124 125
    public function testDoesNotSupportSavePoints()
    {
126
        $this->assertTrue($this->_platform->supportsSavepoints());
127 128
    }

129
    public function getGenerateIndexSql()
130
    {
131
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
132 133
    }

134
    public function getGenerateUniqueIndexSql()
135
    {
136
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2) WHERE test IS NOT NULL AND test2 IS NOT NULL';
137 138
    }

139
    public function getGenerateForeignKeySql()
140
    {
141
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
142
    }
143 144 145 146

    public function testModifyLimitQuery()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
Juozas Kaziukenas's avatar
Juozas Kaziukenas committed
147
        $this->assertEquals('SELECT TOP 10 * FROM user', $sql);
148 149 150 151 152
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
Juozas Kaziukenas's avatar
Juozas Kaziukenas committed
153 154 155 156 157 158
        $this->assertEquals('SELECT TOP 10 * FROM user', $sql);
    }

    public function testModifyLimitQueryWithOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10, 5);
159
        $this->assertEquals('SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY username DESC) AS "doctrine_rownum", * FROM user) AS doctrine_tbl WHERE "doctrine_rownum" BETWEEN 6 AND 15', $sql);
160 161 162 163 164
    }

    public function testModifyLimitQueryWithAscOrderBy()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
Juozas Kaziukenas's avatar
Juozas Kaziukenas committed
165
        $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username ASC', $sql);
166 167 168 169 170
    }

    public function testModifyLimitQueryWithDescOrderBy()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10);
Juozas Kaziukenas's avatar
Juozas Kaziukenas committed
171
        $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql);
172
    }
173

174 175 176 177
    public function testQuoteIdentifier()
    {
        $this->assertEquals('[fo][o]', $this->_platform->quoteIdentifier('fo]o'));
    }
178
}