SchemaTest.php 6.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?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()
    {
15
        $tableName = "public.foo";
16 17 18 19 20 21 22 23 24 25 26 27 28
        $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));
    }

29 30 31 32 33 34 35 36 37 38 39 40 41
    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'));
    }

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    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");

95
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
96 97 98 99 100 101 102 103 104 105 106
        $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"));
107
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
108 109

        $sequences = $schema->getSequences();
110
        $this->assertArrayHasKey('public.a_seq', $sequences);
111 112
    }

113 114 115 116 117 118 119 120 121 122 123 124 125 126
    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'));
    }

127 128 129 130 131 132 133 134 135 136 137
    public function testGetUnknownSequenceThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $schema = new Schema();
        $schema->getSequence("unknown");
    }

    public function testCreateSequence()
    {
        $schema = new Schema();
138 139 140 141 142
        $sequence = $schema->createSequence('a_seq', 10, 20);

        $this->assertEquals('a_seq', $sequence->getName());
        $this->assertEquals(10, $sequence->getAllocationSize());
        $this->assertEquals(20, $sequence->getInitialValue());
143 144

        $this->assertTrue($schema->hasSequence("a_seq"));
145
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
146 147

        $sequences = $schema->getSequences();
148
        $this->assertArrayHasKey('public.a_seq', $sequences);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    }

    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));
    }
169

170 171 172
    public function testConfigMaxIdentifierLength()
    {
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
173
        $schemaConfig->setMaxIdentifierLength(5);
174

175
        $schema = new Schema(array(), array(), $schemaConfig);
176
        $table = $schema->createTable("smalltable");
177
        $table->addColumn('long_id', 'integer');
178 179
        $table->addIndex(array('long_id'));

180 181
        $index = current($table->getIndexes());
        $this->assertEquals(5, strlen($index->getName()));
182
    }
183 184 185 186 187 188 189

    public function testDeepClone()
    {
        $schema = new Schema();
        $sequence = $schema->createSequence('baz');

        $tableA = $schema->createTable('foo');
190
        $tableA->addColumn('id', 'integer');
191 192

        $tableB = $schema->createTable('bar');
193 194
        $tableB->addColumn('id', 'integer');
        $tableB->addColumn('foo_id', 'integer');
195 196 197 198 199 200 201 202 203 204 205 206
        $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'));

jwage's avatar
jwage committed
207 208
        $fk = $schemaNew->getTable('bar')->getForeignKeys();
        $fk = current($fk);
209 210
        $this->assertSame($schemaNew->getTable('bar'), $this->readAttribute($fk, '_localTable'));
    }
211 212 213 214 215 216 217 218 219 220 221 222 223

    /**
     * @group DBAL-219
     */
    public function testHasTableForQuotedAsset()
    {
        $schema = new Schema();

        $tableA = $schema->createTable('foo');
        $tableA->addColumn('id', 'integer');

        $this->assertTrue($schema->hasTable('`foo`'));
    }
224
}