2009-05-03 22:07:57 +04:00
< ? php
namespace Doctrine\Tests\DBAL\Platforms ;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform ;
use Doctrine\DBAL\Types\Type ;
use Doctrine\DBAL\Connection ;
require_once __DIR__ . '/../../TestInit.php' ;
2009-12-02 21:52:21 +03:00
class PostgreSqlPlatformTest extends AbstractPlatformTestCase
2009-05-03 22:07:57 +04:00
{
2009-12-02 21:52:21 +03:00
public function createPlatform ()
2009-05-03 22:07:57 +04:00
{
2009-12-02 21:52:21 +03:00
return new PostgreSqlPlatform ;
2009-05-03 22:07:57 +04:00
}
2009-12-02 21:52:21 +03:00
public function getGenerateTableSql ()
2009-05-03 22:07:57 +04:00
{
2009-12-02 21:52:21 +03:00
return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))' ;
2009-05-03 22:07:57 +04:00
}
2009-07-07 16:00:22 +04:00
public function testGeneratesTableAlterationSqlForAddingAndRenaming ()
2009-05-03 22:07:57 +04:00
{
$changes = array (
'name' => 'userlist' ,
'add' => array (
'quota' => array (
'type' => Type :: getType ( 'integer' )
)
));
$sql = $this -> _platform -> getAlterTableSql ( 'mytable' , $changes );
$this -> assertEquals (
'ALTER TABLE mytable ADD quota INT DEFAULT NULL' ,
$sql [ 0 ]
);
$this -> assertEquals (
'ALTER TABLE mytable RENAME TO userlist' ,
$sql [ 1 ]
);
}
2009-12-02 21:52:21 +03:00
public function getGenerateIndexSql ()
2009-05-03 22:07:57 +04:00
{
2009-12-02 21:52:21 +03:00
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)' ;
}
2009-05-03 22:07:57 +04:00
2009-12-02 21:52:21 +03:00
public function getGenerateForeignKeySql ()
{
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id) NOT DEFERRABLE INITIALLY IMMEDIATE' ;
2009-05-03 22:07:57 +04:00
}
2009-07-11 12:48:57 +04:00
public function testGeneratesForeignKeySqlForNonStandardOptions ()
{
2009-12-02 21:52:21 +03:00
$foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint (
array ( 'foreign_id' ), 'my_table' , array ( 'id' ), 'my_fk' , array ( 'onDelete' => 'CASCADE' )
2009-07-11 12:48:57 +04:00
);
$this -> assertEquals (
2009-12-02 21:52:21 +03:00
" CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table(id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE " ,
$this -> _platform -> getForeignKeyDeclarationSql ( $foreignKey )
2009-07-11 12:48:57 +04:00
);
}
2009-07-07 16:00:22 +04:00
public function testGeneratesSqlSnippets ()
{
$this -> assertEquals ( 'SIMILAR TO' , $this -> _platform -> getRegexpExpression (), 'Regular expression operator is not correct' );
$this -> assertEquals ( '"' , $this -> _platform -> getIdentifierQuoteCharacter (), 'Identifier quote character is not correct' );
$this -> assertEquals ( 'RANDOM()' , $this -> _platform -> getRandomExpression (), 'Random function is not correct' );
$this -> assertEquals ( 'column1 || column2 || column3' , $this -> _platform -> getConcatExpression ( 'column1' , 'column2' , 'column3' ), 'Concatenation expression is not correct' );
2009-07-11 12:48:57 +04:00
$this -> assertEquals ( 'SUBSTR(column, 5)' , $this -> _platform -> getSubstringExpression ( 'column' , 5 ), 'Substring expression without length is not correct' );
$this -> assertEquals ( 'SUBSTR(column, 0, 5)' , $this -> _platform -> getSubstringExpression ( 'column' , 0 , 5 ), 'Substring expression with length is not correct' );
2009-07-07 16:00:22 +04:00
}
public function testGeneratesTransactionCommands ()
2009-05-03 22:07:57 +04:00
{
$this -> assertEquals (
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED' ,
$this -> _platform -> getSetTransactionIsolationSql ( Connection :: TRANSACTION_READ_UNCOMMITTED )
);
$this -> assertEquals (
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED' ,
$this -> _platform -> getSetTransactionIsolationSql ( Connection :: TRANSACTION_READ_COMMITTED )
);
$this -> assertEquals (
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ' ,
$this -> _platform -> getSetTransactionIsolationSql ( Connection :: TRANSACTION_REPEATABLE_READ )
);
$this -> assertEquals (
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE' ,
$this -> _platform -> getSetTransactionIsolationSql ( Connection :: TRANSACTION_SERIALIZABLE )
);
}
2009-07-07 16:00:22 +04:00
public function testGeneratesDDLSnippets ()
2009-05-03 22:07:57 +04:00
{
$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-03 22:07:57 +04:00
{
$this -> assertEquals (
'INT' ,
$this -> _platform -> getIntegerTypeDeclarationSql ( array ())
);
$this -> assertEquals (
'SERIAL' ,
$this -> _platform -> getIntegerTypeDeclarationSql ( array ( 'autoincrement' => true )
));
$this -> assertEquals (
'SERIAL' ,
$this -> _platform -> getIntegerTypeDeclarationSql (
array ( 'autoincrement' => true , 'primary' => true )
));
2009-07-07 16:00:22 +04:00
}
public function testGeneratesTypeDeclarationForStrings ()
{
2009-05-03 22:07:57 +04:00
$this -> assertEquals (
'CHAR(10)' ,
$this -> _platform -> getVarcharTypeDeclarationSql (
2009-07-07 16:00:22 +04:00
array ( 'length' => 10 , 'fixed' => true ))
);
2009-05-03 22:07:57 +04:00
$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-03 22:07:57 +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-03 22:07:57 +04:00
);
}
2009-12-02 21:52:21 +03:00
public function getGenerateUniqueIndexSql ()
{
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)' ;
}
2009-07-07 16:00:22 +04:00
public function testGeneratesSequenceSqlCommands ()
2009-05-03 22:07:57 +04:00
{
$this -> assertEquals (
'CREATE SEQUENCE myseq INCREMENT BY 20 START 1' ,
$this -> _platform -> getCreateSequenceSql ( 'myseq' , 1 , 20 )
);
$this -> assertEquals (
'DROP SEQUENCE myseq' ,
$this -> _platform -> getDropSequenceSql ( 'myseq' )
);
$this -> assertEquals (
" SELECT NEXTVAL('myseq') " ,
$this -> _platform -> getSequenceNextValSql ( 'myseq' )
);
}
2009-07-07 16:00:22 +04:00
public function testDoesNotPreferIdentityColumns ()
2009-05-03 22:07:57 +04:00
{
$this -> assertFalse ( $this -> _platform -> prefersIdentityColumns ());
2009-07-07 16:00:22 +04:00
}
public function testPrefersSequences ()
{
2009-05-03 22:07:57 +04:00
$this -> assertTrue ( $this -> _platform -> prefersSequences ());
2009-07-07 16:00:22 +04:00
}
public function testSupportsIdentityColumns ()
{
2009-05-03 22:07:57 +04:00
$this -> assertTrue ( $this -> _platform -> supportsIdentityColumns ());
2009-07-07 16:00:22 +04:00
}
public function testSupportsSavePoints ()
{
2009-05-03 22:07:57 +04:00
$this -> assertTrue ( $this -> _platform -> supportsSavepoints ());
2009-07-07 16:00:22 +04:00
}
public function testSupportsSequences ()
{
2009-05-03 22:07:57 +04:00
$this -> assertTrue ( $this -> _platform -> supportsSequences ());
}
2009-07-14 02:59:36 +04:00
public function testModifyLimitQuery ()
{
$sql = $this -> _platform -> modifyLimitQuery ( 'SELECT * FROM user' , 10 , 0 );
2009-10-12 23:10:41 +04:00
$this -> assertEquals ( 'SELECT * FROM user LIMIT 10 OFFSET 0' , $sql );
2009-07-14 02:59:36 +04:00
}
public function testModifyLimitQueryWithEmptyOffset ()
{
$sql = $this -> _platform -> modifyLimitQuery ( 'SELECT * FROM user' , 10 );
$this -> assertEquals ( 'SELECT * FROM user LIMIT 10' , $sql );
}
}