|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
19
|
|
|
use think\Validate; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class validateTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
public function testCheck() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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'), |
|
|
|
|
|
|
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;}); |
|
|
|
|
|
|
102
|
|
|
$validate->rule('zip', '/^\d{6}$/'); |
|
103
|
|
|
$validate->rule([ |
|
|
|
|
|
|
104
|
|
|
'ok' => 'require|accepted', |
|
105
|
|
|
'date' => 'date|dateFormat:y-m-d', |
|
106
|
|
|
]); |
|
|
|
|
|
|
107
|
|
|
$result = $validate->batch()->check($data); |
|
108
|
|
|
$this->assertEquals(true, $result); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testMsg() |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$validate = new Validate(); |
|
114
|
|
|
$validate->message('name.require', '名称必须'); |
|
115
|
|
|
$validate->message([ |
|
|
|
|
|
|
116
|
|
|
'name.require' => '名称必须', |
|
117
|
|
|
'name.max' => '名称最多不能超过25个字符', |
|
118
|
|
|
'age.number' => '年龄必须是数字', |
|
119
|
|
|
'age.between' => '年龄只能在1-120之间', |
|
120
|
|
|
'email' => '邮箱格式错误', |
|
121
|
|
|
]); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function testMake() |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testExtend() |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
$validate = new Validate(['name' => 'check:1']); |
|
144
|
|
|
$validate->extend('check', function ($value, $rule) {return $rule == $value ? true : false;}); |
|
|
|
|
|
|
145
|
|
|
$validate->extend(['check' => function ($value, $rule) {return $rule == $value ? true : false;}]); |
|
|
|
|
|
|
146
|
|
|
$data = ['name' => 1]; |
|
147
|
|
|
$result = $validate->check($data); |
|
148
|
|
|
$this->assertEquals(true, $result); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testScene() |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
$rule = [ |
|
154
|
|
|
'name' => 'require|max:25', |
|
155
|
|
|
'age' => 'number|between:1,120', |
|
156
|
|
|
'email' => 'email', |
|
157
|
|
|
]; |
|
158
|
|
|
$msg = [ |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|