Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Rewrite MySQL configuration file formation. Rewrite file test to hand…
Browse files Browse the repository at this point in the history
…le that some params may be missing.
  • Loading branch information
yamilovs committed Oct 12, 2016
1 parent debfc1f commit b7a1835
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
24 changes: 14 additions & 10 deletions Database/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
12 changes: 9 additions & 3 deletions Tests/Database/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand Down

0 comments on commit b7a1835

Please sign in to comment.