Commit d34a05a2 authored by jwage's avatar jwage

[2.0] Oracle SchemaManager tests and general refactoring of tests

parent 92b395cf
...@@ -83,6 +83,6 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -83,6 +83,6 @@ class Driver implements \Doctrine\DBAL\Driver
public function getDatabase(\Doctrine\DBAL\Connection $conn) public function getDatabase(\Doctrine\DBAL\Connection $conn)
{ {
$params = $conn->getParams(); $params = $conn->getParams();
return $params['dbname']; return $params['user'];
} }
} }
\ No newline at end of file
...@@ -520,16 +520,16 @@ abstract class AbstractPlatform ...@@ -520,16 +520,16 @@ abstract class AbstractPlatform
public function getDropConstraintSql($table, $name, $primary = false) public function getDropConstraintSql($table, $name, $primary = false)
{ {
$table = $this->_conn->getDatabasePlatform()->quoteIdentifier($table); $table = $this->quoteIdentifier($table);
$name = $this->_conn->getDatabasePlatform()->quoteIdentifier($name); $name = $this->quoteIdentifier($name);
return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name; return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name;
} }
public function getDropForeignKeySql($table, $name) public function getDropForeignKeySql($table, $name)
{ {
$table = $this->_conn->getDatabasePlatform()->quoteIdentifier($table); $table = $this->quoteIdentifier($table);
$name = $this->_conn->getDatabasePlatform()->quoteIdentifier($name); $name = $this->quoteIdentifier($name);
return 'ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name; return 'ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name;
} }
......
...@@ -101,18 +101,14 @@ class OraclePlatform extends AbstractPlatform ...@@ -101,18 +101,14 @@ class OraclePlatform extends AbstractPlatform
return 'SYS_GUID()'; return 'SYS_GUID()';
} }
/**
* {@inheritdoc}
*
* @return string
* @override
*/
public function getCreateSequenceSql($sequenceName, $start = 1, $allocationSize = 1) public function getCreateSequenceSql($sequenceName, $start = 1, $allocationSize = 1)
{ {
return 'CREATE SEQUENCE ' . $this->quoteIdentifier($sequenceName) $query = 'CREATE SEQUENCE ' . $this->quoteIdentifier($sequenceName) . ' START WITH ' . $start . ' INCREMENT BY 1 NOCACHE';
. ' START WITH ' . $start . ' INCREMENT BY ' . $allocationSize; $query .= ($start < 1 ? ' MINVALUE ' . $start : '');
return $query;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
...@@ -204,6 +200,228 @@ class OraclePlatform extends AbstractPlatform ...@@ -204,6 +200,228 @@ class OraclePlatform extends AbstractPlatform
: ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)'); : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
} }
public function getListDatabasesSql()
{
return 'SELECT username FROM sys.dba_users';
}
public function getListFunctionsSql()
{
return "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
}
public function getCreateTableSql($table, array $columns, array $options = array())
{
$indexes = isset($options['indexes']) ? $options['indexes']:array();
$options['indexes'] = array();
$sql = parent::getCreateTableSql($table, $columns, $options);
foreach ($columns as $name => $column) {
if (isset($column['sequence'])) {
$sql[] = $this->getCreateSequenceSql($column['sequence'], 1);
}
if (isset($column['autoincrement']) && $column['autoincrement'] ||
(isset($column['autoinc']) && $column['autoinc'])) {
$sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
}
}
if (isset($indexes) && ! empty($indexes)) {
foreach ($indexes as $indexName => $definition) {
// create nonunique indexes, as they are a part od CREATE TABLE DDL
if ( ! isset($definition['type']) ||
(isset($definition['type']) && strtolower($definition['type']) != 'unique')) {
$sql[] = $this->getCreateIndexSql($table, $indexName, $definition);
}
}
}
return $sql;
}
public function getListTableIndexesSql($table)
{
return "SELECT * FROM user_indexes"
. " WHERE table_name = '" . strtoupper($table) . "'";
}
public function getListTablesSql()
{
return 'SELECT * FROM sys.user_tables';
}
public function getListUsersSql()
{
return 'SELECT * FROM sys.dba_users';
}
public function getListViewsSql()
{
return 'SELECT view_name FROM sys.user_views';
}
public function getCreateViewSql($name, $sql)
{
return 'CREATE VIEW ' . $name . ' AS ' . $sql;
}
public function getDropViewSql($name)
{
return 'DROP VIEW '. $name;
}
public function getCreateAutoincrementSql($name, $table, $start = 1)
{
$table = strtoupper($table);
$sql = array();
$indexName = $table . '_AI_PK';
$definition = array(
'primary' => true,
'fields' => array($name => true),
);
$sql[] = 'DECLARE
constraints_Count NUMBER;
BEGIN
SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \''.$table.'\' AND CONSTRAINT_TYPE = \'P\';
IF constraints_Count = 0 OR constraints_Count = \'\' THEN
EXECUTE IMMEDIATE \''.$this->getCreateConstraintSql($table, $indexName, $definition).'\';
END IF;
END;';
$sequenceName = $table . '_SEQ';
$sql[] = $this->getCreateSequenceSql($sequenceName, $start);
$triggerName = $this->quoteIdentifier($table . '_AI_PK', true);
$table = $this->quoteIdentifier($table, true);
$name = $this->quoteIdentifier($name, true);
$sql[] = 'CREATE TRIGGER ' . $triggerName . '
BEFORE INSERT
ON ' . $table . '
FOR EACH ROW
DECLARE
last_Sequence NUMBER;
last_InsertID NUMBER;
BEGIN
SELECT ' . $this->quoteIdentifier($sequenceName) . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
IF (:NEW.' . $name . ' IS NULL OR :NEW.'.$name.' = 0) THEN
SELECT ' . $this->quoteIdentifier($sequenceName) . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
ELSE
SELECT NVL(Last_Number, 0) INTO last_Sequence
FROM User_Sequences
WHERE Sequence_Name = \'' . $sequenceName . '\';
SELECT :NEW.' . $name . ' INTO last_InsertID FROM DUAL;
WHILE (last_InsertID > last_Sequence) LOOP
SELECT ' . $this->quoteIdentifier($sequenceName) . '.NEXTVAL INTO last_Sequence FROM DUAL;
END LOOP;
END IF;
END;';
return $sql;
}
public function getDropAutoincrementSql($table)
{
$table = strtoupper($table);
$trigger = $table . '_AI_PK';
if ($trigger) {
$sql[] = 'DROP TRIGGER ' . $trigger;
$sql[] = $this->getDropSequenceSql($table.'_SEQ');
$indexName = $table . '_AI_PK';
$sql[] = $this->getDropConstraintSql($table, $indexName);
}
return $sql;
}
public function getListTableConstraintsSql($table)
{
$table = strtoupper($table);
return 'SELECT * FROM user_constraints WHERE table_name = \'' . $table . '\'';
}
public function getListTableColumnsSql($table)
{
$table = strtoupper($table);
return "SELECT * FROM all_tab_columns WHERE table_name = '" . $table . "' ORDER BY column_name";
}
public function getDropSequenceSql($sequenceName)
{
return 'DROP SEQUENCE ' . $this->quoteIdentifier($sequenceName);
}
public function getDropDatabaseSql($database)
{
return 'DROP USER ' . $database . ' CASCADE';
}
public function getAlterTableSql($name, array $changes, $check = false)
{
if ( ! $name) {
throw DoctrineException::updateMe('no valid table name specified');
}
foreach ($changes as $changeName => $change) {
switch ($changeName) {
case 'add':
case 'remove':
case 'change':
case 'name':
case 'rename':
break;
default:
throw \Doctrine\Common\DoctrineException::updateMe('change type "' . $changeName . '" not yet supported');
}
}
if ($check) {
return false;
}
$name = $this->quoteIdentifier($name);
if ( ! empty($changes['add']) && is_array($changes['add'])) {
$fields = array();
foreach ($changes['add'] as $fieldName => $field) {
$fields[] = $this->getColumnDeclarationSql($fieldName, $field);
}
$sql[] = 'ALTER TABLE ' . $name . ' ADD (' . implode(', ', $fields) . ')';
}
if ( ! empty($changes['change']) && is_array($changes['change'])) {
$fields = array();
foreach ($changes['change'] as $fieldName => $field) {
$fields[] = $fieldName. ' ' . $this->getColumnDeclarationSql('', $field['definition']);
}
$sql[] = 'ALTER TABLE ' . $name . ' MODIFY (' . implode(', ', $fields) . ')';
}
if ( ! empty($changes['rename']) && is_array($changes['rename'])) {
foreach ($changes['rename'] as $fieldName => $field) {
$sql[] = 'ALTER TABLE ' . $name . ' RENAME COLUMN ' . $this->quoteIdentifier($fieldName)
. ' TO ' . $this->quoteIdentifier($field['name']);
}
}
if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
$fields = array();
foreach ($changes['remove'] as $fieldName => $field) {
$fields[] = $this->quoteIdentifier($fieldName);
}
$sql[] = 'ALTER TABLE ' . $name . ' DROP COLUMN ' . implode(', ', $fields);
}
if ( ! empty($changes['name'])) {
$changeName = $this->quoteIdentifier($changes['name']);
$sql[] = 'ALTER TABLE ' . $name . ' RENAME TO ' . $changeName;
}
return $sql;
}
/** /**
* Whether the platform prefers sequences for ID generation. * Whether the platform prefers sequences for ID generation.
* *
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use \Doctrine\DBAL\Types; use \Doctrine\DBAL\Types;
use \Doctrine\Common\DoctrineException;
/** /**
* Base class for schema managers. Schema managers are used to inspect and/or * Base class for schema managers. Schema managers are used to inspect and/or
...@@ -865,4 +866,61 @@ abstract class AbstractSchemaManager ...@@ -865,4 +866,61 @@ abstract class AbstractSchemaManager
} }
return $result; return $result;
} }
public function tryMethod()
{
$args = func_get_args();
$method = $args[0];
unset($args[0]);
$args = array_values($args);
try {
return call_user_func_array(array($this, $method), $args);
} catch (\Exception $e) {
return false;
}
}
private function _handleDropAndCreate($method, $arguments)
{
if (substr($method, 0, 13) == 'dropAndCreate') {
$base = substr($method, 13, strlen($method));
$dropMethod = 'drop' . $base;
$createMethod = 'create' . $base;
call_user_func_array(array($this, 'tryMethod'),
array_merge(array($dropMethod), $arguments));
call_user_func_array(array($this, 'tryMethod'),
array_merge(array($createMethod), $arguments));
return true;
}
return false;
}
private function _handleTryMethod($method, $arguments)
{
if (substr($method, 0, 3) == 'try') {
$method = substr($method, 3, strlen($method));
$method = strtolower($method[0]).substr($method, 1, strlen($method));
return call_user_func_array(array($this, 'tryMethod'),
array_merge(array($method), $arguments));
}
}
public function __call($method, $arguments)
{
if ($result = $this->_handleDropAndCreate($method, $arguments)) {
return $result;
}
if ($result = $this->_handleTryMethod($method, $arguments)) {
return $result;
}
throw DoctrineException::updateMe("Invalid method named `" . $method . "` on class `" . __CLASS__ . "`");
}
} }
\ No newline at end of file
...@@ -579,18 +579,6 @@ class FirebirdSchemaManager extends AbstractSchemaManager ...@@ -579,18 +579,6 @@ class FirebirdSchemaManager extends AbstractSchemaManager
return $result; return $result;
} }
/**
* A method to return the required SQL string that fits between CREATE ... TABLE
* to create the table as a temporary table.
*
* @return string The string required to be placed between "CREATE" and "TABLE"
* to generate a temporary table, if possible.
*/
public function getTemporaryTableQuery()
{
return 'GLOBAL TEMPORARY';
}
/** /**
* create sequence * create sequence
* *
......
...@@ -62,16 +62,6 @@ class MsSqlSchemaManager extends AbstractSchemaManager ...@@ -62,16 +62,6 @@ class MsSqlSchemaManager extends AbstractSchemaManager
return $this->conn->standaloneQuery('DROP DATABASE ' . $name, null, true); return $this->conn->standaloneQuery('DROP DATABASE ' . $name, null, true);
} }
/**
* Override the parent method.
*
* @return string The string required to be placed between "CREATE" and "TABLE"
* to generate a temporary table, if possible.
*/
public function getTemporaryTableQuery()
{
return '';
}
/** /**
* alter an existing table * alter an existing table
* *
......
...@@ -34,10 +34,7 @@ class MySqlSchemaManager extends AbstractSchemaManager ...@@ -34,10 +34,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
{ {
protected function _getPortableViewDefinition($view) protected function _getPortableViewDefinition($view)
{ {
return array( return $view['table_name'];
'name' => $view['table_name'],
'sql' => $view['view_definition']
);
} }
protected function _getPortableTableDefinition($table) protected function _getPortableTableDefinition($table)
......
...@@ -53,8 +53,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -53,8 +53,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
protected function _getPortableViewDefinition($view) protected function _getPortableViewDefinition($view)
{ {
return array( return array(
'name' => $view['viewname'], 'name' => $view['viewname']
'sql' => $view['definition']
); );
} }
......
...@@ -36,15 +36,17 @@ class SqliteSchemaManager extends AbstractSchemaManager ...@@ -36,15 +36,17 @@ class SqliteSchemaManager extends AbstractSchemaManager
public function dropDatabase($database = null) public function dropDatabase($database = null)
{ {
if (is_null($database)) { if (is_null($database)) {
$database = $this->_conn->getDriver()->getDatabase($this->_conn); $database = $this->_conn->getDatabase();
}
if (file_exists($database)) {
unlink($database);
} }
unlink($database);
} }
public function createDatabase($database = null) public function createDatabase($database = null)
{ {
if (is_null($database)) { if (is_null($database)) {
$database = $this->_conn->getDriver()->getDatabase($this->_conn); $database = $this->_conn->getDatabase();
} }
// TODO: Can we do this better? // TODO: Can we do this better?
$this->_conn->close(); $this->_conn->close();
......
<?php
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Schema;
require_once __DIR__ . '/../../../TestInit.php';
class OracleSchemaManagerTest extends SchemaManagerFunctionalTest
{
public function testListDatabases()
{
$this->_sm->dropAndCreateDatabase('test_oracle_create_database');
$databases = $this->_sm->listDatabases();
$this->assertEquals(true, in_array('TEST_ORACLE_CREATE_DATABASE', $databases));
}
public function testListFunctions()
{
$functions = $this->_sm->listFunctions();
$this->assertEquals(array(), $functions);
}
public function testListTriggers()
{
return $this->assertUnsupportedMethod('listTriggers');
}
public function testListSequences()
{
$this->createTestTable('list_sequences_test');
$sequences = $this->_sm->listSequences();
$this->assertEquals(true, in_array('LIST_SEQUENCES_TEST_SEQ', $sequences));
}
public function testListTableConstraints()
{
$this->createTestTable('test_constraints');
$tableConstraints = $this->_sm->listTableConstraints('test_constraints');
$this->assertEquals(2, count($tableConstraints));
}
public function testListTableColumns()
{
$this->createTestTable('list_tables_test');
$columns = $this->_sm->listTableColumns('list_tables_test');
$this->assertEquals('ID', $columns[1]['name']);
$this->assertEquals('Doctrine\DBAL\Types\IntegerType', get_class($columns[1]['type']));
$this->assertEquals(22, $columns[1]['length']);
$this->assertEquals(false, $columns[1]['unsigned']);
$this->assertEquals(false, $columns[1]['fixed']);
$this->assertEquals(true, $columns[1]['notnull']);
$this->assertEquals(null, $columns[1]['default']);
$this->assertEquals('TEST', $columns[2]['name']);
$this->assertEquals('Doctrine\DBAL\Types\StringType', get_class($columns[2]['type']));
$this->assertEquals(255, $columns[2]['length']);
$this->assertEquals(false, $columns[2]['unsigned']);
$this->assertEquals(false, $columns[2]['fixed']);
$this->assertEquals(false, $columns[2]['notnull']);
$this->assertEquals(null, $columns[2]['default']);
}
public function testListTableIndexes()
{
$data['options'] = array(
'indexes' => array(
'test_index_name' => array(
'fields' => array(
'test' => array()
),
'type' => 'unique'
)
)
);
$this->createTestTable('list_table_indexes_test', $data);
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
$this->assertEquals(true, is_string($tableIndexes[0]['name']));
$this->assertEquals(true, $tableIndexes[0]['unique']);
}
public function testListTables()
{
$this->createTestTable('list_tables_test');
$tables = $this->_sm->listTables();
$this->assertEquals(true, in_array('LIST_TABLES_TEST', $tables));
}
public function testListUsers()
{
$users = $this->_sm->listUsers();
$this->assertEquals(true, is_array($users));
$params = $this->_conn->getParams();
$testUser = strtoupper($params['user']);
$found = false;
foreach ($users as $user) {
if ($user['user'] == $testUser) {
$found = true;
}
}
$this->assertEquals(true, $found);
}
public function testListViews()
{
$this->_sm->dropAndCreateView('test_create_view', 'SELECT * FROM sys.user_tables');
$views = $this->_sm->listViews();
$view = end($views);
$this->assertEquals('TEST_CREATE_VIEW', $view['name']);
}
public function testListTableForeignKeys()
{
return $this->assertUnsupportedMethod('listTableForeignKeys');
}
public function testRenameTable()
{
$this->_sm->tryDropTable('list_tables_test');
$this->_sm->tryDropTable('list_tables_test_new_name');
$this->createTestTable('list_tables_test');
$this->_sm->renameTable('list_tables_test', 'list_tables_test_new_name');
$tables = $this->_sm->listTables();
$this->assertEquals(true, in_array('LIST_TABLES_TEST_NEW_NAME', $tables));
}
public function testDropAndCreate()
{
$this->_sm->dropAndCreateView('testing_a_new_view', 'SELECT * FROM sys.user_tables');
$this->_sm->dropAndCreateView('testing_a_new_view', 'SELECT * FROM sys.user_tables');
}
}
\ No newline at end of file
...@@ -2,48 +2,22 @@ ...@@ -2,48 +2,22 @@
namespace Doctrine\Tests\DBAL\Functional\Schema; namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\Tests\TestUtil;
use Doctrine\DBAL\Schema; use Doctrine\DBAL\Schema;
use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../../TestInit.php'; require_once __DIR__ . '/../../../TestInit.php';
class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTest
{ {
private $_conn;
protected function setUp()
{
$this->_conn = TestUtil::getConnection();
if ($this->_conn->getDatabasePlatform()->getName() !== 'postgresql')
{
$this->markTestSkipped('The PostgreSQLSchemaTest requires the use of postgresql');
}
$this->_sm = $this->_conn->getSchemaManager();
}
public function testListDatabases() public function testListDatabases()
{ {
try { $this->_sm->dropAndCreateDatabase('test_create_database');
$this->_sm->dropDatabase('test_pgsql_create_database');
} catch (\Exception $e) {}
$this->_sm->createDatabase('test_pgsql_create_database');
$databases = $this->_sm->listDatabases(); $databases = $this->_sm->listDatabases();
$this->assertEquals(true, in_array('test_create_database', $databases));
$this->assertEquals(in_array('test_pgsql_create_database', $databases), true);
} }
public function testListFunctions() public function testListFunctions()
{ {
try { return $this->assertUnsupportedMethod('listFunctions');
$this->_sm->listFunctions();
} catch (\Exception $e) {
return;
}
$this->fail('PostgreSql listFunctions() should throw an exception because it is not supported');
} }
public function testListTriggers() public function testListTriggers()
...@@ -55,83 +29,21 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -55,83 +29,21 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function testListSequences() public function testListSequences()
{ {
$columns = array( $this->createTestTable('list_sequences_test');
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_sequences_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_sequences_test', $columns, $options);
$sequences = $this->_sm->listSequences(); $sequences = $this->_sm->listSequences();
$this->assertEquals(true, in_array('list_sequences_test_id_seq', $sequences)); $this->assertEquals(true, in_array('list_sequences_test_id_seq', $sequences));
} }
public function testListTableConstraints() public function testListTableConstraints()
{ {
$columns = array( $this->createTestTable('list_table_constraints_test');
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_table_constraints_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_table_constraints_test', $columns, $options);
$tableConstraints = $this->_sm->listTableConstraints('list_table_constraints_test'); $tableConstraints = $this->_sm->listTableConstraints('list_table_constraints_test');
$this->assertEquals(array('list_table_constraints_test_pkey'), $tableConstraints); $this->assertEquals(array('list_table_constraints_test_pkey'), $tableConstraints);
} }
public function testListTableColumns() public function testListTableColumns()
{ {
$columns = array( $this->createTestTable('list_tables_test');
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_tables_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_tables_test', $columns, $options);
$columns = $this->_sm->listTableColumns('list_tables_test'); $columns = $this->_sm->listTableColumns('list_tables_test');
$this->assertEquals('id', $columns[0]['name']); $this->assertEquals('id', $columns[0]['name']);
...@@ -155,20 +67,7 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -155,20 +67,7 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function testListTableIndexes() public function testListTableIndexes()
{ {
$columns = array( $data['options'] = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
)
);
$options = array(
'indexes' => array( 'indexes' => array(
'test' => array( 'test' => array(
'fields' => array( 'fields' => array(
...@@ -179,41 +78,15 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -179,41 +78,15 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase
) )
); );
try { $this->createTestTable('list_table_indexes_test', $data);
$this->_sm->dropTable('list_table_indexes_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_table_indexes_test', $columns, $options);
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test'); $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
$this->assertEquals('test', $tableIndexes[0]['name']); $this->assertEquals('test', $tableIndexes[0]['name']);
$this->assertEquals(true, $tableIndexes[0]['unique']); $this->assertEquals(true, $tableIndexes[0]['unique']);
} }
public function testListTables() public function testListTables()
{ {
$columns = array( $this->createTestTable('list_tables_test');
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_tables_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_tables_test', $columns, $options);
$tables = $this->_sm->listTables(); $tables = $this->_sm->listTables();
$this->assertEquals(true, in_array('list_tables_test', $tables)); $this->assertEquals(true, in_array('list_tables_test', $tables));
} }
...@@ -235,11 +108,7 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -235,11 +108,7 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function testListViews() public function testListViews()
{ {
try { $this->_sm->dropAndCreateView('test_create_view', 'SELECT usename, passwd FROM pg_user');
$this->_sm->dropView('test_create_view');
} catch (\Exception $e) {}
$this->_sm->createView('test_create_view', 'SELECT usename, passwd FROM pg_user');
$views = $this->_sm->listViews(); $views = $this->_sm->listViews();
$found = false; $found = false;
...@@ -251,68 +120,26 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -251,68 +120,26 @@ class PostgreSqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
$this->assertEquals(true, $found); $this->assertEquals(true, $found);
$this->assertEquals('SELECT pg_user.usename, pg_user.passwd FROM pg_user;', $view['sql']);
} }
public function testListTableForeignKeys() public function testListTableForeignKeys()
{ {
// Create table that has foreign key $data['options'] = array('type' => 'innodb');
$columns = array( $this->createTestTable('list_table_foreign_keys_test1', $data);
'id' => array( $this->createTestTable('list_table_foreign_keys_test2', $data);
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('integer'),
'length' => 4
)
);
$options = array('type' => 'innodb');
try {
$this->_sm->dropTable('list_table_foreign_keys_test2');
} catch (\Exception $e) {}
$this->_sm->createTable('list_table_foreign_keys_test2', $columns, $options);
// Create the table that is being referenced in the foreign key
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'whatever' => array(
'type' => Type::getType('string'),
'length' => 255
)
);
$options = array('type' => 'innodb');
try {
$this->_sm->dropTable('list_table_foreign_keys_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_table_foreign_keys_test', $columns, $options);
// Create the foreign key between the tables
$definition = array( $definition = array(
'name' => 'testing', 'name' => 'testing',
'local' => 'test', 'local' => 'foreign_key_test',
'foreign' => 'id', 'foreign' => 'id',
'foreignTable' => 'list_table_foreign_keys_test' 'foreignTable' => 'list_table_foreign_keys_test2'
); );
$this->_sm->createForeignKey('list_table_foreign_keys_test2', $definition); $this->_sm->createForeignKey('list_table_foreign_keys_test1', $definition);
$tableForeignKeys = $this->_sm->listTableForeignKeys('list_table_foreign_keys_test2'); $tableForeignKeys = $this->_sm->listTableForeignKeys('list_table_foreign_keys_test1');
$this->assertEquals(1, count($tableForeignKeys)); $this->assertEquals(1, count($tableForeignKeys));
$this->assertEquals('list_table_foreign_keys_test', $tableForeignKeys[0]['table']); $this->assertEquals('list_table_foreign_keys_test2', $tableForeignKeys[0]['table']);
$this->assertEquals('test', $tableForeignKeys[0]['local']); $this->assertEquals('foreign_key_test', $tableForeignKeys[0]['local']);
$this->assertEquals('id', $tableForeignKeys[0]['foreign']); $this->assertEquals('id', $tableForeignKeys[0]['foreign']);
} }
} }
\ No newline at end of file
<?php
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Types\Type;
class SchemaManagerFunctionalTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$class = get_class($this);
$e = explode('\\', $class);
$testClass = end($e);
$dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));
if ($this->_conn->getDatabasePlatform()->getName() !== $dbms)
{
$this->markTestSkipped('The ' . $testClass .' requires the use of ' . $dbms);
}
$this->_sm = $this->_conn->getSchemaManager();
}
public function createTestTable($name = 'test_table', $data = array())
{
if ( ! isset($data['columns'])) {
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
),
'foreign_key_test' => array(
'type' => Type::getType('integer')
)
);
} else {
$columns = $data['columns'];
}
$options = array();
if (isset($data['options'])) {
$options = $data['options'];
}
$this->_sm->dropAndCreateTable($name, $columns, $options);
}
public function assertUnsupportedMethod($method)
{
try {
$this->_sm->$method();
} catch (\Exception $e) {
return;
}
$this->fail($method . '() should throw an exception because it is not supported in ' . $this->_conn->getDatabasePlatform()->getName());
}
}
\ No newline at end of file
...@@ -4,5 +4,12 @@ namespace Doctrine\Tests; ...@@ -4,5 +4,12 @@ namespace Doctrine\Tests;
class DbalFunctionalTestCase extends DbalTestCase class DbalFunctionalTestCase extends DbalTestCase
{ {
protected $_conn;
protected function setUp()
{
if ( ! isset($this->_conn)) {
$this->_conn = TestUtil::getConnection();
}
}
} }
\ No newline at end of file
...@@ -4,4 +4,17 @@ namespace Doctrine\Tests; ...@@ -4,4 +4,17 @@ namespace Doctrine\Tests;
class DbalFunctionalTestSuite extends DbalTestSuite class DbalFunctionalTestSuite extends DbalTestSuite
{ {
protected $_conn;
protected function setUp()
{
if ( ! isset($this->_conn)) {
$this->_conn = TestUtil::getConnection();
}
}
protected function tearDown()
{
$this->_conn = null;
}
} }
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace Doctrine\Tests; namespace Doctrine\Tests;
class TestUtil class TestUtil
{ {
public static function getConnection() public static function getConnection()
{ {
if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'], if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'],
...@@ -23,6 +23,20 @@ class TestUtil ...@@ -23,6 +23,20 @@ class TestUtil
); );
} }
return \Doctrine\DBAL\DriverManager::getConnection($params); $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
$sm = $conn->getSchemaManager();
try {
$sm->dropDatabase();
} catch (\Exception $e) {}
try {
$sm->createDatabase();
} catch (\Exception $e) {}
$conn->close();
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
return $conn;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment