From 4b8d7bf1d168831079fbc0fc399bfa0cab56d1b5 Mon Sep 17 00:00:00 2001 From: Phillip Look Date: Tue, 6 May 2025 12:06:53 +0200 Subject: [PATCH] feat: Support Doctrine DBAL 4.x --- composer.json | 2 +- .../Strategy/MysqlBasedSchemaStrategy.php | 21 +-- .../Strategy/MysqlDamaBasedSchemaStrategy.php | 21 +-- .../Strategy/MysqlBasedSchemaStrategyTest.php | 142 ++++++++++++++++-- .../MysqlDamaBasedSchemaStrategyTest.php | 136 +++++++++++++++-- .../SqliteFileBasedSchemaStrategyTest.php | 4 +- .../SqliteFileDamaBasedSchemaStrategyTest.php | 4 +- .../SqliteMemoryBasedSchemaStrategyTest.php | 4 +- ...ql_based_schema_strategy_apply_schema.json | 4 +- ..._dropped_before_creating_fresh_schema.json | 58 ++++--- ...om_cache_if_database_and_cache_exists.json | 6 +- ...ma_based_schema_strategy_apply_schema.json | 4 +- ..._dropped_before_creating_fresh_schema.json | 58 ++++--- ...om_cache_if_database_and_cache_exists.json | 6 +- 14 files changed, 363 insertions(+), 107 deletions(-) diff --git a/composer.json b/composer.json index 47a1143..89bdaef 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "brainbits/phpcs-standard": "^8.0.0", "brainbits/phpstan-rules": "^4.0", "dama/doctrine-test-bundle": "^8.2", - "doctrine/dbal": "^3.9", + "doctrine/dbal": "^4.2", "ergebnis/phpstan-rules": "^2.8", "gemorroj/archive7z": "^5.7", "mikey179/vfsstream": "^1.6.12", diff --git a/src/Schema/Strategy/MysqlBasedSchemaStrategy.php b/src/Schema/Strategy/MysqlBasedSchemaStrategy.php index 27ca07b..13ae6bf 100644 --- a/src/Schema/Strategy/MysqlBasedSchemaStrategy.php +++ b/src/Schema/Strategy/MysqlBasedSchemaStrategy.php @@ -31,7 +31,7 @@ public function applySchema(SchemaBuilder $schemaBuilder, Connection $connection { $platform = $connection->getDatabasePlatform(); - $existingTables = $connection->executeQuery($platform->getListTablesSQL())->fetchFirstColumn(); + $existingTables = $connection->createSchemaManager()->listTableNames(); if (count($existingTables) > 0) { if (count(self::$executedStatements) === 0) { @@ -107,25 +107,20 @@ public function applyData(DataBuilder $dataBuilder, Connection $connection): voi } } - /** @param list $tables */ - private function dropTables(array $tables, Connection $connection): void + /** @param list $tableNames */ + private function dropTables(array $tableNames, Connection $connection): void { - $platform = $connection->getDatabasePlatform(); + $schemaManager = $connection->createSchemaManager(); try { $connection->executeStatement('SET foreign_key_checks = 0'); - foreach ($tables as $table) { - $listForeignKeysSql = $platform->getListTableForeignKeysSQL($table); - $foreignKeys = $connection->executeQuery($listForeignKeysSql)->fetchFirstColumn(); - - foreach ($foreignKeys as $foreignKey) { - $dropForeignKeySQL = $platform->getDropForeignKeySQL($foreignKey, $table); - $connection->executeStatement($dropForeignKeySQL); + foreach ($tableNames as $tableName) { + foreach ($schemaManager->listTableForeignKeys($tableName) as $foreignKey) { + $schemaManager->dropForeignKey($foreignKey->getName(), $tableName); } - $dropTableSQL = $platform->getDropTableSQL($table); - $connection->executeStatement($dropTableSQL); + $schemaManager->dropTable($tableName); } } finally { $connection->executeStatement('SET foreign_key_checks = 1'); diff --git a/src/Schema/Strategy/MysqlDamaBasedSchemaStrategy.php b/src/Schema/Strategy/MysqlDamaBasedSchemaStrategy.php index 4e8fa1d..485793b 100644 --- a/src/Schema/Strategy/MysqlDamaBasedSchemaStrategy.php +++ b/src/Schema/Strategy/MysqlDamaBasedSchemaStrategy.php @@ -32,7 +32,7 @@ public function applySchema(SchemaBuilder $schemaBuilder, Connection $connection { $platform = $connection->getDatabasePlatform(); - $existingTables = $connection->executeQuery($platform->getListTablesSQL())->fetchFirstColumn(); + $existingTables = $connection->createSchemaManager()->listTableNames(); StaticDriver::rollBack(); @@ -101,25 +101,20 @@ public function applyData(DataBuilder $dataBuilder, Connection $connection): voi } } - /** @param list $tables */ - private function dropTables(array $tables, Connection $connection): void + /** @param list $tableNames */ + private function dropTables(array $tableNames, Connection $connection): void { - $platform = $connection->getDatabasePlatform(); + $schemaManager = $connection->createSchemaManager(); try { $connection->executeStatement('SET foreign_key_checks = 0'); - foreach ($tables as $table) { - $listForeignKeysSql = $platform->getListTableForeignKeysSQL($table); - $foreignKeys = $connection->executeQuery($listForeignKeysSql)->fetchFirstColumn(); - - foreach ($foreignKeys as $foreignKey) { - $dropForeignKeySQL = $platform->getDropForeignKeySQL($foreignKey, $table); - $connection->executeStatement($dropForeignKeySQL); + foreach ($tableNames as $tableName) { + foreach ($schemaManager->listTableForeignKeys($tableName) as $foreignKey) { + $schemaManager->dropForeignKey($foreignKey->getName(), $tableName); } - $dropTableSQL = $platform->getDropTableSQL($table); - $connection->executeStatement($dropTableSQL); + $schemaManager->dropTable($tableName); } } finally { $connection->executeStatement('SET foreign_key_checks = 1'); diff --git a/tests/Schema/Strategy/MysqlBasedSchemaStrategyTest.php b/tests/Schema/Strategy/MysqlBasedSchemaStrategyTest.php index 35e4e5b..a34a2c1 100644 --- a/tests/Schema/Strategy/MysqlBasedSchemaStrategyTest.php +++ b/tests/Schema/Strategy/MysqlBasedSchemaStrategyTest.php @@ -12,11 +12,18 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Result; +use Doctrine\DBAL\Schema\ForeignKeyConstraint; +use Doctrine\DBAL\Schema\MySQLSchemaManager; use Doctrine\DBAL\Schema\Schema; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use function array_keys; +use function array_map; +use function array_values; use function func_get_arg; +use function func_get_args; use function Safe\preg_match; use function str_starts_with; @@ -26,11 +33,13 @@ final class MysqlBasedSchemaStrategyTest extends TestCase use SnapshotTrait; private MySQLPlatform $platform; + private MySQLSchemaManager&MockObject $schemaManager; private SchemaBuilder $schemaBuilder; protected function setUp(): void { $this->platform = new MySQLPlatform(); + $this->schemaManager = $this->createMock(MySQLSchemaManager::class); $this->schemaBuilder = $this->createSchemaBuilder(); $this->schemaBuilder->foo(); @@ -41,6 +50,22 @@ public function testApplySchema(): void /** @phpstan-var ArrayObject $queryLog */ $queryLog = new ArrayObject(); + $this->schemaManager->expects($this->any()) + ->method('listTableNames') + ->willReturnCallback( + static function () use ($queryLog) { + $result = []; + + $queryLog[] = [ + 'function' => 'listTableNames()', + 'parameters' => func_get_args(), + 'result' => $result, + ]; + + return $result; + }, + ); + $connection = $this->createMock(Connection::class); $connection->expects($this->any()) ->method('quoteIdentifier') @@ -51,11 +76,16 @@ public function testApplySchema(): void $connection->expects($this->any()) ->method('getDatabasePlatform') ->willReturn($this->platform); + $connection->expects($this->any()) + ->method('createSchemaManager') + ->willReturn($this->schemaManager); $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -68,7 +98,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -83,6 +113,57 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void /** @phpstan-var ArrayObject $queryLog */ $queryLog = new ArrayObject(); + $this->schemaManager->expects($this->once()) + ->method('listTableNames') + ->willReturnCallback(static function () use ($queryLog) { + $result = ['old_table_1', 'old_table_2']; + + $queryLog[] = ['function' => 'listTableNames()', 'parameters' => func_get_args(), 'result' => $result]; + + return $result; + }); + + $this->schemaManager->expects($this->exactly(2)) + ->method('listTableForeignKeys') + ->willReturnCallback(static function ($tableName) use ($queryLog) { + $result = []; + + if ($tableName === 'old_table_1') { + $result = [ + new ForeignKeyConstraint([], '', [], 'constraint_1'), + new ForeignKeyConstraint([], '', [], 'constraint_2'), + ]; + } + + $queryLog[] = [ + 'function' => 'listTableForeignKeys()', + 'parameters' => func_get_args(), + 'result' => array_map(static fn ($fk) => $fk->getName(), $result), + ]; + + return $result; + }); + + $this->schemaManager->expects($this->any()) + ->method('dropForeignKey') + ->willReturnCallback(static function ($tableName) use ($queryLog) { + $result = []; + + $queryLog[] = ['function' => 'dropForeignKey()', 'parameters' => func_get_args(), 'result' => $result]; + + return $result; + }); + + $this->schemaManager->expects($this->any()) + ->method('dropTable') + ->willReturnCallback(static function ($tableName) use ($queryLog) { + $result = []; + + $queryLog[] = ['function' => 'dropTable()', 'parameters' => func_get_args(), 'result' => $result]; + + return $result; + }); + $connection = $this->createMock(Connection::class); $connection->expects($this->any()) ->method('quoteIdentifier') @@ -93,11 +174,16 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void $connection->expects($this->any()) ->method('getDatabasePlatform') ->willReturn($this->platform); + $connection->expects($this->any()) + ->method('createSchemaManager') + ->willReturn($this->schemaManager); $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -118,7 +204,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -133,6 +219,22 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void /** @phpstan-var ArrayObject $queryLog */ $queryLog = new ArrayObject(); + $this->schemaManager->expects($this->any()) + ->method('listTableNames') + ->willReturnCallback( + static function () use ($queryLog) { + $result = []; + + $queryLog[] = [ + 'function' => 'listTableNames()', + 'parameters' => func_get_args(), + 'result' => $result, + ]; + + return $result; + }, + ); + $connection = $this->createMock(Connection::class); $connection->expects($this->any()) ->method('quoteIdentifier') @@ -143,11 +245,16 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void $connection->expects($this->any()) ->method('getDatabasePlatform') ->willReturn(new MySQLPlatform()); + $connection->expects($this->any()) + ->method('createSchemaManager') + ->willReturn($this->schemaManager); $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -160,7 +267,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -194,8 +301,10 @@ public function testDeleteData(): void $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -213,7 +322,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -244,8 +353,10 @@ public function testResetSequences(): void $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -264,7 +375,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -298,7 +409,7 @@ public function foo(): void { $table = $this->schema->createTable('foo'); $table->addColumn('id', 'integer', ['autoincrement' => true]); - $table->addColumn('bar', 'string'); + $table->addColumn('bar', 'string', ['length' => 255]); } }; } @@ -307,4 +418,13 @@ private function snapshotPath(): string { return __DIR__; } + + /** @param mixed[] $result */ + private static function createArrayResult(array $result): ArrayResult + { + $columnNames = array_keys($result[0] ?? []); + $rows = array_map(array_values(...), $result); + + return new ArrayResult($columnNames, $rows); + } } diff --git a/tests/Schema/Strategy/MysqlDamaBasedSchemaStrategyTest.php b/tests/Schema/Strategy/MysqlDamaBasedSchemaStrategyTest.php index a53fc1f..f8f271e 100644 --- a/tests/Schema/Strategy/MysqlDamaBasedSchemaStrategyTest.php +++ b/tests/Schema/Strategy/MysqlDamaBasedSchemaStrategyTest.php @@ -12,11 +12,18 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Result; +use Doctrine\DBAL\Schema\ForeignKeyConstraint; +use Doctrine\DBAL\Schema\MySQLSchemaManager; use Doctrine\DBAL\Schema\Schema; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use function array_keys; +use function array_map; +use function array_values; use function func_get_arg; +use function func_get_args; use function Safe\preg_match; use function str_starts_with; @@ -26,11 +33,13 @@ final class MysqlDamaBasedSchemaStrategyTest extends TestCase use SnapshotTrait; private MySQLPlatform $platform; + private MySQLSchemaManager&MockObject $schemaManager; private SchemaBuilder $schemaBuilder; protected function setUp(): void { $this->platform = new MySQLPlatform(); + $this->schemaManager = $this->createMock(MySQLSchemaManager::class); $this->schemaBuilder = $this->createSchemaBuilder(); $this->schemaBuilder->foo(); @@ -41,6 +50,22 @@ public function testApplySchema(): void /** @phpstan-var ArrayObject $queryLog */ $queryLog = new ArrayObject(); + $this->schemaManager->expects($this->any()) + ->method('listTableNames') + ->willReturnCallback( + static function () use ($queryLog) { + $result = []; + + $queryLog[] = [ + 'function' => 'listTableNames()', + 'parameters' => func_get_args(), + 'result' => $result, + ]; + + return $result; + }, + ); + $connection = $this->createMock(Connection::class); $connection->expects($this->any()) ->method('quoteIdentifier') @@ -51,11 +76,16 @@ public function testApplySchema(): void $connection->expects($this->any()) ->method('getDatabasePlatform') ->willReturn($this->platform); + $connection->expects($this->any()) + ->method('createSchemaManager') + ->willReturn($this->schemaManager); $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -68,7 +98,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -83,6 +113,57 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void /** @phpstan-var ArrayObject $queryLog */ $queryLog = new ArrayObject(); + $this->schemaManager->expects($this->once()) + ->method('listTableNames') + ->willReturnCallback(static function () use ($queryLog) { + $result = ['old_table_1', 'old_table_2']; + + $queryLog[] = ['function' => 'listTableNames()', 'parameters' => func_get_args(), 'result' => $result]; + + return $result; + }); + + $this->schemaManager->expects($this->exactly(2)) + ->method('listTableForeignKeys') + ->willReturnCallback(static function ($tableName) use ($queryLog) { + $result = []; + + if ($tableName === 'old_table_1') { + $result = [ + new ForeignKeyConstraint([], '', [], 'constraint_1'), + new ForeignKeyConstraint([], '', [], 'constraint_2'), + ]; + } + + $queryLog[] = [ + 'function' => 'listTableForeignKeys()', + 'parameters' => func_get_args(), + 'result' => array_map(static fn ($fk) => $fk->getName(), $result), + ]; + + return $result; + }); + + $this->schemaManager->expects($this->any()) + ->method('dropForeignKey') + ->willReturnCallback(static function ($tableName) use ($queryLog) { + $result = []; + + $queryLog[] = ['function' => 'dropForeignKey()', 'parameters' => func_get_args(), 'result' => $result]; + + return $result; + }); + + $this->schemaManager->expects($this->any()) + ->method('dropTable') + ->willReturnCallback(static function ($tableName) use ($queryLog) { + $result = []; + + $queryLog[] = ['function' => 'dropTable()', 'parameters' => func_get_args(), 'result' => $result]; + + return $result; + }); + $connection = $this->createMock(Connection::class); $connection->expects($this->any()) ->method('quoteIdentifier') @@ -93,11 +174,16 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void $connection->expects($this->any()) ->method('getDatabasePlatform') ->willReturn($this->platform); + $connection->expects($this->any()) + ->method('createSchemaManager') + ->willReturn($this->schemaManager); $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -118,7 +204,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -133,6 +219,22 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void /** @phpstan-var ArrayObject $queryLog */ $queryLog = new ArrayObject(); + $this->schemaManager->expects($this->any()) + ->method('listTableNames') + ->willReturnCallback( + static function () use ($queryLog) { + $result = []; + + $queryLog[] = [ + 'function' => 'listTableNames()', + 'parameters' => func_get_args(), + 'result' => $result, + ]; + + return $result; + }, + ); + $connection = $this->createMock(Connection::class); $connection->expects($this->any()) ->method('quoteIdentifier') @@ -143,11 +245,16 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void $connection->expects($this->any()) ->method('getDatabasePlatform') ->willReturn(new MySQLPlatform()); + $connection->expects($this->any()) + ->method('createSchemaManager') + ->willReturn($this->schemaManager); $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -160,7 +267,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -194,8 +301,10 @@ public function testResetSequences(): void $connection->expects($this->any()) ->method('executeStatement') ->willReturnCallback( - static function () use ($queryLog): void { + static function () use ($queryLog): int { $queryLog[] = ['statement' => func_get_arg(0)]; + + return 0; }, ); $connection->expects($this->any()) @@ -214,7 +323,7 @@ static function () use ($queryLog, $connection) { $queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result]; - return new Result(new ArrayResult($result), $connection); + return new Result(self::createArrayResult($result), $connection); }, ); @@ -248,7 +357,7 @@ public function foo(): void { $table = $this->schema->createTable('foo'); $table->addColumn('id', 'integer', ['autoincrement' => true]); - $table->addColumn('bar', 'string'); + $table->addColumn('bar', 'string', ['length' => 255]); } }; } @@ -257,4 +366,13 @@ private function snapshotPath(): string { return __DIR__; } + + /** @param mixed[] $result */ + private static function createArrayResult(array $result): ArrayResult + { + $columnNames = array_keys($result[0] ?? []); + $rows = array_map(array_values(...), $result); + + return new ArrayResult($columnNames, $rows); + } } diff --git a/tests/Schema/Strategy/SqliteFileBasedSchemaStrategyTest.php b/tests/Schema/Strategy/SqliteFileBasedSchemaStrategyTest.php index eed78cc..7b6bccf 100644 --- a/tests/Schema/Strategy/SqliteFileBasedSchemaStrategyTest.php +++ b/tests/Schema/Strategy/SqliteFileBasedSchemaStrategyTest.php @@ -7,7 +7,7 @@ use Brainbits\FunctionalTestHelpers\Schema\SchemaBuilder; use Brainbits\FunctionalTestHelpers\Schema\Strategy\SqliteFileBasedSchemaStrategy; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\Platforms\SqlitePlatform; +use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Schema\Schema; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; @@ -144,7 +144,7 @@ public function getSchema(): Schema public function foo(): void { $table = $this->schema->createTable('foo'); - $table->addColumn('bar', 'string'); + $table->addColumn('bar', 'string', ['length' => 255]); } }; } diff --git a/tests/Schema/Strategy/SqliteFileDamaBasedSchemaStrategyTest.php b/tests/Schema/Strategy/SqliteFileDamaBasedSchemaStrategyTest.php index da5841b..85c34d7 100644 --- a/tests/Schema/Strategy/SqliteFileDamaBasedSchemaStrategyTest.php +++ b/tests/Schema/Strategy/SqliteFileDamaBasedSchemaStrategyTest.php @@ -7,7 +7,7 @@ use Brainbits\FunctionalTestHelpers\Schema\SchemaBuilder; use Brainbits\FunctionalTestHelpers\Schema\Strategy\SqliteFileDamaBasedSchemaStrategy; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\Platforms\SqlitePlatform; +use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Schema\Schema; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; @@ -86,7 +86,7 @@ public function getSchema(): Schema public function foo(): void { $table = $this->schema->createTable('foo'); - $table->addColumn('bar', 'string'); + $table->addColumn('bar', 'string', ['length' => 255]); } }; } diff --git a/tests/Schema/Strategy/SqliteMemoryBasedSchemaStrategyTest.php b/tests/Schema/Strategy/SqliteMemoryBasedSchemaStrategyTest.php index 94a952a..0bd6cdd 100644 --- a/tests/Schema/Strategy/SqliteMemoryBasedSchemaStrategyTest.php +++ b/tests/Schema/Strategy/SqliteMemoryBasedSchemaStrategyTest.php @@ -7,7 +7,7 @@ use Brainbits\FunctionalTestHelpers\Schema\SchemaBuilder; use Brainbits\FunctionalTestHelpers\Schema\Strategy\SqliteMemoryBasedSchemaStrategy; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\Platforms\SqlitePlatform; +use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Schema\Schema; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; @@ -55,7 +55,7 @@ public function getSchema(): Schema public function foo(): void { $table = $this->schema->createTable('foo'); - $table->addColumn('bar', 'string'); + $table->addColumn('bar', 'string', ['length' => 255]); } }; } diff --git a/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_apply_schema.json b/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_apply_schema.json index e32cbc5..e4d69ed 100644 --- a/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_apply_schema.json +++ b/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_apply_schema.json @@ -1,10 +1,10 @@ [ { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [] }, { - "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB" + "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL)" } ] \ No newline at end of file diff --git a/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json b/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json index 1524e06..29fff40 100644 --- a/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json +++ b/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json @@ -1,52 +1,66 @@ [ { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [ - { - "name": "old_table_1" - }, - { - "name": "old_table_2" - } + "old_table_1", + "old_table_2" ] }, { "statement": "SET foreign_key_checks = 0" }, { - "query": "SELECT k.CONSTRAINT_NAME, k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME /*!50116 , c.UPDATE_RULE, c.DELETE_RULE */ FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k /*!50116 INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS c ON c.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND c.TABLE_NAME = k.TABLE_NAME */ WHERE k.TABLE_NAME = 'old_table_1' AND k.TABLE_SCHEMA = DATABASE() /*!50116 AND c.CONSTRAINT_SCHEMA = DATABASE() */ORDER BY k.ORDINAL_POSITION", - "parameters": [], + "function": "listTableForeignKeys()", + "parameters": [ + "old_table_1" + ], "result": [ - { - "name": "constraint_1" - }, - { - "name": "constraint_2" - } + "constraint_1", + "constraint_2" ] }, { - "statement": "ALTER TABLE old_table_1 DROP FOREIGN KEY constraint_1" + "function": "dropForeignKey()", + "parameters": [ + "constraint_1", + "old_table_1" + ], + "result": [] }, { - "statement": "ALTER TABLE old_table_1 DROP FOREIGN KEY constraint_2" + "function": "dropForeignKey()", + "parameters": [ + "constraint_2", + "old_table_1" + ], + "result": [] }, { - "statement": "DROP TABLE old_table_1" + "function": "dropTable()", + "parameters": [ + "old_table_1" + ], + "result": [] }, { - "query": "SELECT k.CONSTRAINT_NAME, k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME /*!50116 , c.UPDATE_RULE, c.DELETE_RULE */ FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k /*!50116 INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS c ON c.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND c.TABLE_NAME = k.TABLE_NAME */ WHERE k.TABLE_NAME = 'old_table_2' AND k.TABLE_SCHEMA = DATABASE() /*!50116 AND c.CONSTRAINT_SCHEMA = DATABASE() */ORDER BY k.ORDINAL_POSITION", - "parameters": [], + "function": "listTableForeignKeys()", + "parameters": [ + "old_table_2" + ], "result": [] }, { - "statement": "DROP TABLE old_table_2" + "function": "dropTable()", + "parameters": [ + "old_table_2" + ], + "result": [] }, { "statement": "SET foreign_key_checks = 1" }, { - "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB" + "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL)" } ] \ No newline at end of file diff --git a/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json b/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json index cfdd3eb..d4fd708 100644 --- a/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json +++ b/tests/Schema/Strategy/__snapshots__/mysql_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json @@ -1,14 +1,14 @@ [ { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [] }, { - "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB" + "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL)" }, { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [] } diff --git a/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_apply_schema.json b/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_apply_schema.json index e32cbc5..e4d69ed 100644 --- a/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_apply_schema.json +++ b/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_apply_schema.json @@ -1,10 +1,10 @@ [ { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [] }, { - "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB" + "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL)" } ] \ No newline at end of file diff --git a/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json b/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json index 1524e06..29fff40 100644 --- a/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json +++ b/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_existing_tables_are_dropped_before_creating_fresh_schema.json @@ -1,52 +1,66 @@ [ { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [ - { - "name": "old_table_1" - }, - { - "name": "old_table_2" - } + "old_table_1", + "old_table_2" ] }, { "statement": "SET foreign_key_checks = 0" }, { - "query": "SELECT k.CONSTRAINT_NAME, k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME /*!50116 , c.UPDATE_RULE, c.DELETE_RULE */ FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k /*!50116 INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS c ON c.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND c.TABLE_NAME = k.TABLE_NAME */ WHERE k.TABLE_NAME = 'old_table_1' AND k.TABLE_SCHEMA = DATABASE() /*!50116 AND c.CONSTRAINT_SCHEMA = DATABASE() */ORDER BY k.ORDINAL_POSITION", - "parameters": [], + "function": "listTableForeignKeys()", + "parameters": [ + "old_table_1" + ], "result": [ - { - "name": "constraint_1" - }, - { - "name": "constraint_2" - } + "constraint_1", + "constraint_2" ] }, { - "statement": "ALTER TABLE old_table_1 DROP FOREIGN KEY constraint_1" + "function": "dropForeignKey()", + "parameters": [ + "constraint_1", + "old_table_1" + ], + "result": [] }, { - "statement": "ALTER TABLE old_table_1 DROP FOREIGN KEY constraint_2" + "function": "dropForeignKey()", + "parameters": [ + "constraint_2", + "old_table_1" + ], + "result": [] }, { - "statement": "DROP TABLE old_table_1" + "function": "dropTable()", + "parameters": [ + "old_table_1" + ], + "result": [] }, { - "query": "SELECT k.CONSTRAINT_NAME, k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME /*!50116 , c.UPDATE_RULE, c.DELETE_RULE */ FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k /*!50116 INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS c ON c.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND c.TABLE_NAME = k.TABLE_NAME */ WHERE k.TABLE_NAME = 'old_table_2' AND k.TABLE_SCHEMA = DATABASE() /*!50116 AND c.CONSTRAINT_SCHEMA = DATABASE() */ORDER BY k.ORDINAL_POSITION", - "parameters": [], + "function": "listTableForeignKeys()", + "parameters": [ + "old_table_2" + ], "result": [] }, { - "statement": "DROP TABLE old_table_2" + "function": "dropTable()", + "parameters": [ + "old_table_2" + ], + "result": [] }, { "statement": "SET foreign_key_checks = 1" }, { - "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB" + "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL)" } ] \ No newline at end of file diff --git a/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json b/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json index cfdd3eb..d4fd708 100644 --- a/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json +++ b/tests/Schema/Strategy/__snapshots__/mysql_dama_based_schema_strategy_schema_is_read_from_cache_if_database_and_cache_exists.json @@ -1,14 +1,14 @@ [ { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [] }, { - "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB" + "statement": "CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(255) NOT NULL)" }, { - "query": "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", + "function": "listTableNames()", "parameters": [], "result": [] }