|
5 | 5 |
|
6 | 6 | class MysqlCommandBuilderTest extends PluginTestCase |
7 | 7 | { |
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; |
9 | 16 |
|
10 | 17 | public function setUp(): void |
11 | 18 | { |
12 | 19 | parent::setUp(); |
13 | 20 |
|
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 | + } |
19 | 40 | } |
20 | 41 |
|
21 | | - /** |
22 | | - * Test function: dump |
23 | | - * Test building a basic dump command with gzip enabled. |
24 | | - */ |
25 | 42 | public function testDumpWithGzip(): void |
26 | 43 | { |
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); |
34 | 52 | } |
35 | 53 |
|
36 | | - /** |
37 | | - * Test function: dump |
38 | | - * Test building a dump command without gzip enabled. |
39 | | - */ |
40 | 54 | public function testDumpWithoutGzip(): void |
41 | 55 | { |
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); |
52 | 64 | } |
53 | 65 |
|
54 | | - /** |
55 | | - * Test function: dump |
56 | | - * Test building a dump command with limited tables. |
57 | | - */ |
58 | 66 | public function testDumpWithSpecificTables(): void |
59 | 67 | { |
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); |
61 | 74 |
|
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); |
65 | 81 | } |
66 | 82 |
|
67 | | - /** |
68 | | - * Test function: import |
69 | | - * Test building an import command with normalized file path. |
70 | | - */ |
71 | 83 | public function testImportCommand(): void |
72 | 84 | { |
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'); |
77 | 87 |
|
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); |
80 | 91 |
|
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); |
83 | 94 | } |
84 | 95 | } |
0 commit comments