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

testCreateBackupSuccessWithIoNice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 13
rs 9.9332
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 floor12\backup\logic\FolderBackupMaker;
16
use floor12\backup\tests\TestCase;
17
use Yii;
18
use yii\base\Exception;
19
20
class FolderBackupMakerTest extends TestCase
21
{
22
    public function testCreateBackupFileExists()
23
    {
24
        $this->expectException(Exception::class);
25
        $backupFilePath = Yii::getAlias('@app/tmp/sqlite.db');
26
        $creator = new FolderBackupMaker($backupFilePath, 'tmp');
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $backupFilePath of floor12\backup\logic\Fol...kupMaker::__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

26
        $creator = new FolderBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, 'tmp');
Loading history...
Unused Code introduced by
The assignment to $creator is dead and can be removed.
Loading history...
27
    }
28
29
    public function testCreateBackupTargetNotExists()
30
    {
31
        $this->expectException(Exception::class);
32
        $backupFilePath = Yii::getAlias('@app/tmp/backup.tgz');
33
        $creator = new FolderBackupMaker($backupFilePath, 'not-exists');
0 ignored issues
show
Unused Code introduced by
The assignment to $creator is dead and can be removed.
Loading history...
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $backupFilePath of floor12\backup\logic\Fol...kupMaker::__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

33
        $creator = new FolderBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, 'not-exists');
Loading history...
34
    }
35
36
37
    public function testCreateBackupSuccess()
38
    {
39
        $backupFilePath = Yii::getAlias('@app/tmp/backup.tgz');
40
        $targetFolder = Yii::getAlias('@app/unit');
41
        $creator = new FolderBackupMaker($backupFilePath, $targetFolder);
0 ignored issues
show
Bug introduced by
It seems like $targetFolder can also be of type boolean; however, parameter $targetFolder of floor12\backup\logic\Fol...kupMaker::__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

41
        $creator = new FolderBackupMaker($backupFilePath, /** @scrutinizer ignore-type */ $targetFolder);
Loading history...
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $backupFilePath of floor12\backup\logic\Fol...kupMaker::__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

41
        $creator = new FolderBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, $targetFolder);
Loading history...
42
        $this->assertTrue($creator->execute());
43
        $this->fileExists($backupFilePath);
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\Assert::fileExists() has too many arguments starting with $backupFilePath. ( Ignorable by Annotation )

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

43
        $this->/** @scrutinizer ignore-call */ 
44
               fileExists($backupFilePath);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
44
        unlink($backupFilePath);
45
    }
46
47
    public function testCreateBackupSuccessWithIoNice()
48
    {
49
        $backupFilePath = Yii::getAlias('@app/tmp/backup.tgz');
50
        $testFilePath = Yii::getAlias('@app/tmp/test');
51
        $targetFolder = Yii::getAlias('@app/unit');
52
53
        $this->module->ionice = "touch {$testFilePath} && ";
54
        $creator = new FolderBackupMaker($backupFilePath, $targetFolder);
0 ignored issues
show
Bug introduced by
It seems like $targetFolder can also be of type boolean; however, parameter $targetFolder of floor12\backup\logic\Fol...kupMaker::__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

54
        $creator = new FolderBackupMaker($backupFilePath, /** @scrutinizer ignore-type */ $targetFolder);
Loading history...
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $backupFilePath of floor12\backup\logic\Fol...kupMaker::__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

54
        $creator = new FolderBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, $targetFolder);
Loading history...
55
        $this->assertTrue($creator->execute());
56
        $this->fileExists($backupFilePath);
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\Assert::fileExists() has too many arguments starting with $backupFilePath. ( Ignorable by Annotation )

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

56
        $this->/** @scrutinizer ignore-call */ 
57
               fileExists($backupFilePath);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
57
        $this->fileExists($testFilePath);
58
        unlink($backupFilePath);
59
        unlink($testFilePath);
60
    }
61
62
    public function testCreateBackupSuccessWithChmod()
63
    {
64
        $backupFilePath = Yii::getAlias('@app/tmp/backup.tgz');
65
        @unlink($backupFilePath);
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

65
        /** @scrutinizer ignore-unhandled */ @unlink($backupFilePath);

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...
66
        $targetFolder = Yii::getAlias('@app/unit');
67
        $this->module->chmod = 0700;
68
        $creator = new FolderBackupMaker($backupFilePath, $targetFolder);
0 ignored issues
show
Bug introduced by
It seems like $targetFolder can also be of type boolean; however, parameter $targetFolder of floor12\backup\logic\Fol...kupMaker::__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

68
        $creator = new FolderBackupMaker($backupFilePath, /** @scrutinizer ignore-type */ $targetFolder);
Loading history...
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $backupFilePath of floor12\backup\logic\Fol...kupMaker::__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

68
        $creator = new FolderBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, $targetFolder);
Loading history...
69
        $this->assertTrue($creator->execute());
70
        $this->fileExists($backupFilePath);
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\Assert::fileExists() has too many arguments starting with $backupFilePath. ( Ignorable by Annotation )

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

70
        $this->/** @scrutinizer ignore-call */ 
71
               fileExists($backupFilePath);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
71
        $this->assertEquals('0700', $this->readPerms($backupFilePath));
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $file of floor12\backup\tests\uni...pMakerTest::readPerms() 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

71
        $this->assertEquals('0700', $this->readPerms(/** @scrutinizer ignore-type */ $backupFilePath));
Loading history...
72
        @unlink($backupFilePath);
73
    }
74
75
    protected function readPerms(string $file)
76
    {
77
        return substr(sprintf('%o', fileperms($file)), -4);
78
    }
79
80
81
}