Skip to content

Commit 08e90b5

Browse files
Develop (#43)
* improved exception message when no binary file is found * no longer uses the `Folder` class * updated * fixed * Develop dependencies (#42) added tests for lower dependencies * updated * come back * fixed * fixed * updated * little fixes for `BackupManager` and `BackupExport` classes * test * fixed typo * little fixes * fixed
1 parent 8a256e7 commit 08e90b5

File tree

12 files changed

+72
-75
lines changed

12 files changed

+72
-75
lines changed

.travis.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ matrix:
1111
fast_finish: true
1212

1313
include:
14+
- php: 5.6
15+
env: dependencies=lowest
16+
- php: 7.0
17+
env: dependencies=lowest
18+
- php: 7.1
19+
env: dependencies=lowest
20+
- php: 7.2
21+
env: dependencies=lowest
22+
- php: 7.3
23+
env: dependencies=lowest
1424
- php: 7.0
1525
env: PHPCS=1
16-
1726
- php: 7.0
1827
env: COVERAGE=1
1928

20-
- php: 7.0
21-
env: CAKEPHP_VERSION="3.7.*"
22-
2329
services:
2430
- mysql
2531
- postgresql
@@ -28,9 +34,10 @@ addons:
2834
postgresql: "9.4"
2935

3036
install:
31-
- composer self-update
37+
- composer self-update -q
3238
- composer install --prefer-dist --no-interaction
33-
- if [[ ! -z "$CAKEPHP_VERSION" ]]; then composer require --update-with-dependencies cakephp/cakephp:${CAKEPHP_VERSION}; fi
39+
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest -n; fi;
40+
- composer show -i
3441

3542
before_script:
3643
- mysql -e 'create database test;'

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# 2.x branch
22
## 2.6 branch
3+
### 2.6.4
4+
* little fixes for `BackupManager` and `BackupExport` classes;
5+
* added tests for lower dependencies;
6+
* improved exception message when no binary file is found;
7+
* no longer uses the `Folder` class.
8+
39
### 2.6.3
410
* little fixes.
511

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ before_test:
2727

2828
install:
2929
- cd c:\
30-
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-7.2.20-nts-Win32-VC15-x86.zip
30+
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-7.2.21-nts-Win32-VC15-x86.zip
3131
- 7z x php.zip -oc:\php > nul
3232
- cd c:\php
3333
- copy php.ini-production php.ini

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
"require": {
1212
"php": ">=5.5.9",
1313
"cakephp/cakephp": "^3.7",
14-
"mirko-pagliai/php-tools": "^1.2"
14+
"mirko-pagliai/php-tools": "^1.2.8"
1515
},
1616
"require-dev": {
1717
"cakephp/cakephp-codesniffer": "^3.0",
18-
"mirko-pagliai/me-tools": "^2.18.7",
19-
"phpunit/phpunit": "^5.7|^6.0"
18+
"mirko-pagliai/me-tools": "^2.18.11",
19+
"phpunit/phpunit": "^5.7.14|^6.0"
2020
},
2121
"autoload": {
2222
"psr-4": {

src/BackupTrait.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Cake\Core\Configure;
1717
use Cake\Datasource\ConnectionInterface;
1818
use Cake\Datasource\ConnectionManager;
19-
use Cake\Filesystem\Folder;
2019
use InvalidArgumentException;
20+
use RuntimeException;
2121

2222
/**
2323
* A trait that provides some methods used by all other classes
@@ -39,18 +39,22 @@ trait BackupTrait
3939
*/
4040
public function getAbsolutePath($path)
4141
{
42-
return Folder::isAbsolute($path) ? $path : $this->getTarget() . DS . $path;
42+
return is_absolute($path) ? $path : $this->getTarget() . DS . $path;
4343
}
4444

4545
/**
4646
* Gets a binary path
4747
* @param string $name Binary name
4848
* @return string
4949
* @since 2.0.0
50+
* @throws \RuntimeException
5051
*/
5152
public function getBinary($name)
5253
{
53-
return Configure::readOrFail('DatabaseBackup.binaries.' . $name);
54+
$binary = Configure::read('DatabaseBackup.binaries.' . $name);
55+
is_true_or_fail($binary, sprintf('Binary for `%s` could not be found. You have to set its path manually', $name), RuntimeException::class);
56+
57+
return $binary;
5458
}
5559

5660
/**
@@ -144,7 +148,7 @@ public function getTarget()
144148
* Returns all valid compressions
145149
* @return array
146150
* @since 2.4.0
147-
* @uses $$validExtensions
151+
* @uses $validExtensions
148152
*/
149153
public function getValidCompressions()
150154
{

src/Plugin.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ class Plugin extends BasePlugin
3333
*/
3434
public function console($commands)
3535
{
36-
$commands->add('database_backup.delete_all', DeleteAllCommand::class);
37-
$commands->add('database_backup.export', ExportCommand::class);
38-
$commands->add('database_backup.import', ImportCommand::class);
39-
$commands->add('database_backup.index', IndexCommand::class);
40-
$commands->add('database_backup.rotate', RotateCommand::class);
41-
$commands->add('database_backup.send', SendCommand::class);
42-
43-
return $commands;
36+
return $commands->add('database_backup.delete_all', DeleteAllCommand::class)
37+
->add('database_backup.export', ExportCommand::class)
38+
->add('database_backup.import', ImportCommand::class)
39+
->add('database_backup.index', IndexCommand::class)
40+
->add('database_backup.rotate', RotateCommand::class)
41+
->add('database_backup.send', SendCommand::class);
4442
}
4543
}

src/TestSuite/DriverTestCase.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Cake\Core\Configure;
1717
use Cake\Database\Connection;
1818
use Cake\Event\EventList;
19-
use Cake\ORM\TableRegistry;
2019
use DatabaseBackup\TestSuite\TestCase;
2120
use ErrorException;
2221

@@ -38,13 +37,13 @@ abstract class DriverTestCase extends TestCase
3837
protected $Comments;
3938

4039
/**
41-
* @var object
40+
* @var \DatabaseBackup\Driver\Driver
4241
*/
4342
protected $Driver;
4443

4544
/**
4645
* @since 2.5.1
47-
* @var object
46+
* @var string
4847
*/
4948
protected $DriverClass;
5049

@@ -75,12 +74,11 @@ public function setUp()
7574

7675
Configure::write('DatabaseBackup.connection', $this->connection);
7776
$connection = $this->getConnection();
78-
TableRegistry::clear();
79-
$this->Articles = TableRegistry::get('Articles', compact('connection'));
80-
$this->Comments = TableRegistry::get('Comments', compact('connection'));
77+
$this->Articles = $this->getTable('Articles', compact('connection'));
78+
$this->Comments = $this->getTable('Comments', compact('connection'));
8179

8280
//Enable event tracking
83-
$this->Driver = new $this->DriverClass($this->getConnection());
81+
$this->Driver = new $this->DriverClass($connection);
8482
$this->Driver->getEventManager()->setEventList(new EventList());
8583
}
8684

@@ -324,9 +322,10 @@ public function testImportExecutableWithCompression()
324322
*/
325323
public function testImportOnFailure()
326324
{
325+
$backup = $this->getAbsolutePath('example.sql');
326+
327327
$this->expectException(ErrorException::class);
328328
$this->expectExceptionMessageRegExp('/^Failed with exit code `\d`$/');
329-
$backup = $this->getAbsolutePath('example.sql');
330329
$this->Driver->export($backup);
331330

332331
//Sets a no existing database

src/TestSuite/TestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public function setUp()
4747
*/
4848
public function tearDown()
4949
{
50-
parent::tearDown();
51-
5250
//Deletes all backup files
53-
@unlink_recursive(Configure::read('DatabaseBackup.target'));
51+
unlink_recursive(Configure::read('DatabaseBackup.target'));
52+
53+
parent::tearDown();
5454
}
5555

5656
/**

src/Utility/BackupExport.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\Core\Configure;
1717
use DatabaseBackup\BackupTrait;
1818
use InvalidArgumentException;
19+
use Symfony\Component\Filesystem\Filesystem;
1920

2021
/**
2122
* Utility to export databases
@@ -211,32 +212,19 @@ public function send($recipient = null)
211212
public function export()
212213
{
213214
if (empty($this->filename)) {
214-
if (empty($this->extension)) {
215-
$this->extension = $this->defaultExtension;
216-
}
217-
215+
$this->extension = $this->extension ?: $this->defaultExtension;
218216
$this->filename(sprintf('backup_{$DATABASE}_{$DATETIME}.%s', $this->extension));
219217
}
220218

221-
//This allows the filename to be set again with a next call of this
222-
// method
219+
//This allows the filename to be set again with a next call of this method
223220
$filename = $this->filename;
224221
unset($this->filename);
225222

226223
$this->driver->export($filename);
224+
(new Filesystem())->chmod($filename, Configure::read('DatabaseBackup.chmod'));
227225

228-
if (!IS_WIN) {
229-
chmod($filename, Configure::read('DatabaseBackup.chmod'));
230-
}
231-
232-
if ($this->emailRecipient) {
233-
$this->BackupManager->send($filename, $this->emailRecipient);
234-
}
235-
236-
//Rotates backups
237-
if ($this->rotate) {
238-
$this->BackupManager->rotate($this->rotate);
239-
}
226+
$this->emailRecipient ? $this->BackupManager->send($filename, $this->emailRecipient) : null;
227+
$this->rotate ? $this->BackupManager->rotate($this->rotate) : null;
240228

241229
return $filename;
242230
}

src/Utility/BackupManager.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
namespace DatabaseBackup\Utility;
1515

1616
use Cake\Core\Configure;
17-
use Cake\Filesystem\Folder;
1817
use Cake\I18n\FrozenTime;
1918
use Cake\Mailer\Email;
2019
use Cake\ORM\Entity;
2120
use DatabaseBackup\BackupTrait;
2221
use InvalidArgumentException;
22+
use Symfony\Component\Finder\Finder;
23+
use Symfony\Component\Finder\SplFileInfo;
2324

2425
/**
2526
* Utility to manage database backups
@@ -54,9 +55,9 @@ public function delete($filename)
5455
*/
5556
public function deleteAll()
5657
{
57-
return array_filter(array_map(function ($file) {
58-
return !$this->delete($file->filename) ?: $file->filename;
59-
}, $this->index()->toList()));
58+
return array_filter(array_map(function ($filename) {
59+
return !$this->delete($filename) ?: $filename;
60+
}, $this->index()->extract('filename')->toList()));
6061
}
6162

6263
/**
@@ -67,19 +68,17 @@ public function deleteAll()
6768
*/
6869
public function index()
6970
{
70-
$target = $this->getTarget();
71+
$finder = (new Finder())->files()->name('/\.sql(\.(gz|bz2))?$/')->in($this->getTarget());
7172

72-
return collection((new Folder($target))->find('.+\.sql(\.(gz|bz2))?'))
73-
->map(function ($filename) use ($target) {
74-
return new Entity([
75-
'filename' => $filename,
76-
'extension' => $this->getExtension($filename),
77-
'compression' => $this->getCompression($filename),
78-
'size' => filesize($target . DS . $filename),
79-
'datetime' => new FrozenTime(date('Y-m-d H:i:s', filemtime($target . DS . $filename))),
80-
]);
81-
})
82-
->sortBy('datetime');
73+
return collection($finder)->map(function (SplFileInfo $file) {
74+
return new Entity([
75+
'filename' => $file->getFilename(),
76+
'extension' => $this->getExtension($file->getFilename()),
77+
'compression' => $this->getCompression($file->getFilename()),
78+
'size' => $file->getSize(),
79+
'datetime' => FrozenTime::createFromTimestamp($file->getMTime()),
80+
]);
81+
})->sortBy('datetime');
8382
}
8483

8584
/**
@@ -102,13 +101,9 @@ public function rotate($rotate)
102101
InvalidArgumentException::class
103102
);
104103
$backupsToBeDeleted = $this->index()->skip((int)$rotate);
104+
array_map([$this, 'delete'], $backupsToBeDeleted->extract('filename')->toList());
105105

106-
//Deletes
107-
foreach ($backupsToBeDeleted as $backup) {
108-
$this->delete($backup->filename);
109-
}
110-
111-
return $backupsToBeDeleted->toArray();
106+
return $backupsToBeDeleted->toList();
112107
}
113108

114109
/**

0 commit comments

Comments
 (0)