|
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
|
|
|
* 控制器测试 |
|
14
|
|
|
* @author Haotong Lin <[email protected]> |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace tests\thinkphp\library\think; |
|
18
|
|
|
|
|
19
|
|
|
use ReflectionClass; |
|
20
|
|
|
use think\Controller; |
|
|
|
|
|
|
21
|
|
|
use think\Request; |
|
|
|
|
|
|
22
|
|
|
use think\View; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
require_once CORE_PATH . '../../helper.php'; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class Foo extends Controller |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
public $test = 'test'; |
|
29
|
|
|
|
|
30
|
|
|
public function _initialize() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$this->test = 'abcd'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function assignTest() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$this->assign('abcd', 'dcba'); |
|
38
|
|
|
$this->assign(['key1' => 'value1', 'key2' => 'value2']); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function fetchTest() |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$template = APP_PATH . 'views' . DS .'display.html'; |
|
|
|
|
|
|
44
|
|
|
return $this->fetch($template, ['name' => 'ThinkPHP']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function displayTest() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$template = APP_PATH . 'views' . DS .'display.html'; |
|
|
|
|
|
|
50
|
|
|
return $this->display($template, ['name' => 'ThinkPHP']); |
|
51
|
|
|
} |
|
52
|
|
|
public function test() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$data = [ |
|
55
|
|
|
'username' => 'username', |
|
56
|
|
|
'nickname' => 'nickname', |
|
57
|
|
|
'password' => '123456', |
|
58
|
|
|
'repassword' => '123456', |
|
59
|
|
|
'email' => '[email protected]', |
|
60
|
|
|
'sex' => '0', |
|
61
|
|
|
'age' => '20', |
|
62
|
|
|
'code' => '1234', |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
$validate = [ |
|
66
|
|
|
['username', 'length:5,15', '用户名长度为5到15个字符'], |
|
67
|
|
|
['nickname', 'require', '请填昵称'], |
|
68
|
|
|
['password', '[\w-]{6,15}', '密码长度为6到15个字符'], |
|
69
|
|
|
['repassword', 'confirm:password', '两次密码不一到致'], |
|
70
|
|
|
['email', 'filter:validate_email', '邮箱格式错误'], |
|
71
|
|
|
['sex', 'in:0,1', '性别只能为为男或女'], |
|
72
|
|
|
['age', 'between:1,80', '年龄只能在10-80之间'], |
|
73
|
|
|
]; |
|
74
|
|
|
return $this->validate($data, $validate); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
class Bar extends Controller |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
public $test = 1; |
|
81
|
|
|
|
|
82
|
|
|
public $beforeActionList = ['action1', 'action2']; |
|
83
|
|
|
|
|
84
|
|
|
public function action1() |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
$this->test += 2; |
|
87
|
|
|
return 'action1'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function action2() |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$this->test += 4; |
|
93
|
|
|
return 'action2'; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
class Baz extends Controller |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
public $test = 1; |
|
100
|
|
|
|
|
101
|
|
|
public $beforeActionList = [ |
|
102
|
|
|
'action1' => ['only' => 'index'], |
|
103
|
|
|
'action2' => ['except' => 'index'], |
|
104
|
|
|
'action3' => ['only' => 'abcd'], |
|
105
|
|
|
'action4' => ['except' => 'abcd'], |
|
106
|
|
|
]; |
|
107
|
|
|
|
|
108
|
|
|
public function action1() |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$this->test += 2; |
|
111
|
|
|
return 'action1'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function action2() |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$this->test += 4; |
|
117
|
|
|
return 'action2'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function action3() |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
$this->test += 8; |
|
123
|
|
|
return 'action2'; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function action4() |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
$this->test += 16; |
|
129
|
|
|
return 'action2'; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
class controllerTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
public function testInitialize() |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
$foo = new Foo(Request::instance()); |
|
138
|
|
|
$this->assertEquals('abcd', $foo->test); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testBeforeAction() |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
$obj = new Bar(Request::instance()); |
|
144
|
|
|
$this->assertEquals(7, $obj->test); |
|
145
|
|
|
|
|
146
|
|
|
$obj = new Baz(Request::instance()); |
|
147
|
|
|
$this->assertEquals(19, $obj->test); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
private function getView($controller) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$view = new View(); |
|
153
|
|
|
$rc = new ReflectionClass(get_class($controller)); |
|
154
|
|
|
$property = $rc->getProperty('view'); |
|
155
|
|
|
$property->setAccessible(true); |
|
156
|
|
|
$property->setValue($controller, $view); |
|
157
|
|
|
return $view; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testFetch() |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
$controller = new Foo(Request::instance()); |
|
163
|
|
|
$view = $this->getView($controller); |
|
164
|
|
|
$template = APP_PATH . 'views' . DS .'display.html'; |
|
|
|
|
|
|
165
|
|
|
$viewFetch = $view->fetch($template, ['name' => 'ThinkPHP']); |
|
166
|
|
|
$this->assertEquals($controller->fetchTest(), $viewFetch); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function testDisplay() |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
$controller = new Foo; |
|
172
|
|
|
$view = $this->getView($controller); |
|
173
|
|
|
$template = APP_PATH . 'views' . DS .'display.html'; |
|
|
|
|
|
|
174
|
|
|
$viewFetch = $view->display($template, ['name' => 'ThinkPHP']); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertEquals($controller->displayTest(), $viewFetch); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function testAssign() |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
$controller = new Foo(Request::instance()); |
|
182
|
|
|
$view = $this->getView($controller); |
|
183
|
|
|
$controller->assignTest(); |
|
184
|
|
|
$expect = ['abcd' => 'dcba', 'key1' => 'value1', 'key2' => 'value2']; |
|
185
|
|
|
$this->assertAttributeEquals($expect, 'data', $view); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function testValidate() |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
$controller = new Foo(Request::instance()); |
|
191
|
|
|
$result = $controller->test(); |
|
192
|
|
|
$this->assertTrue($result); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|