MySQLSchemaTest.php 2.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?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
        );
    }
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

    /**
     * @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);
        }

65
        $this->assertEquals(array("ALTER TABLE test ADD CONSTRAINT FK_D87F7E0C8E48560F FOREIGN KEY (foo_id) REFERENCES test_foreign (foo_id)"), $sqls);
66
    }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    /**
     * @group DDC-1737
     */
    public function testClobNoAlterTable()
    {
        $tableOld = new Table("test");
        $tableOld->addColumn('id', 'integer');
        $tableOld->addColumn('description', 'string', array('length' => 65536));
        $tableNew = clone $tableOld;

        $tableNew->setPrimaryKey(array('id'));

        $diff = $this->comparator->diffTable($tableOld, $tableNew);
        $sql = $this->platform->getAlterTableSQL($diff);

        $this->assertEquals(
            array('ALTER TABLE test ADD PRIMARY KEY (id)'),
            $sql
        );
    }
88
}