FolderBackupRestorerTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 20
c 2
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testFileNoExists() 0 7 1
A testFolderNoExists() 0 7 1
A testRestoreSuccess() 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 floor12\backup\logic\FolderBackupRestorer;
16
use floor12\backup\tests\TestCase;
17
use Yii;
18
use yii\base\Exception;
19
20
class FolderBackupRestorerTest extends TestCase
21
{
22
    public function testFolderNoExists()
23
    {
24
        $this->expectException(Exception::class);
25
        $this->expectExceptionMessage('Target folder don`t exist.');
26
        $backupFilePath = Yii::getAlias('@app/data/folder.zip');
27
        $targetFolder = Yii::getAlias('@app/no-exists');
28
        new FolderBackupRestorer($backupFilePath, $targetFolder);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Fol...Restorer::__construct() 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

28
        new FolderBackupRestorer(/** @scrutinizer ignore-type */ $backupFilePath, $targetFolder);
Loading history...
Bug introduced by
It seems like $targetFolder can also be of type false; however, parameter $targetFolder of floor12\backup\logic\Fol...Restorer::__construct() 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

28
        new FolderBackupRestorer($backupFilePath, /** @scrutinizer ignore-type */ $targetFolder);
Loading history...
29
    }
30
31
    public function testFileNoExists()
32
    {
33
        $this->expectException(Exception::class);
34
        $this->expectExceptionMessage('Backup file don`t exist.');
35
        $backupFilePath = Yii::getAlias('@app/data/no-exist.tgz');
36
        $targetFolder = Yii::getAlias('@app/data/');
37
        new FolderBackupRestorer($backupFilePath, $targetFolder);
0 ignored issues
show
Bug introduced by
It seems like $targetFolder can also be of type false; however, parameter $targetFolder of floor12\backup\logic\Fol...Restorer::__construct() 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

37
        new FolderBackupRestorer($backupFilePath, /** @scrutinizer ignore-type */ $targetFolder);
Loading history...
Bug introduced by
It seems like $backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Fol...Restorer::__construct() 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

37
        new FolderBackupRestorer(/** @scrutinizer ignore-type */ $backupFilePath, $targetFolder);
Loading history...
38
    }
39
40
    public function testRestoreSuccess()
41
    {
42
        $fileFromBackup = Yii::getAlias('@app/data/exists_test_file.txt');
43
        $backupFilePath = Yii::getAlias('@app/data/folder.zip');
44
        $targetFolder = Yii::getAlias('@app/data/');
45
        @unlink($fileFromBackup);
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

45
        /** @scrutinizer ignore-unhandled */ @unlink($fileFromBackup);

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 $fileFromBackup 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

45
        @unlink(/** @scrutinizer ignore-type */ $fileFromBackup);
Loading history...
46
        $this->assertFileNotExists($fileFromBackup);
0 ignored issues
show
Bug introduced by
It seems like $fileFromBackup 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

46
        $this->assertFileNotExists(/** @scrutinizer ignore-type */ $fileFromBackup);
Loading history...
47
        $restorer = new FolderBackupRestorer($backupFilePath, $targetFolder);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath can also be of type false; however, parameter $backupFilePath of floor12\backup\logic\Fol...Restorer::__construct() 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

47
        $restorer = new FolderBackupRestorer(/** @scrutinizer ignore-type */ $backupFilePath, $targetFolder);
Loading history...
Bug introduced by
It seems like $targetFolder can also be of type false; however, parameter $targetFolder of floor12\backup\logic\Fol...Restorer::__construct() 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

47
        $restorer = new FolderBackupRestorer($backupFilePath, /** @scrutinizer ignore-type */ $targetFolder);
Loading history...
48
        $restorer->execute();
49
        $this->assertFileExists($fileFromBackup);
0 ignored issues
show
Bug introduced by
It seems like $fileFromBackup 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

49
        $this->assertFileExists(/** @scrutinizer ignore-type */ $fileFromBackup);
Loading history...
50
        @unlink($fileFromBackup);
51
    }
52
53
54
}
55