Skip to content

Commit 91de40b

Browse files
committed
BackupTrait::getBinary() method has been moved to Driver abstract class
1 parent dbecf18 commit 91de40b

File tree

12 files changed

+80
-40
lines changed

12 files changed

+80
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 2.x branch
22
## 2.7 branch
33
### 2.7.0
4+
* `BackupTrait::getBinary()` method has been moved to `Driver` abstract class;
45
* `BackupTrait::getTarget()`, `BackupTrait::getDriverName()` and
56
`BackupTrait::getValidExtensions()` methods have been removed.
67

src/BackupTrait.php

Lines changed: 0 additions & 16 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
/**
@@ -46,21 +45,6 @@ public function getAbsolutePath($path)
4645
return $path;
4746
}
4847

49-
/**
50-
* Gets a binary path
51-
* @param string $name Binary name
52-
* @return string
53-
* @since 2.0.0
54-
* @throws \RuntimeException
55-
*/
56-
public function getBinary($name)
57-
{
58-
$binary = Configure::read('DatabaseBackup.binaries.' . $name);
59-
is_true_or_fail($binary, sprintf('Binary for `%s` could not be found. You have to set its path manually', $name), RuntimeException::class);
60-
61-
return $binary;
62-
}
63-
6448
/**
6549
* Returns the compression type from a filename
6650
* @param string $filename Filename

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function testExportExecutableWithCompression()
219219
$expected = sprintf(
220220
'%s | %s > %s%s',
221221
$basicExecutable,
222-
$this->getBinary($compression),
222+
$this->Driver->getBinary($compression),
223223
escapeshellarg($filename),
224224
REDIRECT_TO_DEV_NULL
225225
);
@@ -310,7 +310,7 @@ public function testImportExecutableWithCompression()
310310
$result = $this->invokeMethod($this->Driver, '_importExecutableWithCompression', [$filename]);
311311
$expected = sprintf(
312312
'%s -dc %s | %s%s',
313-
$this->getBinary($compression),
313+
$this->Driver->getBinary($compression),
314314
escapeshellarg($filename),
315315
$basicExecutable,
316316
REDIRECT_TO_DEV_NULL

tests/TestCase/BackupTraitTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@ public function testGetAbsolutePath()
5656
$this->assertEquals($expected, $this->getAbsolutePath(Configure::read('DatabaseBackup.target') . DS . 'file.txt'));
5757
}
5858

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-
7359
/**
7460
* Test for `getCompression()` method
7561
* @test
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* This file is part of cakephp-database-backup.
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Mirko Pagliai
11+
* @link https://github.com/mirko-pagliai/cakephp-database-backup
12+
* @license https://opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
15+
namespace DatabaseBackup\Test\TestCase\Driver;
16+
17+
use DatabaseBackup\Driver\Mysql;
18+
use DatabaseBackup\TestSuite\TestCase;
19+
use RuntimeException;
20+
21+
/**
22+
* DriverTest class
23+
*/
24+
class DriverTest extends TestCase
25+
{
26+
public function setUp()
27+
{
28+
parent::setUp();
29+
30+
$this->Driver = new Mysql($this->getConnection('test'));
31+
}
32+
33+
/**
34+
* Test for `getBinary()` method
35+
* @test
36+
*/
37+
public function testGetBinary()
38+
{
39+
$this->assertEquals(which('mysql'), $this->invokeMethod($this->Driver, 'getBinary', ['mysql']));
40+
41+
//With a binary not available
42+
$this->expectException(RuntimeException::class);
43+
$this->expectExceptionMessage('Binary for `noExisting` could not be found. You have to set its path manually');
44+
$this->invokeMethod($this->Driver, 'getBinary', ['noExisting']);
45+
}
46+
}

tests/TestCase/Driver/MysqlTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MysqlTest extends DriverTestCase
4646
*/
4747
public function testExportExecutable()
4848
{
49-
$expected = sprintf('%s --defaults-file=%s test', $this->getBinary('mysqldump'), escapeshellarg('authFile'));
49+
$expected = sprintf('%s --defaults-file=%s test', $this->Driver->getBinary('mysqldump'), escapeshellarg('authFile'));
5050
$this->setProperty($this->Driver, 'auth', 'authFile');
5151
$this->assertEquals($expected, $this->invokeMethod($this->Driver, '_exportExecutable'));
5252
}
@@ -67,7 +67,7 @@ public function testExportExecutableWithCompression()
6767
*/
6868
public function testImportExecutable()
6969
{
70-
$expected = sprintf('%s --defaults-extra-file=%s test', $this->getBinary('mysql'), escapeshellarg('authFile'));
70+
$expected = sprintf('%s --defaults-extra-file=%s test', $this->Driver->getBinary('mysql'), escapeshellarg('authFile'));
7171
$this->setProperty($this->Driver, 'auth', 'authFile');
7272
$this->assertEquals($expected, $this->invokeMethod($this->Driver, '_importExecutable'));
7373
}

0 commit comments

Comments
 (0)