From e20c021c9be98b6618ec845ffa7b600824beb3bd Mon Sep 17 00:00:00 2001 From: thePanz Date: Thu, 27 Jun 2024 17:07:55 +0200 Subject: [PATCH] Add(migrations) Add MySQL migration tests for #98 --- .github/workflows/continuous-integration.yml | 12 +++++++ lib/Doctrine/Connection.php | 8 +++-- tests/DoctrineTest/Doctrine_UnitTestCase.php | 11 ++++++ tests/MigrationMysqlTestCase.php | 38 ++++++++++++++++++++ tests/run.php | 1 + 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/MigrationMysqlTestCase.php diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 26b4f3b23..f6cf09ccd 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -35,6 +35,16 @@ jobs: - "8.1" - "8.2" - "8.3" + services: + mysql: + image: mysql:5.7.42-debian + env: + MYSQL_DATABASE: 'doctrine1_test' + MYSQL_USER: 'doctrine1' + MYSQL_PASSWORD: 'd0ctr1n3' + MYSQL_ALLOW_EMPTY_PASSWORD: true + ports: + - 3306:3306 steps: - name: Checkout @@ -62,4 +72,6 @@ jobs: run: composer install --prefer-dist - name: Run Tests + env: + MYSQL_TEST_DSN: 'mysql:host=127.0.0.1;dbname=doctrine1_test;user=doctrine1;password=d0ctr1n3' run: cd tests && php run.php diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index 73a7e5fa7..d884039cd 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -1556,7 +1556,9 @@ public function getTmpConnection($info) $pdoDsn .= 'unix_socket=' . $info['unix_socket'] . ';'; } - $pdoDsn .= 'host=' . $info['host']; + if ($info['host']) { + $pdoDsn .= 'host=' . $info['host']; + } if ($info['port']) { $pdoDsn .= ';port=' . $info['port']; @@ -1566,8 +1568,8 @@ public function getTmpConnection($info) $pdoDsn .= ';dbname=' . $this->export->tmpConnectionDatabase; } - $username = $this->getOption('username'); - $password = $this->getOption('password'); + $username = $info['user'] ?: $this->getOption('username'); + $password = $info['password'] ?: $this->getOption('password'); $conn = $this->getManager()->openConnection(array($pdoDsn, $username, $password), 'doctrine_tmp_connection', false); $conn->setOption('username', $username); diff --git a/tests/DoctrineTest/Doctrine_UnitTestCase.php b/tests/DoctrineTest/Doctrine_UnitTestCase.php index 3fa7bcbcd..f207f239b 100644 --- a/tests/DoctrineTest/Doctrine_UnitTestCase.php +++ b/tests/DoctrineTest/Doctrine_UnitTestCase.php @@ -307,6 +307,17 @@ public function getDeclaration($type) return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true)); } + /** + * @param string $pdoDsn + * @return Doctrine_Connection + */ + protected function openAdditionalPdoConnection(string $pdoDsn) + { + $pdoFromDsn = new PDO($pdoDsn); + + return $this->openAdditionalConnection($pdoFromDsn); + } + protected function openAdditionalConnection($adapter = null, $name = null) { $connection = $this->manager->openConnection($adapter, $name); diff --git a/tests/MigrationMysqlTestCase.php b/tests/MigrationMysqlTestCase.php new file mode 100644 index 000000000..2e5103af9 --- /dev/null +++ b/tests/MigrationMysqlTestCase.php @@ -0,0 +1,38 @@ +connection = $this->openAdditionalPdoConnection($dsn); + $this->connection->setOption('dsn', $dsn); + $this->connection->dropDatabase(); + $this->connection->createDatabase(); + } + + public function disabled_testAfterSuccessfullMigrationItWillSetMigratedVersionAsCurrentVersionInMysqlDB() + { + $migration = new Doctrine_Migration(__DIR__.'/migration_classes', $this->connection); + $this->assertFalse($migration->hasMigrated()); + $migration->setCurrentVersion(0); + $this->assertFalse($this->connection->import->tableExists('migration_phonenumber')); + $this->assertFalse($this->connection->import->tableExists('migration_user')); + $this->assertFalse($this->connection->import->tableExists('migration_profile')); + + $migration->migrate(3); + $this->assertTrue($migration->hasMigrated()); + $this->assertEqual($migration->getCurrentVersion(), 3); + $this->assertTrue($this->connection->import->tableExists('migration_phonenumber')); + $this->assertTrue($this->connection->import->tableExists('migration_user')); + $this->assertTrue($this->connection->import->tableExists('migration_profile')); + + $migration->migrate(4); + $this->assertEqual($migration->getCurrentVersion(), 4); + $this->assertTrue($this->connection->import->tableExists('migration_phonenumber')); + $this->assertTrue($this->connection->import->tableExists('migration_user')); + $this->assertFalse($this->connection->import->tableExists('migration_profile')); + } +} diff --git a/tests/run.php b/tests/run.php index d1dd2c7ee..ddb4619ba 100644 --- a/tests/run.php +++ b/tests/run.php @@ -279,6 +279,7 @@ // Migration Tests $migration = new GroupTest('Migration Tests', 'migration'); $migration->addTestCase(new Doctrine_Migration_TestCase()); +$migration->addTestCase(new Doctrine_MigrationMysql_TestCase()); $migration->addTestCase(new Doctrine_Migration_Base_TestCase()); $migration->addTestCase(new Doctrine_Migration_Diff_TestCase()); $test->addTestCase($migration);