SQLServerPlatformTest.php 7.1 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Platforms;

5
use Doctrine\DBAL\Platforms\SQLServer2008Platform;
6 7
use Doctrine\DBAL\Types\Type;

8
class SQLServerPlatformTest extends AbstractPlatformTestCase
9
{
10
    public function createPlatform()
11
    {
12
        return new SQLServer2008Platform;
13 14
    }

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

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

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

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

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

65
    public function testGeneratesDDLSnippets()
66
    {
67
        $dropDatabaseExpectation = 'DROP DATABASE foobar';
68

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

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

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

111
    public function testPrefersIdentityColumns()
112 113
    {
        $this->assertTrue($this->_platform->prefersIdentityColumns());
114 115 116 117
    }

    public function testSupportsIdentityColumns()
    {
118 119 120
        $this->assertTrue($this->_platform->supportsIdentityColumns());
    }

121 122
    public function testDoesNotSupportSavePoints()
    {
123
        $this->assertTrue($this->_platform->supportsSavepoints());
124 125
    }

126
    public function getGenerateIndexSql()
127
    {
128
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
129 130
    }

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

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

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

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

    public function testModifyLimitQueryWithOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10, 5);
156
        $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);
157 158 159 160 161
    }

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

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

171 172 173
    /**
     * @group DDC-1360
     */
174 175 176
    public function testQuoteIdentifier()
    {
        $this->assertEquals('[fo][o]', $this->_platform->quoteIdentifier('fo]o'));
177 178
        $this->assertEquals('[test]', $this->_platform->quoteIdentifier('test'));
        $this->assertEquals('[test].[test]', $this->_platform->quoteIdentifier('test.test'));
179
    }
180 181 182 183 184 185 186 187 188 189 190

    /**
     * @group DDC-1360
     */
    public function testQuoteSingleIdentifier()
    {
        $this->assertEquals('[fo][o]', $this->_platform->quoteSingleIdentifier('fo]o'));
        $this->assertEquals('[test]', $this->_platform->quoteSingleIdentifier('test'));
        $this->assertEquals('[test.test]', $this->_platform->quoteSingleIdentifier('test.test'));
    }
}