Completed
Push — master ( 8b1e65...999e77 )
by Evgenii
05:00
created

BackupCreateTest::testSaveThreeBackups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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');
0 ignored issues
show
Unused Code introduced by
The assignment to $backupFilePath is dead and can be removed.
Loading history...
32
        $creator = new BackupCreate('main');
0 ignored issues
show
Unused Code introduced by
The assignment to $creator is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $creator is dead and can be removed.
Loading history...
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());
0 ignored issues
show
Security Best Practice introduced by
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 ignore-unhandled  annotation

56
        /** @scrutinizer ignore-unhandled */ @unlink($backup->getFullPath());

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.');
}
Loading history...
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());
0 ignored issues
show
Security Best Practice introduced by
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 ignore-unhandled  annotation

72
        /** @scrutinizer ignore-unhandled */ @unlink($backup->getFullPath());

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.');
}
Loading history...
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