AdminModelTest::testFindIdentityByAccessToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: zjw
5
 * Date: 2017/8/15
6
 * Time: 下午3:12
7
 */
8
9
namespace zacksleo\yii2\backend\tests;
10
11
use yii;
12
use yii\base\Exception;
13
use yii\base\NotSupportedException;
14
use yii\web\UploadedFile;
15
use zacksleo\yii2\backend\models\Admin;
16
17
class AdminModelTest extends TestCase
18
{
19
    /**
20
     * @var \zacksleo\yii2\backend\models\Admin;
21
     */
22
    public $model;
23
24
    public function setUp()
25
    {
26
        parent::setUp();
27
        $this->model = new Admin();
28
    }
29
30
    public function testRules()
31
    {
32
        $this->model->username = "username";
33
        $this->model->name = "name";
34
        $this->model->email = "1222.com";
35
        $this->model->scenario = 'reset';
36
        $this->model->password_hash = sha1("username");
37
        $this->assertFalse($this->model->validate());
38
        $this->model->email = "[email protected]";
39
        $this->assertTrue($this->model->validate());
40
41
        $this->model->username = "username ";           //用户名不能包含空格
42
        $this->assertFalse($this->model->validate());
43
44
        $this->model->username = "1username";           //用户名以字母开头
45
        $this->assertFalse($this->model->validate());
46
47
        $this->model->username = "username";
48
        $this->assertTrue($this->model->validate());
49
50
        $this->model->password_hash = "1111111";        //密码中至少包含一位字母
51
        $this->assertFalse($this->model->validate());
52
53
        $this->model->password_hash = "zxcvbnm";        //密码中至少包含一位数字
54
        $this->assertFalse($this->model->validate());
55
56
        $this->model->password_hash = "zxcvbnm111";
57
        $this->assertTrue($this->model->validate());
58
59
        $this->model->username = "lianluo";             //用户名唯一
60
        $this->assertFalse($this->model->validate());
61
62
        $this->model->username = "username";
63
        $this->model->imageFile = UploadedFile::getInstanceByName('imageFile');
64
        $this->assertTrue($this->model->validate());
65
66
67
        $this->model->email = "[email protected]";     //邮箱唯一
68
        $this->assertFalse($this->model->validate());
69
    }
70
71
    public function testFindIdentity()
72
    {
73
        $id = 1;
74
        $res = Admin::findIdentity($id);
75
        $this->assertTrue($res->id == $id);
76
    }
77
78
    public function testGetId()
79
    {
80
        $model = Admin::findIdentity(1);
81
        $this->assertTrue($model->getId() == 1);
82
    }
83
84
    public function testValidateAuthKey()
85
    {
86
        $model = Admin::findIdentity(1);
87
        $this->assertTrue($model->validateAuthKey($model->auth_key));
88
    }
89
90
    public function testUpload()
91
    {
92
        $model = Admin::findIdentity(1);
93
        $model->imageFile = UploadedFile::getInstanceByName('imageFile');
94
        $this->assertTrue($model->upload());
95
        $this->assertTrue($model->save());
96
97
        $model = new Admin();
98
        $model->scenario = "reset";
99
        $model->imageFile = UploadedFile::getInstanceByName('imageFile');
100
        $this->assertFalse($model->upload());
101
    }
102
103
    public function testGetImageUrl()
104
    {
105
        $model = Admin::findIdentity(1);
106
        $url = $model->getImageUrl();
107
        $res = md5_file($url) == md5_file(__DIR__ . '/web/test.jpg');
108
        $this->assertTrue($res);
109
    }
110
111
    public function testFindIdentityByAccessToken()
112
    {
113
        try {
114
            Admin::findIdentityByAccessToken('');
115
        } catch (Exception $e) {
116
            $this->assertTrue($e instanceof NotSupportedException);
117
            return;
118
        }
119
    }
120
121 View Code Duplication
    public function testValidatePassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $model = Admin::findOne(1);
124
        $model->validatePassword("1234");
125
        $this->assertFalse($model->validatePassword("1234"));
126
        $this->assertTrue($model->validatePassword("1!an1u0"));
127
    }
128
129
    public function testGeneratePasswordResetToken()
130
    {
131
        $model = Admin::findOne(1);
132
        $res = $model->generatePasswordResetToken(true);
133
        $this->assertTrue($res);
134
    }
135
136 View Code Duplication
    public function testResetPassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        $model = Admin::findOne(1);
139
        $this->assertTrue($model->resetPassword('lian1uo'));
140
        $this->assertTrue($model->validatePassword("lian1uo"));
141
        $this->assertTrue($model->resetPassword('1!an1u0'));
142
    }
143
144
    public function testGenerateAuthKey()
145
    {
146
        $model = new Admin();
147
        $model->generateAuthKey();
148
        $this->assertTrue(!empty($model->auth_key));
149
    }
150
151
    public static function setUpBeforeClass()
152
    {
153
        parent::setUpBeforeClass();
154
        $_FILES = [
155
            'imageFile' => [
156
                'name' => 'test.jpg',
157
                'type' => 'image/jpeg',
158
                'size' => 74463,
159
                'tmp_name' => __DIR__ . '/web/test.jpg',
160
                'error' => 0,
161
            ],
162
            'bigFile' => [
163
                'name' => 'test.jpeg',
164
                'type' => 'image/jpeg',
165
                'size' => 74463,
166
                'tmp_name' => __DIR__ . '/web/test.jpeg',
167
                'error' => 0,
168
            ],
169
        ];
170
    }
171
}
172