Skip to content

Commit 8eed2ed

Browse files
introducing --force for not blank database
1 parent 9af1110 commit 8eed2ed

File tree

5 files changed

+246
-165
lines changed

5 files changed

+246
-165
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "javanile/mysql-import",
33
"description": "Import SQL file from command-line",
4-
"version": "0.0.14",
4+
"version": "0.0.15",
55
"license": "MIT",
66
"type": "library",
77
"authors": [

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit bootstrap="./vendor/autoload.php" colors="true">
3+
<php>
4+
<const name="PHPUNIT_MYSQL_IMPORT" value="true"/>
5+
</php>
36
<filter>
47
<whitelist>
58
<directory>./src/</directory>

src/MysqlImport.php

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@ class MysqlImport
8080
protected $force;
8181

8282
/**
83-
* @var boolean
83+
* @var string
8484
*/
8585
protected $error;
8686

87+
/**
88+
* @var string
89+
*/
90+
protected $unknownOption;
91+
8792
/**
8893
* @var loader
8994
*/
@@ -99,8 +104,16 @@ public function __construct($env, $argv)
99104
{
100105
$this->exitCode = 0;
101106
$this->loader = '/\______';
102-
$this->doWhile = in_array('--do-while', $argv);
103-
$this->force = in_array('--force', $argv);
107+
108+
if (in_array('--do-while', $argv)) {
109+
$argv = array_diff($argv, ['--do-while']);
110+
$this->doWhile = true;
111+
}
112+
113+
if (in_array('--force', $argv)) {
114+
$argv = array_diff($argv, ['--force']);
115+
$this->force = true;
116+
}
104117

105118
$defaultDatabase = isset($env['WORDPRESS_DB_PASSWORD']) ? 'wordpress' : 'database';
106119

@@ -124,8 +137,8 @@ public function __construct($env, $argv)
124137

125138
// Get value from command-line argument
126139
if ($opt[2] && $arg = preg_grep('/^'.$opt[2].'[\S]*/', $argv)) {
127-
var_dump($arg);
128140
$value = substr(end($arg), strlen($opt[2]));
141+
$argv = array_diff($argv, $arg);
129142
}
130143

131144
// Place value on property
@@ -143,27 +156,33 @@ public function __construct($env, $argv)
143156
$this->port = $matches[1];
144157
}
145158

146-
//
159+
// Get file to import or work without file
147160
if (in_array('--no-file', $argv)) {
148161
$this->file = false;
149-
150-
return;
151-
}
152-
153-
// Look file to import
154-
foreach ($argv as $arg) {
155-
if ($arg[0] == '-') {
156-
continue;
162+
$argv = array_diff($argv, ['--no-file']);
163+
} else {
164+
foreach ($argv as $arg) {
165+
if ($arg[0] != '-') {
166+
$this->file = $arg;
167+
$argv = array_diff($argv, [$arg]);
168+
break;
169+
}
157170
}
158-
$this->file = $arg;
159171
}
172+
173+
// Set first unprocessed argument as unknown option
174+
$this->unknownOption = reset($argv);
160175
}
161176

162177
/**
163178
* Command entry-point.
164179
*/
165180
public function run()
166181
{
182+
if ($this->unknownOption) {
183+
return $this->messageUnknownOption();
184+
}
185+
167186
if ($this->doWhile) {
168187
$time = time() + 300;
169188
do {
@@ -407,7 +426,7 @@ protected function messageDatabaseNotBlank()
407426
*/
408427
protected function messageConnectionProblem($user)
409428
{
410-
$this->exitCode = 2;
429+
$this->exitCode = 1;
411430

412431
$message = mysqli_connect_error();
413432
$errorNumber = mysqli_connect_errno();
@@ -417,6 +436,22 @@ protected function messageConnectionProblem($user)
417436
);
418437
}
419438

439+
/**
440+
* Message for connection problem.
441+
*
442+
* @param $user
443+
*
444+
* @return string
445+
*/
446+
protected function messageUnknownOption()
447+
{
448+
$this->exitCode = 2;
449+
450+
return $this->message(
451+
"Unknown option '{$this->unknownOption}'."
452+
);
453+
}
454+
420455
/**
421456
* Get exit code after run.
422457
*
@@ -452,10 +487,23 @@ public function waiting($second = 10)
452487
{
453488
$freq = 10;
454489
for ($i = 0; $i < $second * $freq; $i++) {
455-
echo $text = '['.substr($this->loader, 0, -1).'] waiting... ';
490+
$this->print($text = '['.substr($this->loader, 0, -1).'] waiting... ');
456491
usleep(1000000 / $freq);
457492
$this->loader = substr($this->loader, -1).substr($this->loader, 0, -1);
458-
echo str_repeat("\010", strlen($text));
493+
$this->print(str_repeat("\010", strlen($text)));
494+
}
495+
}
496+
497+
/**
498+
* @param $input
499+
* @return mixed
500+
*/
501+
public function print($input)
502+
{
503+
if (defined('PHPUNIT_MYSQL_IMPORT') && PHPUNIT_MYSQL_IMPORT) {
504+
return;
459505
}
506+
507+
echo $input;
460508
}
461509
}

tests/MysqlImportTest.php

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,127 +7,157 @@
77

88
class MysqlImportTest extends TestCase
99
{
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+
1024
public function testImportByDefault()
1125
{
1226
$file = __DIR__.'/fixtures/database.sql';
1327
$message = "Database named 'database' successfully imported.";
1428

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');
1832

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');
2236
}
2337

2438
public function testImportWithUserAndPassword()
2539
{
2640
$file = __DIR__.'/fixtures/database.sql';
2741
$message = "Database named 'database' successfully imported.";
2842

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');
3246

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');
3650
}
3751

3852
public function testDropAndCreateDatabase()
3953
{
4054
$file = __DIR__.'/fixtures/database.sql';
4155
$message = "Database named 'database' successfully imported.";
4256

43-
$app = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$file]);
57+
$mysqlImport = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$file]);
4458

45-
$app->run();
46-
$app->drop();
47-
$app->drop('yes');
59+
$mysqlImport->run();
60+
$mysqlImport->drop();
61+
$mysqlImport->drop('yes');
4862

49-
$this->assertEquals($message, $app->run());
50-
$app->drop('yes');
63+
$this->assertEquals($message, $mysqlImport->run());
64+
$mysqlImport->drop('yes');
5165
}
5266

5367
public function testNotBlankDatabase()
5468
{
5569
$file = __DIR__.'/fixtures/database.sql';
5670
$message = 'Required blank database for import.';
5771

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();
6090

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());
6393

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');
6797
}
6898

6999
public function testConnectionProblemWrongPassword()
70100
{
71101
$file = __DIR__.'/fixtures/database.sql';
72102
$message = "Connection problem for 'root' on 'mysql' with error: ";
73103

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());
76106
}
77107

78108
public function testConnectionProblemWrongHost()
79109
{
80110
$file = __DIR__.'/fixtures/database.sql';
81111
$message = "Connection problem for 'root' on 'wrong' with error: ";
82112

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());
85115

86-
$app = new MysqlImport(
116+
$mysqlImport = new MysqlImport(
87117
['MYSQL_HOST' => 'wrong', 'MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'wrong'],
88118
[$file]
89119
);
90-
$this->assertStringStartsWith($message, $app->run());
91-
$this->assertEquals(2, $app->getExitCode());
120+
$this->assertStringStartsWith($message, $mysqlImport->run());
121+
$this->assertEquals(1, $mysqlImport->getExitCode());
92122
}
93123

94124
public function testMissingSqlFile()
95125
{
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());
98128
}
99129

100130
public function testNotExistsSqlFile()
101131
{
102132
$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());
105135
}
106136

107137
public function testMissingRootPassword()
108138
{
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());
111141
}
112142

113143
public function testHostPortFix()
114144
{
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());
117147
}
118148

119149
public function testDefaultDatabaseName()
120150
{
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());
123153
}
124154

125155
public function testSyntaxError()
126156
{
127157
$file = __DIR__.'/fixtures/syntax_error.sql';
128158
$message = 'You have an error in your SQL syntax; check the manual that corresponds to';
129159

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());
132162
}
133163
}

0 commit comments

Comments
 (0)