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

routeTest::testCheckRouteGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 41
nc 1
nop 0
dl 0
loc 48
rs 9.264
c 0
b 0
f 0
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
 * Route测试
14
 * @author    liu21st <[email protected]>
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 4
Loading history...
15
 */
16
17
namespace tests\thinkphp\library\think;
18
19
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...
20
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...
21
use think\Route;
0 ignored issues
show
Bug introduced by
The type think\Route 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...
22
23
class routeTest 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...
24
{
25
26
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
27
    {
28
        Config::set('app_multi_module', true);
29
    }
30
31
    public function testRegister()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
32
    {
33
        $request = Request::instance();
34
        Route::get('hello/:name', 'index/hello');
35
        Route::get(['hello/:name' => 'index/hello']);
36
        Route::post('hello/:name', 'index/post');
37
        Route::put('hello/:name', 'index/put');
38
        Route::delete('hello/:name', 'index/delete');
39
        Route::patch('hello/:name', 'index/patch');
40
        Route::any('user/:id', 'index/user');
41
        $result = Route::check($request, 'hello/thinkphp');
42
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
43
        $this->assertEquals(['hello' => true, 'user/:id' => true, 'hello/:name' => ['rule' => 'hello/:name', 'route' => 'index/hello', 'var' => ['name' => 1], 'option' => [], 'pattern' => []]], Route::rules('GET'));
44
        Route::rule('type1/:name', 'index/type', 'PUT|POST');
45
        Route::rule(['type2/:name' => 'index/type1']);
46
        Route::rule([['type3/:name', 'index/type2', ['method' => 'POST']]]);
47
        Route::rule(['name', 'type4/:name'], 'index/type4');
48
    }
49
50
    public function testImport()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
51
    {
52
        $rule = [
53
            '__domain__' => ['subdomain2.thinkphp.cn' => 'blog1'],
54
            '__alias__'  => ['blog1' => 'blog1'],
55
            '__rest__'   => ['res' => ['index/blog']],
56
            'bbb'        => ['index/blog1', ['method' => 'POST']],
57
            'ddd'        => '',
58
            ['hello1/:ddd', 'index/hello1', ['method' => 'POST']],
59
        ];
60
        Route::import($rule);
61
    }
62
63
    public function testResource()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
64
    {
65
        $request = Request::instance();
66
        Route::resource('res', 'index/blog');
67
        Route::resource(['res' => ['index/blog']]);
68
        $result = Route::check($request, 'res');
69
        $this->assertEquals(['index', 'blog', 'index'], $result['module']);
70
        $result = Route::check($request, 'res/create');
71
        $this->assertEquals(['index', 'blog', 'create'], $result['module']);
72
        $result = Route::check($request, 'res/8');
73
        $this->assertEquals(['index', 'blog', 'read'], $result['module']);
74
        $result = Route::check($request, 'res/8/edit');
75
        $this->assertEquals(['index', 'blog', 'edit'], $result['module']);
76
77
        Route::resource('blog.comment', 'index/comment');
78
        $result = Route::check($request, 'blog/8/comment/10');
79
        $this->assertEquals(['index', 'comment', 'read'], $result['module']);
80
        $result = Route::check($request, 'blog/8/comment/10/edit');
81
        $this->assertEquals(['index', 'comment', 'edit'], $result['module']);
82
83
    }
84
85
    public function testRest()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
86
    {
87
        $request = Request::instance();
88
        Route::rest('read', ['GET', '/:id', 'look']);
89
        Route::rest('create', ['GET', '/create', 'add']);
90
        Route::rest(['read' => ['GET', '/:id', 'look'], 'create' => ['GET', '/create', 'add']]);
91
        Route::resource('res', 'index/blog');
92
        $result = Route::check($request, 'res/create');
93
        $this->assertEquals(['index', 'blog', 'add'], $result['module']);
94
        $result = Route::check($request, 'res/8');
95
        $this->assertEquals(['index', 'blog', 'look'], $result['module']);
96
97
    }
98
99
    public function testMixVar()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
100
    {
101
        $request = Request::instance();
102
        Route::get('hello-<name>', 'index/hello', [], ['name' => '\w+']);
103
        $result = Route::check($request, 'hello-thinkphp');
104
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
105
        Route::get('hello-<name><id?>', 'index/hello', [], ['name' => '\w+', 'id' => '\d+']);
106
        $result = Route::check($request, 'hello-thinkphp2016');
107
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
108
        Route::get('hello-<name>/[:id]', 'index/hello', [], ['name' => '\w+', 'id' => '\d+']);
109
        $result = Route::check($request, 'hello-thinkphp/2016');
110
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
111
    }
112
113
    public function testParseUrl()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
114
    {
115
        $result = Route::parseUrl('hello');
116
        $this->assertEquals(['hello', null, null], $result['module']);
117
        $result = Route::parseUrl('index/hello');
118
        $this->assertEquals(['index', 'hello', null], $result['module']);
119
        $result = Route::parseUrl('index/hello?name=thinkphp');
120
        $this->assertEquals(['index', 'hello', null], $result['module']);
121
        $result = Route::parseUrl('index/user/hello');
122
        $this->assertEquals(['index', 'user', 'hello'], $result['module']);
123
        $result = Route::parseUrl('index/user/hello/name/thinkphp');
124
        $this->assertEquals(['index', 'user', 'hello'], $result['module']);
125
        $result = Route::parseUrl('index-index-hello', '-');
126
        $this->assertEquals(['index', 'index', 'hello'], $result['module']);
127
    }
128
129
    public function testCheckRoute()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
130
    {
131
        Route::get('hello/:name', 'index/hello');
132
        Route::get('blog/:id', 'blog/read', [], ['id' => '\d+']);
133
        $request = Request::instance();
134
        $this->assertEquals(false, Route::check($request, 'test/thinkphp'));
135
        $this->assertEquals(false, Route::check($request, 'blog/thinkphp'));
136
        $result = Route::check($request, 'blog/5');
137
        $this->assertEquals([null, 'blog', 'read'], $result['module']);
138
        $result = Route::check($request, 'hello/thinkphp/abc/test');
139
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
140
    }
141
142
    public function testCheckRouteGroup()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
143
    {
144
        $request = Request::instance();
145
        Route::pattern(['id' => '\d+']);
146
        Route::pattern('name', '\w{6,25}');
147
        Route::group('group', [':id' => 'index/hello', ':name' => 'index/say']);
148
        $this->assertEquals(false, Route::check($request, 'empty/think'));
149
        $result = Route::check($request, 'group/think');
150
        $this->assertEquals(false, $result['module']);
151
        $result = Route::check($request, 'group/10');
152
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
153
        $result = Route::check($request, 'group/thinkphp');
154
        $this->assertEquals([null, 'index', 'say'], $result['module']);
155
        Route::group('group2', function () {
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...
156
            Route::group('group3', [':id' => 'index/hello', ':name' => 'index/say']);
157
            Route::rule(':name', 'index/hello');
158
            Route::auto('index');
159
        });
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...
160
        $result = Route::check($request, 'group2/thinkphp');
161
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
162
        $result = Route::check($request, 'group2/think');
163
        $this->assertEquals(['index', 'group2', 'think'], $result['module']);
164
        $result = Route::check($request, 'group2/group3/thinkphp');
165
        $this->assertEquals([null, 'index', 'say'], $result['module']);
166
        Route::group('group4', function () {
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...
167
            Route::group('group3', [':id' => 'index/hello', ':name' => 'index/say']);
168
            Route::rule(':name', 'index/hello');
169
            Route::miss('index/__miss__');
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
        $result = Route::check($request, 'group4/thinkphp');
172
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
173
        $result = Route::check($request, 'group4/think');
174
        $this->assertEquals([null, 'index', '__miss__'], $result['module']);
175
176
        Route::group(['prefix' => 'prefix/'], function () {
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...
177
            Route::rule('hello4/:name', 'hello');
178
        });
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...
179
        Route::group(['prefix' => 'prefix/'], [
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...
180
            'hello4/:name' => 'hello',
181
        ]);
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...
182
        $result = Route::check($request, 'hello4/thinkphp');
183
        $this->assertEquals([null, 'prefix', 'hello'], $result['module']);
184
        Route::group('group5', [
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...
185
            [':name', 'hello', ['method' => 'GET|POST']],
186
            ':id' => 'hello',
187
        ], ['prefix' => 'prefix/']);
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
        $result = Route::check($request, 'group5/thinkphp');
189
        $this->assertEquals([null, 'prefix', 'hello'], $result['module']);
190
    }
191
192
    public function testControllerRoute()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
193
    {
194
        $request = Request::instance();
195
        Route::controller('controller', 'index/Blog');
196
        $result = Route::check($request, 'controller/info');
197
        $this->assertEquals(['index', 'Blog', 'getinfo'], $result['module']);
198
        Route::setMethodPrefix('GET', 'read');
199
        Route::setMethodPrefix(['get' => 'read']);
200
        Route::controller('controller', 'index/Blog');
201
        $result = Route::check($request, 'controller/phone');
202
        $this->assertEquals(['index', 'Blog', 'readphone'], $result['module']);
203
    }
204
205
    public function testAliasRoute()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
206
    {
207
        $request = Request::instance();
208
        Route::alias('alias', 'index/Alias');
209
        $result = Route::check($request, 'alias/info');
210
        $this->assertEquals('index/Alias/info', $result['module']);
211
    }
212
213
    public function testRouteToModule()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
214
    {
215
        $request = Request::instance();
216
        Route::get('hello/:name', 'index/hello');
217
        Route::get('blog/:id', 'blog/read', [], ['id' => '\d+']);
218
        $this->assertEquals(false, Route::check($request, 'test/thinkphp'));
219
        $this->assertEquals(false, Route::check($request, 'blog/thinkphp'));
220
        $result = Route::check($request, 'hello/thinkphp');
221
        $this->assertEquals([null, 'index', 'hello'], $result['module']);
222
        $result = Route::check($request, 'blog/5');
223
        $this->assertEquals([null, 'blog', 'read'], $result['module']);
224
    }
225
226
    public function testRouteToController()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
227
    {
228
        $request = Request::instance();
229
        Route::get('say/:name', '@index/hello');
230
        $this->assertEquals(['type' => 'controller', 'controller' => 'index/hello', 'var' => []], Route::check($request, 'say/thinkphp'));
231
    }
232
233
    public function testRouteToMethod()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
234
    {
235
        $request = Request::instance();
236
        Route::get('user/:name', '\app\index\service\User::get', [], ['name' => '\w+']);
237
        Route::get('info/:name', '\app\index\model\Info@getInfo', [], ['name' => '\w+']);
238
        $this->assertEquals(['type' => 'method', 'method' => '\app\index\service\User::get', 'var' => []], Route::check($request, 'user/thinkphp'));
239
        $this->assertEquals(['type' => 'method', 'method' => ['\app\index\model\Info', 'getInfo'], 'var' => []], Route::check($request, 'info/thinkphp'));
240
    }
241
242
    public function testRouteToRedirect()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
243
    {
244
        $request = Request::instance();
245
        Route::get('art/:id', '/article/read/id/:id', [], ['id' => '\d+']);
246
        $this->assertEquals(['type' => 'redirect', 'url' => '/article/read/id/8', 'status' => 301], Route::check($request, 'art/8'));
247
    }
248
249
    public function testBind()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
250
    {
251
        $request = Request::instance();
252
        Route::bind('index/blog');
253
        Route::get('blog/:id', 'index/blog/read');
254
        $result = Route::check($request, 'blog/10');
255
        $this->assertEquals(['index', 'blog', 'read'], $result['module']);
256
        $result = Route::parseUrl('test');
257
        $this->assertEquals(['index', 'blog', 'test'], $result['module']);
258
259
        Route::bind('\app\index\controller', 'namespace');
260
        $this->assertEquals(['type' => 'method', 'method' => ['\app\index\controller\Blog', 'read'], 'var' => []], Route::check($request, 'blog/read'));
261
262
        Route::bind('\app\index\controller\Blog', 'class');
263
        $this->assertEquals(['type' => 'method', 'method' => ['\app\index\controller\Blog', 'read'], 'var' => []], Route::check($request, 'read'));
264
    }
265
266
    public function testDomain()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
267
    {
268
        $request = Request::create('http://subdomain.thinkphp.cn');
269
        Route::domain('subdomain.thinkphp.cn', 'sub?abc=test&status=1');
270
        $rules = Route::rules('GET');
271
        Route::checkDomain($request, $rules);
272
        $this->assertEquals('sub', Route::getbind('module'));
273
        $this->assertEquals('test', $_GET['abc']);
274
        $this->assertEquals(1, $_GET['status']);
275
276
        Route::domain('subdomain.thinkphp.cn', '\app\index\controller');
277
        $rules = Route::rules('GET');
278
        Route::checkDomain($request, $rules);
279
        $this->assertEquals('\app\index\controller', Route::getbind('namespace'));
280
281
        Route::domain(['subdomain.thinkphp.cn' => '@\app\index\controller\blog']);
282
        $rules = Route::rules('GET');
283
        Route::checkDomain($request, $rules);
284
        $this->assertEquals('\app\index\controller\blog', Route::getbind('class'));
285
286
    }
287
}
288