Skip to content

Commit

Permalink
Merge pull request #11 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mirko-pagliai authored Aug 11, 2017
2 parents 6ef66e1 + 49803c9 commit d7e03ab
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 208 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ after_success:
- if [[ $COVERAGE = 1 ]]; then bash <(curl -s https://codecov.io/bash); fi

notifications:
email: false
email: false
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 2.x branch
## 2.1 branch
### 2.1.3
* added `createBackup()` and `createSomeBackups()` to the `TestCase` class;
* `BackupManager::_send()` has become `getEmailInstance()`.

### 2.1.2
* fixed `composer.json`: the plugin requires at least version 3.4 of CakePHP.

Expand Down Expand Up @@ -70,4 +74,4 @@
* improved tests. Also errors in the shell are checked.

### 1.0.0
* first release.
* first release.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
</arguments>
</listener>
</listeners>
</phpunit>
</phpunit>
38 changes: 0 additions & 38 deletions src/Driver/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,4 @@ protected function _importExecutable()
{
return sprintf('%s -c -e --dbname=%s', $this->getBinary('pg_restore'), $this->getDbnameAsString());
}

//
// /**
// * Exports the database
// * @param string $filename Filename where you want to export the database
// * @return bool true on success
// * @uses getExportExecutable()
// * @throws InternalErrorException
// */
// public function export($filename)
// {
// //Executes
// exec($this->getExportExecutable($filename), $output, $returnVar);
//
// if ($returnVar !== 0) {
// throw new InternalErrorException(__d('database_backup', '{0} failed with exit code `{1}`', 'pg_dump', $returnVar));
// }
//
// return file_exists($filename);
// }
// /**
// * Imports the database
// * @param string $filename Filename from which you want to import the database
// * @return bool true on success
// * @uses getImportExecutable()
// * @throws InternalErrorException
// */
// public function import($filename)
// {
// //Executes
// exec($this->getImportExecutable($filename), $output, $returnVar);
//
// if ($returnVar !== 0) {
// throw new InternalErrorException(__d('database_backup', '{0} failed with exit code `{1}`', 'pg_restore', $returnVar));
// }
//
// return true;
// }
}
33 changes: 33 additions & 0 deletions src/TestSuite/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,39 @@ public function tearDown()
Configure::write(DATABASE_BACKUP . '.connection', 'test');
}

/**
* Internal method to create a backup file
* @return string
*/
protected function createBackup()
{
return $this->BackupExport->filename('backup.sql')->export();
}

/**
* Internal method to creates some backup files
* @param bool $sleep If `true`, waits a second for each backup
* @return array
*/
protected function createSomeBackups($sleep = false)
{
$files[] = $this->BackupExport->filename('backup.sql')->export();

if ($sleep) {
sleep(1);
}

$files[] = $this->BackupExport->filename('backup.sql.bz2')->export();

if ($sleep) {
sleep(1);
}

$files[] = $this->BackupExport->filename('backup.sql.gz')->export();

return $files;
}

/**
* Deletes all backups
* @return void
Expand Down
17 changes: 9 additions & 8 deletions src/Utility/BackupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,29 @@ public function rotate($rotate)
}

/**
* Internal method to send a backup file via email
* @param string $filename Filename
* Internal method to get an email instance with all options to send a
* backup file via email
* @param string $backup Backup you want to send
* @param string $recipient Recipient's email address
* @return \Cake\Mailer\Email
* @since 1.1.0
* @throws InternalErrorException
*/
protected function _send($filename, $recipient)
protected function getEmailInstance($backup, $recipient)
{
$sender = Configure::read(DATABASE_BACKUP . '.mailSender');

if (!$sender) {
throw new InternalErrorException(__d('database_backup', 'You must first set the mail sender'));
}

$filename = $this->getAbsolutePath($filename);
$backup = $this->getAbsolutePath($backup);

return (new Email)
->setFrom($sender)
->setTo($recipient)
->setSubject(__d('database_backup', 'Database backup {0} from {1}', basename($filename), env('SERVER_NAME', 'localhost')))
->setAttachments($filename);
->setSubject(__d('database_backup', 'Database backup {0} from {1}', basename($backup), env('SERVER_NAME', 'localhost')))
->setAttachments($backup);
}

/**
Expand All @@ -151,10 +152,10 @@ protected function _send($filename, $recipient)
* @param string $recipient Recipient's email address
* @return array
* @since 1.1.0
* @uses _send()
* @uses getEmailInstance()
*/
public function send($filename, $recipient)
{
return $this->_send($filename, $recipient)->send();
return $this->getEmailInstance($filename, $recipient)->send();
}
}
65 changes: 19 additions & 46 deletions tests/TestCase/BackupTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use DatabaseBackup\BackupTrait;
use DatabaseBackup\TestSuite\TestCase;
use DatabaseBackup\Utility\BackupManager;

/**
* BackupTraitTest class
*/
class BackupTraitTest extends TestCase
{
/**
* @var \DatabaseBackup\Utility\BackupManager
*/
protected $Trait;
use BackupTrait;

/**
* @var bool
Expand All @@ -41,43 +38,19 @@ class BackupTraitTest extends TestCase
'core.comments',
];

/**
* Setup the test case, backup the static object values so they can be
* restored. Specifically backs up the contents of Configure and paths in
* App if they have not already been backed up
* @return void
*/
public function setUp()
{
parent::setUp();

$this->Trait = new BackupManager;
}

/**
* Teardown any static object changes and restore them
* @return void
*/
public function tearDown()
{
parent::tearDown();

unset($this->Trait);
}

/**
* Test for `getAbsolutePath()` method
* @test
*/
public function testGetAbsolutePath()
{
$result = $this->Trait->getAbsolutePath('/file.txt');
$result = $this->getAbsolutePath('/file.txt');
$this->assertEquals('/file.txt', $result);

$result = $this->Trait->getAbsolutePath('file.txt');
$result = $this->getAbsolutePath('file.txt');
$this->assertEquals(Configure::read(DATABASE_BACKUP . '.target') . DS . 'file.txt', $result);

$result = $this->Trait->getAbsolutePath(Configure::read(DATABASE_BACKUP . '.target') . DS . 'file.txt');
$result = $this->getAbsolutePath(Configure::read(DATABASE_BACKUP . '.target') . DS . 'file.txt');
$this->assertEquals(Configure::read(DATABASE_BACKUP . '.target') . DS . 'file.txt', $result);
}

Expand All @@ -87,7 +60,7 @@ public function testGetAbsolutePath()
*/
public function testGetBinary()
{
$this->assertEquals(which('mysql'), $this->Trait->getBinary('mysql'));
$this->assertEquals(which('mysql'), $this->getBinary('mysql'));
}

/**
Expand All @@ -100,7 +73,7 @@ public function testGetBinaryNotAvailable()
{
Configure::write(DATABASE_BACKUP . '.binaries.bzip2', false);

$this->Trait->getBinary('bzip2');
$this->getBinary('bzip2');
}

/**
Expand All @@ -109,8 +82,8 @@ public function testGetBinaryNotAvailable()
*/
public function testGetClassShortName()
{
$this->assertEquals('TestCase', $this->Trait->getClassShortName('\Cake\TestSuite\TestCase'));
$this->assertEquals('TestCase', $this->Trait->getClassShortName('Cake\TestSuite\TestCase'));
$this->assertEquals('TestCase', $this->getClassShortName('\Cake\TestSuite\TestCase'));
$this->assertEquals('TestCase', $this->getClassShortName('Cake\TestSuite\TestCase'));
}

/**
Expand All @@ -127,7 +100,7 @@ public function testGetCompression()
];

foreach ($compressions as $filename => $expectedCompression) {
$this->assertEquals($expectedCompression, $this->Trait->getCompression($filename));
$this->assertEquals($expectedCompression, $this->getCompression($filename));
}
}

Expand All @@ -144,7 +117,7 @@ public function testGetConnection()
Configure::read(DATABASE_BACKUP . '.connection'),
'fake',
] as $name) {
$connection = $this->Trait->getConnection($name);
$connection = $this->getConnection($name);
$this->assertInstanceof('Cake\Database\Connection', $connection);
$this->assertInstanceof('Cake\Database\Driver\Mysql', $connection->getDriver());
}
Expand All @@ -158,7 +131,7 @@ public function testGetConnection()
*/
public function testGetConnectionInvalidConnection()
{
$this->Trait->getConnection('noExisting');
$this->getConnection('noExisting');
}

/**
Expand All @@ -167,10 +140,10 @@ public function testGetConnectionInvalidConnection()
*/
public function testGetDriver()
{
$driver = $this->Trait->getDriver(ConnectionManager::get('test'));
$driver = $this->getDriver(ConnectionManager::get('test'));
$this->assertInstanceof(DATABASE_BACKUP . '\Driver\Mysql', $driver);

$driver = $this->Trait->getDriver();
$driver = $this->getDriver();
$this->assertInstanceof(DATABASE_BACKUP . '\Driver\Mysql', $driver);
}

Expand All @@ -188,7 +161,7 @@ public function testGetExtension()
];

foreach ($extensions as $filename => $expectedExtension) {
$this->assertEquals($expectedExtension, $this->Trait->getExtension($filename));
$this->assertEquals($expectedExtension, $this->getExtension($filename));
}
}

Expand All @@ -200,15 +173,15 @@ public function testGetExtension()
*/
public function testGetDriverNoExistingDriver()
{
$connection = $this->getMockBuilder(get_class($this->Trait->getConnection()))
$connection = $this->getMockBuilder(get_class($this->getConnection()))
->setMethods(['getDriver'])
->setConstructorArgs([$this->Trait->getConnection()->config()])
->setConstructorArgs([$this->getConnection()->config()])
->getMock();

$connection->method('getDriver')
->will($this->returnValue(new \stdClass()));

$this->Trait->getDriver($connection);
$this->getDriver($connection);
}

/**
Expand All @@ -217,6 +190,6 @@ public function testGetDriverNoExistingDriver()
*/
public function testGetTarget()
{
$this->assertEquals(Configure::read(DATABASE_BACKUP . '.target'), $this->Trait->getTarget());
$this->assertEquals(Configure::read(DATABASE_BACKUP . '.target'), $this->getTarget());
}
}
Loading

0 comments on commit d7e03ab

Please sign in to comment.