|
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\FolderBackupMaker; |
|
16
|
|
|
use floor12\backup\tests\TestCase; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
use yii\base\Exception; |
|
19
|
|
|
|
|
20
|
|
|
class FolderBackupMakerTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testCreateBackupFileExists() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->expectException(Exception::class); |
|
25
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/sqlite.db'); |
|
26
|
|
|
$creator = new FolderBackupMaker($backupFilePath, 'tmp'); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testCreateBackupTargetNotExists() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->expectException(Exception::class); |
|
32
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/backup.tgz'); |
|
33
|
|
|
$creator = new FolderBackupMaker($backupFilePath, 'not-exists'); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public function testCreateBackupSuccess() |
|
38
|
|
|
{ |
|
39
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/backup.tgz'); |
|
40
|
|
|
$targetFolder = Yii::getAlias('@app/unit'); |
|
41
|
|
|
$creator = new FolderBackupMaker($backupFilePath, $targetFolder); |
|
|
|
|
|
|
42
|
|
|
$this->assertTrue($creator->execute()); |
|
43
|
|
|
$this->fileExists($backupFilePath); |
|
|
|
|
|
|
44
|
|
|
unlink($backupFilePath); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testCreateBackupSuccessWithIoNice() |
|
48
|
|
|
{ |
|
49
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/backup.tgz'); |
|
50
|
|
|
$testFilePath = Yii::getAlias('@app/tmp/test'); |
|
51
|
|
|
$targetFolder = Yii::getAlias('@app/unit'); |
|
52
|
|
|
|
|
53
|
|
|
$this->module->ionice = "touch {$testFilePath} && "; |
|
54
|
|
|
$creator = new FolderBackupMaker($backupFilePath, $targetFolder); |
|
|
|
|
|
|
55
|
|
|
$this->assertTrue($creator->execute()); |
|
56
|
|
|
$this->fileExists($backupFilePath); |
|
|
|
|
|
|
57
|
|
|
$this->fileExists($testFilePath); |
|
58
|
|
|
unlink($backupFilePath); |
|
59
|
|
|
unlink($testFilePath); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testCreateBackupSuccessWithChmod() |
|
63
|
|
|
{ |
|
64
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/backup.tgz'); |
|
65
|
|
|
@unlink($backupFilePath); |
|
|
|
|
|
|
66
|
|
|
$targetFolder = Yii::getAlias('@app/unit'); |
|
67
|
|
|
$this->module->chmod = 0700; |
|
68
|
|
|
$creator = new FolderBackupMaker($backupFilePath, $targetFolder); |
|
|
|
|
|
|
69
|
|
|
$this->assertTrue($creator->execute()); |
|
70
|
|
|
$this->fileExists($backupFilePath); |
|
|
|
|
|
|
71
|
|
|
$this->assertEquals('0700', $this->readPerms($backupFilePath)); |
|
|
|
|
|
|
72
|
|
|
@unlink($backupFilePath); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function readPerms(string $file) |
|
76
|
|
|
{ |
|
77
|
|
|
return substr(sprintf('%o', fileperms($file)), -4); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
} |