floor12 /
yii2-module-backup
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Created by PhpStorm. |
||||||
| 4 | * User: floor12 |
||||||
| 5 | * Date: 11.11.2019 |
||||||
| 6 | * Time: 07:45 |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | namespace floor12\backup\tests\unit; |
||||||
| 10 | |||||||
| 11 | /** |
||||||
| 12 | * This is a tests for Backup class |
||||||
| 13 | */ |
||||||
| 14 | |||||||
| 15 | use floor12\backup\logic\DatabaseBackuper; |
||||||
| 16 | use floor12\backup\tests\TestCase; |
||||||
| 17 | use Yii; |
||||||
| 18 | use yii\base\Exception; |
||||||
| 19 | use yii\db\Connection; |
||||||
| 20 | |||||||
| 21 | class DatabaseBackuperTest extends TestCase |
||||||
| 22 | { |
||||||
| 23 | |||||||
| 24 | public function testBackupFileExists() |
||||||
| 25 | { |
||||||
| 26 | $this->expectException(Exception::class); |
||||||
| 27 | $backupFilePath = Yii::getAlias('@app/_output/sqlite.db'); // Just existing file |
||||||
| 28 | $connection = Yii::$app->mysql; |
||||||
| 29 | new DatabaseBackuper($backupFilePath, $connection); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
$backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 30 | $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
| 31 | $backuper->backup(); |
||||||
| 32 | } |
||||||
| 33 | |||||||
| 34 | // public function testCreateMysqlBackup() |
||||||
| 35 | // { |
||||||
| 36 | // $backupFilePath = Yii::getAlias('@app/data/mysql.tar'); |
||||||
| 37 | // $connection = Yii::$app->mysql; |
||||||
| 38 | // $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
| 39 | // $backuper->backup(); |
||||||
| 40 | // $this->assertFileExists($backupFilePath); |
||||||
| 41 | // } |
||||||
| 42 | |||||||
| 43 | public function testCreatePostgresBackup() |
||||||
| 44 | { |
||||||
| 45 | $backupFilePath = Yii::getAlias('@app/_output/postgres'); |
||||||
| 46 | $connection = Yii::$app->postgres; |
||||||
| 47 | $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
|
0 ignored issues
–
show
It seems like
$connection can also be of type null; however, parameter $connection of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept yii\db\Connection, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 48 | $backuper->backup(); |
||||||
| 49 | $this->assertFileExists($backupFilePath); |
||||||
|
0 ignored issues
–
show
It seems like
$backupFilePath can also be of type false; however, parameter $filename of PHPUnit\Framework\Assert::assertFileExists() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 50 | @unlink($backupFilePath); |
||||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
unlink(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
It seems like
$backupFilePath can also be of type false; however, parameter $filename of unlink() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 51 | } |
||||||
| 52 | |||||||
| 53 | public function testRestorePostgresBackupEmptyBase() |
||||||
| 54 | { |
||||||
| 55 | $backupFilePath = Yii::getAlias('@app/data/postgres'); |
||||||
| 56 | /** @var $connection Connection */ |
||||||
| 57 | $connection = Yii::$app->postgres; |
||||||
| 58 | $this->assertFalse($this->deleteTableSuccess('test_table', $connection)); |
||||||
| 59 | $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
|
0 ignored issues
–
show
It seems like
$backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 60 | $backuper->restore(); |
||||||
| 61 | $this->assertTrue($this->deleteTableSuccess('test_table', $connection)); |
||||||
| 62 | } |
||||||
| 63 | |||||||
| 64 | public function testRestorePostgresBackupNotEmptyBase() |
||||||
| 65 | { |
||||||
| 66 | $backupFilePath = Yii::getAlias('@app/data/postgres'); |
||||||
| 67 | /** @var $connection Connection */ |
||||||
| 68 | $connection = Yii::$app->postgres; |
||||||
| 69 | $connection->createCommand()->createTable('test_table', ['id' => 'int null'])->execute(); |
||||||
| 70 | $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
|
0 ignored issues
–
show
It seems like
$backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 71 | $backuper->restore(); |
||||||
| 72 | $this->assertTrue($this->deleteTableSuccess('test_table', $connection)); |
||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | public function testCreateMysqlBackup() |
||||||
| 76 | { |
||||||
| 77 | $backupFilePath = Yii::getAlias('@app/_output/mysql.sql.tgz'); |
||||||
| 78 | $connection = Yii::$app->mysql; |
||||||
| 79 | $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
|
0 ignored issues
–
show
It seems like
$connection can also be of type null; however, parameter $connection of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept yii\db\Connection, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 80 | $backuper->backup(); |
||||||
| 81 | $this->assertFileExists($backupFilePath); |
||||||
|
0 ignored issues
–
show
It seems like
$backupFilePath can also be of type false; however, parameter $filename of PHPUnit\Framework\Assert::assertFileExists() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 82 | @unlink($backupFilePath); |
||||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
unlink(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
It seems like
$backupFilePath can also be of type false; however, parameter $filename of unlink() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 83 | } |
||||||
| 84 | |||||||
| 85 | public function testRestoreMysqlBackupEmptyBase() |
||||||
| 86 | { |
||||||
| 87 | $backupFilePath = Yii::getAlias('@app/data/mysql.sql.tgz'); |
||||||
| 88 | /** @var $connection Connection */ |
||||||
| 89 | $connection = Yii::$app->mysql; |
||||||
| 90 | $this->assertFalse($this->deleteTableSuccess('test_table', $connection)); |
||||||
| 91 | $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
|
0 ignored issues
–
show
It seems like
$backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 92 | $backuper->restore(); |
||||||
| 93 | $this->assertTrue($this->deleteTableSuccess('test_table', $connection)); |
||||||
| 94 | } |
||||||
| 95 | |||||||
| 96 | public function testRestoreMysqlBackupNotEmptyBase() |
||||||
| 97 | { |
||||||
| 98 | $backupFilePath = Yii::getAlias('@app/data/mysql.sql.tgz'); |
||||||
| 99 | /** @var $connection Connection */ |
||||||
| 100 | $connection = Yii::$app->mysql; |
||||||
| 101 | $connection->createCommand()->createTable('test_table', ['id' => 'int null'])->execute(); |
||||||
| 102 | $backuper = new DatabaseBackuper($backupFilePath, $connection); |
||||||
|
0 ignored issues
–
show
It seems like
$backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Dat...Backuper::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 103 | $backuper->restore(); |
||||||
| 104 | $this->assertTrue($this->deleteTableSuccess('test_table', $connection)); |
||||||
| 105 | } |
||||||
| 106 | |||||||
| 107 | protected function deleteTableSuccess(string $tableName, $connection) |
||||||
| 108 | { |
||||||
| 109 | try { |
||||||
| 110 | $connection->createCommand()->dropTable($tableName)->execute(); |
||||||
| 111 | } catch (\yii\db\Exception $e) { |
||||||
| 112 | return false; |
||||||
| 113 | } |
||||||
| 114 | return true; |
||||||
| 115 | } |
||||||
| 116 | } |
||||||
| 117 |