AbstractPlatformTestCase.php 5.41 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\DBAL\Platforms;

abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{
7 8 9
    /**
     * @var Doctrine\DBAL\Platforms\AbstractPlatform
     */
10 11 12 13 14 15 16 17 18
    protected $_platform;

    abstract public function createPlatform();

    public function setUp()
    {
        $this->_platform = $this->createPlatform();
    }

19 20 21 22 23
    public function testCreateWithNoColumns()
    {
        $table = new \Doctrine\DBAL\Schema\Table('test');

        $this->setExpectedException('Doctrine\DBAL\DBALException');
24
        $sql = $this->_platform->getCreateTableSQL($table);
25 26
    }

27 28 29
    public function testGeneratesTableCreationSql()
    {
        $table = new \Doctrine\DBAL\Schema\Table('test');
30 31
        $table->addColumn('id', 'integer', array('notnull' => true));
        $table->addColumn('test', 'string', array('notnull' => false, 'length' => 255));
32 33 34
        $table->setPrimaryKey(array('id'));
        $table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);

35
        $sql = $this->_platform->getCreateTableSQL($table);
36 37 38 39 40
        $this->assertEquals($this->getGenerateTableSql(), $sql[0]);
    }

    abstract public function getGenerateTableSql();

41 42 43
    public function testGenerateTableWithMultiColumnUniqueIndex()
    {
        $table = new \Doctrine\DBAL\Schema\Table('test');
44 45
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
46 47
        $table->addUniqueIndex(array("foo", "bar"));

48
        $sql = $this->_platform->getCreateTableSQL($table);
49 50 51 52 53
        $this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
    }

    abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();

54 55 56 57 58 59
    public function testGeneratesIndexCreationSql()
    {
        $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));

        $this->assertEquals(
            $this->getGenerateIndexSql(),
60
            $this->_platform->getCreateIndexSQL($indexDef, 'mytable')
61 62 63 64 65 66 67 68 69
        );
    }

    abstract public function getGenerateIndexSql();

    public function testGeneratesUniqueIndexCreationSql()
    {
        $indexDef = new \Doctrine\DBAL\Schema\Index('index_name', array('test', 'test2'), true);

70
        $sql = $this->_platform->getCreateIndexSQL($indexDef, 'test');
71 72 73 74 75 76 77 78 79
        $this->assertEquals($this->getGenerateUniqueIndexSql(), $sql);
    }

    abstract public function getGenerateUniqueIndexSql();

    public function testGeneratesForeignKeyCreationSql()
    {
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');

80
        $sql = $this->_platform->getCreateForeignKeySQL($fk, 'test');
81 82 83 84
        $this->assertEquals($sql, $this->getGenerateForeignKeySql());
    }

    abstract public function getGenerateForeignKeySql();
85 86 87 88

    public function testGeneratesConstraintCreationSql()
    {
        $idx = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, false);
89
        $sql = $this->_platform->getCreateConstraintSQL($idx, 'test');
90 91 92
        $this->assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);

        $pk = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, true);
93
        $sql = $this->_platform->getCreateConstraintSQL($pk, 'test');
94 95 96
        $this->assertEquals($this->getGenerateConstraintPrimaryIndexSql(), $sql);

        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
97
        $sql = $this->_platform->getCreateConstraintSQL($fk, 'test');
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        $this->assertEquals($this->getGenerateConstraintForeignKeySql(), $sql);
    }

    public function getGenerateConstraintUniqueIndexSql()
    {
        return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
    }

    public function getGenerateConstraintPrimaryIndexSql()
    {
        return 'ALTER TABLE test ADD CONSTRAINT constraint_name PRIMARY KEY (test)';
    }

    public function getGenerateConstraintForeignKeySql()
    {
        return 'ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES foreign (id)';
    }
115 116 117

    abstract public function getGenerateAlterTableSql();

118
    public function testGeneratesTableAlterationSql()
119 120 121
    {
        $expectedSql = $this->getGenerateAlterTableSql();

122 123 124 125 126 127 128
        $columnDiff = new \Doctrine\DBAL\Schema\ColumnDiff(
            'bar', new \Doctrine\DBAL\Schema\Column(
                'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('default' => 'def')
            ),
            array('type', 'notnull', 'default')
        );

129 130 131
        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
        $tableDiff->newName = 'userlist';
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false));
132 133
        $tableDiff->removedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', \Doctrine\DBAL\Types\Type::getType('integer'));
        $tableDiff->changedColumns['bar'] = $columnDiff;
134

135
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
136

137
        $this->assertEquals($expectedSql, $sql);
138
    }
139 140 141 142

    public function testGetCustomColumnDeclarationSql()
    {
        $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
143
        $this->assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
144
    }
145
}