TableTest.php 11.5 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
<?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
{
    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()));
    }

41 42 43 44 45 46 47 48 49 50 51 52 53 54
    public function testColumnsCaseInsensitive()
    {
        $table = new Table("foo");
        $column = $table->createColumn('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'));
    }

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 95 96 97 98 99 100 101 102
    public function testCreateColumn()
    {
        $type = Type::getType('integer');

        $table = new Table("foo");

        $this->assertFalse($table->hasColumn("bar"));
        $table->createColumn("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());
    }

103 104 105 106 107
    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);
108
        
109 110 111
        $table->addIndex(array("foo", "bar", "baz"));
        $table->addUniqueIndex(array("foo", "bar", "baz"));

112 113
        $this->assertTrue($table->hasIndex("foo_foo_bar_baz_idx"));
        $this->assertTrue($table->hasIndex("foo_foo_bar_baz_uniq"));
114 115
    }

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

129 130 131 132 133 134 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
    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');
161
        $columns = array(new Column("foo", $type), new Column("bar", $type));
162 163
        $indexes = array(
            new Index("the_primary", array("foo"), true, true),
164
            new Index("other_primary", array("bar"), true, true),
165 166 167 168 169 170 171 172 173
        );
        $table = new Table("foo", $columns, $indexes, array());
    }

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

        $type = \Doctrine\DBAL\Types\Type::getType('integer');
174
        $columns = array(new Column("foo", $type), new Column("bar", $type));
175 176
        $indexes = array(
            new Index("an_idx", array("foo"), false, false),
177
            new Index("an_idx", array("bar"), false, false),
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        );
        $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));
202
        $constraints = $tableA->getForeignKeys();
203 204

        $this->assertEquals(1, count($constraints));
205
        $this->assertSame($constraint, array_shift($constraints));
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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
    }

    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");

        $table->createColumn("bar", 'integer');
        $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");

        $table->createColumn("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->createColumn("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->createColumn("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 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");
        $table->createColumn("id", 'int');

        $foreignTable = new Table("bar");
        $foreignTable->createColumn("id", 'int');

299
        $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
300 301 302 303 304 305 306 307 308 309 310 311
    }

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

        $table = new Table("foo");
        $table->createColumn("id", 'integer');

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

312
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
313 314 315 316 317 318 319 320 321 322
    }

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

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

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

325
        $constraints = $table->getForeignKeys();
326
        $this->assertEquals(1, count($constraints));
327
        $this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraints["foo_id_fk"]);
328

329 330 331
        $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"));
332 333 334
    }
    
}