AbstractPlatformTestCase.php 48.2 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Platforms;

5 6
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Events;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Schema\Column;
9
use Doctrine\DBAL\Schema\ColumnDiff;
10
use Doctrine\DBAL\Schema\Comparator;
11
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
12
use Doctrine\DBAL\Schema\Index;
13
use Doctrine\DBAL\Schema\Table;
14
use Doctrine\DBAL\Schema\TableDiff;
15
use Doctrine\DBAL\Types\Type;
16

17 18
abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{
19
    /**
20
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
21
     */
22 23 24 25
    protected $_platform;

    abstract public function createPlatform();

26
    protected function setUp()
27 28 29 30
    {
        $this->_platform = $this->createPlatform();
    }

31 32 33
    /**
     * @group DDC-1360
     */
34 35 36 37 38 39 40
    public function testQuoteIdentifier()
    {
        if ($this->_platform->getName() == "mssql") {
            $this->markTestSkipped('Not working this way on mssql.');
        }

        $c = $this->_platform->getIdentifierQuoteCharacter();
41 42
        $this->assertEquals($c."test".$c, $this->_platform->quoteIdentifier("test"));
        $this->assertEquals($c."test".$c.".".$c."test".$c, $this->_platform->quoteIdentifier("test.test"));
43 44 45
        $this->assertEquals(str_repeat($c, 4), $this->_platform->quoteIdentifier($c));
    }

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    /**
     * @group DDC-1360
     */
    public function testQuoteSingleIdentifier()
    {
        if ($this->_platform->getName() == "mssql") {
            $this->markTestSkipped('Not working this way on mssql.');
        }

        $c = $this->_platform->getIdentifierQuoteCharacter();
        $this->assertEquals($c."test".$c, $this->_platform->quoteSingleIdentifier("test"));
        $this->assertEquals($c."test.test".$c, $this->_platform->quoteSingleIdentifier("test.test"));
        $this->assertEquals(str_repeat($c, 4), $this->_platform->quoteSingleIdentifier($c));
    }

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
    /**
     * @group DBAL-1029
     *
     * @dataProvider getReturnsForeignKeyReferentialActionSQL
     */
    public function testReturnsForeignKeyReferentialActionSQL($action, $expectedSQL)
    {
        $this->assertSame($expectedSQL, $this->_platform->getForeignKeyReferentialActionSQL($action));
    }

    /**
     * @return array
     */
    public function getReturnsForeignKeyReferentialActionSQL()
    {
        return array(
            array('CASCADE', 'CASCADE'),
            array('SET NULL', 'SET NULL'),
            array('NO ACTION', 'NO ACTION'),
            array('RESTRICT', 'RESTRICT'),
            array('SET DEFAULT', 'SET DEFAULT'),
            array('CaScAdE', 'CASCADE'),
        );
    }

Possum's avatar
Possum committed
86
    public function testGetInvalidForeignKeyReferentialActionSQL()
87 88 89 90 91
    {
        $this->setExpectedException('InvalidArgumentException');
        $this->_platform->getForeignKeyReferentialActionSQL('unknown');
    }

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    public function testGetUnknownDoctrineMappingType()
    {
        $this->setExpectedException('Doctrine\DBAL\DBALException');
        $this->_platform->getDoctrineTypeMapping('foobar');
    }

    public function testRegisterDoctrineMappingType()
    {
        $this->_platform->registerDoctrineTypeMapping('foo', 'integer');
        $this->assertEquals('integer', $this->_platform->getDoctrineTypeMapping('foo'));
    }

    public function testRegisterUnknownDoctrineMappingType()
    {
        $this->setExpectedException('Doctrine\DBAL\DBALException');
        $this->_platform->registerDoctrineTypeMapping('foo', 'bar');
    }

110 111
    public function testCreateWithNoColumns()
    {
112
        $table = new Table('test');
113 114

        $this->setExpectedException('Doctrine\DBAL\DBALException');
115
        $sql = $this->_platform->getCreateTableSQL($table);
116 117
    }

118 119
    public function testGeneratesTableCreationSql()
    {
120
        $table = new Table('test');
121
        $table->addColumn('id', 'integer', array('notnull' => true, 'autoincrement' => true));
122
        $table->addColumn('test', 'string', array('notnull' => false, 'length' => 255));
123 124
        $table->setPrimaryKey(array('id'));

125
        $sql = $this->_platform->getCreateTableSQL($table);
126 127 128 129 130
        $this->assertEquals($this->getGenerateTableSql(), $sql[0]);
    }

    abstract public function getGenerateTableSql();

131 132
    public function testGenerateTableWithMultiColumnUniqueIndex()
    {
133
        $table = new Table('test');
134 135
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
136 137
        $table->addUniqueIndex(array("foo", "bar"));

138
        $sql = $this->_platform->getCreateTableSQL($table);
139 140 141 142 143
        $this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
    }

    abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();

144 145 146 147 148 149
    public function testGeneratesIndexCreationSql()
    {
        $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));

        $this->assertEquals(
            $this->getGenerateIndexSql(),
150
            $this->_platform->getCreateIndexSQL($indexDef, 'mytable')
151 152 153 154 155 156 157 158 159
        );
    }

    abstract public function getGenerateIndexSql();

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

160
        $sql = $this->_platform->getCreateIndexSQL($indexDef, 'test');
161 162 163 164 165
        $this->assertEquals($this->getGenerateUniqueIndexSql(), $sql);
    }

    abstract public function getGenerateUniqueIndexSql();

166 167 168
    public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes()
    {
        $where = 'test IS NULL AND test2 IS NOT NULL';
169
        $indexDef = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), false, false, array(), array('where' => $where));
170
        $uniqueIndex = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), true, false, array(), array('where' => $where));
171 172 173 174

        $expected = ' WHERE ' . $where;

        $actuals = array();
175 176 177 178 179 180

        if ($this->supportsInlineIndexDeclaration()) {
            $actuals []= $this->_platform->getIndexDeclarationSQL('name', $indexDef);
        }

        $actuals []= $this->_platform->getUniqueConstraintDeclarationSQL('name', $uniqueIndex);
181 182 183 184 185 186 187 188 189 190 191
        $actuals []= $this->_platform->getCreateIndexSQL($indexDef, 'table');

        foreach ($actuals as $actual) {
            if ($this->_platform->supportsPartialIndexes()) {
                $this->assertStringEndsWith($expected, $actual, 'WHERE clause should be present');
            } else {
                $this->assertStringEndsNotWith($expected, $actual, 'WHERE clause should NOT be present');
            }
        }
    }

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

196
        $sql = $this->_platform->getCreateForeignKeySQL($fk, 'test');
197 198 199 200
        $this->assertEquals($sql, $this->getGenerateForeignKeySql());
    }

    abstract public function getGenerateForeignKeySql();
201 202 203 204

    public function testGeneratesConstraintCreationSql()
    {
        $idx = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, false);
205
        $sql = $this->_platform->getCreateConstraintSQL($idx, 'test');
206 207 208
        $this->assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);

        $pk = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, true);
209
        $sql = $this->_platform->getCreateConstraintSQL($pk, 'test');
210 211 212
        $this->assertEquals($this->getGenerateConstraintPrimaryIndexSql(), $sql);

        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
213
        $sql = $this->_platform->getCreateConstraintSQL($fk, 'test');
214
        $this->assertEquals($this->getGenerateConstraintForeignKeySql($fk), $sql);
215
    }
Fabio B. Silva's avatar
Fabio B. Silva committed
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    public function testGeneratesForeignKeySqlOnlyWhenSupportingForeignKeys()
    {
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');

        if ($this->_platform->supportsForeignKeyConstraints()) {
            $this->assertInternalType(
                'string',
                $this->_platform->getCreateForeignKeySQL($fk, 'test')
            );
        } else {
            $this->setExpectedException('Doctrine\DBAL\DBALException');
            $this->_platform->getCreateForeignKeySQL($fk, 'test');
        }
    }

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
    protected function getBitAndComparisonExpressionSql($value1, $value2)
    {
        return '(' . $value1 . ' & ' . $value2 . ')';
    }

    /**
     * @group DDC-1213
     */
    public function testGeneratesBitAndComparisonExpressionSql()
    {
        $sql = $this->_platform->getBitAndComparisonExpression(2, 4);
        $this->assertEquals($this->getBitAndComparisonExpressionSql(2, 4), $sql);
    }

    protected  function getBitOrComparisonExpressionSql($value1, $value2)
    {
        return '(' . $value1 . ' | ' . $value2 . ')';
    }

    /**
     * @group DDC-1213
     */
    public function testGeneratesBitOrComparisonExpressionSql()
    {
        $sql = $this->_platform->getBitOrComparisonExpression(2, 4);
        $this->assertEquals($this->getBitOrComparisonExpressionSql(2, 4), $sql);
    }
259 260 261 262 263 264 265 266 267 268 269

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

270
    public function getGenerateConstraintForeignKeySql(ForeignKeyConstraint $fk)
271
    {
272 273 274
        $quotedForeignTable = $fk->getQuotedForeignTableName($this->_platform);

        return "ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES $quotedForeignTable (id)";
275
    }
276 277 278

    abstract public function getGenerateAlterTableSql();

279
    public function testGeneratesTableAlterationSql()
280 281 282
    {
        $expectedSql = $this->getGenerateAlterTableSql();

283 284 285 286 287 288 289
        $table = new Table('mytable');
        $table->addColumn('id', 'integer', array('autoincrement' => true));
        $table->addColumn('foo', 'integer');
        $table->addColumn('bar', 'string');
        $table->addColumn('bloo', 'boolean');
        $table->setPrimaryKey(array('id'));

290
        $tableDiff = new TableDiff('mytable');
291
        $tableDiff->fromTable = $table;
292 293 294 295
        $tableDiff->newName = 'userlist';
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false));
        $tableDiff->removedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', \Doctrine\DBAL\Types\Type::getType('integer'));
        $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
296 297 298 299 300
            'bar', new \Doctrine\DBAL\Schema\Column(
                'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('default' => 'def')
            ),
            array('type', 'notnull', 'default')
        );
301 302 303 304 305 306
        $tableDiff->changedColumns['bloo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
            'bloo', new \Doctrine\DBAL\Schema\Column(
                'bloo', \Doctrine\DBAL\Types\Type::getType('boolean'), array('default' => false)
            ),
            array('type', 'notnull', 'default')
        );
307

308
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
309

310
        $this->assertEquals($expectedSql, $sql);
311
    }
312 313 314 315

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

319 320
    public function testGetCreateTableSqlDispatchEvent()
    {
321 322 323
        $listenerMock = $this->getMockBuilder('GetCreateTableSqlDispatchEvenListener')
            ->setMethods(array('onSchemaCreateTable', 'onSchemaCreateTableColumn'))
            ->getMock();
324 325
        $listenerMock
            ->expects($this->once())
326
            ->method('onSchemaCreateTable');
327 328 329 330 331
        $listenerMock
            ->expects($this->exactly(2))
            ->method('onSchemaCreateTableColumn');

        $eventManager = new EventManager();
332
        $eventManager->addEventListener(array(Events::onSchemaCreateTable, Events::onSchemaCreateTableColumn), $listenerMock);
333 334 335

        $this->_platform->setEventManager($eventManager);

336
        $table = new Table('test');
337 338 339 340 341 342 343 344
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));

        $this->_platform->getCreateTableSQL($table);
    }

    public function testGetDropTableSqlDispatchEvent()
    {
345 346 347
        $listenerMock = $this->getMockBuilder('GetDropTableSqlDispatchEventListener')
            ->setMethods(array('onSchemaDropTable'))
            ->getMock();
348 349 350 351 352 353 354 355 356 357 358
        $listenerMock
            ->expects($this->once())
            ->method('onSchemaDropTable');

        $eventManager = new EventManager();
        $eventManager->addEventListener(array(Events::onSchemaDropTable), $listenerMock);

        $this->_platform->setEventManager($eventManager);

        $this->_platform->getDropTableSQL('TABLE');
    }
359

360 361 362
    public function testGetAlterTableSqlDispatchEvent()
    {
        $events = array(
363
            'onSchemaAlterTable',
jsor's avatar
jsor committed
364 365 366 367
            'onSchemaAlterTableAddColumn',
            'onSchemaAlterTableRemoveColumn',
            'onSchemaAlterTableChangeColumn',
            'onSchemaAlterTableRenameColumn'
368 369
        );

370 371 372
        $listenerMock = $this->getMockBuilder('GetAlterTableSqlDispatchEvenListener')
            ->setMethods($events)
            ->getMock();
373 374 375
        $listenerMock
            ->expects($this->once())
            ->method('onSchemaAlterTable');
376 377
        $listenerMock
            ->expects($this->once())
jsor's avatar
jsor committed
378
            ->method('onSchemaAlterTableAddColumn');
379 380
        $listenerMock
            ->expects($this->once())
jsor's avatar
jsor committed
381
            ->method('onSchemaAlterTableRemoveColumn');
382 383
        $listenerMock
            ->expects($this->once())
jsor's avatar
jsor committed
384
            ->method('onSchemaAlterTableChangeColumn');
385 386
        $listenerMock
            ->expects($this->once())
jsor's avatar
jsor committed
387
            ->method('onSchemaAlterTableRenameColumn');
388 389 390

        $eventManager = new EventManager();
        $events = array(
391
            Events::onSchemaAlterTable,
jsor's avatar
jsor committed
392 393 394 395
            Events::onSchemaAlterTableAddColumn,
            Events::onSchemaAlterTableRemoveColumn,
            Events::onSchemaAlterTableChangeColumn,
            Events::onSchemaAlterTableRenameColumn
396 397 398 399 400
        );
        $eventManager->addEventListener($events, $listenerMock);

        $this->_platform->setEventManager($eventManager);

401 402 403 404 405
        $table = new Table('mytable');
        $table->addColumn('removed', 'integer');
        $table->addColumn('changed', 'integer');
        $table->addColumn('renamed', 'integer');

406
        $tableDiff = new TableDiff('mytable');
407
        $tableDiff->fromTable = $table;
408 409 410 411 412 413 414 415 416 417 418 419
        $tableDiff->addedColumns['added'] = new \Doctrine\DBAL\Schema\Column('added', \Doctrine\DBAL\Types\Type::getType('integer'), array());
        $tableDiff->removedColumns['removed'] = new \Doctrine\DBAL\Schema\Column('removed', \Doctrine\DBAL\Types\Type::getType('integer'), array());
        $tableDiff->changedColumns['changed'] = new \Doctrine\DBAL\Schema\ColumnDiff(
            'changed', new \Doctrine\DBAL\Schema\Column(
                'changed2', \Doctrine\DBAL\Types\Type::getType('string'), array()
            ),
            array()
        );
        $tableDiff->renamedColumns['renamed'] = new \Doctrine\DBAL\Schema\Column('renamed2', \Doctrine\DBAL\Types\Type::getType('integer'), array());

        $this->_platform->getAlterTableSQL($tableDiff);
    }
420

421 422 423 424 425
    /**
     * @group DBAL-42
     */
    public function testCreateTableColumnComments()
    {
426
        $table = new Table('test');
427 428 429 430 431 432 433 434 435 436 437
        $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
        $table->setPrimaryKey(array('id'));

        $this->assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
    }

    /**
     * @group DBAL-42
     */
    public function testAlterTableColumnComments()
    {
438
        $tableDiff = new TableDiff('mytable');
439
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
440 441 442 443 444 445
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
            'foo', new \Doctrine\DBAL\Schema\Column(
                'foo', \Doctrine\DBAL\Types\Type::getType('string')
            ),
            array('comment')
        );
446 447 448 449 450 451 452 453 454 455
        $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
            'bar', new \Doctrine\DBAL\Schema\Column(
                'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('comment' => 'B comment')
            ),
            array('comment')
        );

        $this->assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
    }

456 457
    public function testCreateTableColumnTypeComments()
    {
458
        $table = new Table('test');
459 460 461 462 463 464 465
        $table->addColumn('id', 'integer');
        $table->addColumn('data', 'array');
        $table->setPrimaryKey(array('id'));

        $this->assertEquals($this->getCreateTableColumnTypeCommentsSQL(), $this->_platform->getCreateTableSQL($table));
    }

466 467 468 469 470 471 472 473 474
    public function getCreateTableColumnCommentsSQL()
    {
        $this->markTestSkipped('Platform does not support Column comments.');
    }

    public function getAlterTableColumnCommentsSQL()
    {
        $this->markTestSkipped('Platform does not support Column comments.');
    }
475

476 477 478 479 480
    public function getCreateTableColumnTypeCommentsSQL()
    {
        $this->markTestSkipped('Platform does not support Column comments.');
    }

481
    public function testGetDefaultValueDeclarationSQL()
482 483 484 485 486 487 488 489 490 491 492
    {
        // non-timestamp value will get single quotes
        $field = array(
            'type' => 'string',
            'default' => 'non_timestamp'
        );

        $this->assertEquals(" DEFAULT 'non_timestamp'", $this->_platform->getDefaultValueDeclarationSQL($field));
    }

    public function testGetDefaultValueDeclarationSQLDateTime()
493 494 495 496 497 498 499 500 501 502 503 504 505 506
    {
        // timestamps on datetime types should not be quoted
        foreach (array('datetime', 'datetimetz') as $type) {

            $field = array(
                'type' => Type::getType($type),
                'default' => $this->_platform->getCurrentTimestampSQL()
            );

            $this->assertEquals(' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(), $this->_platform->getDefaultValueDeclarationSQL($field));

        }
    }

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    public function testGetDefaultValueDeclarationSQLForIntegerTypes()
    {
        foreach(array('bigint', 'integer', 'smallint') as $type) {
            $field = array(
                'type'    => Type::getType($type),
                'default' => 1
            );

            $this->assertEquals(
                ' DEFAULT 1',
                $this->_platform->getDefaultValueDeclarationSQL($field)
            );
        }
    }

522 523 524 525 526 527 528
    /**
     * @group DBAL-45
     */
    public function testKeywordList()
    {
        $keywordList = $this->_platform->getReservedKeywordsList();
        $this->assertInstanceOf('Doctrine\DBAL\Platforms\Keywords\KeywordList', $keywordList);
529

530 531
        $this->assertTrue($keywordList->isKeyword('table'));
    }
532 533 534 535 536 537 538

    /**
     * @group DBAL-374
     */
    public function testQuotedColumnInPrimaryKeyPropagation()
    {
        $table = new Table('`quoted`');
539 540
        $table->addColumn('create', 'string');
        $table->setPrimaryKey(array('create'));
541 542 543 544 545 546 547

        $sql = $this->_platform->getCreateTableSQL($table);
        $this->assertEquals($this->getQuotedColumnInPrimaryKeySQL(), $sql);
    }

    abstract protected function getQuotedColumnInPrimaryKeySQL();
    abstract protected function getQuotedColumnInIndexSQL();
Markus Fasselt's avatar
Markus Fasselt committed
548
    abstract protected function getQuotedNameInIndexSQL();
549
    abstract protected function getQuotedColumnInForeignKeySQL();
550 551 552 553 554 555 556

    /**
     * @group DBAL-374
     */
    public function testQuotedColumnInIndexPropagation()
    {
        $table = new Table('`quoted`');
557 558
        $table->addColumn('create', 'string');
        $table->addIndex(array('create'));
559 560 561 562

        $sql = $this->_platform->getCreateTableSQL($table);
        $this->assertEquals($this->getQuotedColumnInIndexSQL(), $sql);
    }
563

Markus Fasselt's avatar
Markus Fasselt committed
564 565 566 567 568 569 570 571 572 573
    public function testQuotedNameInIndexSQL()
    {
        $table = new Table('test');
        $table->addColumn('column1', 'string');
        $table->addIndex(array('column1'), '`key`');

        $sql = $this->_platform->getCreateTableSQL($table);
        $this->assertEquals($this->getQuotedNameInIndexSQL(), $sql);
    }

574 575 576 577 578 579 580 581
    /**
     * @group DBAL-374
     */
    public function testQuotedColumnInForeignKeyPropagation()
    {
        $table = new Table('`quoted`');
        $table->addColumn('create', 'string');
        $table->addColumn('foo', 'string');
582
        $table->addColumn('`bar`', 'string');
583

584
        // Foreign table with reserved keyword as name (needs quotation).
585
        $foreignTable = new Table('foreign');
586 587 588
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_RESERVED_KEYWORD');

        // Foreign table with non-reserved keyword as name (does not need quotation).
        $foreignTable = new Table('foo');
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).

        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_NON_RESERVED_KEYWORD');

        // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
        $foreignTable = new Table('`foo-bar`');
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).

        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_INTENDED_QUOTATION');
607 608 609 610

        $sql = $this->_platform->getCreateTableSQL($table, AbstractPlatform::CREATE_FOREIGNKEYS);
        $this->assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
    }
611

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    /**
     * @group DBAL-1051
     */
    public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
    {
        $index = new Index('select', array('foo'), true);

        $this->assertSame(
            $this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
            $this->_platform->getUniqueConstraintDeclarationSQL('select', $index)
        );
    }

    /**
     * @return string
     */
    abstract protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL();

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    /**
     * @group DBAL-2270
     */
    public function testQuotesReservedKeywordInTruncateTableSQL()
    {
        $this->assertSame(
            $this->getQuotesReservedKeywordInTruncateTableSQL(),
            $this->_platform->getTruncateTableSQL('select')
        );
    }

    /**
     * @return string
     */
    abstract protected function getQuotesReservedKeywordInTruncateTableSQL();

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
    /**
     * @group DBAL-1051
     */
    public function testQuotesReservedKeywordInIndexDeclarationSQL()
    {
        $index = new Index('select', array('foo'));

        if (! $this->supportsInlineIndexDeclaration()) {
            $this->setExpectedException('Doctrine\DBAL\DBALException');
        }

        $this->assertSame(
            $this->getQuotesReservedKeywordInIndexDeclarationSQL(),
            $this->_platform->getIndexDeclarationSQL('select', $index)
        );
    }

    /**
     * @return string
     */
    abstract protected function getQuotesReservedKeywordInIndexDeclarationSQL();

    /**
     * @return boolean
     */
    protected function supportsInlineIndexDeclaration()
    {
        return true;
    }

676 677 678 679 680 681 682 683
    /**
     * @expectedException \Doctrine\DBAL\DBALException
     */
    public function testGetCreateSchemaSQL()
    {
        $this->_platform->getCreateSchemaSQL('schema');
    }

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    /**
     * @group DBAL-585
     */
    public function testAlterTableChangeQuotedColumn()
    {
        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
        $tableDiff->fromTable = new \Doctrine\DBAL\Schema\Table('mytable');
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
            'select', new \Doctrine\DBAL\Schema\Column(
                'select', \Doctrine\DBAL\Types\Type::getType('string')
            ),
            array('type')
        );

        $this->assertContains(
            $this->_platform->quoteIdentifier('select'),
            implode(';', $this->_platform->getAlterTableSQL($tableDiff))
        );
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
703

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    /**
     * @group DBAL-563
     */
    public function testUsesSequenceEmulatedIdentityColumns()
    {
        $this->assertFalse($this->_platform->usesSequenceEmulatedIdentityColumns());
    }

    /**
     * @group DBAL-563
     * @expectedException \Doctrine\DBAL\DBALException
     */
    public function testReturnsIdentitySequenceName()
    {
        $this->_platform->getIdentitySequenceName('mytable', 'mycolumn');
    }
Steve Müller's avatar
Steve Müller committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

    public function testReturnsBinaryDefaultLength()
    {
        $this->assertSame($this->getBinaryDefaultLength(), $this->_platform->getBinaryDefaultLength());
    }

    protected function getBinaryDefaultLength()
    {
        return 255;
    }

    public function testReturnsBinaryMaxLength()
    {
        $this->assertSame($this->getBinaryMaxLength(), $this->_platform->getBinaryMaxLength());
    }

    protected function getBinaryMaxLength()
    {
        return 4000;
    }

    /**
     * @expectedException \Doctrine\DBAL\DBALException
     */
    public function testReturnsBinaryTypeDeclarationSQL()
    {
        $this->_platform->getBinaryTypeDeclarationSQL(array());
    }
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

    /**
     * @group DBAL-553
     */
    public function hasNativeJsonType()
    {
        $this->assertFalse($this->_platform->hasNativeJsonType());
    }

    /**
     * @group DBAL-553
     */
    public function testReturnsJsonTypeDeclarationSQL()
    {
        $column = array(
            'length'  => 666,
            'notnull' => true,
            'type'    => Type::getType('json_array'),
        );

        $this->assertSame(
            $this->_platform->getClobTypeDeclarationSQL($column),
            $this->_platform->getJsonTypeDeclarationSQL($column)
        );
    }
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

    /**
     * @group DBAL-234
     */
    public function testAlterTableRenameIndex()
    {
        $tableDiff = new TableDiff('mytable');
        $tableDiff->fromTable = new Table('mytable');
        $tableDiff->fromTable->addColumn('id', 'integer');
        $tableDiff->fromTable->setPrimaryKey(array('id'));
        $tableDiff->renamedIndexes = array(
            'idx_foo' => new Index('idx_bar', array('id'))
        );

        $this->assertSame(
            $this->getAlterTableRenameIndexSQL(),
            $this->_platform->getAlterTableSQL($tableDiff)
        );
    }

    /**
     * @group DBAL-234
     */
    protected function getAlterTableRenameIndexSQL()
    {
        return array(
            'DROP INDEX idx_foo',
            'CREATE INDEX idx_bar ON mytable (id)',
        );
    }

    /**
     * @group DBAL-234
     */
    public function testQuotesAlterTableRenameIndex()
    {
        $tableDiff = new TableDiff('table');
        $tableDiff->fromTable = new Table('table');
        $tableDiff->fromTable->addColumn('id', 'integer');
        $tableDiff->fromTable->setPrimaryKey(array('id'));
        $tableDiff->renamedIndexes = array(
            'create' => new Index('select', array('id')),
            '`foo`'  => new Index('`bar`', array('id')),
        );

        $this->assertSame(
            $this->getQuotedAlterTableRenameIndexSQL(),
            $this->_platform->getAlterTableSQL($tableDiff)
        );
    }

    /**
     * @group DBAL-234
     */
    protected function getQuotedAlterTableRenameIndexSQL()
    {
        return array(
            'DROP INDEX "create"',
            'CREATE INDEX "select" ON "table" (id)',
            'DROP INDEX "foo"',
            'CREATE INDEX "bar" ON "table" (id)',
        );
    }
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

    /**
     * @group DBAL-835
     */
    public function testQuotesAlterTableRenameColumn()
    {
        $fromTable = new Table('mytable');

        $fromTable->addColumn('unquoted1', 'integer', array('comment' => 'Unquoted 1'));
        $fromTable->addColumn('unquoted2', 'integer', array('comment' => 'Unquoted 2'));
        $fromTable->addColumn('unquoted3', 'integer', array('comment' => 'Unquoted 3'));

        $fromTable->addColumn('create', 'integer', array('comment' => 'Reserved keyword 1'));
        $fromTable->addColumn('table', 'integer', array('comment' => 'Reserved keyword 2'));
        $fromTable->addColumn('select', 'integer', array('comment' => 'Reserved keyword 3'));

        $fromTable->addColumn('`quoted1`', 'integer', array('comment' => 'Quoted 1'));
        $fromTable->addColumn('`quoted2`', 'integer', array('comment' => 'Quoted 2'));
        $fromTable->addColumn('`quoted3`', 'integer', array('comment' => 'Quoted 3'));

        $toTable = new Table('mytable');

        $toTable->addColumn('unquoted', 'integer', array('comment' => 'Unquoted 1')); // unquoted -> unquoted
        $toTable->addColumn('where', 'integer', array('comment' => 'Unquoted 2')); // unquoted -> reserved keyword
        $toTable->addColumn('`foo`', 'integer', array('comment' => 'Unquoted 3')); // unquoted -> quoted

        $toTable->addColumn('reserved_keyword', 'integer', array('comment' => 'Reserved keyword 1')); // reserved keyword -> unquoted
        $toTable->addColumn('from', 'integer', array('comment' => 'Reserved keyword 2')); // reserved keyword -> reserved keyword
        $toTable->addColumn('`bar`', 'integer', array('comment' => 'Reserved keyword 3')); // reserved keyword -> quoted

        $toTable->addColumn('quoted', 'integer', array('comment' => 'Quoted 1')); // quoted -> unquoted
        $toTable->addColumn('and', 'integer', array('comment' => 'Quoted 2')); // quoted -> reserved keyword
        $toTable->addColumn('`baz`', 'integer', array('comment' => 'Quoted 3')); // quoted -> quoted

        $comparator = new Comparator();

        $this->assertEquals(
            $this->getQuotedAlterTableRenameColumnSQL(),
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
        );
    }

    /**
     * Returns SQL statements for {@link testQuotesAlterTableRenameColumn}.
     *
     * @return array
     *
     * @group DBAL-835
     */
    abstract protected function getQuotedAlterTableRenameColumnSQL();
886

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
    /**
     * @group DBAL-835
     */
    public function testQuotesAlterTableChangeColumnLength()
    {
        $fromTable = new Table('mytable');

        $fromTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 10));
        $fromTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 10));
        $fromTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 10));

        $fromTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 10));
        $fromTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 10));
        $fromTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 10));

        $toTable = new Table('mytable');

        $toTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 255));
        $toTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 255));
        $toTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 255));

        $toTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 255));
        $toTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 255));
        $toTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 255));

        $comparator = new Comparator();

        $this->assertEquals(
            $this->getQuotedAlterTableChangeColumnLengthSQL(),
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
        );
    }

    /**
     * Returns SQL statements for {@link testQuotesAlterTableChangeColumnLength}.
     *
     * @return array
     *
     * @group DBAL-835
     */
    abstract protected function getQuotedAlterTableChangeColumnLengthSQL();

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
    /**
     * @group DBAL-807
     */
    public function testAlterTableRenameIndexInSchema()
    {
        $tableDiff = new TableDiff('myschema.mytable');
        $tableDiff->fromTable = new Table('myschema.mytable');
        $tableDiff->fromTable->addColumn('id', 'integer');
        $tableDiff->fromTable->setPrimaryKey(array('id'));
        $tableDiff->renamedIndexes = array(
            'idx_foo' => new Index('idx_bar', array('id'))
        );

        $this->assertSame(
            $this->getAlterTableRenameIndexInSchemaSQL(),
            $this->_platform->getAlterTableSQL($tableDiff)
        );
    }

    /**
     * @group DBAL-807
     */
    protected function getAlterTableRenameIndexInSchemaSQL()
    {
        return array(
            'DROP INDEX idx_foo',
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
        );
    }

    /**
     * @group DBAL-807
     */
    public function testQuotesAlterTableRenameIndexInSchema()
    {
        $tableDiff = new TableDiff('`schema`.table');
        $tableDiff->fromTable = new Table('`schema`.table');
        $tableDiff->fromTable->addColumn('id', 'integer');
        $tableDiff->fromTable->setPrimaryKey(array('id'));
        $tableDiff->renamedIndexes = array(
            'create' => new Index('select', array('id')),
            '`foo`'  => new Index('`bar`', array('id')),
        );

        $this->assertSame(
            $this->getQuotedAlterTableRenameIndexInSchemaSQL(),
            $this->_platform->getAlterTableSQL($tableDiff)
        );
    }

    /**
     * @group DBAL-234
     */
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
    {
        return array(
            'DROP INDEX "schema"."create"',
            'CREATE INDEX "select" ON "schema"."table" (id)',
            'DROP INDEX "schema"."foo"',
            'CREATE INDEX "bar" ON "schema"."table" (id)',
        );
    }
991

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    /**
     * @group DBAL-1237
     */
    public function testQuotesDropForeignKeySQL()
    {
        if (! $this->_platform->supportsForeignKeyConstraints()) {
            $this->markTestSkipped(
                sprintf('%s does not support foreign key constraints.', get_class($this->_platform))
            );
        }

        $tableName = 'table';
        $table = new Table($tableName);
        $foreignKeyName = 'select';
        $foreignKey = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
        $expectedSql = $this->getQuotesDropForeignKeySQL();

        $this->assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKeyName, $tableName));
        $this->assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKey, $table));
    }

    protected function getQuotesDropForeignKeySQL()
    {
        return 'ALTER TABLE "table" DROP FOREIGN KEY "select"';
    }

    /**
     * @group DBAL-1237
     */
    public function testQuotesDropConstraintSQL()
    {
        $tableName = 'table';
        $table = new Table($tableName);
        $constraintName = 'select';
        $constraint = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
        $expectedSql = $this->getQuotesDropConstraintSQL();

        $this->assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraintName, $tableName));
        $this->assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraint, $table));
    }

    protected function getQuotesDropConstraintSQL()
    {
        return 'ALTER TABLE "table" DROP CONSTRAINT "select"';
    }

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    protected function getStringLiteralQuoteCharacter()
    {
        return "'";
    }

    public function testGetStringLiteralQuoteCharacter()
    {
        $this->assertSame($this->getStringLiteralQuoteCharacter(), $this->_platform->getStringLiteralQuoteCharacter());
    }

1048 1049 1050 1051 1052 1053
    protected function getQuotedCommentOnColumnSQLWithoutQuoteCharacter()
    {
        return "COMMENT ON COLUMN mytable.id IS 'This is a comment'";
    }

    public function testGetCommentOnColumnSQLWithoutQuoteCharacter()
1054 1055
    {
        $this->assertEquals(
1056
            $this->getQuotedCommentOnColumnSQLWithoutQuoteCharacter(),
1057 1058
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', 'This is a comment')
        );
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    }

    protected function getQuotedCommentOnColumnSQLWithQuoteCharacter()
    {
        return "COMMENT ON COLUMN mytable.id IS 'It''s a quote !'";
    }

    public function testGetCommentOnColumnSQLWithQuoteCharacter()
    {
        $c = $this->getStringLiteralQuoteCharacter();
1069 1070

        $this->assertEquals(
1071 1072
            $this->getQuotedCommentOnColumnSQLWithQuoteCharacter(),
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', "It" . $c . "s a quote !")
1073 1074 1075
        );
    }

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    /**
     * @return array
     *
     * @see testGetCommentOnColumnSQL
     */
    abstract protected function getCommentOnColumnSQL();

    /**
     * @group DBAL-1004
     */
    public function testGetCommentOnColumnSQL()
    {
        $this->assertSame(
            $this->getCommentOnColumnSQL(),
            array(
                $this->_platform->getCommentOnColumnSQL('foo', 'bar', 'comment'), // regular identifiers
                $this->_platform->getCommentOnColumnSQL('`Foo`', '`BAR`', 'comment'), // explicitly quoted identifiers
                $this->_platform->getCommentOnColumnSQL('select', 'from', 'comment'), // reserved keyword identifiers
            )
        );
    }

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    /**
     * @group DBAL-1176
     *
     * @dataProvider getGeneratesInlineColumnCommentSQL
     */
    public function testGeneratesInlineColumnCommentSQL($comment, $expectedSql)
    {
        if (! $this->_platform->supportsInlineColumnComments()) {
            $this->markTestSkipped(sprintf('%s does not support inline column comments.', get_class($this->_platform)));
        }

        $this->assertSame($expectedSql, $this->_platform->getInlineColumnCommentSQL($comment));
    }

    public function getGeneratesInlineColumnCommentSQL()
    {
        return array(
            'regular comment' => array('Regular comment', $this->getInlineColumnRegularCommentSQL()),
            'comment requiring escaping' => array(
                sprintf(
                    'Using inline comment delimiter %s works',
                    $this->getInlineColumnCommentDelimiter()
                ),
                $this->getInlineColumnCommentRequiringEscapingSQL()
            ),
            'empty comment' => array('', $this->getInlineColumnEmptyCommentSQL()),
        );
    }

    protected function getInlineColumnCommentDelimiter()
    {
        return "'";
    }

    protected function getInlineColumnRegularCommentSQL()
    {
        return "COMMENT 'Regular comment'";
    }

    protected function getInlineColumnCommentRequiringEscapingSQL()
    {
        return "COMMENT 'Using inline comment delimiter '' works'";
    }

    protected function getInlineColumnEmptyCommentSQL()
    {
        return "COMMENT ''";
    }

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
    protected function getQuotedStringLiteralWithoutQuoteCharacter()
    {
        return "'No quote'";
    }

    protected function getQuotedStringLiteralWithQuoteCharacter()
    {
        return "'It''s a quote'";
    }

    protected function getQuotedStringLiteralQuoteCharacter()
    {
        return "''''";
    }

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    /**
     * @group DBAL-1176
     */
    public function testThrowsExceptionOnGeneratingInlineColumnCommentSQLIfUnsupported()
    {
        if ($this->_platform->supportsInlineColumnComments()) {
            $this->markTestSkipped(sprintf('%s supports inline column comments.', get_class($this->_platform)));
        }

        $this->setExpectedException(
            'Doctrine\DBAL\DBALException',
            "Operation 'Doctrine\\DBAL\\Platforms\\AbstractPlatform::getInlineColumnCommentSQL' is not supported by platform.",
            0
        );

        $this->_platform->getInlineColumnCommentSQL('unsupported');
    }

1180 1181
    public function testQuoteStringLiteral()
    {
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
        $c = $this->getStringLiteralQuoteCharacter();

        $this->assertEquals(
            $this->getQuotedStringLiteralWithoutQuoteCharacter(),
            $this->_platform->quoteStringLiteral('No quote')
        );
        $this->assertEquals(
            $this->getQuotedStringLiteralWithQuoteCharacter(),
            $this->_platform->quoteStringLiteral('It' . $c . 's a quote')
        );
        $this->assertEquals(
            $this->getQuotedStringLiteralQuoteCharacter(),
            $this->_platform->quoteStringLiteral($c)
        );
1196
    }
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206

    /**
     * @group DBAL-423
     *
     * @expectedException \Doctrine\DBAL\DBALException
     */
    public function testReturnsGuidTypeDeclarationSQL()
    {
        $this->_platform->getGuidTypeDeclarationSQL(array());
    }
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

    /**
     * @group DBAL-1010
     */
    public function testGeneratesAlterTableRenameColumnSQL()
    {
        $table = new Table('foo');
        $table->addColumn(
            'bar',
            'integer',
            array('notnull' => true, 'default' => 666, 'comment' => 'rename test')
        );

        $tableDiff = new TableDiff('foo');
        $tableDiff->fromTable = $table;
        $tableDiff->renamedColumns['bar'] = new Column(
            'baz',
            Type::getType('integer'),
            array('notnull' => true, 'default' => 666, 'comment' => 'rename test')
        );

        $this->assertSame($this->getAlterTableRenameColumnSQL(), $this->_platform->getAlterTableSQL($tableDiff));
    }

    /**
     * @return array
     */
    abstract public function getAlterTableRenameColumnSQL();
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

    /**
     * @group DBAL-1016
     */
    public function testQuotesTableIdentifiersInAlterTableSQL()
    {
        $table = new Table('"foo"');
        $table->addColumn('id', 'integer');
        $table->addColumn('fk', 'integer');
        $table->addColumn('fk2', 'integer');
        $table->addColumn('fk3', 'integer');
        $table->addColumn('bar', 'integer');
        $table->addColumn('baz', 'integer');
        $table->addForeignKeyConstraint('fk_table', array('fk'), array('id'), array(), 'fk1');
        $table->addForeignKeyConstraint('fk_table', array('fk2'), array('id'), array(), 'fk2');

        $tableDiff = new TableDiff('"foo"');
        $tableDiff->fromTable = $table;
        $tableDiff->newName = 'table';
        $tableDiff->addedColumns['bloo'] = new Column('bloo', Type::getType('integer'));
        $tableDiff->changedColumns['bar'] = new ColumnDiff(
            'bar',
            new Column('bar', Type::getType('integer'), array('notnull' => false)),
            array('notnull'),
            $table->getColumn('bar')
        );
        $tableDiff->renamedColumns['id'] = new Column('war', Type::getType('integer'));
        $tableDiff->removedColumns['baz'] = new Column('baz', Type::getType('integer'));
        $tableDiff->addedForeignKeys[] = new ForeignKeyConstraint(array('fk3'), 'fk_table', array('id'), 'fk_add');
        $tableDiff->changedForeignKeys[] = new ForeignKeyConstraint(array('fk2'), 'fk_table2', array('id'), 'fk2');
        $tableDiff->removedForeignKeys[] = new ForeignKeyConstraint(array('fk'), 'fk_table', array('id'), 'fk1');

        $this->assertSame(
            $this->getQuotesTableIdentifiersInAlterTableSQL(),
            $this->_platform->getAlterTableSQL($tableDiff)
        );
    }

    /**
     * @return array
     */
    abstract protected function getQuotesTableIdentifiersInAlterTableSQL();
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

    /**
     * @group DBAL-1090
     */
    public function testAlterStringToFixedString()
    {

        $table = new Table('mytable');
        $table->addColumn('name', 'string', array('length' => 2));

        $tableDiff = new TableDiff('mytable');
        $tableDiff->fromTable = $table;

        $tableDiff->changedColumns['name'] = new \Doctrine\DBAL\Schema\ColumnDiff(
            'name', new \Doctrine\DBAL\Schema\Column(
                'name', \Doctrine\DBAL\Types\Type::getType('string'), array('fixed' => true, 'length' => 2)
            ),
            array('fixed')
        );

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

        $expectedSql = $this->getAlterStringToFixedStringSQL();

        $this->assertEquals($expectedSql, $sql);
    }

    /**
     * @return array
     */
    abstract protected function getAlterStringToFixedStringSQL();
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340

    /**
     * @group DBAL-1062
     */
    public function testGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
    {
        $foreignTable = new Table('foreign_table');
        $foreignTable->addColumn('id', 'integer');
        $foreignTable->setPrimaryKey(array('id'));

        $primaryTable = new Table('mytable');
        $primaryTable->addColumn('foo', 'integer');
        $primaryTable->addColumn('bar', 'integer');
        $primaryTable->addColumn('baz', 'integer');
        $primaryTable->addIndex(array('foo'), 'idx_foo');
        $primaryTable->addIndex(array('bar'), 'idx_bar');
        $primaryTable->addForeignKeyConstraint($foreignTable, array('foo'), array('id'), array(), 'fk_foo');
        $primaryTable->addForeignKeyConstraint($foreignTable, array('bar'), array('id'), array(), 'fk_bar');

        $tableDiff = new TableDiff('mytable');
        $tableDiff->fromTable = $primaryTable;
        $tableDiff->renamedIndexes['idx_foo'] = new Index('idx_foo_renamed', array('foo'));

        $this->assertSame(
            $this->getGeneratesAlterTableRenameIndexUsedByForeignKeySQL(),
            $this->_platform->getAlterTableSQL($tableDiff)
        );
    }

    /**
     * @return array
     */
    abstract protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL();
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

    /**
     * @group DBAL-1082
     *
     * @dataProvider getGeneratesDecimalTypeDeclarationSQL
     */
    public function testGeneratesDecimalTypeDeclarationSQL(array $column, $expectedSql)
    {
        $this->assertSame($expectedSql, $this->_platform->getDecimalTypeDeclarationSQL($column));
    }

    /**
     * @return array
     */
    public function getGeneratesDecimalTypeDeclarationSQL()
    {
        return array(
            array(array(), 'NUMERIC(10, 0)'),
            array(array('unsigned' => true), 'NUMERIC(10, 0)'),
            array(array('unsigned' => false), 'NUMERIC(10, 0)'),
            array(array('precision' => 5), 'NUMERIC(5, 0)'),
            array(array('scale' => 5), 'NUMERIC(10, 5)'),
            array(array('precision' => 8, 'scale' => 2), 'NUMERIC(8, 2)'),
        );
    }

    /**
     * @group DBAL-1082
     *
     * @dataProvider getGeneratesFloatDeclarationSQL
     */
    public function testGeneratesFloatDeclarationSQL(array $column, $expectedSql)
    {
        $this->assertSame($expectedSql, $this->_platform->getFloatDeclarationSQL($column));
    }

    /**
     * @return array
     */
    public function getGeneratesFloatDeclarationSQL()
    {
        return array(
            array(array(), 'DOUBLE PRECISION'),
            array(array('unsigned' => true), 'DOUBLE PRECISION'),
            array(array('unsigned' => false), 'DOUBLE PRECISION'),
            array(array('precision' => 5), 'DOUBLE PRECISION'),
            array(array('scale' => 5), 'DOUBLE PRECISION'),
            array(array('precision' => 8, 'scale' => 2), 'DOUBLE PRECISION'),
        );
    }
1391
}