1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Flight\Routing\Tests; |
19
|
|
|
|
20
|
|
|
use BiuradPHP\Http\Factories\GuzzleHttpPsr7Factory; |
21
|
|
|
use Flight\Routing\Concerns\HttpMethods; |
22
|
|
|
use Flight\Routing\Interfaces\RouterInterface; |
23
|
|
|
use Flight\Routing\Services\DefaultFlightRouter; |
24
|
|
|
use Flight\Routing\Services\SimpleRouteCompiler; |
25
|
|
|
use Flight\Routing\Tests\Fixtures\SampleController; |
26
|
|
|
use Generator; |
27
|
|
|
|
28
|
|
|
class DefaultRouterTest extends RouterIntegrationTest |
29
|
|
|
{ |
30
|
|
|
public function getRouter(): RouterInterface |
31
|
|
|
{ |
32
|
|
|
return new DefaultFlightRouter([new SimpleRouteCompiler(), 'compile']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function psrServerResponseFactory(): array |
36
|
|
|
{ |
37
|
|
|
return [GuzzleHttpPsr7Factory::fromGlobalRequest(), new GuzzleHttpPsr7Factory()]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function implicitRoutesAndRequests(): Generator |
41
|
|
|
{ |
42
|
|
|
yield 'Root Route Text: get, callable' => [ |
43
|
|
|
'/', |
44
|
|
|
'/', |
45
|
|
|
HttpMethods::METHOD_GET, |
46
|
|
|
function () { |
47
|
|
|
return 'Hello World'; |
48
|
|
|
}, |
49
|
|
|
[], |
50
|
|
|
['content-type' => 'text/plain; charset=utf-8'], |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
yield 'Root Route Html: get, callable' => [ |
54
|
|
|
'/', |
55
|
|
|
'/', |
56
|
|
|
HttpMethods::METHOD_GET, |
57
|
|
|
function () { |
58
|
|
|
return '<html><body><h1>Hello World</h1></body></html>'; |
59
|
|
|
}, |
60
|
|
|
[ |
61
|
|
|
'name' => 'homepage', |
62
|
|
|
'generate' => [[], ['test' => 'fine']], |
63
|
|
|
], |
64
|
|
|
[ |
65
|
|
|
'generate' => './?test=fine', |
66
|
|
|
'content-type' => 'text/html; charset=utf-8', |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
yield 'Root Route XML: get, callable' => [ |
71
|
|
|
'/', |
72
|
|
|
'/', |
73
|
|
|
HttpMethods::METHOD_GET, |
74
|
|
|
function () { |
75
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?><route>Hello World</route>'; |
76
|
|
|
}, |
77
|
|
|
[], |
78
|
|
|
['content-type' => 'application/xml; charset=utf-8'], |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
yield 'Root Route JSON: get, callable' => [ |
82
|
|
|
'/', |
83
|
|
|
'/', |
84
|
|
|
HttpMethods::METHOD_GET, |
85
|
|
|
function () { |
86
|
|
|
return new Helpers\DumpArrayTest(); |
87
|
|
|
}, |
88
|
|
|
[], |
89
|
|
|
['content-type' => 'application/json'], |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
yield 'Route Controller & Action: post, nullable' => [ |
93
|
|
|
'/test*<Flight\Routing\Tests\Fixtures\SampleController@homePageRequestResponse>', |
94
|
|
|
'/test', |
95
|
|
|
HttpMethods::METHOD_POST, |
96
|
|
|
null, |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
yield 'Route Action: get, string' => [ |
100
|
|
|
'/test*<homePageRequestResponse>', |
101
|
|
|
'/test', |
102
|
|
|
HttpMethods::METHOD_GET, |
103
|
|
|
SampleController::class, |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
yield 'Route Action: get, object' => [ |
107
|
|
|
'/test*<homePageRequestResponse>', |
108
|
|
|
'/test', |
109
|
|
|
HttpMethods::METHOD_GET, |
110
|
|
|
new SampleController(), |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
yield 'Basic Route: get, callable' => [ |
114
|
|
|
'/test', |
115
|
|
|
'/test', |
116
|
|
|
HttpMethods::METHOD_GET, |
117
|
|
|
function () { |
118
|
|
|
return 'Hello, this is a basic test route'; |
119
|
|
|
}, |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
yield 'Basic Route Redirection: get, callable' => [ |
123
|
|
|
'/test', |
124
|
|
|
'/test/', |
125
|
|
|
HttpMethods::METHOD_GET, |
126
|
|
|
function () { |
127
|
|
|
return 'Hello, this is a basic test route'; |
128
|
|
|
}, |
129
|
|
|
[], |
130
|
|
|
['status' => 302], |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
yield 'Paramter Route: get, callable' => [ |
134
|
|
|
'/test/{home}', |
135
|
|
|
'/test/cool', |
136
|
|
|
HttpMethods::METHOD_GET, |
137
|
|
|
function (string $home) { |
138
|
|
|
return 'Hello, this is a basic test route on subpage ' . $home; |
139
|
|
|
}, |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
yield 'Paramter & Default Route: get, callable' => [ |
143
|
|
|
'/test/{home}', |
144
|
|
|
'/test/cool', |
145
|
|
|
HttpMethods::METHOD_GET, |
146
|
|
|
function (string $home, int $id) { |
147
|
|
|
return $home . $id; |
148
|
|
|
}, |
149
|
|
|
['defaults' => ['id' => 233]], |
150
|
|
|
['body' => 'cool233'], |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
yield 'Optional Paramter Route: get, callable' => [ |
154
|
|
|
'/test[/{home}]', |
155
|
|
|
'/test', |
156
|
|
|
HttpMethods::METHOD_GET, |
157
|
|
|
function (?string $home) { |
158
|
|
|
return 'Hello, this is a basic test route on subpage ' . $home; |
159
|
|
|
}, |
160
|
|
|
[], |
161
|
|
|
['body' => 'Hello, this is a basic test route on subpage '], |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
yield 'Optional Paramter Route: path, get, callable' => [ |
165
|
|
|
'/test[/{home}]', |
166
|
|
|
'/test/cool', |
167
|
|
|
HttpMethods::METHOD_GET, |
168
|
|
|
function (?string $home) { |
169
|
|
|
return 'Hello, this is a basic test route on subpage ' . $home; |
170
|
|
|
}, |
171
|
|
|
[], |
172
|
|
|
['body' => 'Hello, this is a basic test route on subpage cool'], |
173
|
|
|
]; |
174
|
|
|
|
175
|
|
|
yield 'Route Domain: get, callable' => [ |
176
|
|
|
'//example.com/test', |
177
|
|
|
'/test', |
178
|
|
|
HttpMethods::METHOD_GET, |
179
|
|
|
function () { |
180
|
|
|
return 'Hello World'; |
181
|
|
|
}, |
182
|
|
|
['domain' => 'example.com'], |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
yield 'Route Domain Regex: get, callable' => [ |
186
|
|
|
'//{id:int}.example.com/test', |
187
|
|
|
'/test', |
188
|
|
|
HttpMethods::METHOD_GET, |
189
|
|
|
function () { |
190
|
|
|
return 'Hello World'; |
191
|
|
|
}, |
192
|
|
|
[ |
193
|
|
|
'name' => 'domainpage', |
194
|
|
|
'domain' => '99.example.com', |
195
|
|
|
'generate' => [['id' => '23'], []], |
196
|
|
|
], |
197
|
|
|
['generate' => 'http://23.example.com/test'], |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
yield 'Route Domain Regex & Scheme: get, callable' => [ |
201
|
|
|
'https://{id:int}.example.com/{action}', |
202
|
|
|
'/tests', |
203
|
|
|
HttpMethods::METHOD_GET, |
204
|
|
|
function (string $action) { |
205
|
|
|
return 'Hello World' . $action; |
206
|
|
|
}, |
207
|
|
|
[ |
208
|
|
|
'name' => 'domain_scheme_page', |
209
|
|
|
'domain' => '99.example.com', |
210
|
|
|
'scheme' => 'https', |
211
|
|
|
'generate' => [['id' => '23', 'action' => 'okay'], []], |
212
|
|
|
], |
213
|
|
|
[ |
214
|
|
|
'scheme' => 'https', |
215
|
|
|
'generate' => 'https://23.example.com/okay', |
216
|
|
|
], |
217
|
|
|
]; |
218
|
|
|
|
219
|
|
|
yield 'Nested Optional Paramter Route 1: get, callable' => [ |
220
|
|
|
'/[{action}/[{id}]]', |
221
|
|
|
'/test/', |
222
|
|
|
HttpMethods::METHOD_GET, |
223
|
|
|
function (?string $action) { |
224
|
|
|
return $action; |
225
|
|
|
}, |
226
|
|
|
[], |
227
|
|
|
['status' => 302], |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
yield 'Nested Optional Paramter Route 2: get, callable' => [ |
231
|
|
|
'/[{action}/[{id}]]', |
232
|
|
|
'/test/id', |
233
|
|
|
HttpMethods::METHOD_GET, |
234
|
|
|
function (?string $action) { |
235
|
|
|
return $action; |
236
|
|
|
}, |
237
|
|
|
[ |
238
|
|
|
'name' => 'nested', |
239
|
|
|
'generate' => [['action' => 'yes_we_can'], []], |
240
|
|
|
], |
241
|
|
|
[ |
242
|
|
|
'status' => 200, |
243
|
|
|
'generate' => './yes_we_can', |
244
|
|
|
], |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
yield 'Regex Paramter Route : get, callable' => [ |
248
|
|
|
'/user/{id:[0-9-]+}', |
249
|
|
|
'user/23', |
250
|
|
|
HttpMethods::METHOD_GET, |
251
|
|
|
function (int $id) { |
252
|
|
|
return $id; |
253
|
|
|
}, |
254
|
|
|
]; |
255
|
|
|
|
256
|
|
|
yield 'Complex Paramter Route 1: get, callable' => [ |
257
|
|
|
'/[{lang:[a-z]{2}}/]hello', |
258
|
|
|
'/hello', |
259
|
|
|
HttpMethods::METHOD_GET, |
260
|
|
|
function (?string $lang) { |
261
|
|
|
return $lang; |
262
|
|
|
}, |
263
|
|
|
]; |
264
|
|
|
|
265
|
|
|
yield 'Complex Paramter Route 2: get, callable' => [ |
266
|
|
|
'/[{lang:[a-z]{2}}/]{name}', |
267
|
|
|
'/en/download', |
268
|
|
|
HttpMethods::METHOD_GET, |
269
|
|
|
function (?string $lang, string $name) { |
270
|
|
|
return $lang . $name; |
271
|
|
|
}, |
272
|
|
|
]; |
273
|
|
|
|
274
|
|
|
yield 'Complex Paramter Route 3: get, callable' => [ |
275
|
|
|
'[{lang:[a-z]{2}}[-{sublang}]/]{name}[/page-{page=<0>}]', |
276
|
|
|
'/download', |
277
|
|
|
HttpMethods::METHOD_GET, |
278
|
|
|
function (?string $lang, ?string $sublang, string $name, $page) { |
279
|
|
|
return $lang . '-' . $sublang . $name . $page; |
280
|
|
|
}, |
281
|
|
|
[], |
282
|
|
|
['body' => '-download0'], |
283
|
|
|
]; |
284
|
|
|
|
285
|
|
|
yield 'Complex Paramter Route 4: get, callable' => [ |
286
|
|
|
'[{lang:[a-z]{2}}[-{sublang}]/]{name}[/page-{page=<0>}]', |
287
|
|
|
'/en-us/download', |
288
|
|
|
HttpMethods::METHOD_GET, |
289
|
|
|
function (?string $lang, ?string $sublang, string $name, $page) { |
290
|
|
|
return $lang . '-' . $sublang . $name . $page; |
291
|
|
|
}, |
292
|
|
|
[], |
293
|
|
|
['body' => 'en-usdownload0'], |
294
|
|
|
]; |
295
|
|
|
|
296
|
|
|
yield 'Complex Paramter Route 5: get, callable' => [ |
297
|
|
|
'[{lang:[a-z]{2}}[-{sublang}]/]{name}[/page-{page=<0>}]', |
298
|
|
|
'/en-us/download/page-12', |
299
|
|
|
HttpMethods::METHOD_GET, |
300
|
|
|
function (?string $lang, ?string $sublang, string $name, $page) { |
301
|
|
|
return $lang . '-' . $sublang . $name . $page; |
302
|
|
|
}, |
303
|
|
|
[], |
304
|
|
|
['body' => 'en-usdownload12'], |
305
|
|
|
]; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|