BackupRestoreTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
eloc 45
c 3
b 0
f 1
dl 0
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testWrongConfigName() 0 11 1
A testDatabaseSuccess() 0 21 1
A testFolderSuccess() 0 18 1
A testEmptyConfigs() 0 11 1
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\Exceptions\ModuleNotConfiguredException;
17
use floor12\backup\logic\BackupRestore;
18
use floor12\backup\models\Backup;
19
use floor12\backup\models\BackupStatus;
20
use floor12\backup\models\BackupType;
21
use floor12\backup\tests\TestCase;
22
use Yii;
23
use yii\base\InvalidConfigException;
24
25
class BackupRestoreTest extends TestCase
26
{
27
    /**
28
     * @throws ErrorException
29
     * @throws InvalidConfigException
30
     */
31
    public function testEmptyConfigs()
32
    {
33
        $backup = new Backup([
34
            'status' => BackupStatus::DONE,
35
            'filename' => 'test.gz',
36
            'type' => BackupType::FILES,
37
            'config_id' => 'tmp_folder'
38
        ]);
39
        $this->expectException(ModuleNotConfiguredException::class);
40
        $this->module->configs = [];
41
        new BackupRestore($backup);
42
    }
43
44
    /**
45
     * @throws ErrorException
46
     * @throws InvalidConfigException
47
     */
48
    public function testWrongConfigName()
49
    {
50
        $config_id = 'wrong_config_id';
51
        $backup = new Backup([
52
            'status' => BackupStatus::DONE,
53
            'filename' => 'test.gz',
54
            'type' => BackupType::FILES,
55
            'config_id' => $config_id
56
        ]);
57
        $this->expectException(ErrorException::class);
58
        new BackupRestore($backup);
59
    }
60
61
    /**
62
     * @throws ErrorException
63
     * @throws InvalidConfigException
64
     */
65
    public function testDatabaseSuccess()
66
    {
67
        $this->module->backupFolder = '@vendor/../tests/data';
68
        $this->module->backupRootPath = Yii::getAlias('@vendor/../tests/data');
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::getAlias('@vendor/../tests/data') can also be of type false. However, the property $backupRootPath is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
70
        $backup = new Backup([
71
            'status' => BackupStatus::DONE,
72
            'filename' => 'postgres',
73
            'type' => BackupType::DB,
74
            'config_id' => 'postgres_db'
75
        ]);
76
        $restorer = new BackupRestore($backup);
77
        $restorer->run();
78
        $tableExists = Yii::$app->postgres->createCommand("SELECT EXISTS (
0 ignored issues
show
Bug introduced by
The method createCommand() does not exist on null. ( Ignorable by Annotation )

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

78
        $tableExists = Yii::$app->postgres->/** @scrutinizer ignore-call */ createCommand("SELECT EXISTS (

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
               SELECT FROM information_schema.tables
80
               WHERE  table_schema = 'public'
81
                 AND    table_name   = 'test_table'
82
           );")->queryScalar() == 'true';
83
        $this->assertTrue($tableExists);
84
        $dropResult = Yii::$app->postgres->createCommand()->dropTable('test_table')->execute();
85
        $this->assertEquals(0, $dropResult);
86
    }
87
88
    /**
89
     * @throws ErrorException
90
     * @throws InvalidConfigException
91
     */
92
    public function testFolderSuccess()
93
    {
94
        $this->module->backupFolder = '@vendor/../tests/data';
95
        $this->module->backupRootPath = Yii::getAlias('@vendor/../tests/data');
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::getAlias('@vendor/../tests/data') can also be of type false. However, the property $backupRootPath is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
96
97
        $resultFilePath = Yii::getAlias('@app/data/folder_for_backup/exists_test_file.txt');
98
        @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

98
        /** @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...
Bug introduced by
It seems like $resultFilePath can also be of type false; however, parameter $filename of unlink() 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

98
        @unlink(/** @scrutinizer ignore-type */ $resultFilePath);
Loading history...
99
        $this->assertFileNotExists($resultFilePath);
0 ignored issues
show
Bug introduced by
It seems like $resultFilePath can also be of type false; 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

99
        $this->assertFileNotExists(/** @scrutinizer ignore-type */ $resultFilePath);
Loading history...
100
        $backup = new Backup([
101
            'status' => BackupStatus::DONE,
102
            'filename' => 'folder.zip',
103
            'type' => BackupType::FILES,
104
            'config_id' => 'backup_test_folder'
105
        ]);
106
        $restorer = new BackupRestore($backup);
107
        $restorer->run();
108
        $this->assertFileExists($resultFilePath);
0 ignored issues
show
Bug introduced by
It seems like $resultFilePath can also be of type false; 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

108
        $this->assertFileExists(/** @scrutinizer ignore-type */ $resultFilePath);
Loading history...
109
        @unlink($resultFilePath);
110
    }
111
112
113
}
114