|
7 | 7 |
|
8 | 8 | class MysqlImportTest extends TestCase |
9 | 9 | { |
| 10 | + public function testWrongArguments() |
| 11 | + { |
| 12 | + $file = __DIR__.'/fixtures/database.sql'; |
| 13 | + $message = "Unknown option '-kWrong'."; |
| 14 | + |
| 15 | + $mysqlImport = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file, '-kWrong']); |
| 16 | + $this->assertEquals($message, $mysqlImport->run()); |
| 17 | + $this->assertEquals(2, $mysqlImport->getExitCode()); |
| 18 | + |
| 19 | + $mysqlImport = new MysqlImport([], ['-psecret', '-kWrong', $file]); |
| 20 | + $this->assertEquals($message, $mysqlImport->run()); |
| 21 | + $this->assertEquals(2, $mysqlImport->getExitCode()); |
| 22 | + } |
| 23 | + |
10 | 24 | public function testImportByDefault() |
11 | 25 | { |
12 | 26 | $file = __DIR__.'/fixtures/database.sql'; |
13 | 27 | $message = "Database named 'database' successfully imported."; |
14 | 28 |
|
15 | | - $app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file]); |
16 | | - $this->assertEquals($message, $app->run()); |
17 | | - $app->drop('yes'); |
| 29 | + $mysqlImport = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file]); |
| 30 | + $this->assertEquals($message, $mysqlImport->run()); |
| 31 | + $mysqlImport->drop('yes'); |
18 | 32 |
|
19 | | - $app = new MysqlImport([], ['-psecret', $file]); |
20 | | - $this->assertEquals($message, $app->run()); |
21 | | - $app->drop('yes'); |
| 33 | + $mysqlImport = new MysqlImport([], ['-psecret', $file]); |
| 34 | + $this->assertEquals($message, $mysqlImport->run()); |
| 35 | + $mysqlImport->drop('yes'); |
22 | 36 | } |
23 | 37 |
|
24 | 38 | public function testImportWithUserAndPassword() |
25 | 39 | { |
26 | 40 | $file = __DIR__.'/fixtures/database.sql'; |
27 | 41 | $message = "Database named 'database' successfully imported."; |
28 | 42 |
|
29 | | - $app = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$file]); |
30 | | - $this->assertEquals($message, $app->run()); |
31 | | - $app->drop('yes'); |
| 43 | + $mysqlImport = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$file]); |
| 44 | + $this->assertEquals($message, $mysqlImport->run()); |
| 45 | + $mysqlImport->drop('yes'); |
32 | 46 |
|
33 | | - $app = new MysqlImport([], ['-uroot', '-psecret', $file]); |
34 | | - $this->assertEquals($message, $app->run()); |
35 | | - $app->drop('yes'); |
| 47 | + $mysqlImport = new MysqlImport([], ['-uroot', '-psecret', $file]); |
| 48 | + $this->assertEquals($message, $mysqlImport->run()); |
| 49 | + $mysqlImport->drop('yes'); |
36 | 50 | } |
37 | 51 |
|
38 | 52 | public function testDropAndCreateDatabase() |
39 | 53 | { |
40 | 54 | $file = __DIR__.'/fixtures/database.sql'; |
41 | 55 | $message = "Database named 'database' successfully imported."; |
42 | 56 |
|
43 | | - $app = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$file]); |
| 57 | + $mysqlImport = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$file]); |
44 | 58 |
|
45 | | - $app->run(); |
46 | | - $app->drop(); |
47 | | - $app->drop('yes'); |
| 59 | + $mysqlImport->run(); |
| 60 | + $mysqlImport->drop(); |
| 61 | + $mysqlImport->drop('yes'); |
48 | 62 |
|
49 | | - $this->assertEquals($message, $app->run()); |
50 | | - $app->drop('yes'); |
| 63 | + $this->assertEquals($message, $mysqlImport->run()); |
| 64 | + $mysqlImport->drop('yes'); |
51 | 65 | } |
52 | 66 |
|
53 | 67 | public function testNotBlankDatabase() |
54 | 68 | { |
55 | 69 | $file = __DIR__.'/fixtures/database.sql'; |
56 | 70 | $message = 'Required blank database for import.'; |
57 | 71 |
|
58 | | - $app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file]); |
59 | | - $app->run(); |
| 72 | + $mysqlImport = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file]); |
| 73 | + $mysqlImport->run(); |
| 74 | + |
| 75 | + $mysqlImport = new MysqlImport([], ['-psecret', $file]); |
| 76 | + $this->assertEquals($message, $mysqlImport->run()); |
| 77 | + |
| 78 | + $mysqlImport = new MysqlImport(['DB_USER' => 'root', 'DB_PASSWORD' => 'secret'], [$file]); |
| 79 | + $this->assertEquals($message, $mysqlImport->run()); |
| 80 | + $mysqlImport->drop('yes'); |
| 81 | + } |
| 82 | + |
| 83 | + public function testForceNotBlankDatabase() |
| 84 | + { |
| 85 | + $file = __DIR__.'/fixtures/database.sql'; |
| 86 | + $message = "Duplicate entry 'A007' for key 'PRIMARY'"; |
| 87 | + |
| 88 | + $mysqlImport = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file]); |
| 89 | + $mysqlImport->run(); |
60 | 90 |
|
61 | | - $app = new MysqlImport([], ['-psecret', $file]); |
62 | | - $this->assertEquals($message, $app->run()); |
| 91 | + $mysqlImport = new MysqlImport([], ['-psecret', $file, '--force']); |
| 92 | + $this->assertEquals($message, $mysqlImport->run()); |
63 | 93 |
|
64 | | - $app = new MysqlImport(['DB_USER' => 'root', 'DB_PASSWORD' => 'secret'], [$file]); |
65 | | - $this->assertEquals($message, $app->run()); |
66 | | - $app->drop('yes'); |
| 94 | + $mysqlImport = new MysqlImport(['DB_USER' => 'root', 'DB_PASSWORD' => 'secret'], [$file, '--force']); |
| 95 | + $this->assertEquals($message, $mysqlImport->run()); |
| 96 | + $mysqlImport->drop('yes'); |
67 | 97 | } |
68 | 98 |
|
69 | 99 | public function testConnectionProblemWrongPassword() |
70 | 100 | { |
71 | 101 | $file = __DIR__.'/fixtures/database.sql'; |
72 | 102 | $message = "Connection problem for 'root' on 'mysql' with error: "; |
73 | 103 |
|
74 | | - $app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'wrong'], [$file]); |
75 | | - $this->assertStringStartsWith($message, $app->run()); |
| 104 | + $mysqlImport = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'wrong'], [$file]); |
| 105 | + $this->assertStringStartsWith($message, $mysqlImport->run()); |
76 | 106 | } |
77 | 107 |
|
78 | 108 | public function testConnectionProblemWrongHost() |
79 | 109 | { |
80 | 110 | $file = __DIR__.'/fixtures/database.sql'; |
81 | 111 | $message = "Connection problem for 'root' on 'wrong' with error: "; |
82 | 112 |
|
83 | | - $app = new MysqlImport(['MYSQL_HOST' => 'wrong', 'MYSQL_ROOT_PASSWORD' => 'wrong'], [$file]); |
84 | | - $this->assertStringStartsWith($message, $app->run()); |
| 113 | + $mysqlImport = new MysqlImport(['MYSQL_HOST' => 'wrong', 'MYSQL_ROOT_PASSWORD' => 'wrong'], [$file]); |
| 114 | + $this->assertStringStartsWith($message, $mysqlImport->run()); |
85 | 115 |
|
86 | | - $app = new MysqlImport( |
| 116 | + $mysqlImport = new MysqlImport( |
87 | 117 | ['MYSQL_HOST' => 'wrong', 'MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'wrong'], |
88 | 118 | [$file] |
89 | 119 | ); |
90 | | - $this->assertStringStartsWith($message, $app->run()); |
91 | | - $this->assertEquals(2, $app->getExitCode()); |
| 120 | + $this->assertStringStartsWith($message, $mysqlImport->run()); |
| 121 | + $this->assertEquals(1, $mysqlImport->getExitCode()); |
92 | 122 | } |
93 | 123 |
|
94 | 124 | public function testMissingSqlFile() |
95 | 125 | { |
96 | | - $app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], []); |
97 | | - $this->assertEquals('Required sql file to import.', $app->run()); |
| 126 | + $mysqlImport = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], []); |
| 127 | + $this->assertEquals('Required sql file to import.', $mysqlImport->run()); |
98 | 128 | } |
99 | 129 |
|
100 | 130 | public function testNotExistsSqlFile() |
101 | 131 | { |
102 | 132 | $file = __DIR__.'/fixtures/not_exists.sql'; |
103 | | - $app = new MysqlImport([], ['-psecret', $file]); |
104 | | - $this->assertEquals("Sql file '{$file}' not found.", $app->run()); |
| 133 | + $mysqlImport = new MysqlImport([], ['-psecret', $file]); |
| 134 | + $this->assertEquals("Sql file '{$file}' not found.", $mysqlImport->run()); |
105 | 135 | } |
106 | 136 |
|
107 | 137 | public function testMissingRootPassword() |
108 | 138 | { |
109 | | - $app = new MysqlImport([], [__DIR__.'/fixtures/database.sql']); |
110 | | - $this->assertEquals('Required at least root password.', $app->run()); |
| 139 | + $mysqlImport = new MysqlImport([], [__DIR__.'/fixtures/database.sql']); |
| 140 | + $this->assertEquals('Required at least root password.', $mysqlImport->run()); |
111 | 141 | } |
112 | 142 |
|
113 | 143 | public function testHostPortFix() |
114 | 144 | { |
115 | | - $app = new MysqlImport(['WORDPRESS_DB_HOST' => 'db:10101'], [__DIR__.'/fixtures/database.sql']); |
116 | | - $this->assertEquals(['host' => 'db', 'port' => 10101, 'database' => 'database'], $app->getInfo()); |
| 145 | + $mysqlImport = new MysqlImport(['WORDPRESS_DB_HOST' => 'db:10101'], [__DIR__.'/fixtures/database.sql']); |
| 146 | + $this->assertEquals(['host' => 'db', 'port' => 10101, 'database' => 'database'], $mysqlImport->getInfo()); |
117 | 147 | } |
118 | 148 |
|
119 | 149 | public function testDefaultDatabaseName() |
120 | 150 | { |
121 | | - $app = new MysqlImport(['WORDPRESS_DB_PASSWORD' => 'secret'], [__DIR__.'/fixtures/database.sql']); |
122 | | - $this->assertEquals(['host' => 'mysql', 'port' => 3306, 'database' => 'wordpress'], $app->getInfo()); |
| 151 | + $mysqlImport = new MysqlImport(['WORDPRESS_DB_PASSWORD' => 'secret'], [__DIR__.'/fixtures/database.sql']); |
| 152 | + $this->assertEquals(['host' => 'mysql', 'port' => 3306, 'database' => 'wordpress'], $mysqlImport->getInfo()); |
123 | 153 | } |
124 | 154 |
|
125 | 155 | public function testSyntaxError() |
126 | 156 | { |
127 | 157 | $file = __DIR__.'/fixtures/syntax_error.sql'; |
128 | 158 | $message = 'You have an error in your SQL syntax; check the manual that corresponds to'; |
129 | 159 |
|
130 | | - $app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file]); |
131 | | - $this->assertStringStartsWith($message, $app->run()); |
| 160 | + $mysqlImport = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$file]); |
| 161 | + $this->assertStringStartsWith($message, $mysqlImport->run()); |
132 | 162 | } |
133 | 163 | } |
0 commit comments