TableTest.php 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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 \PHPUnit_Framework_TestCase
{
17 18 19 20 21 22
    public function testCreateWithInvalidTableName()
    {
        $this->setExpectedException('Doctrine\DBAL\DBALException');
        $table = new \Doctrine\DBAL\Schema\Table('');
    }

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    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->assertType('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
        $this->assertType('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));

        $this->assertEquals(2, count($table->getColumns()));
    }

47 48 49
    public function testColumnsCaseInsensitive()
    {
        $table = new Table("foo");
50
        $column = $table->addColumn('Foo', 'integer');
51 52 53 54 55 56 57 58 59 60

        $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'));
    }

61 62 63 64 65 66 67
    public function testCreateColumn()
    {
        $type = Type::getType('integer');

        $table = new Table("foo");

        $this->assertFalse($table->hasColumn("bar"));
68
        $table->addColumn("bar", 'integer');
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        $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());
    }

109 110 111 112 113
    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);
114
        
115 116 117
        $table->addIndex(array("foo", "bar", "baz"));
        $table->addUniqueIndex(array("foo", "bar", "baz"));

118 119
        $this->assertTrue($table->hasIndex("foo_foo_bar_baz_idx"));
        $this->assertTrue($table->hasIndex("foo_foo_bar_baz_uniq"));
120 121
    }

122 123 124 125 126 127 128 129 130 131 132 133 134
    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'));
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    public function testAddIndexes()
    {
        $type = \Doctrine\DBAL\Types\Type::getType('integer');
        $columns = array(new Column("foo", $type));
        $indexes = array(
            new Index("the_primary", array("foo"), true, true),
            new Index("foo_idx", array("foo"), false, false),
        );
        $table = new Table("foo", $columns, $indexes, array());

        $this->assertTrue($table->hasIndex("the_primary"));
        $this->assertTrue($table->hasIndex("foo_idx"));
        $this->assertFalse($table->hasIndex("some_idx"));

        $this->assertType('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
        $this->assertType('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
        $this->assertType('Doctrine\DBAL\Schema\Index', $table->getIndex('foo_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');
167
        $columns = array(new Column("foo", $type), new Column("bar", $type));
168 169
        $indexes = array(
            new Index("the_primary", array("foo"), true, true),
170
            new Index("other_primary", array("bar"), true, true),
171 172 173 174 175 176 177 178 179
        );
        $table = new Table("foo", $columns, $indexes, array());
    }

    public function testAddTwoIndexesWithSameNameThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $type = \Doctrine\DBAL\Types\Type::getType('integer');
180
        $columns = array(new Column("foo", $type), new Column("bar", $type));
181 182
        $indexes = array(
            new Index("an_idx", array("foo"), false, false),
183
            new Index("an_idx", array("bar"), false, false),
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        );
        $table = new Table("foo", $columns, $indexes, array());
    }

    public function testIdGenerator()
    {
        $tableA = new Table("foo", array(), array(), array(), Table::ID_NONE);
        $this->assertFalse($tableA->isIdGeneratorIdentity());
        $this->assertFalse($tableA->isIdGeneratorSequence());;
        
        $tableB = new Table("foo", array(), array(), array(), Table::ID_IDENTITY);
        $this->assertTrue($tableB->isIdGeneratorIdentity());
        $this->assertFalse($tableB->isIdGeneratorSequence());;

        $tableC = new Table("foo", array(), array(), array(), Table::ID_SEQUENCE);
        $this->assertFalse($tableC->isIdGeneratorIdentity());
        $this->assertTrue($tableC->isIdGeneratorSequence());;
    }

    public function testConstraints()
    {
        $constraint = new ForeignKeyConstraint(array(), "foo", array());

        $tableA = new Table("foo", array(), array(), array($constraint));
208
        $constraints = $tableA->getForeignKeys();
209 210

        $this->assertEquals(1, count($constraints));
211
        $this->assertSame($constraint, array_shift($constraints));
212 213 214 215 216 217 218 219 220 221 222 223 224 225
    }

    public function testOptions()
    {
        $table = new Table("foo", array(), array(), array(), Table::ID_NONE, array("foo" => "bar"));

        $this->assertTrue($table->hasOption("foo"));
        $this->assertEquals("bar", $table->getOption("foo"));
    }

    public function testBuilderSetPrimaryKey()
    {
        $table = new Table("foo");

226
        $table->addColumn("bar", 'integer');
227 228 229 230 231 232 233 234 235 236 237 238
        $table->setPrimaryKey(array("bar"));

        $this->assertTrue($table->hasIndex("primary"));
        $this->assertType('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");

239
        $table->addColumn("bar", 'integer');
240 241 242 243 244 245 246 247 248 249 250
        $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");

251
        $table->addColumn("bar", 'integer');
252 253 254 255 256 257 258 259 260 261 262 263
        $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");
264
        $table->addColumn("bar",'integer');
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        $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 testIdGeneratorType()
    {
        $table = new Table("foo");
        
        $table->setIdGeneratorType(Table::ID_IDENTITY);
        $this->assertTrue($table->isIdGeneratorIdentity());

        $table->setIdGeneratorType(Table::ID_SEQUENCE);
        $this->assertTrue($table->isIdGeneratorSequence());
    }

    public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo");
300
        $table->addColumn("id", 'integer');
301 302

        $foreignTable = new Table("bar");
303
        $foreignTable->addColumn("id", 'integer');
304

305
        $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
306 307 308 309 310 311 312
    }

    public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo");
313
        $table->addColumn("id", 'integer');
314 315

        $foreignTable = new Table("bar");
316
        $foreignTable->addColumn("id", 'integer');
317

318
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
319 320 321 322 323
    }

    public function testAddForeignKeyConstraint()
    {
        $table = new Table("foo");
324
        $table->addColumn("id", 'integer');
325 326

        $foreignTable = new Table("bar");
327
        $foreignTable->addColumn("id", 'integer');
328

329
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
330

331
        $constraints = $table->getForeignKeys();
332
        $this->assertEquals(1, count($constraints));
333
        $this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraints["foo_id_fk"]);
334

335 336 337
        $this->assertEquals("foo_id_fk", $constraints["foo_id_fk"]->getName());
        $this->assertTrue($constraints["foo_id_fk"]->hasOption("foo"));
        $this->assertEquals("bar", $constraints["foo_id_fk"]->getOption("foo"));
338
    }
339 340 341 342

    public function testAddIndexWithCaseSensitiveColumnProblem()
    {
        $table = new Table("foo");
343
        $table->addColumn("id", 'integer');
344 345 346 347 348 349

        $table->addIndex(array("ID"), "my_idx");

        $this->assertTrue($table->hasIndex('my_idx'));
        $this->assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
    }
350 351 352 353

    public function testAddPrimaryKey_ColumnsAreExplicitlySetToNotNull()
    {
        $table = new Table("foo");
354
        $column = $table->addColumn("id", 'integer', array('notnull' => false));
355 356 357 358 359 360 361

        $this->assertFalse($column->getNotnull());

        $table->setPrimaryKey(array('id'));
        
        $this->assertTrue($column->getNotnull());
    }
362
}