diff --git a/UPGRADE.md b/UPGRADE.md index d9d4a6fe7e3..c9491d67704 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,13 @@ awareness about deprecated code. # Upgrade to 3.2 +## Deprecated schema comparison APIs that don't account for the current database connection and the database platform + +1. Instantiation of the `Comparator` class outside the DBAL is deprecated. Use `SchemaManager::createComparator()` + to create the comparator specific to the current database connection and the database platform. +2. The `Schema::getMigrateFromSql()` and `::getMigrateToSql()` methods are deprecated. Compare the schemas using the + connection-aware comparator and produce the SQL by passing the resulting diff to the target platform. + ## Deprecated reference from `ForeignKeyConstraint` to its local (referencing) `Table`. Reference from `ForeignKeyConstraint` to its local (referencing) `Table` is deprecated as well as the following methods: diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 6e2b1469498..e54453c7247 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -129,4 +129,9 @@ src/Driver/SQLSrv/Statement.php + + + + src/Platforms/*/Comparator.php + diff --git a/psalm.xml.dist b/psalm.xml.dist index 23589226327..8939580290b 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -125,6 +125,10 @@ + + @@ -317,6 +321,8 @@ + + diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index 14b7fd16c34..a00d8c84028 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -1787,23 +1787,13 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE } } - $name = $column->getQuotedName($this); - - $columnData = array_merge($column->toArray(), [ - 'name' => $name, - 'version' => $column->hasPlatformOption('version') ? $column->getPlatformOption('version') : false, - 'comment' => $this->getColumnComment($column), - ]); - - if ($columnData['type'] instanceof Types\StringType && $columnData['length'] === null) { - $columnData['length'] = 255; - } + $columnData = $this->columnToArray($column); if (in_array($column->getName(), $options['primary'], true)) { $columnData['primary'] = true; } - $columns[$name] = $columnData; + $columns[$columnData['name']] = $columnData; } if ($this->_eventManager !== null && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) { @@ -3893,6 +3883,27 @@ final public function escapeStringForLike(string $inputString, string $escapeCha ); } + /** + * @return array An associative array with the name of the properties + * of the column being declared as array indexes. + */ + private function columnToArray(Column $column): array + { + $name = $column->getQuotedName($this); + + $columnData = array_merge($column->toArray(), [ + 'name' => $name, + 'version' => $column->hasPlatformOption('version') ? $column->getPlatformOption('version') : false, + 'comment' => $this->getColumnComment($column), + ]); + + if ($columnData['type'] instanceof Types\StringType && $columnData['length'] === null) { + $columnData['length'] = 255; + } + + return $columnData; + } + /** * @internal */ @@ -3905,4 +3916,30 @@ protected function getLikeWildcardCharacters(): string { return '%_'; } + + /** + * Compares the definitions of the given columns in the context of this platform. + * + * @throws Exception + */ + public function columnsEqual(Column $column1, Column $column2): bool + { + if ( + $this->getColumnDeclarationSQL('', $this->columnToArray($column1)) + !== $this->getColumnDeclarationSQL('', $this->columnToArray($column2)) + ) { + return false; + } + + // If the platform supports inline comments, all comparison is already done above + if ($this->supportsInlineColumnComments()) { + return true; + } + + if ($column1->getComment() !== $column2->getComment()) { + return false; + } + + return $column1->getType() === $column2->getType(); + } } diff --git a/src/Platforms/MySQL/Comparator.php b/src/Platforms/MySQL/Comparator.php new file mode 100644 index 00000000000..1e31baa8152 --- /dev/null +++ b/src/Platforms/MySQL/Comparator.php @@ -0,0 +1,66 @@ +getOptions(), [ + 'charset' => null, + 'collation' => null, + ]); + + if ($defaults !== []) { + $fromTable = clone $fromTable; + $toTable = clone $toTable; + + $this->normalizeColumns($fromTable, $defaults); + $this->normalizeColumns($toTable, $defaults); + } + + return parent::diffTable($fromTable, $toTable); + } + + /** + * @param array $defaults + */ + private function normalizeColumns(Table $table, array $defaults): void + { + foreach ($table->getColumns() as $column) { + $options = $column->getPlatformOptions(); + $diff = array_diff_assoc($options, $defaults); + + if ($diff === $options) { + continue; + } + + $column->setPlatformOptions($diff); + } + } +} diff --git a/src/Platforms/MySQLPlatform.php b/src/Platforms/MySQLPlatform.php index a8b8756077a..4e77d1f8c66 100644 --- a/src/Platforms/MySQLPlatform.php +++ b/src/Platforms/MySQLPlatform.php @@ -370,8 +370,15 @@ public function getListTableMetadataSQL(string $table, ?string $database = null) { return sprintf( <<<'SQL' -SELECT ENGINE, AUTO_INCREMENT, TABLE_COLLATION, TABLE_COMMENT, CREATE_OPTIONS -FROM information_schema.TABLES +SELECT t.ENGINE, + t.AUTO_INCREMENT, + t.TABLE_COMMENT, + t.CREATE_OPTIONS, + t.TABLE_COLLATION, + ccsa.CHARACTER_SET_NAME +FROM information_schema.TABLES t + INNER JOIN information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` ccsa + ON ccsa.COLLATION_NAME = t.TABLE_COLLATION WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = %s AND TABLE_NAME = %s SQL , diff --git a/src/Platforms/SQLServer/Comparator.php b/src/Platforms/SQLServer/Comparator.php new file mode 100644 index 00000000000..8602938bb69 --- /dev/null +++ b/src/Platforms/SQLServer/Comparator.php @@ -0,0 +1,56 @@ +databaseCollation = $databaseCollation; + } + + /** + * {@inheritDoc} + */ + public function diffTable(Table $fromTable, Table $toTable) + { + $fromTable = clone $fromTable; + $toTable = clone $toTable; + + $this->normalizeColumns($fromTable); + $this->normalizeColumns($toTable); + + return parent::diffTable($fromTable, $toTable); + } + + private function normalizeColumns(Table $table): void + { + foreach ($table->getColumns() as $column) { + $options = $column->getPlatformOptions(); + + if (! isset($options['collation']) || $options['collation'] !== $this->databaseCollation) { + continue; + } + + unset($options['collation']); + $column->setPlatformOptions($options); + } + } +} diff --git a/src/Platforms/SQLServer2012Platform.php b/src/Platforms/SQLServer2012Platform.php index 40f9bc639e8..7f7d73e6fa5 100644 --- a/src/Platforms/SQLServer2012Platform.php +++ b/src/Platforms/SQLServer2012Platform.php @@ -1625,6 +1625,16 @@ public function getColumnDeclarationSQL($name, array $column) return $name . ' ' . $columnDef; } + public function columnsEqual(Column $column1, Column $column2): bool + { + if (! parent::columnsEqual($column1, $column2)) { + return false; + } + + return $this->getDefaultValueDeclarationSQL($column1->toArray()) + === $this->getDefaultValueDeclarationSQL($column2->toArray()); + } + protected function getLikeWildcardCharacters(): string { return parent::getLikeWildcardCharacters() . '[]^'; diff --git a/src/Platforms/SQLite/Comparator.php b/src/Platforms/SQLite/Comparator.php new file mode 100644 index 00000000000..d27ee86d783 --- /dev/null +++ b/src/Platforms/SQLite/Comparator.php @@ -0,0 +1,53 @@ +normalizeColumns($fromTable); + $this->normalizeColumns($toTable); + + return parent::diffTable($fromTable, $toTable); + } + + private function normalizeColumns(Table $table): void + { + foreach ($table->getColumns() as $column) { + $options = $column->getPlatformOptions(); + + if (! isset($options['collation']) || strcasecmp($options['collation'], 'binary') !== 0) { + continue; + } + + unset($options['collation']); + $column->setPlatformOptions($options); + } + } +} diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index daf000b2f1f..9866e378376 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -699,7 +699,8 @@ public function alterSchema(SchemaDiff $schemaDiff): void */ public function migrateSchema(Schema $toSchema): void { - $schemaDiff = (new Comparator())->compareSchemas($this->createSchema(), $toSchema); + $schemaDiff = $this->createComparator() + ->compareSchemas($this->createSchema(), $toSchema); $this->alterSchema($schemaDiff); } @@ -1249,4 +1250,9 @@ public function removeDoctrineTypeFromComment($comment, $type) return str_replace('(DC2Type:' . $type . ')', '', $comment); } + + public function createComparator(): Comparator + { + return new Comparator($this->getDatabasePlatform()); + } } diff --git a/src/Schema/Comparator.php b/src/Schema/Comparator.php index 14154b58634..ae3b653be12 100644 --- a/src/Schema/Comparator.php +++ b/src/Schema/Comparator.php @@ -2,6 +2,8 @@ namespace Doctrine\DBAL\Schema; +use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types; use Doctrine\Deprecations\Deprecation; @@ -21,6 +23,27 @@ */ class Comparator { + /** @var AbstractPlatform|null */ + private $platform; + + /** + * @internal The comparator can be only instantiated by a schema manager. + */ + public function __construct(?AbstractPlatform $platform = null) + { + if ($platform === null) { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/4659', + 'Not passing a $platform to %s is deprecated.' + . ' Use AbstractSchemaManager::createComparator() to instantiate the comparator.', + __METHOD__ + ); + } + + $this->platform = $platform; + } + /** * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema. * @@ -30,8 +53,10 @@ class Comparator * * @throws SchemaException */ - public static function compareSchemas(Schema $fromSchema, Schema $toSchema) - { + public static function compareSchemas( + Schema $fromSchema, + Schema $toSchema + ) { $comparator = new self(); $diff = new SchemaDiff(); $diff->fromSchema = $fromSchema; @@ -203,7 +228,7 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2) * * @return TableDiff|false * - * @throws SchemaException + * @throws Exception */ public function diffTable(Table $fromTable, Table $toTable) { @@ -233,14 +258,20 @@ public function diffTable(Table $fromTable, Table $toTable) continue; } + $toColumn = $toTable->getColumn($columnName); + // See if column has changed properties in "to" table. - $changedProperties = $this->diffColumn($column, $toTable->getColumn($columnName)); + $changedProperties = $this->diffColumn($column, $toColumn); - if (count($changedProperties) === 0) { + if ($this->platform !== null) { + if ($this->columnsEqual($column, $toColumn)) { + continue; + } + } elseif (count($changedProperties) === 0) { continue; } - $columnDiff = new ColumnDiff($column->getName(), $toTable->getColumn($columnName), $changedProperties); + $columnDiff = new ColumnDiff($column->getName(), $toColumn, $changedProperties); $columnDiff->fromColumn = $column; $tableDifferences->changedColumns[$column->getName()] = $columnDiff; @@ -329,7 +360,7 @@ private function detectColumnRenamings(TableDiff $tableDifferences) $renameCandidates = []; foreach ($tableDifferences->addedColumns as $addedColumnName => $addedColumn) { foreach ($tableDifferences->removedColumns as $removedColumn) { - if (count($this->diffColumn($addedColumn, $removedColumn)) !== 0) { + if (! $this->columnsEqual($addedColumn, $removedColumn)) { continue; } @@ -435,11 +466,25 @@ public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint return $key1->onDelete() !== $key2->onDelete(); } + /** + * Compares the definitions of the given columns + * + * @throws Exception + */ + public function columnsEqual(Column $column1, Column $column2): bool + { + if ($this->platform === null) { + return $this->diffColumn($column1, $column2) === []; + } + + return $this->platform->columnsEqual($column1, $column2); + } + /** * Returns the difference between the columns * - * If there are differences this method returns $field2, otherwise the - * boolean false. + * If there are differences this method returns the changed properties as a + * string array, otherwise an empty array gets returned. * * @return string[] */ diff --git a/src/Schema/MySQLSchemaManager.php b/src/Schema/MySQLSchemaManager.php index c206ec5a939..3cbdc0cb4b6 100644 --- a/src/Schema/MySQLSchemaManager.php +++ b/src/Schema/MySQLSchemaManager.php @@ -3,6 +3,7 @@ namespace Doctrine\DBAL\Schema; use Doctrine\DBAL\Platforms\MariaDb1027Platform; +use Doctrine\DBAL\Platforms\MySQL; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Types\Type; @@ -349,6 +350,8 @@ public function listTableDetails($name) $table->addOption('collation', $tableOptions['TABLE_COLLATION']); } + $table->addOption('charset', $tableOptions['CHARACTER_SET_NAME']); + if ($tableOptions['AUTO_INCREMENT'] !== null) { $table->addOption('autoincrement', $tableOptions['AUTO_INCREMENT']); } @@ -359,6 +362,11 @@ public function listTableDetails($name) return $table; } + public function createComparator(): Comparator + { + return new MySQL\Comparator($this->getDatabasePlatform()); + } + /** * @return string[]|true[] */ diff --git a/src/Schema/SQLServerSchemaManager.php b/src/Schema/SQLServerSchemaManager.php index 721cfe86381..a26f2083b86 100644 --- a/src/Schema/SQLServerSchemaManager.php +++ b/src/Schema/SQLServerSchemaManager.php @@ -3,6 +3,7 @@ namespace Doctrine\DBAL\Schema; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Platforms\SQLServer; use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Types\Type; use Doctrine\Deprecations\Deprecation; @@ -24,6 +25,9 @@ */ class SQLServerSchemaManager extends AbstractSchemaManager { + /** @var string|null */ + private $databaseCollation; + /** * {@inheritDoc} */ @@ -327,4 +331,32 @@ public function listTableDetails($name): Table return $table; } + + /** + * @throws Exception + */ + public function createComparator(): Comparator + { + return new SQLServer\Comparator($this->getDatabasePlatform(), $this->getDatabaseCollation()); + } + + /** + * @throws Exception + */ + private function getDatabaseCollation(): string + { + if ($this->databaseCollation === null) { + $databaseCollation = $this->_conn->fetchOne( + 'SELECT collation_name FROM sys.databases WHERE name = ' + . $this->_platform->getCurrentDatabaseExpression(), + ); + + // a database is always selected, even if omitted in the connection parameters + assert(is_string($databaseCollation)); + + $this->databaseCollation = $databaseCollation; + } + + return $this->databaseCollation; + } } diff --git a/src/Schema/Schema.php b/src/Schema/Schema.php index ec164cfc21e..47d9a0865c6 100644 --- a/src/Schema/Schema.php +++ b/src/Schema/Schema.php @@ -431,6 +431,8 @@ public function toDropSql(AbstractPlatform $platform) } /** + * @deprecated + * * @return string[] * * @throws SchemaException @@ -443,6 +445,8 @@ public function getMigrateToSql(Schema $toSchema, AbstractPlatform $platform) } /** + * @deprecated + * * @return string[] * * @throws SchemaException diff --git a/src/Schema/SqliteSchemaManager.php b/src/Schema/SqliteSchemaManager.php index de37889dbc9..7f1ff326f74 100644 --- a/src/Schema/SqliteSchemaManager.php +++ b/src/Schema/SqliteSchemaManager.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Platforms\SQLite; use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Types\StringType; use Doctrine\DBAL\Types\TextType; @@ -571,4 +572,9 @@ public function listTableDetails($name): Table return $table; } + + public function createComparator(): Comparator + { + return new SQLite\Comparator($this->getDatabasePlatform()); + } } diff --git a/tests/Functional/Platform/NewPrimaryKeyWithNewAutoIncrementColumnTest.php b/tests/Functional/Platform/NewPrimaryKeyWithNewAutoIncrementColumnTest.php index cfabbb80d98..150daf2bdb2 100644 --- a/tests/Functional/Platform/NewPrimaryKeyWithNewAutoIncrementColumnTest.php +++ b/tests/Functional/Platform/NewPrimaryKeyWithNewAutoIncrementColumnTest.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MySQLPlatform; +use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -29,8 +30,12 @@ protected function setUp(): void * Before the fix for this problem this resulted in a database error: (at least on mysql) * SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto * column and it must be defined as a key + * + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider */ - public function testAlterPrimaryKeyToAutoIncrementColumn(): void + public function testAlterPrimaryKeyToAutoIncrementColumn(callable $comparatorFactory): void { $schemaManager = $this->connection->getSchemaManager(); $schemaManager->tryMethod('dropTable', 'dbal2807'); @@ -49,11 +54,10 @@ public function testAlterPrimaryKeyToAutoIncrementColumn(): void $newTable->dropPrimaryKey(); $newTable->setPrimaryKey(['new_id']); - $diff = (new Comparator())->compare($schema, $newSchema); + $diff = $comparatorFactory($schemaManager) + ->compare($schema, $newSchema); - foreach ($diff->toSql($this->getPlatform()) as $sql) { - $this->connection->executeStatement($sql); - } + $schemaManager->alterSchema($diff); $validationSchema = $schemaManager->createSchema(); $validationTable = $validationSchema->getTable($table->getName()); diff --git a/tests/Functional/Schema/ComparatorTest.php b/tests/Functional/Schema/ComparatorTest.php index aa9faf190c0..cdb70c3170f 100644 --- a/tests/Functional/Schema/ComparatorTest.php +++ b/tests/Functional/Schema/ComparatorTest.php @@ -9,28 +9,27 @@ use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use function array_merge; + class ComparatorTest extends FunctionalTestCase { /** @var AbstractSchemaManager */ private $schemaManager; - /** @var Comparator */ - private $comparator; - protected function setUp(): void { parent::setUp(); $this->schemaManager = $this->connection->getSchemaManager(); - $this->comparator = new Comparator(); } /** - * @param mixed $value + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * @param mixed $value * * @dataProvider defaultValueProvider */ - public function testDefaultValueComparison(string $type, $value): void + public function testDefaultValueComparison(callable $comparatorFactory, string $type, $value): void { $table = new Table('default_value'); $table->addColumn('test', $type, ['default' => $value]); @@ -39,17 +38,23 @@ public function testDefaultValueComparison(string $type, $value): void $onlineTable = $this->schemaManager->listTableDetails('default_value'); - self::assertFalse($this->comparator->diffTable($table, $onlineTable)); + self::assertFalse($comparatorFactory($this->schemaManager)->diffTable($table, $onlineTable)); } /** - * @return mixed[][] + * @return iterable */ public static function defaultValueProvider(): iterable { - return [ - ['integer', 1], - ['boolean', false], - ]; + foreach (ComparatorTestUtils::comparatorProvider() as $comparatorArguments) { + foreach ( + [ + ['integer', 1], + ['boolean', false], + ] as $testArguments + ) { + yield array_merge($comparatorArguments, $testArguments); + } + } } } diff --git a/tests/Functional/Schema/ComparatorTestUtils.php b/tests/Functional/Schema/ComparatorTestUtils.php new file mode 100644 index 00000000000..ad84189b63a --- /dev/null +++ b/tests/Functional/Schema/ComparatorTestUtils.php @@ -0,0 +1,61 @@ +diffTable( + $schemaManager->listTableDetails($table->getName()), + $table + ); + } + + public static function assertDiffNotEmpty(Connection $connection, Comparator $comparator, Table $table): void + { + $schemaManager = $connection->createSchemaManager(); + + $diff = self::diffOnlineAndOfflineTable($schemaManager, $comparator, $table); + + TestCase::assertNotFalse($diff); + + $schemaManager->alterTable($diff); + + TestCase::assertFalse(self::diffOnlineAndOfflineTable($schemaManager, $comparator, $table)); + } + + /** + * @return iterable> + */ + public static function comparatorProvider(): iterable + { + yield 'Generic comparator' => [ + static function (): Comparator { + return new Comparator(); + }, + ]; + + yield 'Platform-specific comparator' => [ + static function (AbstractSchemaManager $schemaManager): Comparator { + return $schemaManager->createComparator(); + }, + ]; + } +} diff --git a/tests/Functional/Schema/MySQL/ComparatorTest.php b/tests/Functional/Schema/MySQL/ComparatorTest.php new file mode 100644 index 00000000000..3ce6e4342b8 --- /dev/null +++ b/tests/Functional/Schema/MySQL/ComparatorTest.php @@ -0,0 +1,146 @@ +platform = $this->connection->getDatabasePlatform(); + + if (! $this->platform instanceof MySQLPlatform) { + self::markTestSkipped(); + } + + $this->schemaManager = $this->connection->createSchemaManager(); + $this->comparator = $this->schemaManager->createComparator(); + } + + /** + * @dataProvider lobColumnProvider + */ + public function testLobLengthIncrementWithinLimit(string $type, int $length): void + { + $table = $this->createLobTable($type, $length - 1); + $this->increaseLobLength($table); + + self::assertFalse(ComparatorTestUtils::diffOnlineAndOfflineTable( + $this->schemaManager, + $this->comparator, + $table + )); + } + + /** + * @dataProvider lobColumnProvider + */ + public function testLobLengthIncrementOverLimit(string $type, int $length): void + { + $table = $this->createLobTable($type, $length); + $this->increaseLobLength($table); + ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table); + } + + /** + * @return iterable + */ + public static function lobColumnProvider(): iterable + { + yield [Types::BLOB, MySQLPlatform::LENGTH_LIMIT_TINYBLOB]; + yield [Types::BLOB, MySQLPlatform::LENGTH_LIMIT_BLOB]; + yield [Types::BLOB, MySQLPlatform::LENGTH_LIMIT_MEDIUMBLOB]; + + yield [Types::TEXT, MySQLPlatform::LENGTH_LIMIT_TINYTEXT]; + yield [Types::TEXT, MySQLPlatform::LENGTH_LIMIT_TEXT]; + yield [Types::TEXT, MySQLPlatform::LENGTH_LIMIT_MEDIUMTEXT]; + } + + /** + * @throws Exception + */ + private function createLobTable(string $type, int $length): Table + { + $table = new Table('comparator_test'); + $table->addColumn('lob', $type)->setLength($length); + + $this->schemaManager->dropAndCreateTable($table); + + return $table; + } + + /** + * @throws Exception + */ + private function increaseLobLength(Table $table): void + { + $column = $table->getColumn('lob'); + $column->setLength($column->getLength() + 1); + } + + public function testExplicitDefaultCollation(): void + { + [$table, $column] = $this->createCollationTable(); + $column->setPlatformOption('collation', 'utf8mb4_general_ci'); + + self::assertFalse(ComparatorTestUtils::diffOnlineAndOfflineTable( + $this->schemaManager, + $this->comparator, + $table + )); + } + + public function testChangeColumnCharsetAndCollation(): void + { + [$table, $column] = $this->createCollationTable(); + $column->setPlatformOption('charset', 'utf8'); + $column->setPlatformOption('collation', 'utf8_bin'); + + ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table); + } + + public function testChangeColumnCollation(): void + { + [$table, $column] = $this->createCollationTable(); + $column->setPlatformOption('collation', 'utf8mb4_bin'); + + ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table); + } + + /** + * @return array{Table,Column} + * + * @throws Exception + */ + private function createCollationTable(): array + { + $table = new Table('comparator_test'); + $table->addOption('charset', 'utf8mb4'); + $table->addOption('collate', 'utf8mb4_general_ci'); + $column = $table->addColumn('id', Types::STRING); + $this->schemaManager->dropAndCreateTable($table); + + return [$table, $column]; + } +} diff --git a/tests/Functional/Schema/MySQLSchemaManagerTest.php b/tests/Functional/Schema/MySQLSchemaManagerTest.php index 255823e8abd..01550880f0e 100644 --- a/tests/Functional/Schema/MySQLSchemaManagerTest.php +++ b/tests/Functional/Schema/MySQLSchemaManagerTest.php @@ -6,7 +6,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MySQLPlatform; -use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\Functional\Schema\MySQL\PointType; @@ -44,7 +43,8 @@ public function testSwitchPrimaryKeyColumns(): void $tableNew = clone $tableFetched; $tableNew->setPrimaryKey(['bar_id', 'foo_id']); - $diff = (new Comparator())->diffTable($tableFetched, $tableNew); + $diff = $this->schemaManager->createComparator() + ->diffTable($tableFetched, $tableNew); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -74,7 +74,8 @@ public function testDiffTableBug(): void $this->schemaManager->createTable($table); $tableFetched = $this->schemaManager->listTableDetails('diffbug_routing_translations'); - $diff = (new Comparator())->diffTable($tableFetched, $table); + $diff = $this->schemaManager->createComparator() + ->diffTable($tableFetched, $table); self::assertFalse($diff, 'no changes expected.'); } @@ -141,7 +142,8 @@ public function testAlterTableAddPrimaryKey(): void $diffTable->dropIndex('idx_id'); $diffTable->setPrimaryKey(['id']); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $this->schemaManager->createComparator() + ->diffTable($table, $diffTable); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -165,7 +167,8 @@ public function testDropPrimaryKeyWithAutoincrementColumn(): void $diffTable->dropPrimaryKey(); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $this->schemaManager->createComparator() + ->diffTable($table, $diffTable); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -201,7 +204,8 @@ public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes(): vo self::assertNull($onlineTable->getColumn('def_blob_null')->getDefault()); self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull()); - $diff = (new Comparator())->diffTable($table, $onlineTable); + $diff = $this->schemaManager->createComparator() + ->diffTable($table, $onlineTable); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -245,7 +249,8 @@ public function testAlterColumnCharset(): void $diffTable = clone $table; $diffTable->getColumn('col_text')->setPlatformOption('charset', 'ascii'); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $this->schemaManager->createComparator() + ->diffTable($table, $diffTable); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -363,10 +368,8 @@ public function testDiffListGuidTableColumn(): void $onlineTable = $this->schemaManager->listTableDetails('list_guid_table_column'); - $comparator = new Comparator(); - self::assertFalse( - $comparator->diffTable($offlineTable, $onlineTable), + $this->schemaManager->createComparator()->diffTable($onlineTable, $offlineTable), 'No differences should be detected with the offline vs online schema.' ); } @@ -435,9 +438,7 @@ public function testColumnDefaultCurrentTimestamp(): void self::assertSame($currentTimeStampSql, $onlineTable->getColumn('col_datetime')->getDefault()); self::assertSame($currentTimeStampSql, $onlineTable->getColumn('col_datetime_nullable')->getDefault()); - $comparator = new Comparator(); - - $diff = $comparator->diffTable($table, $onlineTable); + $diff = $this->schemaManager->createComparator()->diffTable($table, $onlineTable); self::assertFalse($diff, 'Tables should be identical with column defaults.'); } @@ -511,9 +512,7 @@ public function testColumnDefaultValuesCurrentTimeAndDate(): void self::assertSame($currentDateSql, $onlineTable->getColumn('col_date')->getDefault()); self::assertSame($currentTimeSql, $onlineTable->getColumn('col_time')->getDefault()); - $comparator = new Comparator(); - - $diff = $comparator->diffTable($table, $onlineTable); + $diff = $this->schemaManager->createComparator()->diffTable($table, $onlineTable); self::assertFalse($diff, 'Tables should be identical with column defauts time and date.'); } diff --git a/tests/Functional/Schema/OracleSchemaManagerTest.php b/tests/Functional/Schema/OracleSchemaManagerTest.php index b70632e0385..51cc5a1a5ba 100644 --- a/tests/Functional/Schema/OracleSchemaManagerTest.php +++ b/tests/Functional/Schema/OracleSchemaManagerTest.php @@ -4,8 +4,9 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; -use Doctrine\DBAL\Schema; +use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Comparator; +use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\TestUtil; use Doctrine\DBAL\Types\BinaryType; @@ -75,7 +76,12 @@ public function testListTableWithBinary(): void self::assertFalse($table->getColumn('column_binary')->getFixed()); } - public function testAlterTableColumnNotNull(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testAlterTableColumnNotNull(callable $comparatorFactory): void { $tableName = 'list_table_column_notnull'; $table = new Table($tableName); @@ -97,7 +103,7 @@ public function testAlterTableColumnNotNull(): void $diffTable->changeColumn('foo', ['notnull' => false]); $diffTable->changeColumn('bar', ['length' => 1024]); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparatorFactory($this->schemaManager)->diffTable($table, $diffTable); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -262,7 +268,7 @@ public function testListTableIndexesPrimaryKeyConstraintNameDiffersFromIndexName // Adding a primary key on already indexed columns // Oracle will reuse the unique index, which cause a constraint name differing from the index name $this->schemaManager->createConstraint( - new Schema\Index('id_pk_id_index', ['id'], true, true), + new Index('id_pk_id_index', ['id'], true, true), 'list_table_indexes_pk_id_test' ); diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index 6c11c178f22..0b067215dbe 100644 --- a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php +++ b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php @@ -4,18 +4,20 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\PostgreSQL94Platform; -use Doctrine\DBAL\Schema; +use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\PostgreSQLSchemaManager; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; +use Doctrine\DBAL\Schema\View; use Doctrine\DBAL\Types\BlobType; use Doctrine\DBAL\Types\DecimalType; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use function array_map; +use function array_merge; use function array_pop; use function array_unshift; use function assert; @@ -81,7 +83,12 @@ public function testDetectsAutoIncrement(): void self::assertTrue($autoincTable->getColumn('id')->getAutoincrement()); } - public function testAlterTableAutoIncrementAdd(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testAlterTableAutoIncrementAdd(callable $comparatorFactory): void { // see https://github.com/doctrine/dbal/issues/4745 $this->schemaManager->tryMethod('dropSequence', 'autoinc_table_add_id_seq'); @@ -96,10 +103,11 @@ public function testAlterTableAutoIncrementAdd(): void $column = $tableTo->addColumn('id', 'integer'); $column->setAutoincrement(true); - $diff = (new Comparator())->diffTable($tableFrom, $tableTo); + $platform = $this->schemaManager->getDatabasePlatform(); + $diff = $comparatorFactory($this->schemaManager)->diffTable($tableFrom, $tableTo); self::assertNotFalse($diff); - $sql = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff); + $sql = $platform->getAlterTableSQL($diff); self::assertEquals([ 'CREATE SEQUENCE autoinc_table_add_id_seq', "SELECT setval('autoinc_table_add_id_seq', (SELECT MAX(id) FROM autoinc_table_add))", @@ -111,7 +119,12 @@ public function testAlterTableAutoIncrementAdd(): void self::assertTrue($tableFinal->getColumn('id')->getAutoincrement()); } - public function testAlterTableAutoIncrementDrop(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testAlterTableAutoIncrementDrop(callable $comparatorFactory): void { $tableFrom = new Table('autoinc_table_drop'); $column = $tableFrom->addColumn('id', 'integer'); @@ -123,12 +136,13 @@ public function testAlterTableAutoIncrementDrop(): void $tableTo = new Table('autoinc_table_drop'); $tableTo->addColumn('id', 'integer'); - $diff = (new Comparator())->diffTable($tableFrom, $tableTo); + $platform = $this->schemaManager->getDatabasePlatform(); + $diff = $comparatorFactory($this->schemaManager)->diffTable($tableFrom, $tableTo); self::assertNotFalse($diff); self::assertEquals( ['ALTER TABLE autoinc_table_drop ALTER id DROP DEFAULT'], - $this->connection->getDatabasePlatform()->getAlterTableSQL($diff) + $platform->getAlterTableSQL($diff) ); $this->schemaManager->alterTable($diff); @@ -270,7 +284,12 @@ public function testDefaultValueCharacterVarying(): void self::assertEquals('foo', $databaseTable->getColumn('def')->getDefault()); } - public function testBooleanDefault(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testBooleanDefault(callable $comparatorFactory): void { $table = new Table('ddc2843_bools'); $table->addColumn('id', 'integer'); @@ -280,8 +299,7 @@ public function testBooleanDefault(): void $databaseTable = $this->schemaManager->listTableDetails($table->getName()); - $c = new Comparator(); - $diff = $c->diffTable($table, $databaseTable); + $diff = $comparatorFactory($this->schemaManager)->diffTable($table, $databaseTable); self::assertFalse($diff); } @@ -309,7 +327,7 @@ public function testListTableWithBinary(): void public function testListQuotedTable(): void { - $offlineTable = new Schema\Table('user'); + $offlineTable = new Table('user'); $offlineTable->addColumn('id', 'integer'); $offlineTable->addColumn('username', 'string'); $offlineTable->addColumn('fk', 'integer'); @@ -320,7 +338,7 @@ public function testListQuotedTable(): void $onlineTable = $this->schemaManager->listTableDetails('"user"'); - $comparator = new Schema\Comparator(); + $comparator = new Comparator(); self::assertFalse($comparator->diffTable($offlineTable, $onlineTable)); } @@ -344,7 +362,7 @@ public function testListTablesExcludesViews(): void $name = 'list_tables_excludes_views_test_view'; $sql = 'SELECT * from list_tables_excludes_views'; - $view = new Schema\View($name, $sql); + $view = new View($name, $sql); $this->schemaManager->dropAndCreateView($view); @@ -365,7 +383,7 @@ public function testListTablesExcludesViews(): void public function testPartialIndexes(): void { - $offlineTable = new Schema\Table('person'); + $offlineTable = new Table('person'); $offlineTable->addColumn('id', 'integer'); $offlineTable->addColumn('name', 'string'); $offlineTable->addColumn('email', 'string'); @@ -375,7 +393,7 @@ public function testPartialIndexes(): void $onlineTable = $this->schemaManager->listTableDetails('person'); - $comparator = new Schema\Comparator(); + $comparator = new Comparator(); self::assertFalse($comparator->diffTable($offlineTable, $onlineTable)); self::assertTrue($onlineTable->hasIndex('simple_partial_index')); @@ -391,7 +409,7 @@ public function testJsonbColumn(): void return; } - $table = new Schema\Table('test_jsonb'); + $table = new Table('test_jsonb'); $table->addColumn('foo', Types::JSON)->setPlatformOption('jsonb', true); $this->schemaManager->dropAndCreateTable($table); @@ -403,7 +421,7 @@ public function testJsonbColumn(): void public function testListNegativeColumnDefaultValue(): void { - $table = new Schema\Table('test_default_negative'); + $table = new Table('test_default_negative'); $table->addColumn('col_smallint', 'smallint', ['default' => -1]); $table->addColumn('col_integer', 'integer', ['default' => -1]); $table->addColumn('col_bigint', 'bigint', ['default' => -1]); @@ -441,7 +459,7 @@ public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValue(stri { $tableName = 'test_serial_type_' . $type; - $table = new Schema\Table($tableName); + $table = new Table($tableName); $table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false]); $this->schemaManager->dropAndCreateTable($table); @@ -458,7 +476,7 @@ public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenW { $tableName = 'test_serial_type_with_default_' . $type; - $table = new Schema\Table($tableName); + $table = new Table($tableName); $table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false, 'default' => 1]); $this->schemaManager->dropAndCreateTable($table); @@ -469,10 +487,16 @@ public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenW } /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * * @dataProvider autoIncrementTypeMigrations */ - public function testAlterTableAutoIncrementIntToBigInt(string $from, string $to, string $expected): void - { + public function testAlterTableAutoIncrementIntToBigInt( + callable $comparatorFactory, + string $from, + string $to, + string $expected + ): void { $tableFrom = new Table('autoinc_type_modification'); $column = $tableFrom->addColumn('id', $from); $column->setAutoincrement(true); @@ -484,8 +508,7 @@ public function testAlterTableAutoIncrementIntToBigInt(string $from, string $to, $column = $tableTo->addColumn('id', $to); $column->setAutoincrement(true); - $c = new Comparator(); - $diff = $c->diffTable($tableFrom, $tableTo); + $diff = $comparatorFactory($this->schemaManager)->diffTable($tableFrom, $tableTo); self::assertInstanceOf(TableDiff::class, $diff); self::assertSame( ['ALTER TABLE autoinc_type_modification ALTER id TYPE ' . $expected], @@ -498,14 +521,20 @@ public function testAlterTableAutoIncrementIntToBigInt(string $from, string $to, } /** - * @return mixed[][] + * @return iterable */ public static function autoIncrementTypeMigrations(): iterable { - return [ - 'int->bigint' => ['integer', 'bigint', 'BIGINT'], - 'bigint->int' => ['bigint', 'integer', 'INT'], - ]; + foreach (ComparatorTestUtils::comparatorProvider() as $comparatorArguments) { + foreach ( + [ + 'int -> bigint' => ['integer', 'bigint', 'BIGINT'], + 'bigint -> int' => ['bigint', 'integer', 'INT'], + ] as $testArguments + ) { + yield array_merge($comparatorArguments, $testArguments); + } + } } } diff --git a/tests/Functional/Schema/SQLite/ComparatorTest.php b/tests/Functional/Schema/SQLite/ComparatorTest.php new file mode 100644 index 00000000000..3b67092211b --- /dev/null +++ b/tests/Functional/Schema/SQLite/ComparatorTest.php @@ -0,0 +1,48 @@ +platform = $this->connection->getDatabasePlatform(); + + if (! $this->platform instanceof SqlitePlatform) { + self::markTestSkipped(); + } + + $this->schemaManager = $this->connection->createSchemaManager(); + $this->comparator = $this->schemaManager->createComparator(); + } + + public function testChangeTableCollation(): void + { + $table = new Table('comparator_test'); + $column = $table->addColumn('id', Types::STRING); + $this->schemaManager->dropAndCreateTable($table); + + $column->setPlatformOption('collation', 'NOCASE'); + ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table); + } +} diff --git a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php index b019f4587ca..bb967fe31eb 100644 --- a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -33,6 +33,7 @@ use function array_filter; use function array_keys; use function array_map; +use function array_merge; use function array_search; use function array_values; use function count; @@ -359,11 +360,16 @@ public function testListTableIndexesDispatchEvent(): void $this->schemaManager->listTableIndexes('list_table_indexes_test'); } - public function testDiffListTableColumns(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testDiffListTableColumns(callable $comparatorFactory): void { if ($this->schemaManager->getDatabasePlatform()->getName() === 'oracle') { self::markTestSkipped( - 'Does not work with Oracle, since it cannot detect DateTime, Date and Time differenecs (at the moment).' + 'Does not work with Oracle, since it cannot detect DateTime, Date and Time differences (at the moment).' ); } @@ -371,8 +377,7 @@ public function testDiffListTableColumns(): void $this->schemaManager->dropAndCreateTable($offlineTable); $onlineTable = $this->schemaManager->listTableDetails('list_table_columns'); - $comparator = new Comparator(); - $diff = $comparator->diffTable($offlineTable, $onlineTable); + $diff = $comparatorFactory($this->schemaManager)->diffTable($onlineTable, $offlineTable); self::assertFalse($diff, 'No differences should be detected with the offline vs online schema.'); } @@ -698,9 +703,16 @@ public function testAutoincrementDetectionMulticolumns(): void self::assertFalse($inferredTable->getColumn('id')->getAutoincrement()); } - public function testUpdateSchemaWithForeignKeyRenaming(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testUpdateSchemaWithForeignKeyRenaming(callable $comparatorFactory): void { - if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) { + $platform = $this->schemaManager->getDatabasePlatform(); + + if (! $platform->supportsForeignKeyConstraints()) { self::markTestSkipped('This test is only supported on platforms that have foreign keys.'); } @@ -730,7 +742,7 @@ public function testUpdateSchemaWithForeignKeyRenaming(): void $tableFKNew->addIndex(['rename_fk_id'], 'fk_idx'); $tableFKNew->addForeignKeyConstraint('test_fk_base', ['rename_fk_id'], ['id']); - $diff = (new Comparator())->diffTable($tableFK, $tableFKNew); + $diff = $comparatorFactory($this->schemaManager)->diffTable($tableFK, $tableFKNew); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -743,9 +755,16 @@ public function testUpdateSchemaWithForeignKeyRenaming(): void self::assertSame(['rename_fk_id'], array_map('strtolower', current($foreignKeys)->getColumns())); } - public function testRenameIndexUsedInForeignKeyConstraint(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testRenameIndexUsedInForeignKeyConstraint(callable $comparatorFactory): void { - if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) { + $platform = $this->schemaManager->getDatabasePlatform(); + + if (! $platform->supportsForeignKeyConstraints()) { self::markTestSkipped('This test is only supported on platforms that have foreign keys.'); } @@ -773,7 +792,7 @@ public function testRenameIndexUsedInForeignKeyConstraint(): void $foreignTable2 = clone $foreignTable; $foreignTable2->renameIndex('rename_index_fk_idx', 'renamed_index_fk_idx'); - $diff = (new Comparator())->diffTable($foreignTable, $foreignTable2); + $diff = $comparatorFactory($this->schemaManager)->diffTable($foreignTable, $foreignTable2); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -1018,7 +1037,12 @@ public function testListForeignKeysComposite(): void self::assertEquals(['id', 'other_id'], array_map('strtolower', $fkeys[0]->getForeignColumns())); } - public function testColumnDefaultLifecycle(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testColumnDefaultLifecycle(callable $comparatorFactory): void { $table = new Table('col_def_lifecycle'); $table->addColumn('id', 'integer', ['autoincrement' => true]); @@ -1054,7 +1078,7 @@ public function testColumnDefaultLifecycle(): void $diffTable->changeColumn('column6', ['default' => 666]); $diffTable->changeColumn('column7', ['default' => null]); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparatorFactory($this->schemaManager)->diffTable($table, $diffTable); self::assertNotFalse($diff); $this->schemaManager->alterTable($diff); @@ -1182,18 +1206,23 @@ public function testCommentNotDuplicated(): void } /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * * @dataProvider getAlterColumnComment */ public function testAlterColumnComment( + callable $comparatorFactory, ?string $comment1, ?string $expectedComment1, ?string $comment2, ?string $expectedComment2 ): void { + $platform = $this->schemaManager->getDatabasePlatform(); + if ( - ! $this->connection->getDatabasePlatform()->supportsInlineColumnComments() && - ! $this->connection->getDatabasePlatform()->supportsCommentOnStatement() && - $this->connection->getDatabasePlatform()->getName() !== 'mssql' + ! $platform->supportsInlineColumnComments() && + ! $platform->supportsCommentOnStatement() && + $platform->getName() !== 'mssql' ) { self::markTestSkipped('Database does not support column comments.'); } @@ -1217,9 +1246,7 @@ public function testAlterColumnComment( $onlineTable->changeColumn('no_comment1', ['comment' => $comment1]); $onlineTable->changeColumn('no_comment2', ['comment' => $comment2]); - $comparator = new Comparator(); - - $tableDiff = $comparator->diffTable($offlineTable, $onlineTable); + $tableDiff = $comparatorFactory($this->schemaManager)->diffTable($offlineTable, $onlineTable); self::assertInstanceOf(TableDiff::class, $tableDiff); @@ -1234,24 +1261,30 @@ public function testAlterColumnComment( } /** - * @return mixed[][] + * @return iterable */ public static function getAlterColumnComment(): iterable { - return [ - [null, null, ' ', ' '], - [null, null, '0', '0'], - [null, null, 'foo', 'foo'], - - ['', null, ' ', ' '], - ['', null, '0', '0'], - ['', null, 'foo', 'foo'], - - [' ', ' ', '0', '0'], - [' ', ' ', 'foo', 'foo'], - - ['0', '0', 'foo', 'foo'], - ]; + foreach (ComparatorTestUtils::comparatorProvider() as $comparatorArguments) { + foreach ( + [ + [null, null, ' ', ' '], + [null, null, '0', '0'], + [null, null, 'foo', 'foo'], + + ['', null, ' ', ' '], + ['', null, '0', '0'], + ['', null, 'foo', 'foo'], + + [' ', ' ', '0', '0'], + [' ', ' ', 'foo', 'foo'], + + ['0', '0', 'foo', 'foo'], + ] as $testArguments + ) { + yield array_merge($comparatorArguments, $testArguments); + } + } } public function testDoesNotListIndexesImplicitlyCreatedByForeignKeys(): void @@ -1281,9 +1314,16 @@ public function testDoesNotListIndexesImplicitlyCreatedByForeignKeys(): void self::assertArrayHasKey('idx_3d6c147fdc58d6c', $indexes); } - public function testComparatorShouldNotAddCommentToJsonTypeSinceItIsTheDefaultNow(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testComparatorShouldNotAddCommentToJsonTypeSinceItIsTheDefaultNow(callable $comparatorFactory): void { - if (! $this->schemaManager->getDatabasePlatform()->hasNativeJsonType()) { + $platform = $this->schemaManager->getDatabasePlatform(); + + if (! $platform->hasNativeJsonType()) { self::markTestSkipped('This test is only supported on platforms that have native JSON type.'); } @@ -1293,8 +1333,8 @@ public function testComparatorShouldNotAddCommentToJsonTypeSinceItIsTheDefaultNo $table = new Table('json_test'); $table->addColumn('parameters', 'json'); - $comparator = new Comparator(); - $tableDiff = $comparator->diffTable($this->schemaManager->listTableDetails('json_test'), $table); + $tableDiff = $comparatorFactory($this->schemaManager) + ->diffTable($this->schemaManager->listTableDetails('json_test'), $table); self::assertFalse($tableDiff); } @@ -1362,9 +1402,16 @@ public function testCreateAndListSequences(): void self::assertEquals($sequence2InitialValue, $actualSequence2->getInitialValue()); } - public function testComparisonWithAutoDetectedSequenceDefinition(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testComparisonWithAutoDetectedSequenceDefinition(callable $comparatorFactory): void { - if (! $this->schemaManager->getDatabasePlatform()->supportsSequences()) { + $platform = $this->schemaManager->getDatabasePlatform(); + + if (! $platform->supportsSequences()) { self::markTestSkipped('This test is only supported on platforms that support sequences.'); } @@ -1386,8 +1433,7 @@ static function (Sequence $sequence) use ($sequenceName): bool { self::assertNotNull($createdSequence); - $comparator = new Comparator(); - $tableDiff = $comparator->diffSequence($createdSequence, $sequence); + $tableDiff = $comparatorFactory($this->schemaManager)->diffSequence($createdSequence, $sequence); self::assertFalse($tableDiff); } diff --git a/tests/Functional/Schema/SqliteSchemaManagerTest.php b/tests/Functional/Schema/SqliteSchemaManagerTest.php index 61b21df3ba1..a639884889c 100644 --- a/tests/Functional/Schema/SqliteSchemaManagerTest.php +++ b/tests/Functional/Schema/SqliteSchemaManagerTest.php @@ -5,8 +5,7 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; -use Doctrine\DBAL\Schema; -use Doctrine\DBAL\Schema\Comparator; +use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\BlobType; use Doctrine\DBAL\Types\Type; @@ -73,21 +72,21 @@ public function testListForeignKeysFromExistingDatabase(): void ); $expected = [ - new Schema\ForeignKeyConstraint( + new ForeignKeyConstraint( ['log'], 'log', [], 'FK_3', ['onUpdate' => 'SET NULL', 'onDelete' => 'NO ACTION', 'deferrable' => false, 'deferred' => false] ), - new Schema\ForeignKeyConstraint( + new ForeignKeyConstraint( ['parent'], 'user', ['id'], '1', ['onUpdate' => 'NO ACTION', 'onDelete' => 'CASCADE', 'deferrable' => false, 'deferred' => false] ), - new Schema\ForeignKeyConstraint( + new ForeignKeyConstraint( ['page'], 'page', ['key'], @@ -101,7 +100,7 @@ public function testListForeignKeysFromExistingDatabase(): void public function testColumnCollation(): void { - $table = new Schema\Table('test_collation'); + $table = new Table('test_collation'); $table->addColumn('id', 'integer'); $table->addColumn('text', 'text'); $table->addColumn('foo', 'text')->setPlatformOption('collation', 'BINARY'); @@ -162,52 +161,9 @@ public function testListTableColumnsWithWhitespacesInTypeDeclarations(): void self::assertSame(100, $columns['bar']->getLength()); } - /** - * @dataProvider getDiffListIntegerAutoincrementTableColumnsData - */ - public function testDiffListIntegerAutoincrementTableColumns( - string $integerType, - bool $unsigned, - bool $expectedComparatorDiff - ): void { - $tableName = 'test_int_autoincrement_table'; - - $offlineTable = new Table($tableName); - $offlineTable->addColumn('id', $integerType, ['autoincrement' => true, 'unsigned' => $unsigned]); - $offlineTable->setPrimaryKey(['id']); - - $this->schemaManager->dropAndCreateTable($offlineTable); - - $onlineTable = $this->schemaManager->listTableDetails($tableName); - - $diff = (new Comparator())->diffTable($offlineTable, $onlineTable); - - if ($expectedComparatorDiff) { - self::assertNotFalse($diff); - self::assertEmpty($this->schemaManager->getDatabasePlatform()->getAlterTableSQL($diff)); - } else { - self::assertFalse($diff); - } - } - - /** - * @return mixed[][] - */ - public static function getDiffListIntegerAutoincrementTableColumnsData(): iterable - { - return [ - ['smallint', false, true], - ['smallint', true, true], - ['integer', false, false], - ['integer', true, true], - ['bigint', false, true], - ['bigint', true, true], - ]; - } - public function testPrimaryKeyNoAutoIncrement(): void { - $table = new Schema\Table('test_pk_auto_increment'); + $table = new Table('test_pk_auto_increment'); $table->addColumn('id', 'integer'); $table->addColumn('text', 'text'); $table->setPrimaryKey(['id']); diff --git a/tests/Functional/Ticket/DBAL461Test.php b/tests/Functional/Ticket/DBAL461Test.php index 6ebec328686..b7af6e7dea4 100644 --- a/tests/Functional/Ticket/DBAL461Test.php +++ b/tests/Functional/Ticket/DBAL461Test.php @@ -3,7 +3,7 @@ namespace Doctrine\DBAL\Tests\Functional\Ticket; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Schema\SQLServerSchemaManager; use Doctrine\DBAL\Types\DecimalType; use PHPUnit\Framework\TestCase; @@ -14,7 +14,7 @@ class DBAL461Test extends TestCase public function testIssue(): void { $conn = $this->createMock(Connection::class); - $platform = $this->getMockForAbstractClass(AbstractPlatform::class); + $platform = $this->getMockForAbstractClass(SQLServer2012Platform::class); $platform->registerDoctrineTypeMapping('numeric', 'decimal'); $schemaManager = new SQLServerSchemaManager($conn, $platform); diff --git a/tests/Functional/Ticket/DBAL510Test.php b/tests/Functional/Ticket/DBAL510Test.php index d65c86359cf..c48aba4a923 100644 --- a/tests/Functional/Ticket/DBAL510Test.php +++ b/tests/Functional/Ticket/DBAL510Test.php @@ -2,6 +2,7 @@ namespace Doctrine\DBAL\Tests\Functional\Ticket; +use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -19,17 +20,23 @@ protected function setUp(): void self::markTestSkipped('PostgreSQL Only test'); } - public function testSearchPathSchemaChanges(): void + /** + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory + * + * @dataProvider \Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils::comparatorProvider + */ + public function testSearchPathSchemaChanges(callable $comparatorFactory): void { $table = new Table('dbal510tbl'); $table->addColumn('id', 'integer'); $table->setPrimaryKey(['id']); - $this->connection->getSchemaManager()->dropAndCreateTable($table); + $schemaManager = $this->connection->getSchemaManager(); + $schemaManager->dropAndCreateTable($table); - $onlineTable = $this->connection->getSchemaManager()->listTableDetails('dbal510tbl'); + $onlineTable = $schemaManager->listTableDetails('dbal510tbl'); - $comparator = new Comparator(); + $comparator = $comparatorFactory($schemaManager); $diff = $comparator->diffTable($onlineTable, $table); self::assertFalse($diff); diff --git a/tests/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Platforms/AbstractMySQLPlatformTestCase.php index cc0591a8776..a9bed36c0d0 100644 --- a/tests/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Platforms/AbstractMySQLPlatformTestCase.php @@ -3,6 +3,7 @@ namespace Doctrine\DBAL\Tests\Platforms; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Platforms\MySQL; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\ForeignKeyConstraint; @@ -172,7 +173,10 @@ protected function getGenerateForeignKeySql(): string return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)'; } - public function testUniquePrimaryKey(): void + /** + * @dataProvider comparatorProvider + */ + public function testUniquePrimaryKey(Comparator $comparator): void { $keyTable = new Table('foo'); $keyTable->addColumn('bar', 'integer'); @@ -184,7 +188,7 @@ public function testUniquePrimaryKey(): void $oldTable->addColumn('bar', 'integer'); $oldTable->addColumn('baz', 'string'); - $diff = (new Comparator())->diffTable($oldTable, $keyTable); + $diff = $comparator->diffTable($oldTable, $keyTable); self::assertNotFalse($diff); $sql = $this->platform->getAlterTableSQL($diff); @@ -377,7 +381,10 @@ public function testBlobTypeDeclarationSQL(): void self::assertEquals('LONGBLOB', $this->platform->getBlobTypeDeclarationSQL([])); } - public function testAlterTableAddPrimaryKey(): void + /** + * @dataProvider comparatorProvider + */ + public function testAlterTableAddPrimaryKey(Comparator $comparator): void { $table = new Table('alter_table_add_pk'); $table->addColumn('id', 'integer'); @@ -389,7 +396,7 @@ public function testAlterTableAddPrimaryKey(): void $diffTable->dropIndex('idx_id'); $diffTable->setPrimaryKey(['id']); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparator->diffTable($table, $diffTable); self::assertNotFalse($diff); self::assertEquals( @@ -398,7 +405,10 @@ public function testAlterTableAddPrimaryKey(): void ); } - public function testAlterPrimaryKeyWithAutoincrementColumn(): void + /** + * @dataProvider comparatorProvider + */ + public function testAlterPrimaryKeyWithAutoincrementColumn(Comparator $comparator): void { $table = new Table('alter_primary_key'); $table->addColumn('id', 'integer', ['autoincrement' => true]); @@ -410,7 +420,7 @@ public function testAlterPrimaryKeyWithAutoincrementColumn(): void $diffTable->dropPrimaryKey(); $diffTable->setPrimaryKey(['foo']); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparator->diffTable($table, $diffTable); self::assertNotFalse($diff); self::assertEquals( @@ -423,7 +433,10 @@ public function testAlterPrimaryKeyWithAutoincrementColumn(): void ); } - public function testDropPrimaryKeyWithAutoincrementColumn(): void + /** + * @dataProvider comparatorProvider + */ + public function testDropPrimaryKeyWithAutoincrementColumn(Comparator $comparator): void { $table = new Table('drop_primary_key'); $table->addColumn('id', 'integer', ['autoincrement' => true]); @@ -435,7 +448,7 @@ public function testDropPrimaryKeyWithAutoincrementColumn(): void $diffTable->dropPrimaryKey(); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparator->diffTable($table, $diffTable); self::assertNotFalse($diff); self::assertEquals( @@ -447,8 +460,12 @@ public function testDropPrimaryKeyWithAutoincrementColumn(): void ); } - public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn(): void - { + /** + * @dataProvider comparatorProvider + */ + public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn( + Comparator $comparator + ): void { $table = new Table('tbl'); $table->addColumn('id', 'integer', ['autoincrement' => true]); $table->addColumn('foo', 'integer'); @@ -460,7 +477,7 @@ public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoinc $diffTable->dropPrimaryKey(); $diffTable->setPrimaryKey(['id']); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparator->diffTable($table, $diffTable); self::assertNotFalse($diff); self::assertSame( @@ -473,7 +490,10 @@ public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoinc ); } - public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn(): void + /** + * @dataProvider comparatorProvider + */ + public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn(Comparator $comparator): void { $table = new Table('tbl'); $table->addColumn('id', 'integer', ['autoincrement' => true]); @@ -486,7 +506,7 @@ public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn $diffTable->dropPrimaryKey(); $diffTable->setPrimaryKey(['id', 'foo']); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparator->diffTable($table, $diffTable); self::assertNotFalse($diff); self::assertSame( @@ -499,7 +519,10 @@ public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn ); } - public function testAddAutoIncrementPrimaryKey(): void + /** + * @dataProvider comparatorProvider + */ + public function testAddAutoIncrementPrimaryKey(Comparator $comparator): void { $keyTable = new Table('foo'); $keyTable->addColumn('id', 'integer', ['autoincrement' => true]); @@ -509,7 +532,7 @@ public function testAddAutoIncrementPrimaryKey(): void $oldTable = new Table('foo'); $oldTable->addColumn('baz', 'string'); - $diff = (new Comparator())->diffTable($oldTable, $keyTable); + $diff = $comparator->diffTable($oldTable, $keyTable); self::assertNotFalse($diff); $sql = $this->platform->getAlterTableSQL($diff); @@ -530,7 +553,10 @@ public function testNamedPrimaryKey(): void ], $sql); } - public function testAlterPrimaryKeyWithNewColumn(): void + /** + * @dataProvider comparatorProvider + */ + public function testAlterPrimaryKeyWithNewColumn(Comparator $comparator): void { $table = new Table('yolo'); $table->addColumn('pkc1', 'integer'); @@ -543,7 +569,7 @@ public function testAlterPrimaryKeyWithNewColumn(): void $diffTable->dropPrimaryKey(); $diffTable->setPrimaryKey(['pkc1', 'pkc2']); - $diff = (new Comparator())->diffTable($table, $diffTable); + $diff = $comparator->diffTable($table, $diffTable); self::assertNotFalse($diff); self::assertSame( @@ -744,7 +770,7 @@ protected function getQuotesDropConstraintSQL(): string return 'ALTER TABLE `table` DROP CONSTRAINT `select`'; } - public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes(): void + public function testIgnoresDifferenceInDefaultValuesForUnsupportedColumnTypes(): void { $table = new Table('text_blob_default_value'); $table->addColumn('def_text', 'text', ['default' => 'def']); @@ -769,10 +795,7 @@ public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes(): vo $diffTable->changeColumn('def_blob', ['default' => null]); $diffTable->changeColumn('def_blob_null', ['default' => null]); - $diff = (new Comparator())->diffTable($table, $diffTable); - self::assertNotFalse($diff); - - self::assertEmpty($this->platform->getAlterTableSQL($diff)); + self::assertFalse((new MySQL\Comparator($this->platform))->diffTable($table, $diffTable)); } /** @@ -1018,4 +1041,18 @@ public function testGetCreateTableSQLWithColumnCollation(): void $this->platform->getCreateTableSQL($table) ); } + + /** + * @return iterable + */ + public static function comparatorProvider(): iterable + { + yield 'Generic comparator' => [ + new Comparator(), + ]; + + yield 'MySQL comparator' => [ + new MySQL\Comparator(new MySQLPlatform()), + ]; + } } diff --git a/tests/Platforms/MariaDb1027PlatformTest.php b/tests/Platforms/MariaDb1027PlatformTest.php index 47c13f215e8..f2e8730906e 100644 --- a/tests/Platforms/MariaDb1027PlatformTest.php +++ b/tests/Platforms/MariaDb1027PlatformTest.php @@ -34,14 +34,8 @@ public function testInitializesJsonTypeMapping(): void self::assertSame(Types::JSON, $this->platform->getDoctrineTypeMapping('json')); } - /** - * Overrides and skips AbstractMySQLPlatformTestCase test regarding propagation - * of unsupported default values for Blob and Text columns. - * - * @see AbstractMySQLPlatformTestCase::testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes() - */ - public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes(): void + public function testIgnoresDifferenceInDefaultValuesForUnsupportedColumnTypes(): void { - self::markTestSkipped('MariaDB102Platform support propagation of default values for BLOB and TEXT columns'); + self::markTestSkipped('MariaDb1027Platform supports default values for BLOB and TEXT columns'); } } diff --git a/tests/Platforms/MySQL/ComparatorTest.php b/tests/Platforms/MySQL/ComparatorTest.php new file mode 100644 index 00000000000..54dea32dc80 --- /dev/null +++ b/tests/Platforms/MySQL/ComparatorTest.php @@ -0,0 +1,15 @@ +comparator = new Comparator(new MySQLPlatform()); + } +} diff --git a/tests/Platforms/OraclePlatformTest.php b/tests/Platforms/OraclePlatformTest.php index 7c9264b689a..78e34d7c45b 100644 --- a/tests/Platforms/OraclePlatformTest.php +++ b/tests/Platforms/OraclePlatformTest.php @@ -575,13 +575,7 @@ public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType(): vo $table2->addColumn('column_varbinary', 'binary', ['fixed' => true]); $table2->addColumn('column_binary', 'binary'); - $comparator = new Comparator(); - - // VARBINARY -> BINARY - // BINARY -> VARBINARY - $diff = (new Comparator())->diffTable($table1, $table2); - self::assertNotFalse($diff); - self::assertEmpty($this->platform->getAlterTableSQL($diff)); + self::assertFalse((new Comparator($this->platform))->diffTable($table1, $table2)); } public function testUsesSequenceEmulatedIdentityColumns(): void diff --git a/tests/Platforms/SQLServer/ComparatorTest.php b/tests/Platforms/SQLServer/ComparatorTest.php new file mode 100644 index 00000000000..bdf14519037 --- /dev/null +++ b/tests/Platforms/SQLServer/ComparatorTest.php @@ -0,0 +1,15 @@ +comparator = new Comparator(new SQLServer2012Platform(), ''); + } +} diff --git a/tests/Platforms/SQLite/ComparatorTest.php b/tests/Platforms/SQLite/ComparatorTest.php new file mode 100644 index 00000000000..972a1081ca0 --- /dev/null +++ b/tests/Platforms/SQLite/ComparatorTest.php @@ -0,0 +1,15 @@ +comparator = new Comparator(new SqlitePlatform()); + } +} diff --git a/tests/Schema/ComparatorTest.php b/tests/Schema/ComparatorTest.php index eff00266630..536aa7ade11 100644 --- a/tests/Schema/ComparatorTest.php +++ b/tests/Schema/ComparatorTest.php @@ -14,6 +14,7 @@ use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\TestCase; use function array_keys; @@ -21,6 +22,14 @@ class ComparatorTest extends TestCase { + /** @var Comparator */ + protected $comparator; + + protected function setUp(): void + { + $this->comparator = new Comparator(); + } + public function testCompareSame1(): void { $schema1 = new Schema([ @@ -42,7 +51,7 @@ public function testCompareSame1(): void $expected = new SchemaDiff(); $expected->fromSchema = $schema1; - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareSame2(): void @@ -68,7 +77,7 @@ public function testCompareSame2(): void $expected = new SchemaDiff(); $expected->fromSchema = $schema1; - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareMissingTable(): void @@ -83,7 +92,7 @@ public function testCompareMissingTable(): void $expected = new SchemaDiff([], [], ['bugdb' => $table], $schema1); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareNewTable(): void @@ -98,7 +107,7 @@ public function testCompareNewTable(): void $expected = new SchemaDiff(['bugdb' => $table], [], [], $schema1); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareOnlyAutoincrementChanged(): void @@ -106,8 +115,7 @@ public function testCompareOnlyAutoincrementChanged(): void $column1 = new Column('foo', Type::getType('integer'), ['autoincrement' => true]); $column2 = new Column('foo', Type::getType('integer'), ['autoincrement' => false]); - $comparator = new Comparator(); - $changedProperties = $comparator->diffColumn($column1, $column2); + $changedProperties = $this->comparator->diffColumn($column1, $column2); self::assertEquals(['autoincrement'], $changedProperties); } @@ -147,7 +155,7 @@ public function testCompareMissingField(): void $expected->fromSchema = $schema1; $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareNewField(): void @@ -184,7 +192,7 @@ public function testCompareNewField(): void $expected->fromSchema = $schema1; $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareChangedColumnsChangeType(): void @@ -192,9 +200,8 @@ public function testCompareChangedColumnsChangeType(): void $column1 = new Column('charcolumn1', Type::getType('string')); $column2 = new Column('charcolumn1', Type::getType('integer')); - $c = new Comparator(); - self::assertEquals(['type'], $c->diffColumn($column1, $column2)); - self::assertEquals([], $c->diffColumn($column1, $column1)); + self::assertEquals(['type'], $this->comparator->diffColumn($column1, $column2)); + self::assertEquals([], $this->comparator->diffColumn($column1, $column1)); } public function testCompareColumnsMultipleTypeInstances(): void @@ -206,8 +213,7 @@ public function testCompareColumnsMultipleTypeInstances(): void $column1 = new Column('integercolumn1', $integerType1); $column2 = new Column('integercolumn1', $integerType2); - $c = new Comparator(); - self::assertEquals([], $c->diffColumn($column1, $column2)); + self::assertEquals([], $this->comparator->diffColumn($column1, $column2)); } public function testCompareColumnsOverriddenType(): void @@ -223,8 +229,7 @@ public function testCompareColumnsOverriddenType(): void $column1 = new Column('integercolumn1', $integerType); $column2 = new Column('integercolumn1', $overriddenStringType); - $c = new Comparator(); - self::assertEquals([], $c->diffColumn($column1, $column2)); + self::assertEquals([], $this->comparator->diffColumn($column1, $column2)); } public function testCompareChangedColumnsChangeCustomSchemaOption(): void @@ -238,9 +243,8 @@ public function testCompareChangedColumnsChangeCustomSchemaOption(): void $column1->setCustomSchemaOption('foo1', 'bar1'); $column2->setCustomSchemaOption('foo2', 'bar2'); - $c = new Comparator(); - self::assertEquals(['foo1', 'foo2'], $c->diffColumn($column1, $column2)); - self::assertEquals([], $c->diffColumn($column1, $column1)); + self::assertEquals(['foo1', 'foo2'], $this->comparator->diffColumn($column1, $column2)); + self::assertEquals([], $this->comparator->diffColumn($column1, $column1)); } public function testCompareChangeColumnsMultipleNewColumnsRename(): void @@ -252,7 +256,7 @@ public function testCompareChangeColumnsMultipleNewColumnsRename(): void $tableB->addColumn('new_datecolumn1', 'datetime'); $tableB->addColumn('new_datecolumn2', 'datetime'); - $tableDiff = (new Comparator())->diffTable($tableA, $tableB); + $tableDiff = $this->comparator->diffTable($tableA, $tableB); self::assertNotFalse($tableDiff); self::assertCount(1, $tableDiff->renamedColumns); @@ -314,7 +318,7 @@ public function testCompareRemovedIndex(): void $expected->fromSchema = $schema1; $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareNewIndex(): void @@ -366,7 +370,7 @@ public function testCompareNewIndex(): void $expected->fromSchema = $schema1; $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareChangedIndex(): void @@ -429,7 +433,7 @@ public function testCompareChangedIndex(): void $expected->fromSchema = $schema1; $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareChangedIndexFieldPositions(): void @@ -477,7 +481,7 @@ public function testCompareChangedIndexFieldPositions(): void $expected->fromSchema = $schema1; $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); + self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } public function testCompareSequences(): void @@ -487,11 +491,9 @@ public function testCompareSequences(): void $seq3 = new Sequence('foo', 2, 1); $seq4 = new Sequence('foo', 1, 1); - $c = new Comparator(); - - self::assertTrue($c->diffSequence($seq1, $seq2)); - self::assertTrue($c->diffSequence($seq1, $seq3)); - self::assertFalse($c->diffSequence($seq1, $seq4)); + self::assertTrue($this->comparator->diffSequence($seq1, $seq2)); + self::assertTrue($this->comparator->diffSequence($seq1, $seq3)); + self::assertFalse($this->comparator->diffSequence($seq1, $seq4)); } public function testRemovedSequence(): void @@ -501,8 +503,7 @@ public function testRemovedSequence(): void $schema2 = new Schema(); - $c = new Comparator(); - $diffSchema = $c->compare($schema1, $schema2); + $diffSchema = $this->comparator->compare($schema1, $schema2); self::assertCount(1, $diffSchema->removedSequences); self::assertSame($seq, $diffSchema->removedSequences[0]); @@ -515,8 +516,7 @@ public function testAddedSequence(): void $schema2 = new Schema(); $seq = $schema2->createSequence('foo'); - $c = new Comparator(); - $diffSchema = $c->compare($schema1, $schema2); + $diffSchema = $this->comparator->compare($schema1, $schema2); self::assertCount(1, $diffSchema->newSequences); self::assertSame($seq, $diffSchema->newSequences[0]); @@ -534,8 +534,7 @@ public function testTableAddForeignKey(): void $table2->addColumn('fk', 'integer'); $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); - $c = new Comparator(); - $tableDiff = $c->diffTable($table1, $table2); + $tableDiff = $this->comparator->diffTable($table1, $table2); self::assertInstanceOf(TableDiff::class, $tableDiff); self::assertCount(1, $tableDiff->addedForeignKeys); @@ -553,8 +552,7 @@ public function testTableRemoveForeignKey(): void $table2->addColumn('fk', 'integer'); $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); - $c = new Comparator(); - $tableDiff = $c->diffTable($table2, $table1); + $tableDiff = $this->comparator->diffTable($table2, $table1); self::assertInstanceOf(TableDiff::class, $tableDiff); self::assertCount(1, $tableDiff->removedForeignKeys); @@ -573,8 +571,7 @@ public function testTableUpdateForeignKey(): void $table2->addColumn('fk', 'integer'); $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id'], ['onUpdate' => 'CASCADE']); - $c = new Comparator(); - $tableDiff = $c->diffTable($table1, $table2); + $tableDiff = $this->comparator->diffTable($table1, $table2); self::assertInstanceOf(TableDiff::class, $tableDiff); self::assertCount(1, $tableDiff->changedForeignKeys); @@ -596,8 +593,7 @@ public function testMovedForeignKeyForeignTable(): void $table2->addColumn('fk', 'integer'); $table2->addForeignKeyConstraint($tableForeign2, ['fk'], ['id']); - $c = new Comparator(); - $tableDiff = $c->diffTable($table1, $table2); + $tableDiff = $this->comparator->diffTable($table1, $table2); self::assertInstanceOf(TableDiff::class, $tableDiff); self::assertCount(1, $tableDiff->changedForeignKeys); @@ -617,8 +613,7 @@ public function testTablesCaseInsensitive(): void $schemaB->createTable('Baz'); $schemaB->createTable('old'); - $c = new Comparator(); - $diff = $c->compare($schemaA, $schemaB); + $diff = $this->comparator->compare($schemaA, $schemaB); $this->assertSchemaTableChangeCount($diff, 1, 0, 1); } @@ -637,8 +632,7 @@ public function testSequencesCaseInsensitive(): void $schemaB->createSequence('baz'); $schemaB->createSequence('old'); - $c = new Comparator(); - $diff = $c->compare($schemaA, $schemaB); + $diff = $this->comparator->compare($schemaA, $schemaB); $this->assertSchemaSequenceChangeCount($diff, 1, 0, 1); } @@ -651,8 +645,7 @@ public function testCompareColumnCompareCaseInsensitive(): void $tableB = new Table('foo'); $tableB->addColumn('ID', 'integer'); - $c = new Comparator(); - $tableDiff = $c->diffTable($tableA, $tableB); + $tableDiff = $this->comparator->diffTable($tableA, $tableB); self::assertFalse($tableDiff); } @@ -667,14 +660,13 @@ public function testCompareIndexBasedOnPropertiesNotName(): void $tableB->addColumn('ID', 'integer'); $tableB->addIndex(['id'], 'bar_foo_idx'); - $c = new Comparator(); $tableDiff = new TableDiff('foo'); $tableDiff->fromTable = $tableA; $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', ['id']); self::assertEquals( $tableDiff, - $c->diffTable($tableA, $tableB) + $this->comparator->diffTable($tableA, $tableB) ); } @@ -688,8 +680,7 @@ public function testCompareForeignKeyBasedOnPropertiesNotName(): void $tableB->addColumn('ID', 'integer'); $tableB->addForeignKeyConstraint('bar', ['id'], ['id'], [], 'bar_constraint'); - $c = new Comparator(); - $tableDiff = $c->diffTable($tableA, $tableB); + $tableDiff = $this->comparator->diffTable($tableA, $tableB); self::assertFalse($tableDiff); } @@ -699,8 +690,7 @@ public function testCompareForeignKeyRestrictNoActionAreTheSame(): void $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']); $fk2 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'RESTRICT']); - $c = new Comparator(); - self::assertFalse($c->diffForeignKey($fk1, $fk2)); + self::assertFalse($this->comparator->diffForeignKey($fk1, $fk2)); } public function testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvailable(): void @@ -708,8 +698,7 @@ public function testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvai $fk1 = new ForeignKeyConstraint(['foo'], 'foo.bar', ['baz'], 'fk1'); $fk2 = new ForeignKeyConstraint(['foo'], 'baz.bar', ['baz'], 'fk1'); - $c = new Comparator(); - self::assertFalse($c->diffForeignKey($fk1, $fk2)); + self::assertFalse($this->comparator->diffForeignKey($fk1, $fk2)); } public function testDetectRenameColumn(): void @@ -720,7 +709,7 @@ public function testDetectRenameColumn(): void $tableB = new Table('foo'); $tableB->addColumn('bar', 'integer'); - $tableDiff = (new Comparator())->diffTable($tableA, $tableB); + $tableDiff = $this->comparator->diffTable($tableA, $tableB); self::assertNotFalse($tableDiff); self::assertCount(0, $tableDiff->addedColumns); @@ -743,7 +732,7 @@ public function testDetectRenameColumnAmbiguous(): void $tableB = new Table('foo'); $tableB->addColumn('baz', 'integer'); - $tableDiff = (new Comparator())->diffTable($tableA, $tableB); + $tableDiff = $this->comparator->diffTable($tableA, $tableB); self::assertNotFalse($tableDiff); self::assertCount(1, $tableDiff->addedColumns); @@ -765,7 +754,7 @@ public function testDetectRenameIndex(): void $table2->addIndex(['foo'], 'idx_bar'); - $tableDiff = (new Comparator())->diffTable($table1, $table2); + $tableDiff = $this->comparator->diffTable($table1, $table2); self::assertNotFalse($tableDiff); self::assertCount(0, $tableDiff->addedIndexes); @@ -791,7 +780,7 @@ public function testDetectRenameIndexAmbiguous(): void $table2->addIndex(['foo'], 'idx_baz'); - $tableDiff = (new Comparator())->diffTable($table1, $table2); + $tableDiff = $this->comparator->diffTable($table1, $table2); self::assertNotFalse($tableDiff); self::assertCount(1, $tableDiff->addedIndexes); @@ -804,16 +793,13 @@ public function testDetectRenameIndexAmbiguous(): void public function testDetectChangeIdentifierType(): void { - $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.'); - $tableA = new Table('foo'); $tableA->addColumn('id', 'integer', ['autoincrement' => false]); $tableB = new Table('foo'); $tableB->addColumn('id', 'integer', ['autoincrement' => true]); - $c = new Comparator(); - $tableDiff = $c->diffTable($tableA, $tableB); + $tableDiff = $this->comparator->diffTable($tableA, $tableB); self::assertInstanceOf(TableDiff::class, $tableDiff); self::assertArrayHasKey('id', $tableDiff->changedColumns); @@ -834,8 +820,7 @@ public function testDiff(): void $newtable->addColumn('logged_in_at', 'datetime'); $newtable->setPrimaryKey(['id']); - $c = new Comparator(); - $tableDiff = $c->diffTable($table, $newtable); + $tableDiff = $this->comparator->diffTable($table, $newtable); self::assertInstanceOf(TableDiff::class, $tableDiff); self::assertEquals(['twitterid', 'displayname'], array_keys($tableDiff->renamedColumns)); @@ -851,8 +836,7 @@ public function testChangedSequence(): void $schemaNew = clone $schema; $schemaNew->getSequence('baz')->setAllocationSize(20); - $c = new Comparator(); - $diff = $c->compare($schema, $schemaNew); + $diff = $this->comparator->compare($schema, $schemaNew); self::assertSame($diff->changedSequences[0], $schemaNew->getSequence('baz')); } @@ -867,8 +851,7 @@ public function testDiffDecimalWithNullPrecision(): void $column2 = new Column('foo', Type::getType('decimal')); - $c = new Comparator(); - self::assertEquals([], $c->diffColumn($column, $column2)); + self::assertEquals([], $this->comparator->diffColumn($column, $column2)); } public function testFqnSchemaComparison(): void @@ -885,7 +868,7 @@ public function testFqnSchemaComparison(): void $expected = new SchemaDiff(); $expected->fromSchema = $oldSchema; - self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); + self::assertEquals($expected, $this->comparator->compareSchemas($oldSchema, $newSchema)); } public function testNamespacesComparison(): void @@ -906,7 +889,7 @@ public function testNamespacesComparison(): void $expected->fromSchema = $oldSchema; $expected->newNamespaces = ['bar' => 'bar', 'baz' => 'baz']; - $diff = Comparator::compareSchemas($oldSchema, $newSchema); + $diff = $this->comparator->compareSchemas($oldSchema, $newSchema); self::assertEquals(['bar' => 'bar', 'baz' => 'baz'], $diff->newNamespaces); self::assertCount(2, $diff->newTables); @@ -926,7 +909,7 @@ public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff(): $expected = new SchemaDiff(); $expected->fromSchema = $oldSchema; - self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); + self::assertEquals($expected, $this->comparator->compareSchemas($oldSchema, $newSchema)); } public function testFqnSchemaComparisonNoSchemaSame(): void @@ -942,7 +925,7 @@ public function testFqnSchemaComparisonNoSchemaSame(): void $expected = new SchemaDiff(); $expected->fromSchema = $oldSchema; - self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); + self::assertEquals($expected, $this->comparator->compareSchemas($oldSchema, $newSchema)); } public function testAutoIncrementSequences(): void @@ -958,8 +941,7 @@ public function testAutoIncrementSequences(): void $table->addColumn('id', 'integer', ['autoincrement' => true]); $table->setPrimaryKey(['id']); - $c = new Comparator(); - $diff = $c->compare($oldSchema, $newSchema); + $diff = $this->comparator->compare($oldSchema, $newSchema); self::assertCount(0, $diff->removedSequences); } @@ -980,8 +962,7 @@ public function testAutoIncrementNoSequences(): void $table->setPrimaryKey(['id']); $newSchema->createSequence('foo_id_seq'); - $c = new Comparator(); - $diff = $c->compare($oldSchema, $newSchema); + $diff = $this->comparator->compare($oldSchema, $newSchema); self::assertCount(0, $diff->newSequences); } @@ -1018,8 +999,7 @@ public function testAvoidMultipleDropForeignKey(): void $tableC = $newSchema->createTable('table_c'); $tableC->addColumn('id', 'integer'); - $comparator = new Comparator(); - $schemaDiff = $comparator->compare($oldSchema, $newSchema); + $schemaDiff = $this->comparator->compare($oldSchema, $newSchema); self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys); self::assertCount(1, $schemaDiff->orphanedForeignKeys); @@ -1047,7 +1027,7 @@ public function testCompareChangedColumn(): void $columnDiff->fromColumn = $tableFoo->getColumn('id'); $columnDiff->changedProperties = ['type']; - self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); + self::assertEquals($expected, $this->comparator->compareSchemas($oldSchema, $newSchema)); } public function testCompareChangedBinaryColumn(): void @@ -1072,7 +1052,7 @@ public function testCompareChangedBinaryColumn(): void $columnDiff->fromColumn = $tableFoo->getColumn('id'); $columnDiff->changedProperties = ['length', 'fixed']; - self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); + self::assertEquals($expected, $this->comparator->compareSchemas($oldSchema, $newSchema)); } public function testCompareQuotedAndUnquotedForeignKeyColumns(): void @@ -1080,8 +1060,7 @@ public function testCompareQuotedAndUnquotedForeignKeyColumns(): void $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']); $fk2 = new ForeignKeyConstraint(['`foo`'], 'bar', ['`baz`'], 'fk1', ['onDelete' => 'NO ACTION']); - $comparator = new Comparator(); - $diff = $comparator->diffForeignKey($fk1, $fk2); + $diff = $this->comparator->diffForeignKey($fk1, $fk2); self::assertFalse($diff); } @@ -1133,14 +1112,12 @@ public function testDiffColumnPlatformOptions(): void $column4 = new Column('foo', Type::getType('string')); - $comparator = new Comparator(); - - self::assertEquals([], $comparator->diffColumn($column1, $column2)); - self::assertEquals([], $comparator->diffColumn($column2, $column1)); - self::assertEquals(['bar'], $comparator->diffColumn($column1, $column3)); - self::assertEquals(['bar'], $comparator->diffColumn($column3, $column1)); - self::assertEquals([], $comparator->diffColumn($column1, $column4)); - self::assertEquals([], $comparator->diffColumn($column4, $column1)); + self::assertEquals([], $this->comparator->diffColumn($column1, $column2)); + self::assertEquals([], $this->comparator->diffColumn($column2, $column1)); + self::assertEquals(['bar'], $this->comparator->diffColumn($column1, $column3)); + self::assertEquals(['bar'], $this->comparator->diffColumn($column3, $column1)); + self::assertEquals([], $this->comparator->diffColumn($column1, $column4)); + self::assertEquals([], $this->comparator->diffColumn($column4, $column1)); } public function testComplexDiffColumn(): void @@ -1154,15 +1131,12 @@ public function testComplexDiffColumn(): void 'platformOptions' => ['foo' => 'bar'], ]); - $comparator = new Comparator(); - - self::assertEquals([], $comparator->diffColumn($column1, $column2)); - self::assertEquals([], $comparator->diffColumn($column2, $column1)); + self::assertEquals([], $this->comparator->diffColumn($column1, $column2)); + self::assertEquals([], $this->comparator->diffColumn($column2, $column1)); } public function testComparesNamespaces(): void { - $comparator = new Comparator(); $fromSchema = $this->getMockBuilder(Schema::class) ->onlyMethods(['getNamespaces', 'hasNamespace']) ->getMock(); @@ -1191,13 +1165,11 @@ public function testComparesNamespaces(): void $expected->newNamespaces = ['baz' => 'baz']; $expected->removedNamespaces = ['foo' => 'foo']; - self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema)); + self::assertEquals($expected, $this->comparator->compare($fromSchema, $toSchema)); } public function testCompareGuidColumns(): void { - $comparator = new Comparator(); - $column1 = new Column('foo', Type::getType('guid'), ['comment' => 'GUID 1']); $column2 = new Column( 'foo', @@ -1205,8 +1177,8 @@ public function testCompareGuidColumns(): void ['notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.'] ); - self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column1, $column2)); - self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column2, $column1)); + self::assertEquals(['notnull', 'default', 'comment'], $this->comparator->diffColumn($column1, $column2)); + self::assertEquals(['notnull', 'default', 'comment'], $this->comparator->diffColumn($column2, $column1)); } /** @@ -1217,15 +1189,13 @@ public function testCompareColumnComments(?string $comment1, ?string $comment2, $column1 = new Column('foo', Type::getType('integer'), ['comment' => $comment1]); $column2 = new Column('foo', Type::getType('integer'), ['comment' => $comment2]); - $comparator = new Comparator(); - $expectedDiff = $equals ? [] : ['comment']; - $actualDiff = $comparator->diffColumn($column1, $column2); + $actualDiff = $this->comparator->diffColumn($column1, $column2); self::assertSame($expectedDiff, $actualDiff); - $actualDiff = $comparator->diffColumn($column2, $column1); + $actualDiff = $this->comparator->diffColumn($column2, $column1); self::assertSame($expectedDiff, $actualDiff); } @@ -1258,6 +1228,14 @@ public static function getCompareColumnComments(): iterable ]; } + public function testCompareCommentedTypes(): void + { + $column1 = new Column('foo', Type::getType(Types::ARRAY)); + $column2 = new Column('foo', Type::getType(Types::OBJECT)); + + self::assertFalse($this->comparator->columnsEqual($column1, $column2)); + } + public function testForeignKeyRemovalWithRenamedLocalColumn(): void { $fromSchema = new Schema([ @@ -1300,7 +1278,7 @@ public function testForeignKeyRemovalWithRenamedLocalColumn(): void ] ), ]); - $actual = Comparator::compareSchemas($fromSchema, $toSchema); + $actual = $this->comparator->compareSchemas($fromSchema, $toSchema); self::assertArrayHasKey('table2', $actual->changedTables); self::assertCount(1, $actual->orphanedForeignKeys); diff --git a/tests/Schema/Platforms/MySQLSchemaTest.php b/tests/Schema/Platforms/MySQLSchemaTest.php index 71a658ea7c9..73236f3e2e9 100644 --- a/tests/Schema/Platforms/MySQLSchemaTest.php +++ b/tests/Schema/Platforms/MySQLSchemaTest.php @@ -3,6 +3,7 @@ namespace Doctrine\DBAL\Tests\Schema\Platforms; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Platforms\MySQL; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Table; @@ -10,19 +11,18 @@ class MySQLSchemaTest extends TestCase { - /** @var Comparator */ - private $comparator; - /** @var AbstractPlatform */ private $platform; protected function setUp(): void { - $this->comparator = new Comparator(); - $this->platform = new MySQLPlatform(); + $this->platform = new MySQLPlatform(); } - public function testSwitchPrimaryKeyOrder(): void + /** + * @dataProvider comparatorProvider + */ + public function testSwitchPrimaryKeyOrder(Comparator $comparator): void { $tableOld = new Table('test'); $tableOld->addColumn('foo_id', 'integer'); @@ -32,7 +32,7 @@ public function testSwitchPrimaryKeyOrder(): void $tableOld->setPrimaryKey(['foo_id', 'bar_id']); $tableNew->setPrimaryKey(['bar_id', 'foo_id']); - $diff = $this->comparator->diffTable($tableOld, $tableNew); + $diff = $comparator->diffTable($tableOld, $tableNew); self::assertNotFalse($diff); $sql = $this->platform->getAlterTableSQL($diff); @@ -66,7 +66,10 @@ public function testGenerateForeignKeySQL(): void ); } - public function testClobNoAlterTable(): void + /** + * @dataProvider comparatorProvider + */ + public function testClobNoAlterTable(Comparator $comparator): void { $tableOld = new Table('test'); $tableOld->addColumn('id', 'integer'); @@ -75,7 +78,7 @@ public function testClobNoAlterTable(): void $tableNew->setPrimaryKey(['id']); - $diff = $this->comparator->diffTable($tableOld, $tableNew); + $diff = $comparator->diffTable($tableOld, $tableNew); self::assertNotFalse($diff); $sql = $this->platform->getAlterTableSQL($diff); @@ -85,4 +88,18 @@ public function testClobNoAlterTable(): void $sql ); } + + /** + * @return iterable + */ + public static function comparatorProvider(): iterable + { + yield 'Generic comparator' => [ + new Comparator(), + ]; + + yield 'MySQL comparator' => [ + new MySQL\Comparator(new MySQLPlatform()), + ]; + } }