Skip to content

Commit 40e5a87

Browse files
committed
Fix
1 parent 7ae3d2a commit 40e5a87

1 file changed

Lines changed: 62 additions & 51 deletions

File tree

tests/support/MysqlCommandBuilderTest.php

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,91 @@
55

66
class MysqlCommandBuilderTest extends PluginTestCase
77
{
8-
protected array $config;
8+
protected string $expectedUser;
9+
protected string $expectedPass;
10+
protected string $expectedDb;
11+
protected string $expectedUsersTable;
12+
protected string $expectedOrdersTable;
13+
protected string $expectedDbTest;
14+
protected string $expectedGzipOutput;
15+
protected string $expectedPlainOutput;
916

1017
public function setUp(): void
1118
{
1219
parent::setUp();
1320

14-
$this->config = [
15-
'username' => 'testuser',
16-
'password' => 'secret',
17-
'database' => 'mydb',
18-
];
21+
if (PHP_OS_FAMILY === 'Windows') {
22+
$this->expectedUser = '-u"testuser"';
23+
$this->expectedPass = "-p'secret'";
24+
$this->expectedDb = '"mydb"';
25+
$this->expectedUsersTable = '"users"';
26+
$this->expectedOrdersTable = '"orders"';
27+
$this->expectedDbTest = '"mydbtest"';
28+
$this->expectedGzipOutput = '| gzip > "/backups/db.sql.gz"';
29+
$this->expectedPlainOutput = '> "/backups/db.sql"';
30+
} else {
31+
$this->expectedUser = "-u'testuser'";
32+
$this->expectedPass = "-p'secret'";
33+
$this->expectedDb = "'mydb'";
34+
$this->expectedUsersTable = "'users'";
35+
$this->expectedOrdersTable = "'orders'";
36+
$this->expectedDbTest = "'mydbtest'";
37+
$this->expectedGzipOutput = '| gzip > \'/backups/db.sql.gz\'';
38+
$this->expectedPlainOutput = '> \'/backups/db.sql\'';
39+
}
1940
}
2041

21-
/**
22-
* Test function: dump
23-
* Test building a basic dump command with gzip enabled.
24-
*/
2542
public function testDumpWithGzip(): void
2643
{
27-
$command = MysqlCommandBuilder::dump($this->config, '/backups/db.sql.gz');
28-
29-
// Assert command contains mysqldump with correct credentials and database
30-
$this->assertStringContainsString('mysqldump --skip-comments --replace -u"testuser" -p\'secret\' "mydb"', $command);
31-
32-
// Assert command includes gzip output redirection
33-
$this->assertStringContainsString('| gzip > "/backups/db.sql.gz"', $command);
44+
$config = ['username' => 'testuser', 'password' => 'secret', 'database' => 'mydb'];
45+
$command = MysqlCommandBuilder::dump($config, '/backups/db.sql.gz', true);
46+
47+
$this->assertStringContainsString(
48+
"mysqldump --skip-comments --replace {$this->expectedUser} {$this->expectedPass} {$this->expectedDb}",
49+
$command
50+
);
51+
$this->assertStringContainsString($this->expectedGzipOutput, $command);
3452
}
3553

36-
/**
37-
* Test function: dump
38-
* Test building a dump command without gzip enabled.
39-
*/
4054
public function testDumpWithoutGzip(): void
4155
{
42-
$command = MysqlCommandBuilder::dump($this->config, '/backups/db.sql', false);
43-
44-
// Assert command contains mysqldump with correct credentials and database
45-
$this->assertStringContainsString('mysqldump --skip-comments --replace -u"testuser" -p\'secret\' "mydb"', $command);
46-
47-
// Assert command includes plain file output redirection
48-
$this->assertStringContainsString('> "/backups/db.sql"', $command);
49-
50-
// Assert gzip is not included
51-
$this->assertStringNotContainsString('gzip', $command);
56+
$config = ['username' => 'testuser', 'password' => 'secret', 'database' => 'mydb'];
57+
$command = MysqlCommandBuilder::dump($config, '/backups/db.sql', false);
58+
59+
$this->assertStringContainsString(
60+
"mysqldump --skip-comments --replace {$this->expectedUser} {$this->expectedPass} {$this->expectedDb}",
61+
$command
62+
);
63+
$this->assertStringContainsString($this->expectedPlainOutput, $command);
5264
}
5365

54-
/**
55-
* Test function: dump
56-
* Test building a dump command with limited tables.
57-
*/
5866
public function testDumpWithSpecificTables(): void
5967
{
60-
$command = MysqlCommandBuilder::dump($this->config, '/backups/tables.sql', true, ['users', 'orders']);
68+
$config = ['username' => 'testuser', 'password' => 'secret', 'database' => 'mydb'];
69+
$tables = ['users', 'orders'];
70+
$command = MysqlCommandBuilder::dump($config, '/backups/tables.sql', true, $tables);
71+
72+
$this->assertStringContainsString($this->expectedUsersTable, $command);
73+
$this->assertStringContainsString($this->expectedOrdersTable, $command);
6174

62-
// Assert both tables are included and shell-escaped
63-
$this->assertStringContainsString('"users"', $command);
64-
$this->assertStringContainsString('"orders"', $command);
75+
// Output path for gzip depends on platform
76+
$expectedGzip = PHP_OS_FAMILY === 'Windows'
77+
? '| gzip > "/backups/tables.sql"'
78+
: '| gzip > \'/backups/tables.sql\'';
79+
80+
$this->assertStringContainsString($expectedGzip, $command);
6581
}
6682

67-
/**
68-
* Test function: import
69-
* Test building an import command with normalized file path.
70-
*/
7183
public function testImportCommand(): void
7284
{
73-
$command = MysqlCommandBuilder::import($this->config, 'C:\\backups\\db.sql.gz');
74-
75-
// Assert correct username and password
76-
$this->assertStringContainsString('mysql -utestuser -psecret', $command);
85+
$dbConfig = ['username' => 'testuser', 'password' => 'secret', 'database' => 'mydb'];
86+
$command = MysqlCommandBuilder::import($dbConfig, 'C:\\backups\\db.sql.gz');
7787

78-
// Assert database name has 'test' suffix and is escaped with double quotes
79-
$this->assertStringContainsString('"mydbtest"', $command);
88+
$this->assertStringContainsString("-utestuser", $command);
89+
$this->assertStringContainsString("-psecret", $command);
90+
$this->assertStringContainsString($this->expectedDbTest, $command);
8091

81-
// Assert Windows path is normalized to forward slashes
82-
$this->assertStringContainsString('C:/backups/db.sql.gz', $command);
92+
// Path should always be normalized to forward slashes
93+
$this->assertStringContainsString("C:/backups/db.sql.gz", $command);
8394
}
8495
}

0 commit comments

Comments
 (0)