diff --git a/Database/MySQL.php b/Database/MySQL.php index 682334f..b2939d2 100644 --- a/Database/MySQL.php +++ b/Database/MySQL.php @@ -71,18 +71,22 @@ protected function prepareIgnoreTables() */ protected function prepareConfigurationFile() { - if ($this->params['db_user']) { - $cnfParams['user'] = $this->params['db_user']; - $cnfFile = "[client]\n"; - - if ($this->params['db_password']) { - $cnfParams += array( - "password" => $this->params['db_password'], - "host" => $this->params['db_host'], - "port" => $this->params['db_port'] - ); + $cnfFile = "[client]\n"; + $cnfParams = array(); + $configurationMapping = array( + 'user' => 'db_user', + 'password' => 'db_password', + 'host' => 'db_host', + 'port' => 'db_port', + ); + + foreach ($configurationMapping as $key => $param) { + if ($this->params[$param]) { + $cnfParams[$key] = $this->params[$param]; } + } + if (!empty($cnfParams)) { foreach ($cnfParams as $key => $value) { $cnfFile .= "$key = \"$value\"\n"; } diff --git a/Tests/Database/MySQLTest.php b/Tests/Database/MySQLTest.php index 63a5cfc..a93f209 100644 --- a/Tests/Database/MySQLTest.php +++ b/Tests/Database/MySQLTest.php @@ -12,7 +12,11 @@ class MySQLTest extends \PHPUnit_Framework_TestCase protected function checkConfigurationFileExistsAndValid($user, $password, $host, $port) { $filePath = '/tmp/backup/mysql/mysql.cnf'; - $cnfFileContent = "[client]\nuser = \"$user\"\npassword = \"$password\"\nhost = \"$host\"\nport = \"$port\"\n"; + $cnfFileContent = "[client]\n"; + $cnfFileContent .= $user ? "user = \"$user\"\n" : ""; + $cnfFileContent .= $password ? "password = \"$password\"\n" : ""; + $cnfFileContent .= $host ? "host = \"$host\"\n" : ""; + $cnfFileContent .= $port ? "port = \"$port\"\n" : ""; $this->assertFileExists($filePath); $this->assertContains(file_get_contents($filePath), $cnfFileContent); @@ -83,7 +87,8 @@ public function shouldDumpSpecifiedDatabase() ), ), '/tmp/backup/'); - $this->assertEquals($mysql->getCommand(), 'mysqldump somebdd > \'/tmp/backup/mysql/somebdd.sql\''); + $this->assertEquals($mysql->getCommand(), 'mysqldump --defaults-extra-file="/tmp/backup/mysql/mysql.cnf" somebdd > \'/tmp/backup/mysql/somebdd.sql\''); + $this->checkConfigurationFileExistsAndValid(null, null, 'somehost', '2222'); } /** @@ -103,7 +108,8 @@ public function shouldDumpAllDatabasesWithNoAuth() ), ), '/tmp/backup/'); - $this->assertEquals($mysql->getCommand(), 'mysqldump --all-databases > \'/tmp/backup/mysql/all-databases.sql\''); + $this->assertEquals($mysql->getCommand(), 'mysqldump --defaults-extra-file="/tmp/backup/mysql/mysql.cnf" --all-databases > \'/tmp/backup/mysql/all-databases.sql\''); + $this->checkConfigurationFileExistsAndValid(null, null, 'somehost', '2222'); } /**