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

DatabaseBackupRestorerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testFileNoExists() 0 7 1
A testRestoreSuccess() 0 16 2
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\DatabaseBackupRestorer;
16
use floor12\backup\tests\ConnectionMock;
17
use floor12\backup\tests\TestCase;
18
use Yii;
19
use yii\base\Exception;
20
21
class DatabaseBackupRestorerTest extends TestCase
22
{
23
24
    public function testFileNoExists()
25
    {
26
        $connection = new ConnectionMock();
27
        $this->expectException(Exception::class);
28
        $this->expectExceptionMessage('Backup file don`t exist.');
29
        $backupFilePath = Yii::getAlias('@app/data/no-exist.tgz');
30
        new DatabaseBackupRestorer($backupFilePath, $connection);
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...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

30
        new DatabaseBackupRestorer(/** @scrutinizer ignore-type */ $backupFilePath, $connection);
Loading history...
31
    }
32
33
    public function testRestoreSuccess()
34
    {
35
        $databaseName = 'testDataBaseName';
36
        $connection = new ConnectionMock(['databaseName' => $databaseName]);
37
        $commandsToCheck = [
38
            'SELECT DATABASE()',
39
            "DROP DATABASE `{$databaseName}`",
40
            "CREATE DATABASE `{$databaseName}`",
41
            "USE `{$databaseName}`",
42
            "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;",
43
        ];
44
        $backupFilePath = Yii::getAlias('@app/data/test.zip');
45
        $restorer = new DatabaseBackupRestorer($backupFilePath, $connection);
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...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

45
        $restorer = new DatabaseBackupRestorer(/** @scrutinizer ignore-type */ $backupFilePath, $connection);
Loading history...
46
        $restorer->execute();
47
        foreach ($commandsToCheck as $command) {
48
            $this->assertTrue(in_array($command, $connection->sql));
49
        }
50
    }
51
52
53
}