Skip to content

Commit abec9a2

Browse files
Issue #9 'Mongodump' is no longer handling the compression.
'Runner' is able to handle uncompressed source data
1 parent 3414a06 commit abec9a2

File tree

7 files changed

+234
-75
lines changed

7 files changed

+234
-75
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
namespace phpbu\App\Backup\Compressor;
3+
4+
use phpbu\App\Backup\Source\Tar;
5+
use phpbu\App\Backup\Target;
6+
use phpbu\App\Exception;
7+
use phpbu\App\Result;
8+
9+
/**
10+
* Directory
11+
*
12+
* @package phpbu
13+
* @subpackage Backup
14+
* @author Sebastian Feldmann <sebastian@phpbu.de>
15+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
16+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
17+
* @link http://phpbu.de/
18+
* @since Class available since Release 2.0.1
19+
*/
20+
class Directory
21+
{
22+
/**
23+
* Path to dir to compress.
24+
*
25+
* @var string
26+
*/
27+
private $path;
28+
29+
/**
30+
* Constructor.
31+
*
32+
* @param string $path
33+
* @throws \phpbu\App\Exception
34+
*/
35+
public function __construct($path)
36+
{
37+
if (empty($path)) {
38+
throw new Exception('no path to compress set');
39+
}
40+
if (!is_dir($path)) {
41+
throw new Exception('path to compress should be a directory');
42+
}
43+
$this->path = $path;
44+
}
45+
46+
/**
47+
* Compress the configured directory.
48+
*
49+
* @param \phpbu\App\Backup\Target $target
50+
* @param \phpbu\App\Result $result
51+
* @throws \phpbu\App\Exception
52+
*/
53+
public function compress(Target $target, Result $result)
54+
{
55+
try {
56+
$tar = new Tar();
57+
$tar->setup(
58+
array(
59+
'path' => $this->path,
60+
'removeDir' => 'true',
61+
)
62+
);
63+
$tar->backup($target, $result);
64+
} catch (\Exception $e) {
65+
throw new Exception('Failed to \'tar\' directory: ' . $this->path . PHP_EOL . $e->getMessage(), 1, $e);
66+
}
67+
}
68+
}

src/Backup/Source/Mongodump.php

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,6 @@ class Mongodump extends Binary implements Source
109109
*/
110110
private $validateConnection;
111111

112-
/**
113-
* Tar source to compress MongoDB dump directory
114-
*
115-
* @var \phpbu\App\Backup\Source\Tar
116-
*/
117-
private $tar;
118-
119112
/**
120113
* (No PHPDoc)
121114
*
@@ -185,15 +178,7 @@ public function backup(Target $target, Result $result)
185178
throw new Exception('Mongodump failed');
186179
}
187180

188-
try {
189-
$tar = $this->getTar($target);
190-
$tar->backup($target, $result);
191-
$result->debug('remove dump directory');
192-
} catch (\Exception $e) {
193-
throw new Exception('Failed to \'tar\' Mongodump directory', 1, $e);
194-
}
195-
// return Status::create()->uncompressed()->dataPath($this->getDumpDir($target));
196-
return Status::create();
181+
return Status::create()->uncompressed()->dataPath($this->getDumpDir($target));
197182
}
198183

199184
/**
@@ -242,37 +227,6 @@ public function getExec(Target $target)
242227
return $this->exec;
243228
}
244229

245-
/**
246-
* Tar setter, mostly for test purposes.
247-
*
248-
* @param \phpbu\App\Backup\Source\Tar $tar
249-
*/
250-
public function setTar(Tar $tar)
251-
{
252-
$this->tar = $tar;
253-
}
254-
255-
/**
256-
* Create a Tar backup source to compress the MongoDB dump directory.
257-
*
258-
* @param \phpbu\App\Backup\Target $target
259-
* @return \phpbu\App\Backup\Source\Tar
260-
* @throws \phpbu\App\Exception
261-
*/
262-
public function getTar(Target $target)
263-
{
264-
if (null == $this->tar) {
265-
$this->tar = new Tar();
266-
$this->tar->setup(
267-
array(
268-
'path' => $this->getDumpDir($target),
269-
'removeDir' => 'true',
270-
)
271-
);
272-
}
273-
return $this->tar;
274-
}
275-
276230
/**
277231
* Get the MongoDB dump directory.
278232
*

src/Configuration/Backup/Target.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace phpbu\App\Configuration\Backup;
33

4+
use phpbu\App\Exception;
5+
46
/**
57
* Target Configuration
68
*
@@ -38,12 +40,17 @@ class Target
3840
/**
3941
* Constructor.
4042
*
41-
* @param string $dir
42-
* @param string $file
43-
* @param string $compression
43+
* @param string $dir
44+
* @param string $file
45+
* @param string $compression
46+
* @throws \phpbu\App\Exception
4447
*/
4548
public function __construct($dir, $file, $compression = null)
4649
{
50+
// check dirname and filename
51+
if ($dir == '' || $file == '') {
52+
throw new Exception('dirname and filename must be set');
53+
}
4754
$this->dirname = $dir;
4855
$this->filename = $file;
4956

src/Runner.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -254,25 +254,8 @@ protected function executeBackup(Configuration\Backup $conf, Target $target)
254254
*/
255255
protected function handleCompression(Target $target, Status $status)
256256
{
257-
$pathToCompress = $status->getDataPath();
258-
if (empty($pathToCompress)) {
259-
throw new Exception('no path to compress set');
260-
}
261-
if (!is_dir($pathToCompress)) {
262-
throw new Exception('path to compress should be a directory');
263-
}
264-
try {
265-
$tar = new Backup\Source\Tar();
266-
$tar->setup(
267-
array(
268-
'path' => $pathToCompress,
269-
'removeDir' => 'true',
270-
)
271-
);
272-
$tar->backup($target, $this->result);
273-
} catch (\Exception $e) {
274-
throw new Exception('Failed to \'tar\' directory: ' . $pathToCompress . PHP_EOL . $e->getMessage(), 1, $e);
275-
}
257+
$dirCompressor = new Compressor\Directory($status->getDataPath());
258+
$dirCompressor->compress($target, $this->result);
276259
}
277260

278261
/**

tests/phpbu/Backup/Source/MongodumpTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,12 @@ public function testBackupOk()
181181
$exec = $this->getMockBuilder('\\phpbu\\App\\Backup\\Cli\\Exec')
182182
->disableOriginalConstructor()
183183
->getMock();
184-
$tar = $this->getMockBuilder('\\phpbu\\App\\Backup\\Source\\Tar')
185-
->disableOriginalConstructor()
186-
->getMock();
187184

188-
$appResult->expects($this->exactly(2))->method('debug');
185+
$appResult->expects($this->once())->method('debug');
189186
$exec->expects($this->once())->method('execute')->willReturn($cliResult);
190-
$tar->expects($this->once())->method('backup');
191187

192188
$this->mongodump->setup(array());
193189
$this->mongodump->setExec($exec);
194-
$this->mongodump->setTar($tar);
195190
$this->mongodump->backup($target, $appResult);
196191
}
197192

tests/phpbu/Backup/TargetTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,63 @@
1414
*/
1515
class TargetTest extends \PHPUnit_Framework_TestCase
1616
{
17+
/**
18+
* Tests Target::setupPath
19+
*/
20+
public function testSetupPath()
21+
{
22+
$path = sys_get_temp_dir() . '/foo';
23+
$filename = 'foo.txt';
24+
$target = new Target($path, $filename);
25+
$target->setupPath();
26+
27+
$this->assertTrue(is_dir($target->getPath()));
28+
29+
rmdir($target->getPath());
30+
}
31+
32+
/**
33+
* Tests Target::setupPath
34+
*
35+
* @expectedException \phpbu\App\Exception
36+
*/
37+
public function testSetupPathNotWritable()
38+
{
39+
$filename = 'foo.txt';
40+
$path = sys_get_temp_dir() . '/bar';
41+
mkdir($path, 0100);
42+
43+
44+
try {
45+
$target = new Target($path, $filename);
46+
$target->setupPath();
47+
} catch (\Exception $e) {
48+
chmod($target->getPath(), 0755);
49+
rmdir($target->getPath());
50+
throw $e;
51+
}
52+
}
53+
54+
/**
55+
* Tests Target::setupPath
56+
*
57+
* @expectedException \phpbu\App\Exception
58+
*/
59+
public function testSetupPathCantCreateDir()
60+
{
61+
$filename = 'foo.txt';
62+
$path = sys_get_temp_dir() . '/bar';
63+
mkdir($path, 0100);
64+
try {
65+
$target = new Target($path . '/baz', $filename);
66+
$target->setupPath();
67+
} catch (\Exception $e) {
68+
chmod($path, 0755);
69+
rmdir($path);
70+
throw $e;
71+
}
72+
}
73+
1774
/**
1875
* Tests Target::isCompressed
1976
*/
@@ -396,6 +453,65 @@ public function testGetSizeFail()
396453
$this->assertFalse(true, 'exception should be thrown');
397454
}
398455

456+
/**
457+
* Tests Target::unlink
458+
*
459+
* @expectedException \phpbu\App\Exception
460+
*/
461+
public function testUnlinkNotExists()
462+
{
463+
$path = sys_get_temp_dir() . '/foo';
464+
$filename = 'foo.txt';
465+
$target = new Target($path, $filename);
466+
$target->unlink();
467+
}
468+
469+
/**
470+
* Tests Target::unlink
471+
*
472+
* @expectedException \phpbu\App\Exception
473+
*/
474+
public function testUnlinkNotWritable()
475+
{
476+
$path = sys_get_temp_dir() . '/foo';
477+
$filename = 'foo.txt';
478+
$target = new Target($path, $filename);
479+
480+
// create the file
481+
mkdir($target->getPath(), 0755);
482+
file_put_contents($target->getPathname(), 'content');
483+
chmod($target->getPathname(), 0444);
484+
485+
try {
486+
$target->unlink();
487+
} catch (\Exception $e) {
488+
chmod($target->getPathname(), 0644);
489+
unlink($target->getPathname());
490+
rmdir($target->getPath());
491+
throw $e;
492+
}
493+
}
494+
495+
/**
496+
* Tests Target::unlink
497+
*/
498+
public function testUnlinkSuccess()
499+
{
500+
$path = sys_get_temp_dir() . '/foo';
501+
$filename = 'foo.txt';
502+
$target = new Target($path, $filename);
503+
$target->setupPath();
504+
505+
// create the file
506+
file_put_contents($target->getPathname(), 'content');
507+
508+
$target->unlink();
509+
rmdir($target->getPath());
510+
511+
$this->assertFalse(file_exists($target->getPathname()));
512+
$this->assertFalse(is_dir($target->getPath()));
513+
}
514+
399515
/**
400516
* Create Compressor Mock.
401517
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace phpbu\App\Configuration\Backup;
3+
4+
/**
5+
* Target Configuration test
6+
*
7+
* @package phpbu
8+
* @subpackage tests
9+
* @author Sebastian Feldmann <sebastian@phpbu.de>
10+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
11+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
12+
* @link http://www.phpbu.de/
13+
* @since Class available since Release 2.0.0
14+
*/
15+
class TargetTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* Tests Target::__construct()
19+
*
20+
* @expectedException \phpbu\App\Exception
21+
*/
22+
public function testMandatoryDir()
23+
{
24+
$target = new Target('', 'bar.txt');
25+
}
26+
27+
/**
28+
* Tests Target::__construct()
29+
*
30+
* @expectedException \phpbu\App\Exception
31+
*/
32+
public function testMandatoryFile()
33+
{
34+
$target = new Target('/foo', '');
35+
}
36+
}

0 commit comments

Comments
 (0)