Skip to content

Commit 7176169

Browse files
Merge pull request #292 from jbouzekri
Add support for uri in mongodump
2 parents a9ad8d9 + 1d72be5 commit 7176169

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

src/Backup/Source/Mongodump.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ class Mongodump extends SimulatorExecutable implements Simulator
3535
*/
3636
private $useIPv6;
3737

38+
/**
39+
* Uri to connect to
40+
* --uri <uri>
41+
*
42+
* @var string
43+
*/
44+
private $uri;
45+
3846
/**
3947
* Host to connect to
4048
* --host <hostname:port>
@@ -113,6 +121,8 @@ public function setup(array $conf = [])
113121

114122
$this->pathToMongodump = Util\Arr::getValue($conf, 'pathToMongodump', '');
115123
$this->useIPv6 = Util\Str::toBoolean(Util\Arr::getValue($conf, 'ipv6', ''), false);
124+
125+
$this->setupValidation();
116126
}
117127

118128
/**
@@ -137,12 +147,32 @@ protected function setupSourceData(array $conf)
137147
*/
138148
protected function setupCredentials(array $conf)
139149
{
150+
$this->uri = Util\Arr::getValue($conf, 'uri', '');
140151
$this->host = Util\Arr::getValue($conf, 'host', '');
141152
$this->user = Util\Arr::getValue($conf, 'user', '');
142153
$this->password = Util\Arr::getValue($conf, 'password', '');
143154
$this->authenticationDatabase = Util\Arr::getValue($conf, 'authenticationDatabase', '');
144155
}
145156

157+
/**
158+
* Validate source setup
159+
*
160+
* @throws \phpbu\App\Exception
161+
*/
162+
protected function setupValidation()
163+
{
164+
if (empty($this->uri)) {
165+
return;
166+
}
167+
168+
// If uri is set, cannot set other configurations
169+
foreach (['user', 'host', 'password', 'authenticationDatabase', 'databases'] as $attr) {
170+
if (!empty($this->{$attr})) {
171+
throw new Exception("cannot specify $attr and uri");
172+
}
173+
}
174+
}
175+
146176
/**
147177
* Execute the backup.
148178
*
@@ -177,6 +207,7 @@ protected function createExecutable(Target $target) : Executable
177207
$executable = new Executable\Mongodump($this->pathToMongodump);
178208
$executable->dumpToDirectory($this->getDumpDir($target))
179209
->useIpv6($this->useIPv6)
210+
->useUri($this->uri)
180211
->useHost($this->host)
181212
->credentials($this->user, $this->password, $this->authenticationDatabase)
182213
->dumpDatabases($this->databases)

src/Cli/Executable/Mongodump.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ class Mongodump extends Abstraction implements Executable
3636
*/
3737
private $useIPv6;
3838

39+
/**
40+
* Uri to connect to
41+
* --uri <uri>
42+
*
43+
* @var string
44+
*/
45+
private $uri;
46+
3947
/**
4048
* Host to connect to
4149
* --host <hostname:port>
@@ -135,6 +143,18 @@ public function useIpv6(bool $bool) : Mongodump
135143
return $this;
136144
}
137145

146+
/**
147+
* Set uri to dump from
148+
*
149+
* @param string $uri
150+
* @return \phpbu\App\Cli\Executable\Mongodump
151+
*/
152+
public function useUri(string $uri) : Mongodump
153+
{
154+
$this->uri = $uri;
155+
return $this;
156+
}
157+
138158
/**
139159
* Set host to dump from.
140160
*
@@ -228,6 +248,7 @@ protected function createCommandLine() : CommandLine
228248

229249
$cmd->addOption('--out', $this->dumpDir, ' ');
230250
$cmd->addOptionIfNotEmpty('--ipv6', $this->useIPv6, false);
251+
$cmd->addOptionIfNotEmpty('--uri', $this->uri, true, ' ');
231252
$cmd->addOptionIfNotEmpty('--host', $this->host, true, ' ');
232253
$cmd->addOptionIfNotEmpty('--username', $this->user, true, ' ');
233254
$cmd->addOptionIfNotEmpty('--password', $this->password, true, ' ');

tests/phpbu/Backup/Source/MongodumpTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,26 @@ public function testBackupFail()
7575
$mongodump->setup(['pathToMongodump' => PHPBU_TEST_BIN]);
7676
$mongodump->backup($target, $appResult);
7777
}
78+
79+
/**
80+
* Tests Mongodump::backup
81+
*
82+
* @testWith ["user", "myuser"]
83+
* ["host", "myhost"]
84+
* ["password", "mypassword"]
85+
* ["authenticationDatabase", "myAuthenticationDatabase"]
86+
*/
87+
public function testUriNotCompatibleWithOtherSettings($key, $value)
88+
{
89+
$this->expectException('phpbu\App\Exception');
90+
91+
$runner = $this->getRunnerMock();
92+
$runner->expects($this->never())
93+
->method('run');
94+
95+
$conf = array_merge(['uri' => 'mymongouri'], [$key => $value]);
96+
97+
$mongo = new Mongodump($runner);
98+
$mongo->setup($conf);
99+
}
78100
}

tests/phpbu/Cli/Executable/MongodumpTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,18 @@ public function testExcludeCollectionsWithPrefix()
152152
$mongo->getCommand()
153153
);
154154
}
155+
156+
/**
157+
* Tests Mongodump::createCommandLine
158+
*/
159+
public function testUri()
160+
{
161+
$mongo = new Mongodump(PHPBU_TEST_BIN);
162+
$mongo->dumpToDirectory('./dump')->useUri('mymongouri');
163+
164+
$this->assertEquals(
165+
PHPBU_TEST_BIN . '/mongodump --out \'./dump' . '\' --uri \'mymongouri\'',
166+
$mongo->getCommand()
167+
);
168+
}
155169
}

0 commit comments

Comments
 (0)