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

jumpTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 178
dl 0
loc 316
rs 10
c 0
b 0
f 0
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 3 1
A provideTestError() 0 53 1
A testResult() 0 21 3
A provideTestRedirect() 0 26 1
A setUp() 0 6 1
A testRedirect() 0 14 2
A testGetResponseType() 0 10 1
A testSuccess() 0 21 3
A provideTestResult() 0 35 1
A provideTestSuccess() 0 53 1
A testError() 0 21 3
1
<?php
2
namespace tests\thinkphp\library\traits\controller;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use think\Config;
0 ignored issues
show
Bug introduced by
The type think\Config 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...
5
use think\Request;
0 ignored issues
show
Bug introduced by
The type think\Request 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...
6
use think\Response;
0 ignored issues
show
Bug introduced by
The type think\Response 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...
7
use think\response\Redirect;
0 ignored issues
show
Bug introduced by
The type think\response\Redirect 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...
8
use think\View;
0 ignored issues
show
Bug introduced by
The type think\View 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...
9
use traits\controller\Jump;
0 ignored issues
show
Bug introduced by
The type traits\controller\Jump 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...
10
11
class jumpTest 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...
12
{
13
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
14
     * @var testClassWithJump
15
     */
16
    protected $testClass;
17
18
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
19
     * @var Request
20
     */
21
    protected $request;
22
23
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
     * @var mixed
25
     */
26
    protected $originServerData;
27
28
    public function setUp()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
29
    {
30
        $this->testClass = new testClassWithJump();
31
        $this->request   = Request::create('');
32
33
        $this->originServerData = Request::instance()->server();
34
    }
35
36
    public function tearDown()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
37
    {
38
        Request::instance()->server($this->originServerData);
39
    }
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $extra should have a doc-comment as per coding-style.
Loading history...
42
     * @dataProvider provideTestSuccess
43
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
60
            $response = $e->getResponse();
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on Exception. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            /** @scrutinizer ignore-call */ 
61
            $response = $e->getResponse();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $extra should have a doc-comment as per coding-style.
Loading history...
69
     * @dataProvider provideTestError
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $extra should have a doc-comment as per coding-style.
Loading history...
96
     * @dataProvider provideTestResult
97
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
123
     * @dataProvider provideTestRedirect
124
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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'), [
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...
165
                    'code' => 1,
166
                    'msg'  => '',
167
                    'data' => '',
168
                    'url'  => '/index.php/',
169
                    'wait' => 3,
170
                ])
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...
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'), [
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...
182
                    'code' => 1,
183
                    'msg'  => 'thinkphp',
184
                    'data' => ['foo'],
185
                    'url'  => 'http://www.thinkphp.cn',
186
                    'wait' => 4,
187
                ])
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...
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()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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'), [
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...
220
                    'code' => 0,
221
                    'msg'  => '',
222
                    'data' => '',
223
                    'url'  => 'javascript:history.back(-1);',
224
                    'wait' => 3,
225
                ])
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...
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'), [
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...
237
                    'code' => 0,
238
                    'msg'  => 'thinkphp',
239
                    'data' => ['foo'],
240
                    'url'  => 'http://www.thinkphp.cn',
241
                    'wait' => 4,
242
                ])
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...
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()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
Coding Style introduced by
Class name must begin with a capital letter
Loading history...
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