TestCase::mockWebApplication()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace yii\web;
4
5
/**
6
 * Mock for the is_uploaded_file() function for web classes.
7
 * @return boolean
8
 */
9
function is_uploaded_file($filename)
10
{
11
    return file_exists($filename);
12
}
13
14
/**
15
 * Mock for the move_uploaded_file() function for web classes.
16
 * @return boolean
17
 */
18
function move_uploaded_file($filename, $destination)
19
{
20
    return copy($filename, $destination);
21
}
22
23
namespace zacksleo\yii2\backend\tests;
24
25
use PHPUnit_Framework_TestCase;
26
use yii\helpers\ArrayHelper;
27
use yii;
28
29
/**
30
 * Created by PhpStorm.
31
 * User: zjw
32
 * Date: 2017/8/15
33
 * Time: 上午11:33
34
 */
35
class TestCase extends PHPUnit_Framework_TestCase
36
{
37
    public function setUp()
38
    {
39
        parent::setUp();
40
        $this->mockWebApplication();
41
        $this->createTestData();
42
    }
43
44
    public function createTestData()
45
    {
46
        $adminSql = <<<EOF
47
        -- auto-generated definition
48
        create table test_admin
49
        (
50
            id int auto_increment
51
                primary key,
52
            auth_key varchar(125) null,
53
            avatar varchar(255) null comment '头像',
54
            username varchar(20) not null comment '用户名',
55
            name varchar(20) not null comment '姓名',
56
            email varchar(64) not null comment '邮箱',
57
            password_hash varchar(64) not null comment '密码',
58
            password_reset_token varchar(255) null comment '重置密码Token',
59
            status tinyint(1) default '10' not null comment '状态',
60
            created_at int not null comment '创建时间',
61
            updated_at int not null comment '更新时间'
62
        )
63
        comment '管理员';  
64
        INSERT INTO test.test_admin 
65
        (
66
        auth_key, avatar, username, name, email, password_hash, password_reset_token, status, created_at, updated_at
67
        ) 
68
        VALUES 
69
        (
70
        'CccLhn6aqp_Y-XYh-JzfXSCfxJNkKC8w', '', 'lianluo', '管理员', '[email protected]', '$2y$13\$dbGxVyj3kglcJNUEsKyiu.5KQ9We3AqAFncYkdAS1iNRYf/RA37Ay', null, 10, 1502856859, 1502856859
71
        );
72
EOF;
73
        $userSql = <<<EOF
74
        -- auto-generated definition
75
        create table `test_user`
76
        (
77
            `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
78
            `username` varchar(32) NOT NULL,
79
            `auth_key` varchar(32) NOT NULL,
80
            `password_hash` varchar(256) NOT NULL,
81
            `password_reset_token` varchar(256),
82
            `email` varchar(256) NOT NULL,
83
            `status` integer not null default 10,
84
            `created_at` integer not null,
85
            `updated_at` integer not null
86
        )ENGINE=InnoDB DEFAULT CHARSET=utf8;
87
EOF;
88
        try {
89
            $db = Yii::$app->getDb();
90
            $db->createCommand($adminSql)->execute();
91
            $db->createCommand($userSql)->execute();
92
        } catch (yii\db\Exception $e) {
93
            //var_dump($e->getMessage());
94
            return;
95
        }
96
    }
97
98
    protected function mockWebApplication()
99
    {
100
        $appClass = "\yii\web\Application";
101
        $this->mockApplication([], $appClass);
102
    }
103
104
    protected function mockConsoleApplication()
105
    {
106
        $appClass = "\yii\console\Application";
107
        return $this->mockApplication([], $appClass);
108
    }
109
110
    protected function mockApplication($config = [], $appClass = '\yii\console\Application')
111
    {
112
        $config['params'] = [
113
            'user.passwordResetTokenExpire' => 3600,
114
            'user.emailConfirmationTokenExpire' => 3600,
115
            'admin.email' => '[email protected]',
116
            'support.email' => '[email protected]',
117
            'support.name' => '技术支持',
118
        ];
119
120
        new $appClass(ArrayHelper::merge([
121
            'id' => 'testapp',
122
            'basePath' => __DIR__,
123
            'vendorPath' => $this->getVendorPath(),
124
            'components' => [
125
                'db' => [
126
                    'class' => 'yii\db\Connection',
127
                    'dsn' => 'mysql:host=localhost:3306;dbname=test',
128
                    'username' => 'root',
129
                    'password' => '',
130
                    'charset' => 'utf8',
131
                    'tablePrefix' => 'test_'
132
                ],
133
                'i18n' => [
134
                    'translations' => [
135
                        '*' => [
136
                            'class' => 'yii\i18n\PhpMessageSource',
137
                        ]
138
                    ]
139
                ],
140
                'user' => [
141
                    'identityClass' => 'zacksleo\yii2\backend\models\Admin',
142
                ]
143
            ],
144
            'modules' => [
145
                'backend' => [
146
                    'class' => 'zacksleo\yii2\backend\Module',
147
                    'controllerNamespace' => 'zacksleo\yii2\backend\tests\controllers'
148
                ]
149
            ]
150
        ], $config));
151
        \Yii::setAlias('@web', __DIR__ . '/web');
152
    }
153
154
    private function getVendorPath()
155
    {
156
        return dirname(__DIR__) . '/vendor';
157
    }
158
}
159