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); |
|
|
|
|
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); |
|
|
|
|
48
|
|
|
$backuper->backup(); |
49
|
|
|
$this->assertFileExists($backupFilePath); |
|
|
|
|
50
|
|
|
@unlink($backupFilePath); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
80
|
|
|
$backuper->backup(); |
81
|
|
|
$this->assertFileExists($backupFilePath); |
|
|
|
|
82
|
|
|
@unlink($backupFilePath); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|