2009-05-26 22:14:19 +04:00
< ? php
namespace Doctrine\Tests\DBAL\Platforms ;
use Doctrine\DBAL\Platforms\MsSqlPlatform ;
use Doctrine\DBAL\Types\Type ;
require_once __DIR__ . '/../../TestInit.php' ;
class MsSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
{
private $_platform ;
public function setUp ()
{
2009-07-14 02:59:36 +04:00
$this -> _platform = new MsSqlPlatform ;
2009-05-26 22:14:19 +04:00
}
2009-07-07 16:00:22 +04:00
public function testGeneratesTableCreationSql ()
2009-05-26 22:14:19 +04:00
{
$columns = array (
'id' => array (
'type' => Type :: getType ( 'integer' ),
'autoincrement' => true ,
'primary' => true ,
'notnull' => true
),
'test' => array (
'type' => Type :: getType ( 'string' ),
'length' => 255 ,
'notnull' => true
)
);
$options = array (
'primary' => array ( 'id' )
);
$sql = $this -> _platform -> getCreateTableSql ( 'test' , $columns , $options );
$this -> assertEquals ( 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) NOT NULL, PRIMARY KEY(id))' , $sql [ 0 ]);
}
2009-07-07 16:00:22 +04:00
public function testGeneratesTableAlterationSql ()
2009-05-26 22:14:19 +04:00
{
$changes = array (
'name' => 'userlist' ,
'add' => array (
'quota' => array (
'type' => Type :: getType ( 'integer' ),
'unsigned' => 1
)
));
$this -> assertEquals (
'ALTER TABLE mytable RENAME TO userlist, ADD quota INT UNSIGNED DEFAULT NULL' ,
$this -> _platform -> getAlterTableSql ( 'mytable' , $changes )
);
}
2009-07-07 16:00:22 +04:00
public function testGeneratesSqlSnippets ()
2009-05-26 22:14:19 +04:00
{
2009-07-07 16:00:22 +04:00
$this -> assertEquals ( 'RLIKE' , $this -> _platform -> getRegexpExpression (), 'Regular expression operator is not correct' );
2009-08-11 14:51:38 +04:00
$this -> assertEquals ( '"' , $this -> _platform -> getIdentifierQuoteCharacter (), 'Identifier quote character is not correct' );
2009-07-07 16:00:22 +04:00
$this -> assertEquals ( 'RAND()' , $this -> _platform -> getRandomExpression (), 'Random function is not correct' );
$this -> assertEquals ( '(column1 + column2 + column3)' , $this -> _platform -> getConcatExpression ( 'column1' , 'column2' , 'column3' ), 'Concatenation expression is not correct' );
$this -> assertEquals ( 'CHARACTER SET utf8' , $this -> _platform -> getCharsetFieldDeclaration ( 'utf8' ), 'Charset declaration is not correct' );
2009-05-26 22:14:19 +04:00
}
2009-07-07 16:00:22 +04:00
public function testGeneratesTransactionsCommands ()
2009-05-26 22:14:19 +04:00
{
$this -> assertEquals (
'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED' ,
$this -> _platform -> getSetTransactionIsolationSql ( \Doctrine\DBAL\Connection :: TRANSACTION_READ_UNCOMMITTED )
);
$this -> assertEquals (
'SET TRANSACTION ISOLATION LEVEL READ COMMITTED' ,
$this -> _platform -> getSetTransactionIsolationSql ( \Doctrine\DBAL\Connection :: TRANSACTION_READ_COMMITTED )
);
$this -> assertEquals (
'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ' ,
$this -> _platform -> getSetTransactionIsolationSql ( \Doctrine\DBAL\Connection :: TRANSACTION_REPEATABLE_READ )
);
$this -> assertEquals (
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' ,
$this -> _platform -> getSetTransactionIsolationSql ( \Doctrine\DBAL\Connection :: TRANSACTION_SERIALIZABLE )
);
}
2009-07-07 16:00:22 +04:00
public function testGeneratesDDLSnippets ()
2009-05-26 22:14:19 +04:00
{
$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' ));
}
2009-07-07 16:00:22 +04:00
public function testGeneratesTypeDeclarationForIntegers ()
2009-05-26 22:14:19 +04:00
{
$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 )
));
2009-07-07 16:00:22 +04:00
}
public function testGeneratesTypeDeclarationsForStrings ()
{
2009-05-26 22:14:19 +04:00
$this -> assertEquals (
'CHAR(10)' ,
$this -> _platform -> getVarcharTypeDeclarationSql (
array ( 'length' => 10 , 'fixed' => true )
));
$this -> assertEquals (
'VARCHAR(50)' ,
2009-07-07 16:00:22 +04:00
$this -> _platform -> getVarcharTypeDeclarationSql ( array ( 'length' => 50 )),
'Variable string declaration is not correct'
2009-05-26 22:14:19 +04:00
);
$this -> assertEquals (
'TEXT' ,
2009-07-07 16:00:22 +04:00
$this -> _platform -> getVarcharTypeDeclarationSql ( array ()),
'Long string declaration is not correct'
2009-05-26 22:14:19 +04:00
);
}
2009-07-07 16:00:22 +04:00
public function testPrefersIdentityColumns ()
2009-05-26 22:14:19 +04:00
{
$this -> assertTrue ( $this -> _platform -> prefersIdentityColumns ());
2009-07-07 16:00:22 +04:00
}
public function testSupportsIdentityColumns ()
{
2009-05-26 22:14:19 +04:00
$this -> assertTrue ( $this -> _platform -> supportsIdentityColumns ());
}
2009-07-07 16:00:22 +04:00
public function testDoesNotSupportSavePoints ()
{
$this -> assertFalse ( $this -> _platform -> supportsSavepoints ());
}
public function testGeneratesConstraintCreationSql ()
2009-05-26 22:14:19 +04:00
{
2009-10-07 02:38:34 +04:00
$sql = $this -> _platform -> getCreateConstraintSql ( 'test' , 'constraint_name' , array ( 'columns' => array ( 'test' => array ())));
2009-05-26 22:14:19 +04:00
$this -> assertEquals ( $sql , 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)' );
}
2009-07-07 16:00:22 +04:00
public function testGeneratesIndexCreationSql ()
{
$indexDef = array (
2009-10-07 02:38:34 +04:00
'columns' => array (
2009-07-07 16:00:22 +04:00
'user_name' => array (
'sorting' => 'ASC' ,
'length' => 10
),
'last_login' => array ()
)
);
$this -> assertEquals (
'CREATE INDEX my_idx ON mytable (user_name, last_login)' ,
$this -> _platform -> getCreateIndexSql ( 'mytable' , 'my_idx' , $indexDef )
);
}
public function testGeneratesUniqueIndexCreationSql ()
2009-05-26 22:14:19 +04:00
{
2009-10-07 02:38:34 +04:00
$sql = $this -> _platform -> getCreateIndexSql ( 'test' , 'index_name' , array ( 'type' => 'unique' , 'columns' => array ( 'test' , 'test2' )));
2009-05-26 22:14:19 +04:00
$this -> assertEquals ( $sql , 'CREATE UNIQUE INDEX index_name ON test (test, test2)' );
}
2009-07-07 16:00:22 +04:00
public function testGeneratesForeignKeyCreationSql ()
2009-05-26 22:14:19 +04:00
{
$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)' );
}
2009-07-14 02:59:36 +04:00
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 );
}
}