Completed
Pull Request — 5.1 (#1327)
by
unknown
05:23
created

validateTest::testRule()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 61
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 56
nc 1
nop 0
dl 0
loc 61
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
12
/**
13
 * Validate类测试
14
 */
15
16
namespace tests\thinkphp\library\think;
17
18
use think\File;
0 ignored issues
show
Bug introduced by
The type think\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use think\Validate;
0 ignored issues
show
Bug introduced by
The type think\Validate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
class validateTest extends \PHPUnit_Framework_TestCase
1 ignored issue
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Missing class doc comment
Loading history...
Coding Style introduced by
Class name must begin with a capital letter
Loading history...
22
{
23
24
    public function testCheck()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
25
    {
26
        $rule = [
27
            'name'  => 'require|max:25',
28
            'age'   => 'number|between:1,120',
29
            'email' => 'email',
30
        ];
31
        $msg = [
32
            'name.require' => '名称必须',
33
            'name.max'     => '名称最多不能超过25个字符',
34
            'age.number'   => '年龄必须是数字',
35
            'age.between'  => '年龄只能在1-120之间',
36
            'email'        => '邮箱格式错误',
37
        ];
38
        $data = [
39
            'name'  => 'thinkphp',
40
            'age'   => 10,
41
            'email' => '[email protected]',
42
        ];
43
        $validate = new Validate($rule, $msg);
44
        $result   = $validate->check($data);
45
        $this->assertEquals(true, $result);
46
    }
47
48
    public function testRule()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
49
    {
50
        $rule = [
51
            'name'       => 'require|method:get|alphaNum|max:25|expire:2016-1-1,2026-1-1',
52
            'account'    => 'requireIf:name,thinkphp|alphaDash|min:4|length:4,30',
53
            'age'        => 'number|between:1,120',
54
            'email'      => 'requireWith:name|email',
55
            'host'       => 'activeUrl|activeUrl:A',
56
            'url'        => 'url',
57
            'ip'         => 'ip|ip:ipv4',
58
            'score'      => 'float|gt:60|notBetween:90,100|notIn:70,80|lt:100|elt:100|egt:60',
59
            'status'     => 'integer|in:0,1,2',
60
            'begin_time' => 'after:2016-3-18',
61
            'end_time'   => 'before:2016-10-01',
62
            'info'       => 'require|array|length:4|max:5|min:2',
63
            'info.name'  => 'require|length:8|alpha|same:thinkphp',
64
            'value'      => 'same:100|different:status',
65
            'bool'       => 'boolean',
66
            'title'      => 'chsAlpha',
67
            'city'       => 'chs',
68
            'nickname'   => 'chsDash',
69
            'aliasname'  => 'chsAlphaNum',
70
            'file'       => 'file|fileSize:20480',
71
            'image'      => 'image|fileMime:image/png|image:80,80,png',
72
            'test'       => 'test',
73
        ];
74
        $data = [
75
            'name'       => 'thinkphp',
76
            'account'    => 'liuchen',
77
            'age'        => 10,
78
            'email'      => '[email protected]',
79
            'host'       => 'thinkphp.cn',
80
            'url'        => 'http://thinkphp.cn/topic',
81
            'ip'         => '114.34.54.5',
82
            'score'      => '89.15',
83
            'status'     => 1,
84
            'begin_time' => '2016-3-20',
85
            'end_time'   => '2016-5-1',
86
            'info'       => [1, 2, 3, 'name' => 'thinkphp'],
87
            'zip'        => '200000',
88
            'date'       => '16-3-8',
89
            'ok'         => 'yes',
90
            'value'      => 100,
91
            'bool'       => true,
92
            'title'      => '流年ThinkPHP',
93
            'city'       => '上海',
94
            'nickname'   => '流年ThinkPHP_2016',
95
            'aliasname'  => '流年Think2016',
96
            'file'       => new File(THINK_PATH . 'base.php'),
0 ignored issues
show
Bug introduced by
The constant tests\thinkphp\library\think\THINK_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
97
            'image'      => new File(THINK_PATH . 'logo.png'),
98
            'test'       => 'test',
99
        ];
100
        $validate = new Validate($rule);
101
        $validate->extend('test', function ($value) {return 'test' == $value ? true : false;});
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
102
        $validate->rule('zip', '/^\d{6}$/');
103
        $validate->rule([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
104
            'ok'   => 'require|accepted',
105
            'date' => 'date|dateFormat:y-m-d',
106
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
107
        $result = $validate->batch()->check($data);
108
        $this->assertEquals(true, $result);
109
    }
110
111
    public function testMsg()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
112
    {
113
        $validate = new Validate();
114
        $validate->message('name.require', '名称必须');
115
        $validate->message([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
116
            'name.require' => '名称必须',
117
            'name.max'     => '名称最多不能超过25个字符',
118
            'age.number'   => '年龄必须是数字',
119
            'age.between'  => '年龄只能在1-120之间',
120
            'email'        => '邮箱格式错误',
121
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
122
    }
123
124
    public function testMake()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
125
    {
126
        $rule = [
127
            'name'  => 'require|max:25',
128
            'age'   => 'number|between:1,120',
129
            'email' => 'email',
130
        ];
131
        $msg = [
132
            'name.require' => '名称必须',
133
            'name.max'     => '名称最多不能超过25个字符',
134
            'age.number'   => '年龄必须是数字',
135
            'age.between'  => '年龄只能在1-120之间',
136
            'email'        => '邮箱格式错误',
137
        ];
138
        $validate = Validate::make($rule, $msg);
0 ignored issues
show
Unused Code introduced by
The assignment to $validate is dead and can be removed.
Loading history...
139
    }
140
141
    public function testExtend()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
142
    {
143
        $validate = new Validate(['name' => 'check:1']);
144
        $validate->extend('check', function ($value, $rule) {return $rule == $value ? true : false;});
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
145
        $validate->extend(['check' => function ($value, $rule) {return $rule == $value ? true : false;}]);
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
146
        $data   = ['name' => 1];
147
        $result = $validate->check($data);
148
        $this->assertEquals(true, $result);
149
    }
150
151
    public function testScene()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
152
    {
153
        $rule = [
154
            'name'  => 'require|max:25',
155
            'age'   => 'number|between:1,120',
156
            'email' => 'email',
157
        ];
158
        $msg = [
0 ignored issues
show
Unused Code introduced by
The assignment to $msg is dead and can be removed.
Loading history...
159
            'name.require' => '名称必须',
160
            'name.max'     => '名称最多不能超过25个字符',
161
            'age.number'   => '年龄必须是数字',
162
            'age.between'  => '年龄只能在1-120之间',
163
            'email'        => '邮箱格式错误',
164
        ];
165
        $data = [
166
            'name'  => 'thinkphp',
167
            'age'   => 10,
168
            'email' => '[email protected]',
169
        ];
170
        $validate = new Validate($rule);
171
        $validate->scene(['edit' => ['name', 'age']]);
172
        $validate->scene('edit', ['name', 'age']);
173
        $validate->scene('edit');
174
        $result = $validate->check($data);
175
        $this->assertEquals(true, $result);
176
    }
177
178
    public function testSetTypeMsg()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
179
    {
180
        $rule = [
181
            'name|名称' => 'require|max:25',
182
            'age'     => 'number|between:1,120',
183
            'email'   => 'email',
184
            ['sex', 'in:1,2', '性别错误'],
185
        ];
186
        $data = [
187
            'name'  => '',
188
            'age'   => 10,
189
            'email' => '[email protected]',
190
            'sex'   => '3',
191
        ];
192
        $validate = new Validate($rule);
193
        $validate->setTypeMsg('require', ':attribute必须');
194
        $validate->setTypeMsg(['require' => ':attribute必须']);
195
        $result = $validate->batch()->check($data);
196
        $this->assertFalse($result);
197
        $this->assertEquals(['name' => '名称必须', 'sex' => '性别错误'], $validate->getError());
198
    }
199
200
}
201