TestCase   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 146
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 4 1
A mockApplication() 0 38 1
A getVendorPath() 0 4 1
A destroyApplication() 0 4 1
B setupTestDbData() 0 58 1
1
<?php
2
3
namespace yii2mod\rbac\tests;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
8
/**
9
 * This is the base class for all yii framework unit tests.
10
 */
11
class TestCase extends \PHPUnit\Framework\TestCase
12
{
13
    /**
14
     * @inheritdoc
15
     */
16
    protected function setUp()
17
    {
18
        parent::setUp();
19
20
        $this->mockApplication();
21
22
        $this->setupTestDbData();
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    protected function tearDown()
29
    {
30
        $this->destroyApplication();
31
    }
32
33
    /**
34
     * Populates Yii::$app with a new application
35
     * The application will be destroyed on tearDown() automatically.
36
     *
37
     * @param array $config The application configuration, if needed
38
     * @param string $appClass name of the application class to create
39
     */
40
    protected function mockApplication($config = [], $appClass = '\yii\web\Application')
41
    {
42
        new $appClass(ArrayHelper::merge([
43
            'id' => 'testapp',
44
            'basePath' => __DIR__,
45
            'vendorPath' => $this->getVendorPath(),
46
            'modules' => [
47
                'rbac' => [
48
                    'class' => 'yii2mod\rbac\Module',
49
                ],
50
            ],
51
            'components' => [
52
                'db' => [
53
                    'class' => 'yii\db\Connection',
54
                    'dsn' => 'sqlite::memory:',
55
                ],
56
                'authManager' => [
57
                    'class' => 'yii\rbac\DbManager',
58
                    'defaultRoles' => ['guest', 'user'],
59
                ],
60
                'user' => [
61
                    'identityClass' => 'yii2mod\rbac\tests\data\User',
62
                ],
63
                'request' => [
64
                    'hostInfo' => 'http://domain.com',
65
                    'scriptUrl' => 'index.php',
66
                ],
67
                'i18n' => [
68
                    'translations' => [
69
                        'yii2mod.rbac' => [
70
                            'class' => 'yii\i18n\PhpMessageSource',
71
                            'basePath' => '@yii2mod/rbac/messages',
72
                        ],
73
                    ],
74
                ],
75
            ],
76
        ], $config));
77
    }
78
79
    /**
80
     * @return string vendor path
81
     */
82
    protected function getVendorPath()
83
    {
84
        return dirname(__DIR__) . '/vendor';
85
    }
86
87
    /**
88
     * Destroys application in Yii::$app by setting it to null.
89
     */
90
    protected function destroyApplication()
91
    {
92
        Yii::$app = null;
93
    }
94
95
    /**
96
     * Setup tables for test ActiveRecord
97
     */
98
    protected function setupTestDbData()
99
    {
100
        $db = Yii::$app->getDb();
101
102
        // Structure :
103
104
        $db->createCommand()->createTable('{{%auth_rule}}', [
105
            'name' => 'string',
106
            'data' => 'text',
107
            'created_at' => 'integer',
108
            'updated_at' => 'integer',
109
            'PRIMARY KEY (name)',
110
        ])->execute();
111
112
        $db->createCommand()->createTable('{{%auth_item}}', [
113
            'name' => 'string',
114
            'type' => 'integer',
115
            'description' => 'text',
116
            'rule_name' => 'string',
117
            'data' => 'string',
118
            'created_at' => 'integer',
119
            'updated_at' => 'integer',
120
            'FOREIGN KEY (rule_name) REFERENCES ' . '{{%auth_rule}}' . ' (name) ON DELETE SET NULL ON UPDATE CASCADE',
121
        ])->execute();
122
123
        $db->createCommand()->createTable('{{%auth_item_child}}', [
124
            'parent' => 'string',
125
            'child' => 'string',
126
            'PRIMARY KEY (parent, child)',
127
            'FOREIGN KEY (parent) REFERENCES ' . '{{%auth_item}}' . ' (name) ON DELETE CASCADE ON UPDATE CASCADE',
128
            'FOREIGN KEY (child) REFERENCES ' . '{{%auth_item}}' . ' (name) ON DELETE CASCADE ON UPDATE CASCADE',
129
        ])->execute();
130
131
        $db->createCommand()->createTable('{{%auth_assignment}}', [
132
            'item_name' => 'string',
133
            'user_id' => 'integer',
134
            'created_at' => 'integer',
135
            'PRIMARY KEY (item_name, user_id)',
136
            'FOREIGN KEY (item_name) REFERENCES ' . '{{%auth_item}}' . ' (name) ON DELETE CASCADE ON UPDATE CASCADE',
137
        ])->execute();
138
139
        $db->createCommand()->createTable('{{%user}}', [
140
            'id' => 'pk',
141
            'username' => 'string not null unique',
142
            'auth_key' => 'string(32) not null',
143
            'password_hash' => 'string not null',
144
            'email' => 'string not null unique',
145
        ])->execute();
146
147
        // Data :
148
149
        $db->createCommand()->insert('{{%user}}', [
150
            'username' => 'demo',
151
            'auth_key' => Yii::$app->getSecurity()->generateRandomString(),
152
            'password_hash' => Yii::$app->getSecurity()->generatePasswordHash('password'),
153
            'email' => '[email protected]',
154
        ])->execute();
155
    }
156
}
157