Skip to content

Commit 6b2da52

Browse files
committed
Add MySQL CLI tests
1 parent 38a7476 commit 6b2da52

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
use Symfony\Component\Process\Process;
4+
5+
class WP_MySQL_Proxy_CLI_Test extends WP_MySQL_Proxy_Test {
6+
public function test_auth_without_password(): void {
7+
$process = Process::fromShellCommandline(
8+
"mysql -h 127.0.0.1 -P {$this->port} -u root -e 'SELECT 123'"
9+
);
10+
$process->run();
11+
12+
$this->assertEquals( 0, $process->getExitCode() );
13+
$this->assertStringContainsString( '123', $process->getOutput() );
14+
}
15+
16+
public function test_auth_with_password(): void {
17+
$process = Process::fromShellCommandline(
18+
"mysql -h 127.0.0.1 -P {$this->port} -u root -proot -e 'SELECT 123'"
19+
);
20+
$process->run();
21+
22+
$this->assertEquals( 0, $process->getExitCode() );
23+
$this->assertStringContainsString( '123', $process->getOutput() );
24+
}
25+
26+
public function test_auth_with_database(): void {
27+
$process = Process::fromShellCommandline(
28+
"mysql -h 127.0.0.1 -P {$this->port} -u root -proot -D sqlite_database -e 'SELECT 123'"
29+
);
30+
$process->run();
31+
32+
$this->assertEquals( 0, $process->getExitCode() );
33+
$this->assertStringContainsString( '123', $process->getOutput() );
34+
}
35+
36+
37+
public function test_auth_with_unknown_database(): void {
38+
$process = Process::fromShellCommandline(
39+
"mysql -h 127.0.0.1 -P {$this->port} -u root -proot -D unknown_database -e 'SELECT 123'"
40+
);
41+
$process->run();
42+
43+
$this->assertEquals( 1, $process->getExitCode() );
44+
$this->assertStringContainsString( "Unknown database: 'unknown_database'", $process->getErrorOutput() );
45+
}
46+
47+
public function test_query(): void {
48+
$query = 'CREATE TABLE t (id INT PRIMARY KEY, name TEXT)';
49+
$process = Process::fromShellCommandline(
50+
"mysql -h 127.0.0.1 -P {$this->port} -u root -proot -e " . escapeshellarg( $query )
51+
);
52+
$process->run();
53+
$this->assertEquals( 0, $process->getExitCode() );
54+
55+
$query = 'INSERT INTO t (id, name) VALUES (123, "abc"), (456, "def")';
56+
$process = Process::fromShellCommandline(
57+
"mysql -h 127.0.0.1 -P {$this->port} -u root -proot -e " . escapeshellarg( $query )
58+
);
59+
$process->run();
60+
$this->assertEquals( 0, $process->getExitCode() );
61+
62+
$query = 'SELECT * FROM t';
63+
$process = Process::fromShellCommandline(
64+
"mysql -h 127.0.0.1 -P {$this->port} -u root -proot -e " . escapeshellarg( $query )
65+
);
66+
$process->run();
67+
$this->assertEquals( 0, $process->getExitCode() );
68+
$this->assertSame(
69+
"id\tname\n123\tabc\n456\tdef\n",
70+
$process->getOutput()
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)