Completed
Push — master ( 550d29...83ed84 )
by Evgenii
07:27 queued 04:58
created

BackupRestoreTest::testEmptyConfigs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
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 ErrorException;
16
use floor12\backup\logic\BackupRestore;
17
use floor12\backup\models\Backup;
18
use floor12\backup\models\BackupStatus;
19
use floor12\backup\models\BackupType;
20
use floor12\backup\tests\TestCase;
21
use Yii;
22
use yii\base\InvalidConfigException;
23
24
class BackupRestoreTest extends TestCase
25
{
26
27
    public function testEmptyConfigs()
28
    {
29
        $backup = new Backup([
30
            'status' => BackupStatus::DONE,
31
            'filename' => 'test.gz',
32
            'type' => BackupType::FILES,
33
            'config_id' => 'tmp_folder'
34
        ]);
35
        $this->expectException(InvalidConfigException::class);
36
        $this->module->configs = [];
37
        $this->expectExceptionMessage('Backup module need to be configured with `config array`');
38
        $restorer = new BackupRestore($backup);
0 ignored issues
show
Unused Code introduced by
The assignment to $restorer is dead and can be removed.
Loading history...
39
    }
40
41
    public function testWrongConfigName()
42
    {
43
        $config_id = 'wrong_config_id';
44
        $backup = new Backup([
45
            'status' => BackupStatus::DONE,
46
            'filename' => 'test.gz',
47
            'type' => BackupType::FILES,
48
            'config_id' => $config_id
49
        ]);
50
        $this->expectException(ErrorException::class);
51
        $this->expectExceptionMessage("Config `{$config_id}` not found.");
52
        $restorer = new BackupRestore($backup);
0 ignored issues
show
Unused Code introduced by
The assignment to $restorer is dead and can be removed.
Loading history...
53
    }
54
55
    public function testDatabaseSuccess()
56
    {
57
        $this->module->backupFolder = '@vendor/../tests/data';
58
59
        $backup = new Backup([
60
            'status' => BackupStatus::DONE,
61
            'filename' => 'test.zip',
62
            'type' => BackupType::DB,
63
            'config_id' => 'main_db'
64
        ]);
65
        $restorer = new BackupRestore($backup);
66
        $restorer->run();
67
68
        $databaseName = Yii::$app->db->databaseName;
69
70
        $commandsToCheck = [
71
            'SELECT DATABASE()',
72
            "DROP DATABASE `{$databaseName}`",
73
            "CREATE DATABASE `{$databaseName}`",
74
            "USE `{$databaseName}`",
75
            "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;",
76
        ];
77
78
        foreach ($commandsToCheck as $command) {
79
            $this->assertTrue(in_array($command, Yii::$app->db->sql));
80
        }
81
    }
82
83
    public function testFolderSuccess()
84
    {
85
        $this->module->backupFolder = '@vendor/../tests/data';
86
        $resultFilePath = Yii::getAlias('@app/tmp/test.txt');
87
        @unlink($resultFilePath);
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

87
        /** @scrutinizer ignore-unhandled */ @unlink($resultFilePath);

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...
88
        $this->assertFileNotExists($resultFilePath);
0 ignored issues
show
Bug introduced by
It seems like $resultFilePath can also be of type boolean; however, parameter $filename of PHPUnit\Framework\Assert::assertFileNotExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $this->assertFileNotExists(/** @scrutinizer ignore-type */ $resultFilePath);
Loading history...
89
        $backup = new Backup([
90
            'status' => BackupStatus::DONE,
91
            'filename' => 'test.tgz',
92
            'type' => BackupType::FILES,
93
            'config_id' => 'tmp_folder'
94
        ]);
95
        $restorer = new BackupRestore($backup);
96
        $restorer->run();
97
        $this->assertFileExists($resultFilePath);
0 ignored issues
show
Bug introduced by
It seems like $resultFilePath can also be of type boolean; however, parameter $filename of PHPUnit\Framework\Assert::assertFileExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $this->assertFileExists(/** @scrutinizer ignore-type */ $resultFilePath);
Loading history...
98
        @unlink($resultFilePath);
99
    }
100
101
102
}