|
1
|
|
|
<?php |
|
2
|
|
|
namespace tests\thinkphp\library\traits\controller; |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
use think\Config; |
|
|
|
|
|
|
5
|
|
|
use think\Request; |
|
|
|
|
|
|
6
|
|
|
use think\Response; |
|
|
|
|
|
|
7
|
|
|
use think\response\Redirect; |
|
|
|
|
|
|
8
|
|
|
use think\View; |
|
|
|
|
|
|
9
|
|
|
use traits\controller\Jump; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class jumpTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
|
|
|
|
|
14
|
|
|
* @var testClassWithJump |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $testClass; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
|
|
|
|
|
19
|
|
|
* @var Request |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $request; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
|
|
|
|
|
24
|
|
|
* @var mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $originServerData; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$this->testClass = new testClassWithJump(); |
|
31
|
|
|
$this->request = Request::create(''); |
|
32
|
|
|
|
|
33
|
|
|
$this->originServerData = Request::instance()->server(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function tearDown() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
Request::instance()->server($this->originServerData); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
|
|
|
|
|
42
|
|
|
* @dataProvider provideTestSuccess |
|
43
|
|
|
*/ |
|
|
|
|
|
|
44
|
|
|
public function testSuccess($arguments, $expected, array $extra) |
|
45
|
|
|
{ |
|
46
|
|
|
if (isset($extra['server'])) { |
|
47
|
|
|
$this->request->server($extra['server']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$mock = $this->getMockBuilder(get_class($this->testClass))->setMethods(['getResponseType'])->getMock(); |
|
51
|
|
|
$mock->expects($this->any())->method('getResponseType')->willReturn($extra['return']); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
call_user_func_array([$mock, 'success'], $arguments); |
|
55
|
|
|
$this->setExpectedException('\think\exception\HttpResponseException'); |
|
56
|
|
|
} catch (\Exception $e) { |
|
57
|
|
|
$this->assertInstanceOf('\think\exception\HttpResponseException', $e); |
|
58
|
|
|
|
|
59
|
|
|
/** @var Response $response */ |
|
|
|
|
|
|
60
|
|
|
$response = $e->getResponse(); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$this->assertInstanceOf('\Think\Response', $response); |
|
63
|
|
|
$this->assertEquals($expected['header'], $response->getHeader()); |
|
64
|
|
|
$this->assertEquals($expected['data'], $response->getData()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
|
|
|
|
|
69
|
|
|
* @dataProvider provideTestError |
|
70
|
|
|
*/ |
|
|
|
|
|
|
71
|
|
|
public function testError($arguments, $expected, array $extra) |
|
72
|
|
|
{ |
|
73
|
|
|
if (isset($extra['server'])) { |
|
74
|
|
|
$this->request->server($extra['server']); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$mock = $this->getMockBuilder(get_class($this->testClass))->setMethods(['getResponseType'])->getMock(); |
|
78
|
|
|
$mock->expects($this->any())->method('getResponseType')->willReturn($extra['return']); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
call_user_func_array([$mock, 'error'], $arguments); |
|
82
|
|
|
$this->setExpectedException('\think\exception\HttpResponseException'); |
|
83
|
|
|
} catch (\Exception $e) { |
|
84
|
|
|
$this->assertInstanceOf('\think\exception\HttpResponseException', $e); |
|
85
|
|
|
|
|
86
|
|
|
/** @var Response $response */ |
|
|
|
|
|
|
87
|
|
|
$response = $e->getResponse(); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertInstanceOf('\Think\Response', $response); |
|
90
|
|
|
$this->assertEquals($expected['header'], $response->getHeader()); |
|
91
|
|
|
$this->assertEquals($expected['data'], $response->getData()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
|
|
|
|
|
96
|
|
|
* @dataProvider provideTestResult |
|
97
|
|
|
*/ |
|
|
|
|
|
|
98
|
|
|
public function testResult($arguments, $expected, array $extra) |
|
99
|
|
|
{ |
|
100
|
|
|
if (isset($extra['server'])) { |
|
101
|
|
|
$this->request->server($extra['server']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$mock = $this->getMockBuilder(get_class($this->testClass))->setMethods(['getResponseType'])->getMock(); |
|
105
|
|
|
$mock->expects($this->any())->method('getResponseType')->willReturn($extra['return']); |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
call_user_func_array([$mock, 'result'], $arguments); |
|
109
|
|
|
$this->setExpectedException('\think\exception\HttpResponseException'); |
|
110
|
|
|
} catch (\Exception $e) { |
|
111
|
|
|
$this->assertInstanceOf('\think\exception\HttpResponseException', $e); |
|
112
|
|
|
|
|
113
|
|
|
/** @var Response $response */ |
|
|
|
|
|
|
114
|
|
|
$response = $e->getResponse(); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertInstanceOf('\Think\Response', $response); |
|
117
|
|
|
$this->assertEquals($expected['header'], $response->getHeader()); |
|
118
|
|
|
$this->assertEquals($expected['data'], $response->getData()); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
|
|
|
|
|
123
|
|
|
* @dataProvider provideTestRedirect |
|
124
|
|
|
*/ |
|
|
|
|
|
|
125
|
|
|
public function testRedirect($arguments, $expected) |
|
126
|
|
|
{ |
|
127
|
|
|
try { |
|
128
|
|
|
call_user_func_array([$this->testClass, 'redirect'], $arguments); |
|
129
|
|
|
$this->setExpectedException('\think\exception\HttpResponseException'); |
|
130
|
|
|
} catch (\Exception $e) { |
|
131
|
|
|
$this->assertInstanceOf('\think\exception\HttpResponseException', $e); |
|
132
|
|
|
|
|
133
|
|
|
/** @var Redirect $response */ |
|
|
|
|
|
|
134
|
|
|
$response = $e->getResponse(); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertInstanceOf('\think\response\Redirect', $response); |
|
137
|
|
|
$this->assertEquals($expected['url'], $response->getTargetUrl()); |
|
138
|
|
|
$this->assertEquals($expected['code'], $response->getCode()); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testGetResponseType() |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
Request::instance()->server(['HTTP_X_REQUESTED_WITH' => null]); |
|
145
|
|
|
$this->assertEquals('html', $this->testClass->getResponseType()); |
|
146
|
|
|
|
|
147
|
|
|
Request::instance()->server(['HTTP_X_REQUESTED_WITH' => true]); |
|
148
|
|
|
$this->assertEquals('html', $this->testClass->getResponseType()); |
|
149
|
|
|
|
|
150
|
|
|
Request::instance()->server(['HTTP_X_REQUESTED_WITH' => 'xmlhttprequest']); |
|
151
|
|
|
$this->assertEquals('json', $this->testClass->getResponseType()); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function provideTestSuccess() |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
$provideData = []; |
|
157
|
|
|
|
|
158
|
|
|
$arguments = ['', null, '', 3, []]; |
|
159
|
|
|
$expected = [ |
|
160
|
|
|
'header' => [ |
|
161
|
|
|
'Content-Type' => 'text/html; charset=utf-8' |
|
162
|
|
|
], |
|
163
|
|
|
'data' => View::instance(Config::get('template'), Config::get('view_replace_str')) |
|
164
|
|
|
->fetch(Config::get('dispatch_error_tmpl'), [ |
|
|
|
|
|
|
165
|
|
|
'code' => 1, |
|
166
|
|
|
'msg' => '', |
|
167
|
|
|
'data' => '', |
|
168
|
|
|
'url' => '/index.php/', |
|
169
|
|
|
'wait' => 3, |
|
170
|
|
|
]) |
|
|
|
|
|
|
171
|
|
|
]; |
|
172
|
|
|
$provideData[] = [$arguments, $expected, ['server' => ['HTTP_REFERER' => null], 'return' => 'html']]; |
|
173
|
|
|
|
|
174
|
|
|
$arguments = ['thinkphp', null, ['foo'], 4, ['Power-By' => 'thinkphp', 'Content-Type' => 'text/html; charset=gbk']]; |
|
175
|
|
|
$expected = [ |
|
176
|
|
|
'header' => [ |
|
177
|
|
|
'Content-Type' => 'text/html; charset=gbk', |
|
178
|
|
|
'Power-By' => 'thinkphp' |
|
179
|
|
|
], |
|
180
|
|
|
'data' => View::instance(Config::get('template'), Config::get('view_replace_str')) |
|
181
|
|
|
->fetch(Config::get('dispatch_error_tmpl'), [ |
|
|
|
|
|
|
182
|
|
|
'code' => 1, |
|
183
|
|
|
'msg' => 'thinkphp', |
|
184
|
|
|
'data' => ['foo'], |
|
185
|
|
|
'url' => 'http://www.thinkphp.cn', |
|
186
|
|
|
'wait' => 4, |
|
187
|
|
|
]) |
|
|
|
|
|
|
188
|
|
|
]; |
|
189
|
|
|
$provideData[] = [$arguments, $expected, ['server' => ['HTTP_REFERER' => 'http://www.thinkphp.cn'], 'return' => 'html']]; |
|
190
|
|
|
|
|
191
|
|
|
$arguments = ['thinkphp', 'index', ['foo'], 5, []]; |
|
192
|
|
|
$expected = [ |
|
193
|
|
|
'header' => [ |
|
194
|
|
|
'Content-Type' => 'application/json; charset=utf-8' |
|
195
|
|
|
], |
|
196
|
|
|
'data' => [ |
|
197
|
|
|
'code' => 1, |
|
198
|
|
|
'msg' => 'thinkphp', |
|
199
|
|
|
'data' => ['foo'], |
|
200
|
|
|
'url' => '/index.php/index.html', |
|
201
|
|
|
'wait' => 5, |
|
202
|
|
|
] |
|
203
|
|
|
]; |
|
204
|
|
|
$provideData[] = [$arguments, $expected, ['server' => ['HTTP_REFERER' => null], 'return' => 'json']]; |
|
205
|
|
|
|
|
206
|
|
|
return $provideData; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function provideTestError() |
|
|
|
|
|
|
210
|
|
|
{ |
|
211
|
|
|
$provideData = []; |
|
212
|
|
|
|
|
213
|
|
|
$arguments = ['', null, '', 3, []]; |
|
214
|
|
|
$expected = [ |
|
215
|
|
|
'header' => [ |
|
216
|
|
|
'Content-Type' => 'text/html; charset=utf-8' |
|
217
|
|
|
], |
|
218
|
|
|
'data' => View::instance(Config::get('template'), Config::get('view_replace_str')) |
|
219
|
|
|
->fetch(Config::get('dispatch_error_tmpl'), [ |
|
|
|
|
|
|
220
|
|
|
'code' => 0, |
|
221
|
|
|
'msg' => '', |
|
222
|
|
|
'data' => '', |
|
223
|
|
|
'url' => 'javascript:history.back(-1);', |
|
224
|
|
|
'wait' => 3, |
|
225
|
|
|
]) |
|
|
|
|
|
|
226
|
|
|
]; |
|
227
|
|
|
$provideData[] = [$arguments, $expected, ['return' => 'html']]; |
|
228
|
|
|
|
|
229
|
|
|
$arguments = ['thinkphp', 'http://www.thinkphp.cn', ['foo'], 4, ['Power-By' => 'thinkphp', 'Content-Type' => 'text/html; charset=gbk']]; |
|
230
|
|
|
$expected = [ |
|
231
|
|
|
'header' => [ |
|
232
|
|
|
'Content-Type' => 'text/html; charset=gbk', |
|
233
|
|
|
'Power-By' => 'thinkphp' |
|
234
|
|
|
], |
|
235
|
|
|
'data' => View::instance(Config::get('template'), Config::get('view_replace_str')) |
|
236
|
|
|
->fetch(Config::get('dispatch_error_tmpl'), [ |
|
|
|
|
|
|
237
|
|
|
'code' => 0, |
|
238
|
|
|
'msg' => 'thinkphp', |
|
239
|
|
|
'data' => ['foo'], |
|
240
|
|
|
'url' => 'http://www.thinkphp.cn', |
|
241
|
|
|
'wait' => 4, |
|
242
|
|
|
]) |
|
|
|
|
|
|
243
|
|
|
]; |
|
244
|
|
|
$provideData[] = [$arguments, $expected, ['return' => 'html']]; |
|
245
|
|
|
|
|
246
|
|
|
$arguments = ['thinkphp', '', ['foo'], 5, []]; |
|
247
|
|
|
$expected = [ |
|
248
|
|
|
'header' => [ |
|
249
|
|
|
'Content-Type' => 'application/json; charset=utf-8' |
|
250
|
|
|
], |
|
251
|
|
|
'data' => [ |
|
252
|
|
|
'code' => 0, |
|
253
|
|
|
'msg' => 'thinkphp', |
|
254
|
|
|
'data' => ['foo'], |
|
255
|
|
|
'url' => '', |
|
256
|
|
|
'wait' => 5, |
|
257
|
|
|
] |
|
258
|
|
|
]; |
|
259
|
|
|
$provideData[] = [$arguments, $expected, ['return' => 'json']]; |
|
260
|
|
|
|
|
261
|
|
|
return $provideData; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function provideTestResult() |
|
|
|
|
|
|
265
|
|
|
{ |
|
266
|
|
|
$provideData = []; |
|
267
|
|
|
|
|
268
|
|
|
$arguments = [null, 0, '', '', []]; |
|
269
|
|
|
$expected = [ |
|
270
|
|
|
'header' => [ |
|
271
|
|
|
'Content-Type' => 'text/html; charset=utf-8' |
|
272
|
|
|
], |
|
273
|
|
|
'data' => [ |
|
274
|
|
|
'code' => 0, |
|
275
|
|
|
'msg' => '', |
|
276
|
|
|
'time' => Request::create('')->server('REQUEST_TIME'), |
|
277
|
|
|
'data' => null, |
|
278
|
|
|
] |
|
279
|
|
|
]; |
|
280
|
|
|
$provideData[] = [$arguments, $expected, ['return' => 'html']]; |
|
281
|
|
|
|
|
282
|
|
|
$arguments = [['foo'], 200, 'thinkphp', 'json', ['Power-By' => 'thinkphp']]; |
|
283
|
|
|
$expected = [ |
|
284
|
|
|
'header' => [ |
|
285
|
|
|
'Power-By' => 'thinkphp', |
|
286
|
|
|
'Content-Type' => 'application/json; charset=utf-8' |
|
287
|
|
|
], |
|
288
|
|
|
'data' => [ |
|
289
|
|
|
'code' => 200, |
|
290
|
|
|
'msg' => 'thinkphp', |
|
291
|
|
|
'time' => 1000, |
|
292
|
|
|
'data' => ['foo'], |
|
293
|
|
|
] |
|
294
|
|
|
]; |
|
295
|
|
|
|
|
296
|
|
|
$provideData[] = [$arguments, $expected, ['server' => ['REQUEST_TIME' => 1000], 'return' => 'json']]; |
|
297
|
|
|
|
|
298
|
|
|
return $provideData; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function provideTestRedirect() |
|
|
|
|
|
|
302
|
|
|
{ |
|
303
|
|
|
$provideData = []; |
|
304
|
|
|
|
|
305
|
|
|
$arguments = ['', [], 302, []]; |
|
306
|
|
|
$expected = [ |
|
307
|
|
|
'code'=> 302, |
|
308
|
|
|
'url' => '/index.php/' |
|
309
|
|
|
]; |
|
310
|
|
|
$provideData[] = [$arguments, $expected, []]; |
|
311
|
|
|
|
|
312
|
|
|
$arguments = ['index', 302, null, []]; |
|
313
|
|
|
$expected = [ |
|
314
|
|
|
'code'=> 302, |
|
315
|
|
|
'url' => '/index.php/index.html' |
|
316
|
|
|
]; |
|
317
|
|
|
$provideData[] = [$arguments, $expected, []]; |
|
318
|
|
|
|
|
319
|
|
|
$arguments = ['http://www.thinkphp.cn', 301, 302, []]; |
|
320
|
|
|
$expected = [ |
|
321
|
|
|
'code'=> 301, |
|
322
|
|
|
'url' => 'http://www.thinkphp.cn' |
|
323
|
|
|
]; |
|
324
|
|
|
$provideData[] = [$arguments, $expected, []]; |
|
325
|
|
|
|
|
326
|
|
|
return $provideData; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
class testClassWithJump |
|
|
|
|
|
|
331
|
|
|
{ |
|
332
|
|
|
use Jump { |
|
333
|
|
|
success as public; |
|
334
|
|
|
error as public; |
|
335
|
|
|
result as public; |
|
336
|
|
|
redirect as public; |
|
337
|
|
|
getResponseType as public; |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|