|
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\BackupCreate; |
|
16
|
|
|
use floor12\backup\models\Backup; |
|
17
|
|
|
use floor12\backup\models\BackupType; |
|
18
|
|
|
use floor12\backup\tests\MysqldumpMock; |
|
19
|
|
|
use floor12\backup\tests\TestCase; |
|
20
|
|
|
use Yii; |
|
21
|
|
|
use yii\base\InvalidConfigException; |
|
22
|
|
|
|
|
23
|
|
|
class BackupCreateTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
public function testEmptyConfigs() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->expectException(InvalidConfigException::class); |
|
29
|
|
|
$this->module->configs = []; |
|
30
|
|
|
$this->expectExceptionMessage('Backup module need to be configured with `config array`'); |
|
31
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/sqlite.db'); |
|
|
|
|
|
|
32
|
|
|
$creator = new BackupCreate('main'); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testWrongConfigName() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->expectException(InvalidConfigException::class); |
|
38
|
|
|
$config_id = 'wrong'; |
|
39
|
|
|
$this->expectExceptionMessage("Config `{$config_id}` not found."); |
|
40
|
|
|
$creator = new BackupCreate($config_id); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testDatabaseSuccess() |
|
44
|
|
|
{ |
|
45
|
|
|
Backup::deleteAll(); |
|
46
|
|
|
$this->assertEquals(0, Backup::find()->count()); |
|
47
|
|
|
$config_id = 'main_db'; |
|
48
|
|
|
$dumperClass = MysqldumpMock::class; |
|
49
|
|
|
$creator = new BackupCreate($config_id, $dumperClass); |
|
50
|
|
|
$creator->run(); |
|
51
|
|
|
|
|
52
|
|
|
$backup = Backup::find()->one(); |
|
53
|
|
|
$this->assertTrue(is_object($backup)); |
|
54
|
|
|
$this->assertEquals(BackupType::DB, $backup->type); |
|
55
|
|
|
$this->assertFileExists($backup->getFullPath()); |
|
56
|
|
|
@unlink($backup->getFullPath()); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testFolderSuccess() |
|
60
|
|
|
{ |
|
61
|
|
|
Backup::deleteAll(); |
|
62
|
|
|
$this->assertEquals(0, Backup::find()->count()); |
|
63
|
|
|
$config_id = 'tmp_folder'; |
|
64
|
|
|
$dumperClass = MysqldumpMock::class; |
|
65
|
|
|
$creator = new BackupCreate($config_id, $dumperClass); |
|
66
|
|
|
$creator->run(); |
|
67
|
|
|
|
|
68
|
|
|
$backup = Backup::find()->one(); |
|
69
|
|
|
$this->assertTrue(is_object($backup)); |
|
70
|
|
|
$this->assertEquals(BackupType::FILES, $backup->type); |
|
71
|
|
|
$this->assertFileExists($backup->getFullPath()); |
|
72
|
|
|
@unlink($backup->getFullPath()); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testSaveAllOldBackups() |
|
76
|
|
|
{ |
|
77
|
|
|
Backup::deleteAll(); |
|
78
|
|
|
$this->assertEquals(0, Yii::$app->getModule('backup')->configs[1]['limit']); |
|
79
|
|
|
$this->assertEquals(0, Backup::find()->count()); |
|
80
|
|
|
$this->create5TestsBackups(); |
|
81
|
|
|
$this->assertEquals(5, Backup::find()->count()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testSaveOnlyOneBackup() |
|
85
|
|
|
{ |
|
86
|
|
|
Backup::deleteAll(); |
|
87
|
|
|
Yii::$app->getModule('backup')->configs[1]['limit'] = 1; |
|
88
|
|
|
|
|
89
|
|
|
$this->assertEquals(0, Backup::find()->count()); |
|
90
|
|
|
$this->create5TestsBackups(); |
|
91
|
|
|
$this->assertEquals(1, Backup::find()->count()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testSaveThreeBackups() |
|
95
|
|
|
{ |
|
96
|
|
|
Backup::deleteAll(); |
|
97
|
|
|
Yii::$app->getModule('backup')->configs[1]['limit'] = 3; |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals(0, Backup::find()->count()); |
|
100
|
|
|
$this->create5TestsBackups(); |
|
101
|
|
|
$this->assertEquals(3, Backup::find()->count()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function create5TestsBackups() |
|
105
|
|
|
{ |
|
106
|
|
|
$config_id = 'tmp_folder'; |
|
107
|
|
|
$creator = new BackupCreate($config_id); |
|
108
|
|
|
for ($i = 0; $i < 5; $i++) |
|
109
|
|
|
$creator->run(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|