-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Convert some unit tests to integration tests #6820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
morozov
merged 1 commit into
doctrine:4.2.x
from
morozov:alter-table-integration-testing
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Schema; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; | ||
use Doctrine\DBAL\Platforms\DB2Platform; | ||
use Doctrine\DBAL\Platforms\OraclePlatform; | ||
use Doctrine\DBAL\Platforms\SQLitePlatform; | ||
use Doctrine\DBAL\Platforms\SQLServerPlatform; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
class AlterTableTest extends FunctionalTestCase | ||
{ | ||
public function testAddPrimaryKeyOnExistingColumn(): void | ||
{ | ||
if ($this->connection->getDatabasePlatform() instanceof SQLitePlatform) { | ||
self::markTestSkipped( | ||
'SQLite will automatically set up auto-increment behavior on the primary key column, which this test' | ||
. ' does not expect.', | ||
); | ||
} | ||
|
||
$table = new Table('alter_pk'); | ||
$table->addColumn('id', Types::INTEGER); | ||
$table->addColumn('val', Types::INTEGER); | ||
|
||
$this->testMigration($table, static function (Table $table): void { | ||
$table->setPrimaryKey(['id']); | ||
}); | ||
} | ||
|
||
public function testAddPrimaryKeyOnNewAutoIncrementColumn(): void | ||
{ | ||
if ($this->connection->getDatabasePlatform() instanceof DB2Platform) { | ||
self::markTestSkipped( | ||
'IBM DB2 LUW does not support adding identity columns to an existing table.', | ||
); | ||
} | ||
|
||
$table = new Table('alter_pk'); | ||
$table->addColumn('val', Types::INTEGER); | ||
|
||
$this->testMigration($table, static function (Table $table): void { | ||
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); | ||
$table->setPrimaryKey(['id']); | ||
}); | ||
} | ||
|
||
public function testAlterPrimaryKeyFromAutoincrementToNonAutoincrementColumn(): void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ($platform instanceof AbstractMySQLPlatform) { | ||
self::markTestIncomplete( | ||
'DBAL should not allow this migration on MySQL because an auto-increment column must be part of the' | ||
. ' primary key constraint.', | ||
); | ||
} | ||
|
||
if ($platform instanceof SQLitePlatform) { | ||
self::markTestSkipped( | ||
'SQLite does not support auto-increment columns that are not part the primary key constraint', | ||
); | ||
} | ||
|
||
$this->ensureDroppingPrimaryKeyConstraintIsSupported(); | ||
|
||
$table = new Table('alter_pk'); | ||
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); | ||
$table->addColumn('id2', Types::INTEGER); | ||
$table->setPrimaryKey(['id1']); | ||
|
||
$this->testMigration($table, static function (Table $table): void { | ||
$table->dropPrimaryKey(); | ||
$table->setPrimaryKey(['id2']); | ||
}); | ||
} | ||
|
||
public function testDropPrimaryKeyWithAutoincrementColumn(): void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ($platform instanceof AbstractMySQLPlatform) { | ||
self::markTestIncomplete( | ||
'DBAL should not allow this migration on MySQL because an auto-increment column must be part of the' | ||
. ' primary key constraint.', | ||
); | ||
} | ||
|
||
if ($platform instanceof SQLitePlatform) { | ||
self::markTestSkipped( | ||
'SQLite does not support auto-increment columns as part of composite primary key constraint', | ||
); | ||
} | ||
|
||
$this->ensureDroppingPrimaryKeyConstraintIsSupported(); | ||
|
||
$table = new Table('alter_pk'); | ||
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); | ||
$table->addColumn('id2', Types::INTEGER); | ||
$table->setPrimaryKey(['id1', 'id2']); | ||
|
||
$this->testMigration($table, static function (Table $table): void { | ||
$table->dropPrimaryKey(); | ||
}); | ||
} | ||
|
||
public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn(): void | ||
{ | ||
if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) { | ||
self::markTestIncomplete( | ||
'DBAL does not restore the auto-increment attribute after dropping and adding the constraint,' | ||
. ' which is a bug.', | ||
); | ||
} | ||
|
||
$this->ensureDroppingPrimaryKeyConstraintIsSupported(); | ||
|
||
$table = new Table('alter_pk'); | ||
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); | ||
$table->addColumn('id2', Types::INTEGER); | ||
$table->setPrimaryKey(['id1', 'id2']); | ||
|
||
$this->testMigration($table, static function (Table $table): void { | ||
$table->dropPrimaryKey(); | ||
$table->setPrimaryKey(['id1']); | ||
}); | ||
} | ||
|
||
public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn(): void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ($platform instanceof AbstractMySQLPlatform) { | ||
self::markTestIncomplete( | ||
'DBAL does not restore the auto-increment attribute after dropping and adding the constraint,' | ||
. ' which is a bug.', | ||
); | ||
} | ||
|
||
if ($platform instanceof SQLitePlatform) { | ||
self::markTestSkipped( | ||
'SQLite does not support auto-increment columns as part of composite primary key constraint', | ||
); | ||
} | ||
|
||
$this->ensureDroppingPrimaryKeyConstraintIsSupported(); | ||
|
||
$table = new Table('alter_pk'); | ||
$table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); | ||
$table->addColumn('id2', Types::INTEGER); | ||
$table->setPrimaryKey(['id1']); | ||
|
||
$this->testMigration($table, static function (Table $table): void { | ||
$table->dropPrimaryKey(); | ||
$table->setPrimaryKey(['id1', 'id2']); | ||
}); | ||
} | ||
|
||
public function testAddNewColumnToPrimaryKey(): void | ||
{ | ||
$this->ensureDroppingPrimaryKeyConstraintIsSupported(); | ||
|
||
$table = new Table('alter_pk'); | ||
$table->addColumn('id1', Types::INTEGER); | ||
$table->setPrimaryKey(['id1']); | ||
|
||
$this->testMigration($table, static function (Table $table): void { | ||
$table->addColumn('id2', Types::INTEGER); | ||
$table->dropPrimaryKey(); | ||
$table->setPrimaryKey(['id1', 'id2']); | ||
}); | ||
} | ||
|
||
private function ensureDroppingPrimaryKeyConstraintIsSupported(): void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ( | ||
! ($platform instanceof DB2Platform) | ||
&& ! ($platform instanceof OraclePlatform) | ||
&& ! ($platform instanceof SQLServerPlatform) | ||
) { | ||
return; | ||
} | ||
|
||
self::markTestIncomplete( | ||
'Dropping primary key constraint on the currently used database platform is not implemented.', | ||
); | ||
} | ||
|
||
private function testMigration(Table $oldTable, callable $migration): void | ||
{ | ||
$this->dropAndCreateTable($oldTable); | ||
|
||
$newTable = clone $oldTable; | ||
|
||
$migration($newTable); | ||
|
||
$schemaManager = $this->connection->createSchemaManager(); | ||
|
||
$diff = $schemaManager->createComparator() | ||
->compareTables($oldTable, $newTable); | ||
|
||
$schemaManager->alterTable($diff); | ||
|
||
$introspectedTable = $schemaManager->introspectTable($newTable->getName()); | ||
|
||
$diff = $schemaManager->createComparator() | ||
->compareTables($newTable, $introspectedTable); | ||
|
||
self::assertTrue($diff->isEmpty()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not use the
test
prefix on methods that we don't want PHPUnit to pick up as tests. I am aware that PHPUnit will skip the method because it's private, but still: we can probably find a better name.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think using the
test
prefix is a problem – right because PHPUnit will skip it, and in this specific case, the method does contain the body of the test (so the name is appropriate). I'm open to suggestions on renaming the method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to merge as it's not a blocker. Happy to rename this one and potentially other similar methods in a separate PR.