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\Exceptions\ConfigurationNotFoundException; |
||||
16 | use floor12\backup\Exceptions\ModuleNotConfiguredException; |
||||
17 | use floor12\backup\logic\BackupCreate; |
||||
18 | use floor12\backup\models\Backup; |
||||
19 | use floor12\backup\models\BackupType; |
||||
20 | use floor12\backup\tests\TestCase; |
||||
21 | use Yii; |
||||
22 | |||||
23 | class BackupCreateTest extends TestCase |
||||
24 | { |
||||
25 | |||||
26 | public function testEmptyConfigs() |
||||
27 | { |
||||
28 | $this->expectException(ModuleNotConfiguredException::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'); |
||||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||
32 | $creator = new BackupCreate('main'); |
||||
0 ignored issues
–
show
|
|||||
33 | } |
||||
34 | |||||
35 | public function testWrongConfigName() |
||||
36 | { |
||||
37 | $this->expectException(ConfigurationNotFoundException::class); |
||||
38 | $config_id = 'wrong'; |
||||
39 | $creator = new BackupCreate($config_id); |
||||
0 ignored issues
–
show
|
|||||
40 | } |
||||
41 | |||||
42 | public function testDatabaseSuccess() |
||||
43 | { |
||||
44 | Backup::deleteAll(); |
||||
45 | $this->assertEquals(0, Backup::find()->count()); |
||||
46 | $config_id = 'postgres_db'; |
||||
47 | $creator = new BackupCreate($config_id); |
||||
48 | $creator->run(); |
||||
49 | $backup = Backup::find()->one(); |
||||
50 | $this->assertTrue(is_object($backup)); |
||||
51 | $this->assertEquals(BackupType::DB, $backup->type); |
||||
52 | $this->assertFileExists($backup->getFullPath()); |
||||
53 | @unlink($backup->getFullPath()); |
||||
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.');
}
![]() |
|||||
54 | } |
||||
55 | |||||
56 | public function testFolderSuccess() |
||||
57 | { |
||||
58 | Backup::deleteAll(); |
||||
59 | $this->assertEquals(0, Backup::find()->count()); |
||||
60 | $config_id = 'backup_test_folder'; |
||||
61 | $creator = new BackupCreate($config_id); |
||||
62 | $creator->run(); |
||||
63 | |||||
64 | $backup = Backup::find()->one(); |
||||
65 | $this->assertTrue(is_object($backup)); |
||||
66 | $this->assertEquals(BackupType::FILES, $backup->type); |
||||
67 | $this->assertFileExists($backup->getFullPath()); |
||||
68 | // @unlink($backup->getFullPath()); |
||||
69 | } |
||||
70 | |||||
71 | public function testSaveAllOldBackups() |
||||
72 | { |
||||
73 | Backup::deleteAll(); |
||||
74 | $this->assertEquals(0, Yii::$app->getModule('backup')->configs[1]['limit']); |
||||
75 | $this->assertEquals(0, Backup::find()->count()); |
||||
76 | $this->create5TestsBackups(); |
||||
77 | $this->assertEquals(5, Backup::find()->count()); |
||||
78 | } |
||||
79 | |||||
80 | public function testSaveOnlyOneBackup() |
||||
81 | { |
||||
82 | Backup::deleteAll(); |
||||
83 | Yii::$app->getModule('backup')->configs['backup_test_folder']['limit'] = 1; |
||||
84 | |||||
85 | $this->assertEquals(0, Backup::find()->count()); |
||||
86 | $this->create5TestsBackups(); |
||||
87 | $this->assertEquals(1, Backup::find()->count()); |
||||
88 | } |
||||
89 | |||||
90 | public function testSaveThreeBackups() |
||||
91 | { |
||||
92 | Backup::deleteAll(); |
||||
93 | Yii::$app->getModule('backup')->configs['backup_test_folder']['limit'] = 3; |
||||
94 | |||||
95 | $this->assertEquals(0, Backup::find()->count()); |
||||
96 | $this->create5TestsBackups(); |
||||
97 | $this->assertEquals(3, Backup::find()->count()); |
||||
98 | } |
||||
99 | |||||
100 | protected function create5TestsBackups() |
||||
101 | { |
||||
102 | $config_id = 'backup_test_folder'; |
||||
103 | $creator = new BackupCreate($config_id); |
||||
104 | for ($i = 0; $i < 5; $i++) |
||||
105 | $creator->run(); |
||||
106 | } |
||||
107 | |||||
108 | |||||
109 | } |
||||
110 |