2009-05-30 06:27:50 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\DBAL\Functional\Schema;
|
|
|
|
|
2009-12-05 00:40:03 +03:00
|
|
|
use Doctrine\DBAL\Types\Type,
|
|
|
|
Doctrine\DBAL\Schema\AbstractSchemaManager;
|
2009-05-30 06:27:50 +04:00
|
|
|
|
2009-06-15 22:25:47 +04:00
|
|
|
require_once __DIR__ . '/../../../TestInit.php';
|
|
|
|
|
|
|
|
class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2009-11-30 22:02:05 +03:00
|
|
|
public function testListSequences()
|
|
|
|
{
|
|
|
|
if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
|
|
|
|
$this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
|
|
|
|
}
|
|
|
|
|
2009-12-02 22:07:38 +03:00
|
|
|
$sequence = new \Doctrine\DBAL\Schema\Sequence('list_sequences_test_seq', 20, 10);
|
|
|
|
$this->_sm->createSequence($sequence);
|
2009-11-30 22:02:05 +03:00
|
|
|
|
|
|
|
$sequences = $this->_sm->listSequences();
|
|
|
|
|
|
|
|
$this->assertType('array', $sequences, 'listSequences() should return an array.');
|
|
|
|
|
|
|
|
$foundSequence = null;
|
|
|
|
foreach($sequences AS $sequence) {
|
|
|
|
$this->assertType('Doctrine\DBAL\Schema\Sequence', $sequence, 'Array elements of listSequences() should be Sequence instances.');
|
|
|
|
if(strtolower($sequence->getName()) == 'list_sequences_test_seq') {
|
|
|
|
$foundSequence = $sequence;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertNotNull($foundSequence, "Sequence with name 'list_sequences_test_seq' was not found.");
|
|
|
|
$this->assertEquals(20, $foundSequence->getAllocationSize(), "Allocation Size is expected to be 20.");
|
|
|
|
$this->assertEquals(10, $foundSequence->getInitialValue(), "Initial Value is expected to be 10.");
|
|
|
|
}
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
public function testListFunctions()
|
|
|
|
{
|
|
|
|
$funcs = $this->_sm->listFunctions();
|
|
|
|
$this->assertType('array', $funcs);
|
|
|
|
$this->assertTrue(count($funcs)>=0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testListTriggers()
|
|
|
|
{
|
|
|
|
$triggers = $this->_sm->listTriggers();
|
|
|
|
$this->assertType('array', $triggers);
|
|
|
|
$this->assertTrue(count($triggers) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testListDatabases()
|
|
|
|
{
|
|
|
|
$this->_sm->dropAndCreateDatabase('test_create_database');
|
|
|
|
$databases = $this->_sm->listDatabases();
|
|
|
|
|
|
|
|
$databases = \array_map('strtolower', $databases);
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals(true, \in_array('test_create_database', $databases));
|
2009-11-03 19:56:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testListTables()
|
|
|
|
{
|
|
|
|
$this->createTestTable('list_tables_test');
|
|
|
|
$tables = $this->_sm->listTables();
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertType('array', $tables);
|
2009-12-01 02:12:26 +03:00
|
|
|
$this->assertTrue(count($tables) > 0);
|
|
|
|
|
|
|
|
$foundTable = false;
|
|
|
|
foreach ($tables AS $table) {
|
|
|
|
$this->assertType('Doctrine\DBAL\Schema\Table', $table);
|
2009-12-04 01:10:13 +03:00
|
|
|
if ($table->getName() == 'list_tables_test') {
|
2009-12-01 02:12:26 +03:00
|
|
|
$foundTable = true;
|
|
|
|
|
|
|
|
$this->assertTrue($table->hasColumn('id'));
|
|
|
|
$this->assertTrue($table->hasColumn('test'));
|
|
|
|
$this->assertTrue($table->hasColumn('foreign_key_test'));
|
|
|
|
}
|
|
|
|
}
|
2009-11-03 19:56:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testListTableColumns()
|
|
|
|
{
|
2009-12-02 21:52:21 +03:00
|
|
|
$table = new \Doctrine\DBAL\Schema\Table('list_table_columns');
|
|
|
|
$table->createColumn('id', 'integer', array('notnull' => true));
|
|
|
|
$table->createColumn('test', 'string', array('length' => 255, 'notnull' => false));
|
|
|
|
$table->createColumn('foo', 'text', array('notnull' => true));
|
|
|
|
$table->createColumn('bar', 'decimal', array('precision' => 10, 'scale' => 4, 'notnull' => false));
|
|
|
|
$table->createColumn('baz1', 'datetime');
|
|
|
|
$table->createColumn('baz2', 'time');
|
|
|
|
$table->createColumn('baz3', 'date');
|
|
|
|
|
|
|
|
$this->_sm->dropAndCreateTable($table);
|
2009-11-03 19:56:05 +03:00
|
|
|
|
|
|
|
$columns = $this->_sm->listTableColumns('list_table_columns');
|
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
foreach ($columns AS $column) {
|
|
|
|
$column->setCaseMode("lower");
|
|
|
|
}
|
2009-11-03 19:56:05 +03:00
|
|
|
|
|
|
|
$this->assertArrayHasKey('id', $columns);
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('id', $columns['id']->getname());
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertType('Doctrine\DBAL\Types\IntegerType', $columns['id']->gettype());
|
|
|
|
$this->assertEquals(false, $columns['id']->getunsigned());
|
|
|
|
$this->assertEquals(true, $columns['id']->getnotnull());
|
|
|
|
$this->assertEquals(null, $columns['id']->getdefault());
|
|
|
|
$this->assertType('array', $columns['id']->getPlatformOptions());
|
2009-11-03 19:56:05 +03:00
|
|
|
|
|
|
|
$this->assertArrayHasKey('test', $columns);
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('test', $columns['test']->getname());
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertType('Doctrine\DBAL\Types\StringType', $columns['test']->gettype());
|
|
|
|
$this->assertEquals(255, $columns['test']->getlength());
|
|
|
|
$this->assertEquals(false, $columns['test']->getfixed());
|
|
|
|
$this->assertEquals(false, $columns['test']->getnotnull());
|
|
|
|
$this->assertEquals(null, $columns['test']->getdefault());
|
|
|
|
$this->assertType('array', $columns['test']->getPlatformOptions());
|
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('foo', ($columns['foo']->getname()));
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertType('Doctrine\DBAL\Types\TextType', $columns['foo']->gettype());
|
|
|
|
$this->assertEquals(null, $columns['foo']->getlength());
|
|
|
|
$this->assertEquals(false, $columns['foo']->getunsigned());
|
|
|
|
$this->assertEquals(false, $columns['foo']->getfixed());
|
|
|
|
$this->assertEquals(true, $columns['foo']->getnotnull());
|
|
|
|
$this->assertEquals(null, $columns['foo']->getdefault());
|
|
|
|
$this->assertType('array', $columns['foo']->getPlatformOptions());
|
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('bar', ($columns['bar']->getname()));
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertType('Doctrine\DBAL\Types\DecimalType', $columns['bar']->gettype());
|
|
|
|
$this->assertEquals(null, $columns['bar']->getlength());
|
|
|
|
$this->assertEquals(10, $columns['bar']->getprecision());
|
|
|
|
$this->assertEquals(4, $columns['bar']->getscale());
|
|
|
|
$this->assertEquals(false, $columns['bar']->getunsigned());
|
|
|
|
$this->assertEquals(false, $columns['bar']->getfixed());
|
|
|
|
$this->assertEquals(false, $columns['bar']->getnotnull());
|
|
|
|
$this->assertEquals(null, $columns['bar']->getdefault());
|
|
|
|
$this->assertType('array', $columns['bar']->getPlatformOptions());
|
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('baz1', ($columns['baz1']->getname()));
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertType('Doctrine\DBAL\Types\DateTimeType', $columns['baz1']->gettype());
|
2009-12-02 21:52:21 +03:00
|
|
|
$this->assertEquals(true, $columns['baz1']->getnotnull());
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals(null, $columns['baz1']->getdefault());
|
|
|
|
$this->assertType('array', $columns['baz1']->getPlatformOptions());
|
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('baz2', ($columns['baz2']->getname()));
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertContains($columns['baz2']->gettype()->getName(), array('Time', 'Date', 'DateTime'));
|
2009-12-02 21:52:21 +03:00
|
|
|
$this->assertEquals(true, $columns['baz2']->getnotnull());
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals(null, $columns['baz2']->getdefault());
|
|
|
|
$this->assertType('array', $columns['baz2']->getPlatformOptions());
|
2009-11-03 19:56:05 +03:00
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('baz3', ($columns['baz3']->getname()));
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertContains($columns['baz2']->gettype()->getName(), array('Time', 'Date', 'DateTime'));
|
2009-12-02 21:52:21 +03:00
|
|
|
$this->assertEquals(true, $columns['baz3']->getnotnull());
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals(null, $columns['baz3']->getdefault());
|
|
|
|
$this->assertType('array', $columns['baz3']->getPlatformOptions());
|
2009-11-03 19:56:05 +03:00
|
|
|
}
|
|
|
|
|
2009-11-03 11:58:03 +03:00
|
|
|
public function testListTableIndexes()
|
|
|
|
{
|
2009-12-02 21:52:21 +03:00
|
|
|
$table = $this->getTestTable('list_table_indexes_test');
|
|
|
|
$table->addUniqueIndex(array('test'), 'test_index_name');
|
|
|
|
$table->addIndex(array('id', 'test'), 'test_composite_idx');
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
$this->_sm->createTable($table);
|
2009-11-03 11:58:03 +03:00
|
|
|
|
|
|
|
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
|
2009-11-30 22:02:05 +03:00
|
|
|
|
2009-11-03 11:58:03 +03:00
|
|
|
$this->assertEquals(3, count($tableIndexes));
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals(array('id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
|
|
|
|
$this->assertTrue($tableIndexes['primary']->isUnique());
|
|
|
|
$this->assertTrue($tableIndexes['primary']->isPrimary());
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals('test_index_name', $tableIndexes['test_index_name']->getName());
|
|
|
|
$this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test_index_name']->getColumns()));
|
|
|
|
$this->assertTrue($tableIndexes['test_index_name']->isUnique());
|
|
|
|
$this->assertFalse($tableIndexes['test_index_name']->isPrimary());
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals('test_composite_idx', $tableIndexes['test_composite_idx']->getName());
|
|
|
|
$this->assertEquals(array('id', 'test'), array_map('strtolower', $tableIndexes['test_composite_idx']->getColumns()));
|
|
|
|
$this->assertFalse($tableIndexes['test_composite_idx']->isUnique());
|
|
|
|
$this->assertFalse($tableIndexes['test_composite_idx']->isPrimary());
|
2009-11-03 11:58:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDropAndCreateIndex()
|
|
|
|
{
|
2009-12-02 21:52:21 +03:00
|
|
|
$table = $this->getTestTable('test_create_index');
|
|
|
|
$table->addUniqueIndex(array('test'), 'test');
|
|
|
|
$this->_sm->dropAndCreateTable($table);
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
$this->_sm->dropAndCreateIndex($table->getIndex('test'), $table);
|
2009-11-03 11:58:03 +03:00
|
|
|
$tableIndexes = $this->_sm->listTableIndexes('test_create_index');
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertType('array', $tableIndexes);
|
|
|
|
|
|
|
|
$this->assertEquals('test', $tableIndexes['test']->getName());
|
|
|
|
$this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test']->getColumns()));
|
|
|
|
$this->assertTrue($tableIndexes['test']->isUnique());
|
|
|
|
$this->assertFalse($tableIndexes['test']->isPrimary());
|
|
|
|
}
|
|
|
|
|
2009-12-05 00:40:03 +03:00
|
|
|
public function testCreateTableWithForeignKeys()
|
|
|
|
{
|
|
|
|
if(!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
|
|
|
|
$this->markTestSkipped('Platform does not support foreign keys.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$tableB = $this->getTestTable('test_foreign');
|
|
|
|
|
|
|
|
$this->_sm->dropAndCreateTable($tableB);
|
|
|
|
|
|
|
|
$tableA = $this->getTestTable('test_create_fk');
|
|
|
|
$tableA->addForeignKeyConstraint('test_foreign', array('foreign_key_test'), array('id'));
|
|
|
|
|
|
|
|
$this->_sm->dropAndCreateTable($tableA);
|
|
|
|
|
|
|
|
$fkConstraints = $this->_sm->listTableForeignKeys('test_create_fk');
|
|
|
|
$this->assertEquals(1, count($fkConstraints));
|
|
|
|
|
|
|
|
$fkConstraint = current($fkConstraints);
|
|
|
|
$fkConstraint->setCaseMode("lower");
|
|
|
|
$this->assertEquals('test_foreign', $fkConstraint->getForeignTableName());
|
|
|
|
$this->assertEquals(array('foreign_key_test'), $fkConstraint->getColumns());
|
|
|
|
$this->assertEquals(array('id'), $fkConstraint->getForeignColumns());
|
|
|
|
}
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
public function testListForeignKeys()
|
|
|
|
{
|
|
|
|
if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
|
|
|
|
$this->markTestSkipped('Does not support foreign key constraints.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->createTestTable('test_create_fk1');
|
|
|
|
$this->createTestTable('test_create_fk2');
|
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
$foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
|
|
|
|
array('foreign_key_test'), 'test_create_fk2', array('id'), 'foreign_key_test_fk', array('onUpdate' => 'CASCADE', 'onDelete' => 'CASCADE')
|
2009-11-30 22:02:05 +03:00
|
|
|
);
|
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
$this->_sm->createForeignKey($foreignKey, 'test_create_fk1');
|
2009-11-30 22:02:05 +03:00
|
|
|
|
|
|
|
$fkeys = $this->_sm->listTableForeignKeys('test_create_fk1');
|
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
foreach ($fkeys AS $fkey) {
|
|
|
|
$fkey->setCaseMode("lower");
|
|
|
|
}
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$this->assertEquals(1, count($fkeys));
|
|
|
|
$this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
|
|
|
|
$this->assertEquals(array('foreign_key_test'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
|
|
|
|
$this->assertEquals(array('id'), array_map('strtolower', $fkeys[0]->getForeignColumns()));
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->assertEquals('test_create_fk2', ($fkeys[0]->getForeignTableName()));
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
if($fkeys[0]->hasOption('onUpdate')) {
|
|
|
|
$this->assertEquals('CASCADE', $fkeys[0]->getOption('onUpdate'));
|
|
|
|
}
|
|
|
|
if($fkeys[0]->hasOption('onDelete')) {
|
|
|
|
$this->assertEquals('CASCADE', $fkeys[0]->getOption('onDelete'));
|
|
|
|
}
|
2009-11-03 11:58:03 +03:00
|
|
|
}
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
protected function getCreateExampleViewSql()
|
|
|
|
{
|
|
|
|
$this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testListViews()
|
|
|
|
{
|
|
|
|
$this->_sm->dropAndCreateView('test_create_view', $this->getCreateExampleViewSql());
|
|
|
|
$views = $this->_sm->listViews();
|
|
|
|
$this->assertTrue(count($views) >= 1, "There should be at least the fixture view created in the database, but none were found.");
|
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach($views AS $view) {
|
|
|
|
if(!isset($view['name']) || !isset($view['sql'])) {
|
|
|
|
$this->fail(
|
|
|
|
"listViews() has to return entries with both name ".
|
|
|
|
"and sql keys, but only ".implode(", ", array_keys($view))." are present."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($view['name'] == 'test_create_view') {
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertTrue($found, "'test_create_view' View was not found in listViews().");
|
|
|
|
}
|
|
|
|
|
2009-12-01 02:12:26 +03:00
|
|
|
public function testCreateSchema()
|
|
|
|
{
|
|
|
|
$this->createTestTable('test_table');
|
|
|
|
|
|
|
|
$schema = $this->_sm->createSchema();
|
|
|
|
|
|
|
|
$this->assertTrue($schema->hasTable('test_table'));
|
|
|
|
}
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
|
|
|
|
*/
|
|
|
|
protected $_sm;
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2009-06-15 22:25:47 +04:00
|
|
|
protected function createTestTable($name = 'test_table', $data = array())
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
$options = array();
|
|
|
|
if (isset($data['options'])) {
|
|
|
|
$options = $data['options'];
|
|
|
|
}
|
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
$table = $this->getTestTable($name, $options);
|
|
|
|
|
|
|
|
$this->_sm->dropAndCreateTable($table);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTestTable($name, $options=array())
|
|
|
|
{
|
|
|
|
$table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), \Doctrine\DBAL\Schema\Table::ID_NONE, $options);
|
|
|
|
$table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);
|
|
|
|
$table->createColumn('id', 'integer', array('notnull' => true));
|
|
|
|
$table->setPrimaryKey(array('id'));
|
|
|
|
$table->createColumn('test', 'string', array('length' => 255));
|
|
|
|
$table->createColumn('foreign_key_test', 'integer');
|
|
|
|
return $table;
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
2009-12-01 02:12:26 +03:00
|
|
|
|
|
|
|
protected function assertHasTable($tables, $tableName)
|
|
|
|
{
|
|
|
|
$foundTable = false;
|
|
|
|
foreach ($tables AS $table) {
|
|
|
|
$this->assertType('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.');
|
|
|
|
if (strtolower($table->getName()) == 'list_tables_test_new_name') {
|
|
|
|
$foundTable = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertTrue($foundTable, "Could not find new table");
|
|
|
|
}
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|