TestCase   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 5
eloc 50
c 3
b 2
f 0
dl 0
loc 100
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A destroyApplication() 0 3 1
A tearDown() 0 4 1
A setUp() 0 5 1
A mockApplication() 0 7 1
A setApp() 0 49 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 07.01.2018
6
 * Time: 12:40
7
 */
8
9
namespace floor12\backup\tests;
10
11
use floor12\backup\models\BackupType;
12
use floor12\backup\models\IOPriority;
13
use floor12\backup\Module;
14
use Yii;
15
use yii\base\InvalidConfigException;
16
use yii\console\Application;
17
18
abstract class TestCase extends \PHPUnit\Framework\TestCase
19
{
20
    /**
21
     * @var Module
22
     */
23
    public $module;
24
25
    /**
26
     * @inheritdoc
27
     * @throws InvalidConfigException
28
     */
29
    protected function setUp()
30
    {
31
        $this->mockApplication();
32
        $this->setApp();
33
        parent::setUp();
34
    }
35
36
    /**
37
     * @throws InvalidConfigException
38
     */
39
    protected function mockApplication()
40
    {
41
        new Application([
42
            'id' => 'testApp',
43
            'basePath' => __DIR__,
44
            'vendorPath' => dirname(__DIR__) . '/vendor',
45
            'runtimePath' => __DIR__ . '/runtime',
46
        ]);
47
    }
48
49
    /**
50
     * Adds backup module to tmp app
51
     */
52
    protected function setApp()
53
    {
54
        $backupModule = [
55
            'class' => 'floor12\backup\Module',
56
            'backupFolder' => '@vendor/../tests/_output',
57
            'configs' => [
58
                'mysql_db' => [
59
                    'type' => BackupType::DB,
60
                    'title' => 'Mysql Database',
61
                    'connection' => 'mysql',
62
                    'limit' => 0
63
                ],
64
                'postgres_db' => [
65
                    'type' => BackupType::DB,
66
                    'title' => 'PostgresQL database',
67
                    'connection' => 'postgres',
68
                    'io' => IOPriority::REALTIME,
69
                    'limit' => 0
70
                ],
71
                'backup_test_folder' => [
72
                    'type' => BackupType::FILES,
73
                    'title' => 'TMP folder',
74
                    'path' => '@app/data/folder_for_backup',
75
                    'io' => IOPriority::IDLE,
76
                    'limit' => 0
77
                ]
78
            ]
79
        ];
80
        Yii::$app->setModule('backup', $backupModule);
81
82
        $mysql = [
83
            'class' => 'yii\db\Connection',
84
            'dsn' => 'mysql:host=mysql;dbname=tester',
85
            'username' => 'tester',
86
            'password' => 'tester',
87
            'charset' => 'utf8',
88
        ];
89
        Yii::$app->set('mysql', $mysql);
90
91
        $postgres = [
92
            'class' => 'yii\db\Connection',
93
            'dsn' => 'pgsql:host=postgres;port=5432;dbname=tester',
94
            'username' => 'tester',
95
            'password' => 'tester',
96
            'charset' => 'utf8',
97
        ];
98
        Yii::$app->set('postgres', $postgres);
99
100
        $this->module = Yii::$app->getModule('backup');
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    protected function tearDown()
107
    {
108
        $this->destroyApplication();
109
        parent::tearDown();
110
    }
111
112
    /**
113
     * Destroy test application
114
     */
115
    protected function destroyApplication()
116
    {
117
        Yii::$app = null;
118
    }
119
}
120