FolderBackupMakerTest::testCreateBackupSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
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\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/data/sqlite.db');
26
        $creator = new FolderBackupMaker($backupFilePath, 'tmp');
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 false; 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...
27
    }
28
29
    public function testCreateBackupTargetNotExists()
30
    {
31
        $this->expectException(Exception::class);
32
        $backupFilePath = Yii::getAlias('@app/data/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 false; 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/data/backup.tgz');
40
        $targetFolder = Yii::getAlias('@app/unit');
41
        $creator = new FolderBackupMaker($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...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...
Bug introduced by
It seems like $targetFolder can also be of type false; 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...
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);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath 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

44
        unlink(/** @scrutinizer ignore-type */ $backupFilePath);
Loading history...
45
    }
46
47
    public function testCreateBackupSuccessWithIoNice()
48
    {
49
        $backupFilePath = Yii::getAlias('@app/data/backup.tgz');
50
        $targetFolder = Yii::getAlias('@app/data/folder_for_backup');
51
52
        $this->assertFileNotExists($backupFilePath);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath 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

52
        $this->assertFileNotExists(/** @scrutinizer ignore-type */ $backupFilePath);
Loading history...
53
        $creator = new FolderBackupMaker($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...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

53
        $creator = new FolderBackupMaker($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...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

53
        $creator = new FolderBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, $targetFolder);
Loading history...
54
        $this->assertTrue($creator->execute());
55
        $this->assertFileExists($backupFilePath);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath 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

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

56
        unlink(/** @scrutinizer ignore-type */ $backupFilePath);
Loading history...
57
    }
58
59
    public function testCreateBackupSuccessWithChmod()
60
    {
61
        $backupFilePath = Yii::getAlias('@app/data/backup.tgz');
62
        $targetFolder = Yii::getAlias('@app/data/folder_for_backup');
63
        Yii::$app->getModule('backup')->chmod = 0700;
64
        $this->assertFileNotExists($backupFilePath);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath 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

64
        $this->assertFileNotExists(/** @scrutinizer ignore-type */ $backupFilePath);
Loading history...
65
        $creator = new FolderBackupMaker($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...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

65
        $creator = new FolderBackupMaker(/** @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...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

65
        $creator = new FolderBackupMaker($backupFilePath, /** @scrutinizer ignore-type */ $targetFolder);
Loading history...
66
        $this->assertTrue($creator->execute());
67
        $this->assertFileExists($backupFilePath);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath 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

67
        $this->assertFileExists(/** @scrutinizer ignore-type */ $backupFilePath);
Loading history...
68
        $this->assertEquals('0700', $this->readPerms($backupFilePath));
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath can also be of type false; 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

68
        $this->assertEquals('0700', $this->readPerms(/** @scrutinizer ignore-type */ $backupFilePath));
Loading history...
69
        @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

69
        /** @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...
Bug introduced by
It seems like $backupFilePath 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

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