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

testCreateBackupSWithChmod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 11
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 floor12\backup\logic\DatabaseBackupMaker;
16
use floor12\backup\tests\MysqldumpMock;
17
use floor12\backup\tests\TestCase;
18
use Yii;
19
use yii\base\Exception;
20
use yii\db\Connection;
21
22
class DatabaseBackupMakerTest extends TestCase
23
{
24
    public function testCreateBackupFileExists()
25
    {
26
        $this->expectException(Exception::class);
27
        $connection = new Connection(['dsn' => 'sqlite:tests/tmp/app.db']);
28
        $backupFilePath = Yii::getAlias('@app/tmp/sqlite.db');
29
        $dumper = MysqldumpMock::class;
30
        $creator = new DatabaseBackupMaker($backupFilePath, $connection, $dumper);
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\Dat...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

30
        $creator = new DatabaseBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, $connection, $dumper);
Loading history...
31
    }
32
33
    public function testCreateBackupSuccess()
34
    {
35
        $connection = new Connection(['dsn' => 'sqlite:tests/tmp/app.db']);
36
        $backupFilePath = Yii::getAlias('@app/tmp/backup.tgz');
37
        $dumper = MysqldumpMock::class;
38
        $creator = new DatabaseBackupMaker($backupFilePath, $connection, $dumper);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $backupFilePath of floor12\backup\logic\Dat...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

38
        $creator = new DatabaseBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, $connection, $dumper);
Loading history...
39
        $this->assertTrue($creator->execute());
40
        $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

40
        $this->/** @scrutinizer ignore-call */ 
41
               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...
41
        @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

41
        /** @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...
42
    }
43
44
    public function testCreateBackupSWithChmod()
45
    {
46
        $connection = new Connection(['dsn' => 'sqlite:tests/tmp/app.db']);
47
        $backupFilePath = Yii::getAlias('@app/tmp/backup.tgz');
48
        $this->module->chmod = 0700;
49
        $dumper = MysqldumpMock::class;
50
        $creator = new DatabaseBackupMaker($backupFilePath, $connection, $dumper);
0 ignored issues
show
Bug introduced by
It seems like $backupFilePath can also be of type boolean; however, parameter $backupFilePath of floor12\backup\logic\Dat...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

50
        $creator = new DatabaseBackupMaker(/** @scrutinizer ignore-type */ $backupFilePath, $connection, $dumper);
Loading history...
51
        $this->assertTrue($creator->execute());
52
        $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

52
        $this->/** @scrutinizer ignore-call */ 
53
               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...
53
        $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

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

54
        /** @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...
55
    }
56
57
    protected function readPerms(string $file)
58
    {
59
        return substr(sprintf('%o', fileperms($file)), -4);
60
    }
61
62
63
}