|
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 ErrorException; |
|
16
|
|
|
use floor12\backup\logic\BackupRestore; |
|
17
|
|
|
use floor12\backup\models\Backup; |
|
18
|
|
|
use floor12\backup\models\BackupStatus; |
|
19
|
|
|
use floor12\backup\models\BackupType; |
|
20
|
|
|
use floor12\backup\tests\TestCase; |
|
21
|
|
|
use Yii; |
|
22
|
|
|
use yii\base\InvalidConfigException; |
|
23
|
|
|
|
|
24
|
|
|
class BackupRestoreTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
public function testEmptyConfigs() |
|
28
|
|
|
{ |
|
29
|
|
|
$backup = new Backup([ |
|
30
|
|
|
'status' => BackupStatus::DONE, |
|
31
|
|
|
'filename' => 'test.gz', |
|
32
|
|
|
'type' => BackupType::FILES, |
|
33
|
|
|
'config_id' => 'tmp_folder' |
|
34
|
|
|
]); |
|
35
|
|
|
$this->expectException(InvalidConfigException::class); |
|
36
|
|
|
$this->module->configs = []; |
|
37
|
|
|
$this->expectExceptionMessage('Backup module need to be configured with `config array`'); |
|
38
|
|
|
$restorer = new BackupRestore($backup); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testWrongConfigName() |
|
42
|
|
|
{ |
|
43
|
|
|
$config_id = 'wrong_config_id'; |
|
44
|
|
|
$backup = new Backup([ |
|
45
|
|
|
'status' => BackupStatus::DONE, |
|
46
|
|
|
'filename' => 'test.gz', |
|
47
|
|
|
'type' => BackupType::FILES, |
|
48
|
|
|
'config_id' => $config_id |
|
49
|
|
|
]); |
|
50
|
|
|
$this->expectException(ErrorException::class); |
|
51
|
|
|
$this->expectExceptionMessage("Config `{$config_id}` not found."); |
|
52
|
|
|
$restorer = new BackupRestore($backup); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testDatabaseSuccess() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->module->backupFolder = '@vendor/../tests/data'; |
|
58
|
|
|
|
|
59
|
|
|
$backup = new Backup([ |
|
60
|
|
|
'status' => BackupStatus::DONE, |
|
61
|
|
|
'filename' => 'test.zip', |
|
62
|
|
|
'type' => BackupType::DB, |
|
63
|
|
|
'config_id' => 'main_db' |
|
64
|
|
|
]); |
|
65
|
|
|
$restorer = new BackupRestore($backup); |
|
66
|
|
|
$restorer->run(); |
|
67
|
|
|
|
|
68
|
|
|
$databaseName = Yii::$app->db->databaseName; |
|
69
|
|
|
|
|
70
|
|
|
$commandsToCheck = [ |
|
71
|
|
|
'SELECT DATABASE()', |
|
72
|
|
|
"DROP DATABASE `{$databaseName}`", |
|
73
|
|
|
"CREATE DATABASE `{$databaseName}`", |
|
74
|
|
|
"USE `{$databaseName}`", |
|
75
|
|
|
"/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;", |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($commandsToCheck as $command) { |
|
79
|
|
|
$this->assertTrue(in_array($command, Yii::$app->db->sql)); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testFolderSuccess() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->module->backupFolder = '@vendor/../tests/data'; |
|
86
|
|
|
$resultFilePath = Yii::getAlias('@app/tmp/test.txt'); |
|
87
|
|
|
@unlink($resultFilePath); |
|
|
|
|
|
|
88
|
|
|
$this->assertFileNotExists($resultFilePath); |
|
|
|
|
|
|
89
|
|
|
$backup = new Backup([ |
|
90
|
|
|
'status' => BackupStatus::DONE, |
|
91
|
|
|
'filename' => 'test.tgz', |
|
92
|
|
|
'type' => BackupType::FILES, |
|
93
|
|
|
'config_id' => 'tmp_folder' |
|
94
|
|
|
]); |
|
95
|
|
|
$restorer = new BackupRestore($backup); |
|
96
|
|
|
$restorer->run(); |
|
97
|
|
|
$this->assertFileExists($resultFilePath); |
|
|
|
|
|
|
98
|
|
|
@unlink($resultFilePath); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
} |