Skip to content

Commit fb65b20

Browse files
Merge pull request #51 from mirko-pagliai/develop
Develop
2 parents eb471fe + cb97dd7 commit fb65b20

File tree

16 files changed

+67
-105
lines changed

16 files changed

+67
-105
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# 2.x branch
2+
## 2.7 branch
3+
### 2.7.0
4+
* `BackupTrait::getBinary()` method has been moved to `Driver` abstract class;
5+
* `BackupTrait::getTarget()`, `BackupTrait::getDriverName()` and
6+
`BackupTrait::getValidExtensions()` methods have been removed.
7+
28
## 2.6 branch
39
### 2.6.6
410
* tests have been optimized and speeded up;

src/BackupTrait.php

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Cake\Datasource\ConnectionInterface;
1818
use Cake\Datasource\ConnectionManager;
1919
use InvalidArgumentException;
20-
use RuntimeException;
2120
use Symfony\Component\Filesystem\Filesystem;
2221

2322
/**
@@ -30,32 +29,20 @@ trait BackupTrait
3029
* @since 2.4.0
3130
* @var array
3231
*/
33-
private static $validExtensions = ['sql.bz2' => 'bzip2', 'sql.gz' => 'gzip', 'sql' => false];
32+
protected static $validExtensions = ['sql.bz2' => 'bzip2', 'sql.gz' => 'gzip', 'sql' => false];
3433

3534
/**
3635
* Returns an absolute path
3736
* @param string $path Relative or absolute path
3837
* @return string
39-
* @uses getTarget()
4038
*/
4139
public function getAbsolutePath($path)
4240
{
43-
return (new Filesystem())->isAbsolutePath($path) ? $path : add_slash_term($this->getTarget()) . $path;
44-
}
45-
46-
/**
47-
* Gets a binary path
48-
* @param string $name Binary name
49-
* @return string
50-
* @since 2.0.0
51-
* @throws \RuntimeException
52-
*/
53-
public function getBinary($name)
54-
{
55-
$binary = Configure::read('DatabaseBackup.binaries.' . $name);
56-
is_true_or_fail($binary, __d('database_backup', 'Binary for `{0}` could not be found. You have to set its path manually', $name), RuntimeException::class);
41+
if (!(new Filesystem())->isAbsolutePath($path)) {
42+
return add_slash_term(Configure::read('DatabaseBackup.target')) . $path;
43+
}
5744

58-
return $binary;
45+
return $path;
5946
}
6047

6148
/**
@@ -92,12 +79,11 @@ public function getConnection($name = null)
9279
* @since 2.0.0
9380
* @throws \InvalidArgumentException
9481
* @uses getConnection()
95-
* @uses getDriverName()
9682
*/
9783
public function getDriver(ConnectionInterface $connection = null)
9884
{
9985
$connection = $connection ?: $this->getConnection();
100-
$className = $this->getDriverName($connection);
86+
$className = get_class_short_name($connection->getDriver());
10187
$driver = App::classname(sprintf('%s.%s', 'DatabaseBackup', $className), 'Driver');
10288
is_true_or_fail(
10389
$driver,
@@ -108,41 +94,18 @@ public function getDriver(ConnectionInterface $connection = null)
10894
return new $driver($connection);
10995
}
11096

111-
/**
112-
* Gets the driver name, according to the database engine
113-
* @param \Cake\Datasource\ConnectionInterface|null $connection A connection object
114-
* @return string The driver name
115-
* @since 2.6.2
116-
* @uses getConnection()
117-
*/
118-
public function getDriverName(ConnectionInterface $connection = null)
119-
{
120-
$connection = $connection ?: $this->getConnection();
121-
122-
return get_class_short_name($connection->getDriver());
123-
}
124-
12597
/**
12698
* Returns the extension from a filename
12799
* @param string $filename Filename
128100
* @return string|null Extension or `null` if the extension is not found or
129101
* if is an invalid extension
130-
* @uses getValidExtensions()
102+
* @uses $validExtensions
131103
*/
132104
public function getExtension($filename)
133105
{
134106
$extension = get_extension($filename);
135107

136-
return in_array($extension, $this->getValidExtensions()) ? $extension : null;
137-
}
138-
139-
/**
140-
* Returns the target path
141-
* @return string
142-
*/
143-
public function getTarget()
144-
{
145-
return Configure::read('DatabaseBackup.target');
108+
return in_array($extension, array_keys(self::$validExtensions)) ? $extension : null;
146109
}
147110

148111
/**
@@ -155,15 +118,4 @@ public function getValidCompressions()
155118
{
156119
return array_filter(self::$validExtensions);
157120
}
158-
159-
/**
160-
* Returns all valid extensions
161-
* @return array
162-
* @since 2.4.0
163-
* @uses $validExtensions
164-
*/
165-
public function getValidExtensions()
166-
{
167-
return array_keys(self::$validExtensions);
168-
}
169121
}

src/Console/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function execute(Arguments $args, ConsoleIo $io)
3636
$config = $this->getConnection()->config();
3737

3838
$io->out(__d('database_backup', 'Connection: {0}', $config['name']));
39-
$io->out(__d('database_backup', 'Driver: {0}', $this->getDriverName($this->getConnection())));
39+
$io->out(__d('database_backup', 'Driver: {0}', get_class_short_name($this->getConnection()->getDriver())));
4040
$io->hr();
4141

4242
return null;

src/Driver/Driver.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Cake\Event\EventDispatcherTrait;
1818
use Cake\Event\EventListenerInterface;
1919
use DatabaseBackup\BackupTrait;
20+
use RuntimeException;
2021

2122
/**
2223
* Represents a driver containing all methods to export/import database backups
@@ -120,6 +121,7 @@ public function beforeImport()
120121
* @param string $filename Filename where you want to export the database
121122
* @return string
122123
* @uses _exportExecutable()
124+
* @uses getBinary()
123125
*/
124126
protected function _exportExecutableWithCompression($filename)
125127
{
@@ -143,6 +145,7 @@ protected function _exportExecutableWithCompression($filename)
143145
* @param string $filename Filename from which you want to import the database
144146
* @return string
145147
* @uses _importExecutable()
148+
* @uses getBinary()
146149
*/
147150
protected function _importExecutableWithCompression($filename)
148151
{
@@ -189,6 +192,20 @@ final public function export($filename)
189192
return file_exists($filename);
190193
}
191194

195+
/**
196+
* Gets a binary path
197+
* @param string $name Binary name
198+
* @return string
199+
* @throws \RuntimeException
200+
*/
201+
public function getBinary($name)
202+
{
203+
$binary = Configure::read('DatabaseBackup.binaries.' . $name);
204+
is_true_or_fail($binary, sprintf('Binary for `%s` could not be found. You have to set its path manually', $name), RuntimeException::class);
205+
206+
return $binary;
207+
}
208+
192209
/**
193210
* Gets a config value or the whole configuration
194211
* @param string|null $key Config key or `null` to get all config values

src/Driver/Mysql.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Mysql extends Driver
3030
/**
3131
* Gets the executable command to export the database
3232
* @return string
33+
* @uses getBinary()
3334
* @uses getConfig()
3435
* @uses $auth
3536
*/
@@ -46,6 +47,7 @@ protected function _exportExecutable()
4647
/**
4748
* Gets the executable command to import the database
4849
* @return string
50+
* @uses getBinary()
4951
* @uses getConfig()
5052
* @uses $auth
5153
*/

src/Driver/Postgres.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function getDbnameAsString()
4646
/**
4747
* Gets the executable command to export the database
4848
* @return string
49+
* @uses getBinary()
4950
* @uses getDbnameAsString()
5051
*/
5152
protected function _exportExecutable()
@@ -56,6 +57,7 @@ protected function _exportExecutable()
5657
/**
5758
* Gets the executable command to import the database
5859
* @return string
60+
* @uses getBinary()
5961
* @uses getDbnameAsString()
6062
*/
6163
protected function _importExecutable()

src/Driver/Sqlite.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Sqlite extends Driver
2323
/**
2424
* Gets the executable command to export the database
2525
* @return string
26+
* @uses getBinary()
2627
* @uses getConfig()
2728
*/
2829
protected function _exportExecutable()
@@ -33,6 +34,7 @@ protected function _exportExecutable()
3334
/**
3435
* Gets the executable command to import the database
3536
* @return string
37+
* @uses getBinary()
3638
* @uses getConfig()
3739
*/
3840
protected function _importExecutable()

src/TestSuite/DriverTestCase.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ public function testExport()
121121
*/
122122
public function testExportAndImport()
123123
{
124-
foreach ($this->getValidExtensions() as $extension) {
124+
foreach (self::$validExtensions as $extension) {
125125
$this->loadFixtures();
126-
$backup = $this->getAbsolutePath(sprintf('example.%s', $extension));
126+
$backup = uniqid('example_');
127+
$backup = $this->getAbsolutePath($extension ? $backup . '.' . $extension : $backup);
127128

128129
//Initial records. 3 articles and 6 comments
129130
$initial = $this->getAllRecords();
@@ -192,7 +193,7 @@ public function testExportExecutableWithCompression()
192193
$expected = sprintf(
193194
'%s | %s > %s%s',
194195
$basicExecutable,
195-
$this->getBinary($compression),
196+
$this->Driver->getBinary($compression),
196197
escapeshellarg($filename),
197198
REDIRECT_TO_DEV_NULL
198199
);
@@ -239,7 +240,7 @@ public function testImportExecutableWithCompression()
239240
$result = $this->invokeMethod($this->Driver, '_importExecutableWithCompression', [$filename]);
240241
$expected = sprintf(
241242
'%s -dc %s | %s%s',
242-
$this->getBinary($compression),
243+
$this->Driver->getBinary($compression),
243244
escapeshellarg($filename),
244245
$basicExecutable,
245246
REDIRECT_TO_DEV_NULL

src/Utility/BackupManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function deleteAll()
6868
*/
6969
public function index()
7070
{
71-
$finder = (new Finder())->files()->name('/\.sql(\.(gz|bz2))?$/')->in($this->getTarget());
71+
$finder = (new Finder())->files()->name('/\.sql(\.(gz|bz2))?$/')->in(Configure::read('DatabaseBackup.target'));
7272

7373
return collection($finder)->map(function (SplFileInfo $file) {
7474
return new Entity([

tests/TestCase/BackupTraitTest.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use DatabaseBackup\Driver\Mysql;
2222
use DatabaseBackup\TestSuite\TestCase;
2323
use InvalidArgumentException;
24-
use RuntimeException;
2524

2625
/**
2726
* BackupTraitTest class
@@ -56,20 +55,6 @@ public function testGetAbsolutePath()
5655
$this->assertEquals($expected, $this->getAbsolutePath(Configure::read('DatabaseBackup.target') . DS . 'file.txt'));
5756
}
5857

59-
/**
60-
* Test for `getBinary()` method
61-
* @test
62-
*/
63-
public function testGetBinary()
64-
{
65-
$this->assertEquals(which('mysql'), $this->getBinary('mysql'));
66-
67-
//With a binary not available
68-
$this->expectException(RuntimeException::class);
69-
$this->expectExceptionMessage('Binary for `noExisting` could not be found. You have to set its path manually');
70-
$this->getBinary('noExisting');
71-
}
72-
7358
/**
7459
* Test for `getCompression()` method
7560
* @test
@@ -131,17 +116,6 @@ public function testGetDriver()
131116
$this->getDriver($connection);
132117
}
133118

134-
/**
135-
* Test for `getDriverName()` method
136-
* @test
137-
*/
138-
public function testGetDriverName()
139-
{
140-
foreach ([ConnectionManager::get('test'), null] as $driver) {
141-
$this->assertEquals('Mysql', $this->getDriverName($driver));
142-
}
143-
}
144-
145119
/**
146120
* Test for `getExtension()` method
147121
* @test
@@ -171,13 +145,4 @@ public function testGetValidCompressions()
171145
{
172146
$this->assertNotEmpty($this->getValidCompressions());
173147
}
174-
175-
/**
176-
* Test for `getValidExtensions()` method
177-
* @test
178-
*/
179-
public function testGetValidExtensions()
180-
{
181-
$this->assertNotEmpty($this->getValidExtensions());
182-
}
183148
}

0 commit comments

Comments
 (0)