Vendor update && Started using DoctrineMigrations

This commit is contained in:
Polonkai Gergely
2012-07-23 17:09:03 +02:00
parent 7c36f93436
commit bf46316347
1102 changed files with 103189 additions and 7 deletions

View File

@@ -0,0 +1,114 @@
<?php
namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;
class ColumnTest extends \PHPUnit_Framework_TestCase
{
public function testGet()
{
$column = $this->createColumn();
$this->assertEquals("foo", $column->getName());
$this->assertSame(Type::getType('string'), $column->getType());
$this->assertEquals(200, $column->getLength());
$this->assertEquals(5, $column->getPrecision());
$this->assertEquals(2, $column->getScale());
$this->assertTrue($column->getUnsigned());
$this->assertFalse($column->getNotNull());
$this->assertTrue($column->getFixed());
$this->assertEquals("baz", $column->getDefault());
$this->assertEquals(array('foo' => 'bar'), $column->getPlatformOptions());
$this->assertTrue($column->hasPlatformOption('foo'));
$this->assertEquals('bar', $column->getPlatformOption('foo'));
$this->assertFalse($column->hasPlatformOption('bar'));
$this->assertEquals(array('bar' => 'baz'), $column->getCustomSchemaOptions());
$this->assertTrue($column->hasCustomSchemaOption('bar'));
$this->assertEquals('baz', $column->getCustomSchemaOption('bar'));
$this->assertFalse($column->hasCustomSchemaOption('foo'));
}
public function testToArray()
{
$expected = array(
'name' => 'foo',
'type' => Type::getType('string'),
'default' => 'baz',
'notnull' => false,
'length' => 200,
'precision' => 5,
'scale' => 2,
'fixed' => true,
'unsigned' => true,
'autoincrement' => false,
'columnDefinition' => null,
'comment' => null,
'foo' => 'bar',
'bar' => 'baz'
);
$this->assertEquals($expected, $this->createColumn()->toArray());
}
/**
* @return Column
*/
public function createColumn()
{
$options = array(
'length' => 200,
'precision' => 5,
'scale' => 2,
'unsigned' => true,
'notnull' => false,
'fixed' => true,
'default' => 'baz',
'platformOptions' => array('foo' => 'bar'),
'customSchemaOptions' => array('bar' => 'baz'),
);
$string = Type::getType('string');
return new Column("foo", $string, $options);
}
/**
* @group DBAL-64
*/
public function testQuotedColumnName()
{
$string = Type::getType('string');
$column = new Column("`bar`", $string, array());
$mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
$sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
$this->assertEquals('bar', $column->getName());
$this->assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
$this->assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
}
/**
* @group DBAL-42
*/
public function testColumnComment()
{
$column = new Column("bar", Type::getType('string'));
$this->assertNull($column->getComment());
$column->setComment("foo");
$this->assertEquals("foo", $column->getComment());
$columnArray = $column->toArray();
$this->assertArrayHasKey('comment', $columnArray);
$this->assertEquals('foo', $columnArray['comment']);
}
}

View File

@@ -0,0 +1,787 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Schema,
Doctrine\DBAL\Schema\SchemaConfig,
Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Schema\Column,
Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Sequence,
Doctrine\DBAL\Schema\SchemaDiff,
Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Comparator,
Doctrine\DBAL\Types\Type,
Doctrine\DBAL\Schema\ForeignKeyConstraint;
/**
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class ComparatorTest extends \PHPUnit_Framework_TestCase
{
public function testCompareSame1()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer' ) ),
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer') ),
)
),
) );
$this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareSame2()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
)
),
) );
$this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareMissingTable()
{
$schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
$table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
$table->setSchemaConfig($schemaConfig);
$schema1 = new Schema( array($table), array(), $schemaConfig );
$schema2 = new Schema( array(), array(), $schemaConfig );
$expected = new SchemaDiff( array(), array(), array('bugdb' => $table) );
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareNewTable()
{
$schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
$table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
$table->setSchemaConfig($schemaConfig);
$schema1 = new Schema( array(), array(), $schemaConfig );
$schema2 = new Schema( array($table), array(), $schemaConfig );
$expected = new SchemaDiff( array('bugdb' => $table), array(), array() );
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareOnlyAutoincrementChanged()
{
$column1 = new Column('foo', Type::getType('integer'), array('autoincrement' => true));
$column2 = new Column('foo', Type::getType('integer'), array('autoincrement' => false));
$comparator = new Comparator();
$changedProperties = $comparator->diffColumn($column1, $column2);
$this->assertEquals(array('autoincrement'), $changedProperties);
}
public function testCompareMissingField()
{
$missingColumn = new Column('integerfield1', Type::getType('integer'));
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => $missingColumn,
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
) );
$expected = new SchemaDiff ( array(),
array (
'bugdb' => new TableDiff( 'bugdb', array(), array(),
array (
'integerfield1' => $missingColumn,
)
)
)
);
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareNewField()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
) );
$expected = new SchemaDiff ( array(),
array (
'bugdb' => new TableDiff ('bugdb',
array (
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
)
);
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareChangedColumns_ChangeType()
{
$column1 = new Column('charfield1', Type::getType('string'));
$column2 = new Column('charfield1', Type::getType('integer'));
$c = new Comparator();
$this->assertEquals(array('type'), $c->diffColumn($column1, $column2));
$this->assertEquals(array(), $c->diffColumn($column1, $column1));
}
public function testCompareChangedColumns_ChangeCustomSchemaOption()
{
$column1 = new Column('charfield1', Type::getType('string'));
$column2 = new Column('charfield1', Type::getType('string'));
$column1->setCustomSchemaOption('foo', 'bar');
$column2->setCustomSchemaOption('foo', 'bar');
$column1->setCustomSchemaOption('foo1', 'bar1');
$column2->setCustomSchemaOption('foo2', 'bar2');
$c = new Comparator();
$this->assertEquals(array('foo1', 'foo2'), $c->diffColumn($column1, $column2));
$this->assertEquals(array(), $c->diffColumn($column1, $column1));
}
public function testCompareRemovedIndex()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
),
array (
'primary' => new Index('primary',
array(
'integerfield1'
),
true
)
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
) );
$expected = new SchemaDiff ( array(),
array (
'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(), array(),
array (
'primary' => new Index('primary',
array(
'integerfield1'
),
true
)
)
),
)
);
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareNewIndex()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
),
array (
'primary' => new Index('primary',
array(
'integerfield1'
),
true
)
)
),
) );
$expected = new SchemaDiff ( array(),
array (
'bugdb' => new TableDiff( 'bugdb', array(), array(), array(),
array (
'primary' => new Index('primary',
array(
'integerfield1'
),
true
)
)
),
)
);
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareChangedIndex()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
),
array (
'primary' => new Index('primary',
array(
'integerfield1'
),
true
)
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
),
array (
'primary' => new Index('primary',
array('integerfield1', 'integerfield2'),
true
)
)
),
) );
$expected = new SchemaDiff ( array(),
array (
'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(),
array (
'primary' => new Index('primary',
array(
'integerfield1',
'integerfield2'
),
true
)
)
),
)
);
$actual = Comparator::compareSchemas( $schema1, $schema2 );
$this->assertEquals($expected, $actual);
}
public function testCompareChangedIndexFieldPositions()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
),
array (
'primary' => new Index('primary', array('integerfield1', 'integerfield2'), true)
)
),
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
),
array (
'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
)
),
) );
$expected = new SchemaDiff ( array(),
array (
'bugdb' => new TableDiff('bugdb', array(), array(), array(), array(),
array (
'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
)
),
)
);
$actual = Comparator::compareSchemas( $schema1, $schema2 );
$this->assertEquals($expected, $actual);
}
public function testCompareSequences()
{
$seq1 = new Sequence('foo', 1, 1);
$seq2 = new Sequence('foo', 1, 2);
$seq3 = new Sequence('foo', 2, 1);
$c = new Comparator();
$this->assertTrue($c->diffSequence($seq1, $seq2));
$this->assertTrue($c->diffSequence($seq1, $seq3));
}
public function testRemovedSequence()
{
$schema1 = new Schema();
$seq = $schema1->createSequence('foo');
$schema2 = new Schema();
$c = new Comparator();
$diffSchema = $c->compare($schema1, $schema2);
$this->assertEquals(1, count($diffSchema->removedSequences));
$this->assertSame($seq, $diffSchema->removedSequences[0]);
}
public function testAddedSequence()
{
$schema1 = new Schema();
$schema2 = new Schema();
$seq = $schema2->createSequence('foo');
$c = new Comparator();
$diffSchema = $c->compare($schema1, $schema2);
$this->assertEquals(1, count($diffSchema->newSequences));
$this->assertSame($seq, $diffSchema->newSequences[0]);
}
public function testTableAddForeignKey()
{
$tableForeign = new Table("bar");
$tableForeign->addColumn('id', 'integer');
$table1 = new Table("foo");
$table1->addColumn('fk', 'integer');
$table2 = new Table("foo");
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
$c = new Comparator();
$tableDiff = $c->diffTable($table1, $table2);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertEquals(1, count($tableDiff->addedForeignKeys));
}
public function testTableRemoveForeignKey()
{
$tableForeign = new Table("bar");
$tableForeign->addColumn('id', 'integer');
$table1 = new Table("foo");
$table1->addColumn('fk', 'integer');
$table2 = new Table("foo");
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
$c = new Comparator();
$tableDiff = $c->diffTable($table2, $table1);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertEquals(1, count($tableDiff->removedForeignKeys));
}
public function testTableUpdateForeignKey()
{
$tableForeign = new Table("bar");
$tableForeign->addColumn('id', 'integer');
$table1 = new Table("foo");
$table1->addColumn('fk', 'integer');
$table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
$table2 = new Table("foo");
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE'));
$c = new Comparator();
$tableDiff = $c->diffTable($table1, $table2);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertEquals(1, count($tableDiff->changedForeignKeys));
}
public function testTablesCaseInsensitive()
{
$schemaA = new Schema();
$schemaA->createTable('foo');
$schemaA->createTable('bAr');
$schemaA->createTable('BAZ');
$schemaA->createTable('new');
$schemaB = new Schema();
$schemaB->createTable('FOO');
$schemaB->createTable('bar');
$schemaB->createTable('Baz');
$schemaB->createTable('old');
$c = new Comparator();
$diff = $c->compare($schemaA, $schemaB);
$this->assertSchemaTableChangeCount($diff, 1, 0, 1);
}
public function testSequencesCaseInsenstive()
{
$schemaA = new Schema();
$schemaA->createSequence('foo');
$schemaA->createSequence('BAR');
$schemaA->createSequence('Baz');
$schemaA->createSequence('new');
$schemaB = new Schema();
$schemaB->createSequence('FOO');
$schemaB->createSequence('Bar');
$schemaB->createSequence('baz');
$schemaB->createSequence('old');
$c = new Comparator();
$diff = $c->compare($schemaA, $schemaB);
$this->assertSchemaSequenceChangeCount($diff, 1, 0, 1);
}
public function testCompareColumnCompareCaseInsensitive()
{
$tableA = new Table("foo");
$tableA->addColumn('id', 'integer');
$tableB = new Table("foo");
$tableB->addColumn('ID', 'integer');
$c = new Comparator();
$tableDiff = $c->diffTable($tableA, $tableB);
$this->assertFalse($tableDiff);
}
public function testCompareIndexBasedOnPropertiesNotName()
{
$tableA = new Table("foo");
$tableA->addColumn('id', 'integer');
$tableA->addIndex(array("id"), "foo_bar_idx");
$tableB = new Table("foo");
$tableB->addColumn('ID', 'integer');
$tableB->addIndex(array("id"), "bar_foo_idx");
$c = new Comparator();
$tableDiff = $c->diffTable($tableA, $tableB);
$this->assertFalse($tableDiff);
}
public function testCompareForeignKeyBasedOnPropertiesNotName()
{
$tableA = new Table("foo");
$tableA->addColumn('id', 'integer');
$tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('id'));
$tableB = new Table("foo");
$tableB->addColumn('ID', 'integer');
$tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('id'));
$c = new Comparator();
$tableDiff = $c->diffTable($tableA, $tableB);
$this->assertFalse($tableDiff);
}
public function testCompareForeignKey_RestrictNoAction_AreTheSame()
{
$fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
$fk2 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'RESTRICT'));
$c = new Comparator();
$this->assertFalse($c->diffForeignKey($fk1, $fk2));
}
public function testDetectRenameColumn()
{
$tableA = new Table("foo");
$tableA->addColumn('foo', 'integer');
$tableB = new Table("foo");
$tableB->addColumn('bar', 'integer');
$c = new Comparator();
$tableDiff = $c->diffTable($tableA, $tableB);
$this->assertEquals(0, count($tableDiff->addedColumns));
$this->assertEquals(0, count($tableDiff->removedColumns));
$this->assertArrayHasKey('foo', $tableDiff->renamedColumns);
$this->assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
}
/**
* You can easily have ambiguouties in the column renaming. If these
* are detected no renaming should take place, instead adding and dropping
* should be used exclusively.
*
* @group DBAL-24
*/
public function testDetectRenameColumnAmbiguous()
{
$tableA = new Table("foo");
$tableA->addColumn('foo', 'integer');
$tableA->addColumn('bar', 'integer');
$tableB = new Table("foo");
$tableB->addColumn('baz', 'integer');
$c = new Comparator();
$tableDiff = $c->diffTable($tableA, $tableB);
$this->assertEquals(1, count($tableDiff->addedColumns), "'baz' should be added, not created through renaming!");
$this->assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
$this->assertEquals(2, count($tableDiff->removedColumns), "'foo' and 'bar' should both be dropped, an ambigouty exists which one could be renamed to 'baz'.");
$this->assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed.");
$this->assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed.");
$this->assertEquals(0, count($tableDiff->renamedColumns), "no renamings should take place.");
}
public function testDetectChangeIdentifierType()
{
$this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');
$tableA = new Table("foo");
$tableA->addColumn('id', 'integer', array('autoincrement' => false));
$tableB = new Table("foo");
$tableB->addColumn('id', 'integer', array('autoincrement' => true));
$c = new Comparator();
$tableDiff = $c->diffTable($tableA, $tableB);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertArrayHasKey('id', $tableDiff->changedColumns);
}
/**
* @group DBAL-105
*/
public function testDiff()
{
$table = new \Doctrine\DBAL\Schema\Table('twitter_users');
$table->addColumn('id', 'integer', array('autoincrement' => true));
$table->addColumn('twitterId', 'integer', array('nullable' => false));
$table->addColumn('displayName', 'string', array('nullable' => false));
$table->setPrimaryKey(array('id'));
$newtable = new \Doctrine\DBAL\Schema\Table('twitter_users');
$newtable->addColumn('id', 'integer', array('autoincrement' => true));
$newtable->addColumn('twitter_id', 'integer', array('nullable' => false));
$newtable->addColumn('display_name', 'string', array('nullable' => false));
$newtable->addColumn('logged_in_at', 'datetime', array('nullable' => true));
$newtable->setPrimaryKey(array('id'));
$c = new Comparator();
$tableDiff = $c->diffTable($table, $newtable);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertEquals(array('twitterid', 'displayname'), array_keys($tableDiff->renamedColumns));
$this->assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
$this->assertEquals(0, count($tableDiff->removedColumns));
}
/**
* @group DBAL-112
*/
public function testChangedSequence()
{
$schema = new Schema();
$sequence = $schema->createSequence('baz');
$schemaNew = clone $schema;
/* @var $schemaNew Schema */
$schemaNew->getSequence('baz')->setAllocationSize(20);
$c = new \Doctrine\DBAL\Schema\Comparator;
$diff = $c->compare($schema, $schemaNew);
$this->assertSame($diff->changedSequences[0] , $schemaNew->getSequence('baz'));
}
/**
* @group DBAL-106
*/
public function testDiffDecimalWithNullPrecision()
{
$column = new Column('foo', Type::getType('decimal'));
$column->setPrecision(null);
$column2 = new Column('foo', Type::getType('decimal'));
$c = new Comparator();
$this->assertEquals(array(), $c->diffColumn($column, $column2));
}
/**
* @group DBAL-204
*/
public function testFqnSchemaComparision()
{
$config = new SchemaConfig();
$config->setName("foo");
$oldSchema = new Schema(array(), array(), $config);
$oldSchema->createTable('bar');
$newSchema= new Schema(array(), array(), $config);
$newSchema->createTable('foo.bar');
$c = new Comparator();
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
}
/**
* @group DBAL-204
*/
public function testFqnSchemaComparisionDifferentSchemaNameButSameTableNoDiff()
{
$config = new SchemaConfig();
$config->setName("foo");
$oldSchema = new Schema(array(), array(), $config);
$oldSchema->createTable('foo.bar');
$newSchema = new Schema();
$newSchema->createTable('bar');
$c = new Comparator();
$diff = $c->compare($oldSchema, $newSchema);
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
}
/**
* @group DBAL-204
*/
public function testFqnSchemaComparisionNoSchemaSame()
{
$config = new SchemaConfig();
$config->setName("foo");
$oldSchema = new Schema(array(), array(), $config);
$oldSchema->createTable('bar');
$newSchema = new Schema();
$newSchema->createTable('bar');
$c = new Comparator();
$diff = $c->compare($oldSchema, $newSchema);
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
}
/**
* @param SchemaDiff $diff
* @param int $newTableCount
* @param int $changeTableCount
* @param int $removeTableCount
*/
public function assertSchemaTableChangeCount($diff, $newTableCount=0, $changeTableCount=0, $removeTableCount=0)
{
$this->assertEquals($newTableCount, count($diff->newTables));
$this->assertEquals($changeTableCount, count($diff->changedTables));
$this->assertEquals($removeTableCount, count($diff->removedTables));
}
/**
* @param SchemaDiff $diff
* @param int $newSequenceCount
* @param int $changeSequenceCount
* @param int $changeSequenceCount
*/
public function assertSchemaSequenceChangeCount($diff, $newSequenceCount=0, $changeSequenceCount=0, $removeSequenceCount=0)
{
$this->assertEquals($newSequenceCount, count($diff->newSequences), "Expected number of new sequences is wrong.");
$this->assertEquals($changeSequenceCount, count($diff->changedSequences), "Expected number of changed sequences is wrong.");
$this->assertEquals($removeSequenceCount, count($diff->removedSequences), "Expected number of removed sequences is wrong.");
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
class IndexTest extends \PHPUnit_Framework_TestCase
{
public function createIndex($unique=false, $primary=false)
{
return new Index("foo", array("bar", "baz"), $unique, $primary);
}
public function testCreateIndex()
{
$idx = $this->createIndex();
$this->assertEquals("foo", $idx->getName());
$columns = $idx->getColumns();
$this->assertEquals(2, count($columns));
$this->assertEquals(array("bar", "baz"), $columns);
$this->assertFalse($idx->isUnique());
$this->assertFalse($idx->isPrimary());
}
public function testCreatePrimary()
{
$idx = $this->createIndex(false, true);
$this->assertTrue($idx->isUnique());
$this->assertTrue($idx->isPrimary());
}
public function testCreateUnique()
{
$idx = $this->createIndex(true, false);
$this->assertTrue($idx->isUnique());
$this->assertFalse($idx->isPrimary());
}
/**
* @group DBAL-50
*/
public function testFullfilledByUnique()
{
$idx1 = $this->createIndex(true, false);
$idx2 = $this->createIndex(true, false);
$idx3 = $this->createIndex();
$this->assertTrue($idx1->isFullfilledBy($idx2));
$this->assertFalse($idx1->isFullfilledBy($idx3));
}
/**
* @group DBAL-50
*/
public function testFullfilledByPrimary()
{
$idx1 = $this->createIndex(true, true);
$idx2 = $this->createIndex(true, true);
$idx3 = $this->createIndex(true, false);
$this->assertTrue($idx1->isFullfilledBy($idx2));
$this->assertFalse($idx1->isFullfilledBy($idx3));
}
/**
* @group DBAL-50
*/
public function testFullfilledByIndex()
{
$idx1 = $this->createIndex();
$idx2 = $this->createIndex();
$pri = $this->createIndex(true, true);
$uniq = $this->createIndex(true);
$this->assertTrue($idx1->isFullfilledBy($idx2));
$this->assertTrue($idx1->isFullfilledBy($pri));
$this->assertTrue($idx1->isFullfilledBy($uniq));
}
/**
* @group DBAL-285
*/
public function testIndexQuotes()
{
$index = new Index("foo", array("`bar`", "`baz`"));
$this->assertTrue($index->spansColumns(array("bar", "baz")));
$this->assertTrue($index->hasColumnAtPosition("bar", 0));
$this->assertTrue($index->hasColumnAtPosition("baz", 1));
$this->assertFalse($index->hasColumnAtPosition("bar", 1));
$this->assertFalse($index->hasColumnAtPosition("baz", 0));
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\Tests\DBAL\Mocks;
use Doctrine\Tests\TestUtil;
class MySqlSchemaManagerTest extends \PHPUnit_Framework_TestCase
{
/**
*
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
*/
private $manager;
public function setUp()
{
$eventManager = new EventManager();
$driverMock = $this->getMock('Doctrine\DBAL\Driver');
$platform = $this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform');
$this->conn = $this->getMock(
'Doctrine\DBAL\Connection',
array('fetchAll'),
array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager)
);
$this->manager = new MySqlSchemaManager($this->conn);
}
public function testCompositeForeignKeys()
{
$this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getFKDefinition()));
$fkeys = $this->manager->listTableForeignKeys('dummy');
$this->assertEquals(1, count($fkeys), "Table has to have one foreign key.");
$this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
$this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
$this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getForeignColumns()));
}
public function getFKDefinition()
{
return array(
array(
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
"COLUMN_NAME" => "column_1",
"REFERENCED_TABLE_NAME" => "dummy",
"REFERENCED_COLUMN_NAME" => "column_1",
"update_rule" => "RESTRICT",
"delete_rule" => "RESTRICT",
),
array(
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
"COLUMN_NAME" => "column_2",
"REFERENCED_TABLE_NAME" => "dummy",
"REFERENCED_COLUMN_NAME" => "column_2",
"update_rule" => "RESTRICT",
"delete_rule" => "RESTRICT",
),
array(
"CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
"COLUMN_NAME" => "column_3",
"REFERENCED_TABLE_NAME" => "dummy",
"REFERENCED_COLUMN_NAME" => "column_3",
"update_rule" => "RESTRICT",
"delete_rule" => "RESTRICT",
)
);
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Doctrine\Tests\DBAL\Schema\Platforms;
require_once __DIR__ . '/../../../TestInit.php';
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;
class MySQLSchemaTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Comparator
*/
private $comparator;
/**
*
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
*/
private $platform;
public function setUp()
{
$this->comparator = new \Doctrine\DBAL\Schema\Comparator;
$this->platform = new \Doctrine\DBAL\Platforms\MySqlPlatform;
}
public function testSwitchPrimaryKeyOrder()
{
$tableOld = new Table("test");
$tableOld->addColumn('foo_id', 'integer');
$tableOld->addColumn('bar_id', 'integer');
$tableNew = clone $tableOld;
$tableOld->setPrimaryKey(array('foo_id', 'bar_id'));
$tableNew->setPrimaryKey(array('bar_id', 'foo_id'));
$diff = $this->comparator->diffTable($tableOld, $tableNew);
$sql = $this->platform->getAlterTableSQL($diff);
$this->assertEquals(
array(
'ALTER TABLE test DROP PRIMARY KEY',
'ALTER TABLE test ADD PRIMARY KEY (bar_id, foo_id)'
), $sql
);
}
/**
* @group DBAL-132
*/
public function testGenerateForeignKeySQL()
{
$tableOld = new Table("test");
$tableOld->addColumn('foo_id', 'integer');
$tableOld->addUnnamedForeignKeyConstraint('test_foreign', array('foo_id'), array('foo_id'));
$sqls = array();
foreach ($tableOld->getForeignKeys() AS $fk) {
$sqls[] = $this->platform->getCreateForeignKeySQL($fk, $tableOld);
}
$this->assertEquals(array("ALTER TABLE test ADD CONSTRAINT FK_D87F7E0C8E48560F FOREIGN KEY (foo_id) REFERENCES test_foreign (foo_id)"), $sqls);
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Schema,
Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Schema\Column,
Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Sequence,
Doctrine\DBAL\Schema\SchemaDiff,
Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Comparator,
Doctrine\DBAL\Types\Type;
class SchemaDiffTest extends \PHPUnit_Framework_TestCase
{
public function testSchemaDiffToSql()
{
$diff = $this->createSchemaDiff();
$platform = $this->createPlatform(true);
$sql = $diff->toSql($platform);
$expected = array('drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table');
$this->assertEquals($expected, $sql);
}
public function testSchemaDiffToSaveSql()
{
$diff = $this->createSchemaDiff();
$platform = $this->createPlatform(false);
$sql = $diff->toSaveSql($platform);
$expected = array('alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table');
$this->assertEquals($expected, $sql);
}
public function createPlatform($unsafe = false)
{
$platform = $this->getMock('Doctrine\Tests\DBAL\Mocks\MockPlatform');
if ($unsafe) {
$platform->expects($this->exactly(1))
->method('getDropSequenceSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->will($this->returnValue('drop_seq'));
}
$platform->expects($this->exactly(1))
->method('getAlterSequenceSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->will($this->returnValue('alter_seq'));
$platform->expects($this->exactly(1))
->method('getCreateSequenceSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
->will($this->returnValue('create_seq'));
if ($unsafe) {
$platform->expects($this->exactly(1))
->method('getDropTableSql')
->with($this->isInstanceof('Doctrine\DBAL\Schema\Table'))
->will($this->returnValue('drop_table'));
}
$platform->expects($this->exactly(1))
->method('getCreateTableSql')
->with($this->isInstanceof('Doctrine\DBAL\Schema\Table'))
->will($this->returnValue(array('create_table')));
$platform->expects($this->exactly(1))
->method('getCreateForeignKeySQL')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint'))
->will($this->returnValue('create_foreign_key'));
$platform->expects($this->exactly(1))
->method('getAlterTableSql')
->with($this->isInstanceOf('Doctrine\DBAL\Schema\TableDiff'))
->will($this->returnValue(array('alter_table')));
if ($unsafe) {
$platform->expects($this->exactly(1))
->method('getDropForeignKeySql')
->with($this->isInstanceof('Doctrine\DBAL\Schema\ForeignKeyConstraint'), $this->equalTo('local_table'))
->will($this->returnValue('drop_orphan_fk'));
}
$platform->expects($this->exactly(1))
->method('supportsSequences')
->will($this->returnValue(true));
$platform->expects($this->exactly(2))
->method('supportsForeignKeyConstraints')
->will($this->returnValue(true));
return $platform;
}
public function createSchemaDiff()
{
$diff = new SchemaDiff();
$diff->changedSequences['foo_seq'] = new Sequence('foo_seq');
$diff->newSequences['bar_seq'] = new Sequence('bar_seq');
$diff->removedSequences['baz_seq'] = new Sequence('baz_seq');
$diff->newTables['foo_table'] = new Table('foo_table');
$diff->removedTables['bar_table'] = new Table('bar_table');
$diff->changedTables['baz_table'] = new TableDiff('baz_table');
$diff->newTables['foo_table']->addColumn('foreign_id', 'integer');
$diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', array('foreign_id'), array('id'));
$fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('id'), 'foreign_table', array('id'));
$fk->setLocalTable(new Table('local_table'));
$diff->orphanedForeignKeys[] = $fk;
return $diff;
}
}

View File

@@ -0,0 +1,224 @@
<?php
namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Sequence;
class SchemaTest extends \PHPUnit_Framework_TestCase
{
public function testAddTable()
{
$tableName = "public.foo";
$table = new Table($tableName);
$schema = new Schema(array($table));
$this->assertTrue($schema->hasTable($tableName));
$tables = $schema->getTables();
$this->assertTrue( isset($tables[$tableName]) );
$this->assertSame($table, $tables[$tableName]);
$this->assertSame($table, $schema->getTable($tableName));
$this->assertTrue($schema->hasTable($tableName));
}
public function testTableMatchingCaseInsenstive()
{
$table = new Table("Foo");
$schema = new Schema(array($table));
$this->assertTrue($schema->hasTable("foo"));
$this->assertTrue($schema->hasTable("FOO"));
$this->assertSame($table, $schema->getTable('FOO'));
$this->assertSame($table, $schema->getTable('foo'));
$this->assertSame($table, $schema->getTable('Foo'));
}
public function testGetUnknownTableThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$schema = new Schema();
$schema->getTable("unknown");
}
public function testCreateTableTwiceThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$tableName = "foo";
$table = new Table($tableName);
$tables = array($table, $table);
$schema = new Schema($tables);
}
public function testRenameTable()
{
$tableName = "foo";
$table = new Table($tableName);
$schema = new Schema(array($table));
$this->assertTrue($schema->hasTable("foo"));
$schema->renameTable("foo", "bar");
$this->assertFalse($schema->hasTable("foo"));
$this->assertTrue($schema->hasTable("bar"));
$this->assertSame($table, $schema->getTable("bar"));
}
public function testDropTable()
{
$tableName = "foo";
$table = new Table($tableName);
$schema = new Schema(array($table));
$this->assertTrue($schema->hasTable("foo"));
$schema->dropTable("foo");
$this->assertFalse($schema->hasTable("foo"));
}
public function testCreateTable()
{
$schema = new Schema();
$this->assertFalse($schema->hasTable("foo"));
$table = $schema->createTable("foo");
$this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
$this->assertEquals("foo", $table->getName());
$this->assertTrue($schema->hasTable("foo"));
}
public function testAddSequences()
{
$sequence = new Sequence("a_seq", 1, 1);
$schema = new Schema(array(), array($sequence));
$this->assertTrue($schema->hasSequence("a_seq"));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
$sequences = $schema->getSequences();
$this->assertArrayHasKey('public.a_seq', $sequences);
}
public function testSequenceAccessCaseInsensitive()
{
$sequence = new Sequence("a_Seq");
$schema = new Schema(array(), array($sequence));
$this->assertTrue($schema->hasSequence('a_seq'));
$this->assertTrue($schema->hasSequence('a_Seq'));
$this->assertTrue($schema->hasSequence('A_SEQ'));
$this->assertEquals($sequence, $schema->getSequence('a_seq'));
$this->assertEquals($sequence, $schema->getSequence('a_Seq'));
$this->assertEquals($sequence, $schema->getSequence('A_SEQ'));
}
public function testGetUnknownSequenceThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$schema = new Schema();
$schema->getSequence("unknown");
}
public function testCreateSequence()
{
$schema = new Schema();
$sequence = $schema->createSequence('a_seq', 10, 20);
$this->assertEquals('a_seq', $sequence->getName());
$this->assertEquals(10, $sequence->getAllocationSize());
$this->assertEquals(20, $sequence->getInitialValue());
$this->assertTrue($schema->hasSequence("a_seq"));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
$sequences = $schema->getSequences();
$this->assertArrayHasKey('public.a_seq', $sequences);
}
public function testDropSequence()
{
$sequence = new Sequence("a_seq", 1, 1);
$schema = new Schema(array(), array($sequence));
$schema->dropSequence("a_seq");
$this->assertFalse($schema->hasSequence("a_seq"));
}
public function testAddSequenceTwiceThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$sequence = new Sequence("a_seq", 1, 1);
$schema = new Schema(array(), array($sequence, $sequence));
}
public function testConfigMaxIdentifierLength()
{
$schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
$schemaConfig->setMaxIdentifierLength(5);
$schema = new Schema(array(), array(), $schemaConfig);
$table = $schema->createTable("smalltable");
$table->addColumn('long_id', 'integer');
$table->addIndex(array('long_id'));
$index = current($table->getIndexes());
$this->assertEquals(5, strlen($index->getName()));
}
public function testDeepClone()
{
$schema = new Schema();
$sequence = $schema->createSequence('baz');
$tableA = $schema->createTable('foo');
$tableA->addColumn('id', 'integer');
$tableB = $schema->createTable('bar');
$tableB->addColumn('id', 'integer');
$tableB->addColumn('foo_id', 'integer');
$tableB->addForeignKeyConstraint($tableA, array('foo_id'), array('id'));
$schemaNew = clone $schema;
$this->assertNotSame($sequence, $schemaNew->getSequence('baz'));
$this->assertNotSame($tableA, $schemaNew->getTable('foo'));
$this->assertNotSame($tableA->getColumn('id'), $schemaNew->getTable('foo')->getColumn('id'));
$this->assertNotSame($tableB, $schemaNew->getTable('bar'));
$this->assertNotSame($tableB->getColumn('id'), $schemaNew->getTable('bar')->getColumn('id'));
$fk = $schemaNew->getTable('bar')->getForeignKeys();
$fk = current($fk);
$this->assertSame($schemaNew->getTable('bar'), $this->readAttribute($fk, '_localTable'));
}
/**
* @group DBAL-219
*/
public function testHasTableForQuotedAsset()
{
$schema = new Schema();
$tableA = $schema->createTable('foo');
$tableA->addColumn('id', 'integer');
$this->assertTrue($schema->hasTable('`foo`'));
}
}

View File

@@ -0,0 +1,500 @@
<?php
namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableBuilder;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Types\Type;
class TableTest extends \Doctrine\Tests\DbalTestCase
{
public function testCreateWithInvalidTableName()
{
$this->setExpectedException('Doctrine\DBAL\DBALException');
$table = new \Doctrine\DBAL\Schema\Table('');
}
public function testGetName()
{
$table = new Table("foo", array(), array(), array());
$this->assertEquals("foo", $table->getName());
}
public function testColumns()
{
$type = Type::getType('integer');
$columns = array();
$columns[] = new Column("foo", $type);
$columns[] = new Column("bar", $type);
$table = new Table("foo", $columns, array(), array());
$this->assertTrue($table->hasColumn("foo"));
$this->assertTrue($table->hasColumn("bar"));
$this->assertFalse($table->hasColumn("baz"));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
$this->assertEquals(2, count($table->getColumns()));
}
public function testColumnsCaseInsensitive()
{
$table = new Table("foo");
$column = $table->addColumn('Foo', 'integer');
$this->assertTrue($table->hasColumn('Foo'));
$this->assertTrue($table->hasColumn('foo'));
$this->assertTrue($table->hasColumn('FOO'));
$this->assertSame($column, $table->getColumn('Foo'));
$this->assertSame($column, $table->getColumn('foo'));
$this->assertSame($column, $table->getColumn('FOO'));
}
public function testCreateColumn()
{
$type = Type::getType('integer');
$table = new Table("foo");
$this->assertFalse($table->hasColumn("bar"));
$table->addColumn("bar", 'integer');
$this->assertTrue($table->hasColumn("bar"));
$this->assertSame($type, $table->getColumn("bar")->getType());
}
public function testDropColumn()
{
$type = Type::getType('integer');
$columns = array();
$columns[] = new Column("foo", $type);
$columns[] = new Column("bar", $type);
$table = new Table("foo", $columns, array(), array());
$this->assertTrue($table->hasColumn("foo"));
$this->assertTrue($table->hasColumn("bar"));
$table->dropColumn("foo")->dropColumn("bar");
$this->assertFalse($table->hasColumn("foo"));
$this->assertFalse($table->hasColumn("bar"));
}
public function testGetUnknownColumnThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$table = new Table("foo", array(), array(), array());
$table->getColumn('unknown');
}
public function testAddColumnTwiceThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$type = \Doctrine\DBAL\Types\Type::getType('integer');
$columns = array();
$columns[] = new Column("foo", $type);
$columns[] = new Column("foo", $type);
$table = new Table("foo", $columns, array(), array());
}
public function testCreateIndex()
{
$type = \Doctrine\DBAL\Types\Type::getType('integer');
$columns = array(new Column("foo", $type), new Column("bar", $type), new Column("baz", $type));
$table = new Table("foo", $columns);
$table->addIndex(array("foo", "bar"), "foo_foo_bar_idx");
$table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
$this->assertTrue($table->hasIndex("foo_foo_bar_idx"));
$this->assertTrue($table->hasIndex("foo_bar_baz_uniq"));
}
public function testIndexCaseInsensitive()
{
$type = \Doctrine\DBAL\Types\Type::getType('integer');
$columns = array(
new Column("foo", $type),
new Column("bar", $type),
new Column("baz", $type)
);
$table = new Table("foo", $columns);
$table->addIndex(array("foo", "bar", "baz"), "Foo_Idx");
$this->assertTrue($table->hasIndex('foo_idx'));
$this->assertTrue($table->hasIndex('Foo_Idx'));
$this->assertTrue($table->hasIndex('FOO_IDX'));
}
public function testAddIndexes()
{
$type = \Doctrine\DBAL\Types\Type::getType('integer');
$columns = array(
new Column("foo", $type),
new Column("bar", $type),
);
$indexes = array(
new Index("the_primary", array("foo"), true, true),
new Index("bar_idx", array("bar"), false, false),
);
$table = new Table("foo", $columns, $indexes, array());
$this->assertTrue($table->hasIndex("the_primary"));
$this->assertTrue($table->hasIndex("bar_idx"));
$this->assertFalse($table->hasIndex("some_idx"));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
$this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('bar_idx'));
}
public function testGetUnknownIndexThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$table = new Table("foo", array(), array(), array());
$table->getIndex("unknownIndex");
}
public function testAddTwoPrimaryThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$type = \Doctrine\DBAL\Types\Type::getType('integer');
$columns = array(new Column("foo", $type), new Column("bar", $type));
$indexes = array(
new Index("the_primary", array("foo"), true, true),
new Index("other_primary", array("bar"), true, true),
);
$table = new Table("foo", $columns, $indexes, array());
}
public function testAddTwoIndexesWithSameNameThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$type = \Doctrine\DBAL\Types\Type::getType('integer');
$columns = array(new Column("foo", $type), new Column("bar", $type));
$indexes = array(
new Index("an_idx", array("foo"), false, false),
new Index("an_idx", array("bar"), false, false),
);
$table = new Table("foo", $columns, $indexes, array());
}
public function testConstraints()
{
$constraint = new ForeignKeyConstraint(array(), "foo", array());
$tableA = new Table("foo", array(), array(), array($constraint));
$constraints = $tableA->getForeignKeys();
$this->assertEquals(1, count($constraints));
$this->assertSame($constraint, array_shift($constraints));
}
public function testOptions()
{
$table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
$this->assertTrue($table->hasOption("foo"));
$this->assertEquals("bar", $table->getOption("foo"));
}
public function testBuilderSetPrimaryKey()
{
$table = new Table("foo");
$table->addColumn("bar", 'integer');
$table->setPrimaryKey(array("bar"));
$this->assertTrue($table->hasIndex("primary"));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
$this->assertTrue($table->getIndex("primary")->isUnique());
$this->assertTrue($table->getIndex("primary")->isPrimary());
}
public function testBuilderAddUniqueIndex()
{
$table = new Table("foo");
$table->addColumn("bar", 'integer');
$table->addUniqueIndex(array("bar"), "my_idx");
$this->assertTrue($table->hasIndex("my_idx"));
$this->assertTrue($table->getIndex("my_idx")->isUnique());
$this->assertFalse($table->getIndex("my_idx")->isPrimary());
}
public function testBuilderAddIndex()
{
$table = new Table("foo");
$table->addColumn("bar", 'integer');
$table->addIndex(array("bar"), "my_idx");
$this->assertTrue($table->hasIndex("my_idx"));
$this->assertFalse($table->getIndex("my_idx")->isUnique());
$this->assertFalse($table->getIndex("my_idx")->isPrimary());
}
public function testBuilderAddIndexWithInvalidNameThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$table = new Table("foo");
$table->addColumn("bar",'integer');
$table->addIndex(array("bar"), "invalid name %&/");
}
public function testBuilderAddIndexWithUnknownColumnThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$table = new Table("foo");
$table->addIndex(array("bar"), "invalidName");
}
public function testBuilderOptions()
{
$table = new Table("foo");
$table->addOption("foo", "bar");
$this->assertTrue($table->hasOption("foo"));
$this->assertEquals("bar", $table->getOption("foo"));
}
public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$table = new Table("foo");
$table->addColumn("id", 'integer');
$foreignTable = new Table("bar");
$foreignTable->addColumn("id", 'integer');
$table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
}
public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
{
$this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
$table = new Table("foo");
$table->addColumn("id", 'integer');
$foreignTable = new Table("bar");
$foreignTable->addColumn("id", 'integer');
$table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
}
public function testAddForeignKeyConstraint()
{
$table = new Table("foo");
$table->addColumn("id", 'integer');
$foreignTable = new Table("bar");
$foreignTable->addColumn("id", 'integer');
$table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
$constraints = $table->getForeignKeys();
$this->assertEquals(1, count($constraints));
$constraint = current($constraints);
$this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
$this->assertTrue($constraint->hasOption("foo"));
$this->assertEquals("bar", $constraint->getOption("foo"));
}
public function testAddIndexWithCaseSensitiveColumnProblem()
{
$table = new Table("foo");
$table->addColumn("id", 'integer');
$table->addIndex(array("ID"), "my_idx");
$this->assertTrue($table->hasIndex('my_idx'));
$this->assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
$this->assertTrue($table->getIndex('my_idx')->spansColumns(array('id')));
}
public function testAddPrimaryKey_ColumnsAreExplicitlySetToNotNull()
{
$table = new Table("foo");
$column = $table->addColumn("id", 'integer', array('notnull' => false));
$this->assertFalse($column->getNotnull());
$table->setPrimaryKey(array('id'));
$this->assertTrue($column->getNotnull());
}
/**
* @group DDC-133
*/
public function testAllowImplicitSchemaTableInAutogeneratedIndexNames()
{
$table = new Table("foo.bar");
$table->addColumn('baz', 'integer', array());
$table->addIndex(array('baz'));
$this->assertEquals(1, count($table->getIndexes()));
}
/**
* @group DBAL-50
*/
public function testAddIndexTwice_IgnoreSecond()
{
$table = new Table("foo.bar");
$table->addColumn('baz', 'integer', array());
$table->addIndex(array('baz'));
$table->addIndex(array('baz'));
$this->assertEquals(1, count($table->getIndexes()));
}
/**
* @group DBAL-50
*/
public function testAddForeignKeyIndexImplicitly()
{
$table = new Table("foo");
$table->addColumn("id", 'integer');
$foreignTable = new Table("bar");
$foreignTable->addColumn("id", 'integer');
$table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
$indexes = $table->getIndexes();
$this->assertEquals(1, count($indexes));
$index = current($indexes);
$this->assertTrue($table->hasIndex($index->getName()));
$this->assertEquals(array('id'), $index->getColumns());
}
/**
* @group DBAL-50
*/
public function testOverruleIndex()
{
$table = new Table("bar");
$table->addColumn('baz', 'integer', array());
$table->addIndex(array('baz'));
$indexes = $table->getIndexes();
$this->assertEquals(1, count($indexes));
$index = current($indexes);
$table->addUniqueIndex(array('baz'));
$this->assertEquals(1, count($table->getIndexes()));
$this->assertFalse($table->hasIndex($index->getName()));
}
public function testPrimaryKeyOverrulesUniqueIndex()
{
$table = new Table("bar");
$table->addColumn('baz', 'integer', array());
$table->addUniqueIndex(array('baz'));
$table->setPrimaryKey(array('baz'));
$indexes = $table->getIndexes();
$this->assertEquals(1, count($indexes), "Table should only contain the primary key table index, not the unique one anymore, because it was overruled.");
$index = current($indexes);
$this->assertTrue($index->isPrimary());
}
/**
* @group DBAL-64
*/
public function testQuotedTableName()
{
$table = new Table("`bar`");
$mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
$sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
$this->assertEquals('bar', $table->getName());
$this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
$this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
}
/**
* @group DBAL-79
*/
public function testTableHasPrimaryKey()
{
$table = new Table("test");
$this->assertFalse($table->hasPrimaryKey());
$table->addColumn("foo", "integer");
$table->setPrimaryKey(array("foo"));
$this->assertTrue($table->hasPrimaryKey());
}
/**
* @group DBAL-91
*/
public function testAddIndexWithQuotedColumns()
{
$table = new Table("test");
$table->addColumn('"foo"', 'integer');
$table->addColumn('bar', 'integer');
$table->addIndex(array('"foo"', '"bar"'));
}
/**
* @group DBAL-91
*/
public function testAddForeignKeyWithQuotedColumnsAndTable()
{
$table = new Table("test");
$table->addColumn('"foo"', 'integer');
$table->addColumn('bar', 'integer');
$table->addForeignKeyConstraint('"boing"', array('"foo"', '"bar"'), array("id"));
}
/**
* @group DBAL-177
*/
public function testQuoteSchemaPrefixed()
{
$table = new Table("`test`.`test`");
$this->assertEquals("test.test", $table->getName());
$this->assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform));
}
/**
* @group DBAL-204
*/
public function testFullQualifiedTableName()
{
$table = new Table("`test`.`test`");
$this->assertEquals('test.test', $table->getFullQualifiedName("test"));
$this->assertEquals('test.test', $table->getFullQualifiedName("other"));
$table = new Table("test");
$this->assertEquals('test.test', $table->getFullQualifiedName("test"));
$this->assertEquals('other.test', $table->getFullQualifiedName("other"));
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Doctrine\Tests\DBAL\Schema\Visitor;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets;
use Doctrine\DBAL\Platforms\MySqlPlatform;
class RemoveNamespacedAssetsTest extends \PHPUnit_Framework_TestCase
{
/**
* @group DBAL-204
*/
public function testRemoveNamespacedAssets()
{
$config = new SchemaConfig;
$config->setName("test");
$schema = new Schema(array(), array(), $config);
$schema->createTable("test.test");
$schema->createTable("foo.bar");
$schema->createTable("baz");
$schema->visit(new RemoveNamespacedAssets());
$tables = $schema->getTables();
$this->assertEquals(array("test.test", "test.baz"), array_keys($tables), "Only 2 tables should be present, both in 'test' namespace.");
}
/**
* @group DBAL-204
*/
public function testCleanupForeignKeys()
{
$config = new SchemaConfig;
$config->setName("test");
$schema = new Schema(array(), array(), $config);
$fooTable = $schema->createTable("foo.bar");
$fooTable->addColumn('id', 'integer');
$testTable = $schema->createTable("test.test");
$testTable->addColumn('id', 'integer');
$testTable->addForeignKeyConstraint("foo.bar", array("id"), array("id"));
$schema->visit(new RemoveNamespacedAssets());
$sql = $schema->toSql(new MySqlPlatform());
$this->assertEquals(1, count($sql), "Just one CREATE TABLE statement, no foreign key and table to foo.bar");
}
/**
* @group DBAL-204
*/
public function testCleanupForeignKeysDifferentOrder()
{
$config = new SchemaConfig;
$config->setName("test");
$schema = new Schema(array(), array(), $config);
$testTable = $schema->createTable("test.test");
$testTable->addColumn('id', 'integer');
$fooTable = $schema->createTable("foo.bar");
$fooTable->addColumn('id', 'integer');
$testTable->addForeignKeyConstraint("foo.bar", array("id"), array("id"));
$schema->visit(new RemoveNamespacedAssets());
$sql = $schema->toSql(new MySqlPlatform());
$this->assertEquals(1, count($sql), "Just one CREATE TABLE statement, no foreign key and table to foo.bar");
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace Doctrine\Tests\DBAL\Schema\Visitor;
require_once __DIR__ . '/../../../TestInit.php';
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
class SchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCreateSchema()
{
$platformMock = $this->getMock(
'Doctrine\DBAL\Platforms\MySqlPlatform',
array('getCreateTableSql', 'getCreateSequenceSql', 'getCreateForeignKeySql')
);
$platformMock->expects($this->exactly(2))
->method('getCreateTableSql')
->will($this->returnValue(array("foo")));
$platformMock->expects($this->exactly(1))
->method('getCreateSequenceSql')
->will($this->returnValue(array("bar")));
$platformMock->expects($this->exactly(1))
->method('getCreateForeignKeySql')
->will($this->returnValue(array("baz")));
$schema = $this->createFixtureSchema();
$sql = $schema->toSql($platformMock);
$this->assertEquals(array("foo", "foo", "bar", "baz"), $sql);
}
public function testDropSchema()
{
$platformMock = $this->getMock(
'Doctrine\DBAL\Platforms\MySqlPlatform',
array('getDropTableSql', 'getDropSequenceSql', 'getDropForeignKeySql')
);
$platformMock->expects($this->exactly(2))
->method('getDropTableSql')
->will($this->returnValue("tbl"));
$platformMock->expects($this->exactly(1))
->method('getDropSequenceSql')
->will($this->returnValue("seq"));
$platformMock->expects($this->exactly(1))
->method('getDropForeignKeySql')
->will($this->returnValue("fk"));
$schema = $this->createFixtureSchema();
$sql = $schema->toDropSql($platformMock);
$this->assertEquals(array("fk", "seq", "tbl", "tbl"), $sql);
}
/**
* @return Schema
*/
public function createFixtureSchema()
{
$schema = new Schema();
$tableA = $schema->createTable("foo");
$tableA->addColumn("id", 'integer');
$tableA->addColumn("bar", 'string', array('length' => 255));
$tableA->setPrimaryKey(array("id"));
$schema->createSequence("foo_seq");
$tableB = $schema->createTable("bar");
$tableB->addColumn("id", 'integer');
$tableB->setPrimaryKey(array("id"));
$tableA->addForeignKeyConstraint($tableB, array("bar"), array("id"));
return $schema;
}
}