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\DatabaseBackupMaker; |
16
|
|
|
use floor12\backup\tests\MysqldumpMock; |
17
|
|
|
use floor12\backup\tests\TestCase; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\base\Exception; |
20
|
|
|
use yii\db\Connection; |
21
|
|
|
|
22
|
|
|
class DatabaseBackupMakerTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function testCreateBackupFileExists() |
25
|
|
|
{ |
26
|
|
|
$this->expectException(Exception::class); |
27
|
|
|
$connection = new Connection(['dsn' => 'sqlite:tests/tmp/app.db']); |
28
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/sqlite.db'); |
29
|
|
|
$dumper = MysqldumpMock::class; |
30
|
|
|
$creator = new DatabaseBackupMaker($backupFilePath, $connection, $dumper); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testCreateBackupSuccess() |
34
|
|
|
{ |
35
|
|
|
$connection = new Connection(['dsn' => 'sqlite:tests/tmp/app.db']); |
36
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/backup.tgz'); |
37
|
|
|
$dumper = MysqldumpMock::class; |
38
|
|
|
$creator = new DatabaseBackupMaker($backupFilePath, $connection, $dumper); |
|
|
|
|
39
|
|
|
$this->assertTrue($creator->execute()); |
40
|
|
|
$this->fileExists($backupFilePath); |
|
|
|
|
41
|
|
|
@unlink($backupFilePath); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testCreateBackupSWithChmod() |
45
|
|
|
{ |
46
|
|
|
$connection = new Connection(['dsn' => 'sqlite:tests/tmp/app.db']); |
47
|
|
|
$backupFilePath = Yii::getAlias('@app/tmp/backup.tgz'); |
48
|
|
|
$this->module->chmod = 0700; |
49
|
|
|
$dumper = MysqldumpMock::class; |
50
|
|
|
$creator = new DatabaseBackupMaker($backupFilePath, $connection, $dumper); |
|
|
|
|
51
|
|
|
$this->assertTrue($creator->execute()); |
52
|
|
|
$this->fileExists($backupFilePath); |
|
|
|
|
53
|
|
|
$this->assertEquals('0700', $this->readPerms($backupFilePath)); |
|
|
|
|
54
|
|
|
@unlink($backupFilePath); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function readPerms(string $file) |
58
|
|
|
{ |
59
|
|
|
return substr(sprintf('%o', fileperms($file)), -4); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
} |