|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunrise\Http\Router\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Sunrise\Http\Router\Route; |
|
7
|
|
|
use Sunrise\Http\Router\RouteInterface; |
|
8
|
|
|
|
|
9
|
|
|
// fake middlewares |
|
10
|
|
|
use Sunrise\Http\Router\Tests\Middleware\FooMiddlewareTest; |
|
11
|
|
|
use Sunrise\Http\Router\Tests\Middleware\BarMiddlewareTest; |
|
12
|
|
|
|
|
13
|
|
|
class RouteTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testConstructor() |
|
16
|
|
|
{ |
|
17
|
|
|
$route = new Route('home', '/', []); |
|
18
|
|
|
|
|
19
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testGetId() |
|
23
|
|
|
{ |
|
24
|
|
|
$foo = 'home'; |
|
25
|
|
|
|
|
26
|
|
|
$route = new Route($foo, '/', []); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals($foo, $route->getId()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testGetPath() |
|
32
|
|
|
{ |
|
33
|
|
|
$foo = '/'; |
|
34
|
|
|
|
|
35
|
|
|
$route = new Route('home', $foo, []); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals($foo, $route->getPath()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testGetMethods() |
|
41
|
|
|
{ |
|
42
|
|
|
$foo = ['HEAD', 'GET']; |
|
43
|
|
|
|
|
44
|
|
|
$route = new Route('home', '/', $foo); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertEquals($foo, $route->getMethods()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testGetPatterns() |
|
50
|
|
|
{ |
|
51
|
|
|
$foo = []; |
|
52
|
|
|
|
|
53
|
|
|
$route = new Route('home', '/', []); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals($foo, $route->getPatterns()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testGetAttributes() |
|
59
|
|
|
{ |
|
60
|
|
|
$foo = []; |
|
61
|
|
|
|
|
62
|
|
|
$route = new Route('home', '/', []); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals($foo, $route->getAttributes()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testGetMiddlewareStack() |
|
68
|
|
|
{ |
|
69
|
|
|
$foo = []; |
|
70
|
|
|
|
|
71
|
|
|
$route = new Route('home', '/', []); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals($foo, $route->getMiddlewareStack()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testSetId() |
|
77
|
|
|
{ |
|
78
|
|
|
$foo = 'foo'; |
|
79
|
|
|
$bar = 'bar'; |
|
80
|
|
|
|
|
81
|
|
|
$route = new Route($foo, '/', []); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->setId($bar)); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals($bar, $route->getId()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testSetPath() |
|
89
|
|
|
{ |
|
90
|
|
|
$foo = '/foo'; |
|
91
|
|
|
$bar = '/bar'; |
|
92
|
|
|
|
|
93
|
|
|
$route = new Route('test', $foo, []); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->setPath($bar)); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertEquals($bar, $route->getPath()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testAddPrefix() |
|
101
|
|
|
{ |
|
102
|
|
|
$foo = '/foo'; |
|
103
|
|
|
$bar = '/bar'; |
|
104
|
|
|
|
|
105
|
|
|
$route = new Route('home', $foo, []); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addPrefix($bar)); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals($bar.$foo, $route->getPath()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testAddSeveralPrefixex() |
|
113
|
|
|
{ |
|
114
|
|
|
$foo = '/foo'; |
|
115
|
|
|
$bar = '/bar'; |
|
116
|
|
|
$baz = '/baz'; |
|
117
|
|
|
|
|
118
|
|
|
$route = new Route('home', $foo, []); |
|
119
|
|
|
|
|
120
|
|
|
$route->addPrefix($bar); |
|
121
|
|
|
$route->addPrefix($baz); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertEquals($baz.$bar.$foo, $route->getPath()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testAddSuffix() |
|
127
|
|
|
{ |
|
128
|
|
|
$foo = '/foo'; |
|
129
|
|
|
$bar = '/bar'; |
|
130
|
|
|
|
|
131
|
|
|
$route = new Route('home', $foo, []); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addSuffix($bar)); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertEquals($foo.$bar, $route->getPath()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testAddSeveralSuffixes() |
|
139
|
|
|
{ |
|
140
|
|
|
$foo = '/foo'; |
|
141
|
|
|
$bar = '/bar'; |
|
142
|
|
|
$baz = '/baz'; |
|
143
|
|
|
|
|
144
|
|
|
$route = new Route('home', $foo, []); |
|
145
|
|
|
|
|
146
|
|
|
$route->addSuffix($bar); |
|
147
|
|
|
$route->addSuffix($baz); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertEquals($foo.$bar.$baz, $route->getPath()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testAddMethod() |
|
153
|
|
|
{ |
|
154
|
|
|
$foo = 'HEAD'; |
|
155
|
|
|
$bar = 'GET'; |
|
156
|
|
|
|
|
157
|
|
|
$route = new Route('home', '/', [$foo]); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addMethod($bar)); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertEquals([$foo, $bar], $route->getMethods()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function testAddSeveralMethods() |
|
165
|
|
|
{ |
|
166
|
|
|
$foo = 'HEAD'; |
|
167
|
|
|
$bar = 'GET'; |
|
168
|
|
|
$baz = 'POST'; |
|
169
|
|
|
$qux = 'PATCH'; |
|
170
|
|
|
|
|
171
|
|
|
$route = new Route('home', '/', [$foo, $bar]); |
|
172
|
|
|
|
|
173
|
|
|
$route->addMethod($baz); |
|
174
|
|
|
$route->addMethod($qux); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertEquals([ |
|
177
|
|
|
$foo, |
|
178
|
|
|
$bar, |
|
179
|
|
|
$baz, |
|
180
|
|
|
$qux, |
|
181
|
|
|
], $route->getMethods()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testAddPattern() |
|
185
|
|
|
{ |
|
186
|
|
|
$foo = ['id', '\d+']; |
|
187
|
|
|
|
|
188
|
|
|
$route = new Route('home', '/', []); |
|
189
|
|
|
|
|
190
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addPattern($foo[0], $foo[1])); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertEquals([$foo[0] => $foo[1]], $route->getPatterns()); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testAddSeveralPatterns() |
|
196
|
|
|
{ |
|
197
|
|
|
$foo = ['id', '\d+']; |
|
198
|
|
|
$bar = ['word', '\w+']; |
|
199
|
|
|
|
|
200
|
|
|
$route = new Route('home', '/', []); |
|
201
|
|
|
|
|
202
|
|
|
$route->addPattern($foo[0], $foo[1]); |
|
203
|
|
|
$route->addPattern($bar[0], $bar[1]); |
|
204
|
|
|
|
|
205
|
|
|
$this->assertEquals([ |
|
206
|
|
|
$foo[0] => $foo[1], |
|
207
|
|
|
$bar[0] => $bar[1], |
|
208
|
|
|
], $route->getPatterns()); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function testAddMiddleware() |
|
212
|
|
|
{ |
|
213
|
|
|
$foo = new FooMiddlewareTest(); |
|
214
|
|
|
|
|
215
|
|
|
$route = new Route('home', '/', []); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addMiddleware($foo)); |
|
218
|
|
|
|
|
219
|
|
|
$this->assertEquals([$foo], $route->getMiddlewareStack()); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function testAddSeveralMiddlewares() |
|
223
|
|
|
{ |
|
224
|
|
|
$foo = new FooMiddlewareTest(); |
|
225
|
|
|
$bar = new BarMiddlewareTest(); |
|
226
|
|
|
|
|
227
|
|
|
$route = new Route('home', '/', []); |
|
228
|
|
|
|
|
229
|
|
|
$route->addMiddleware($foo); |
|
230
|
|
|
$route->addMiddleware($bar); |
|
231
|
|
|
|
|
232
|
|
|
$this->assertEquals([ |
|
233
|
|
|
$foo, |
|
234
|
|
|
$bar, |
|
235
|
|
|
], $route->getMiddlewareStack()); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function testSetAttributes() |
|
239
|
|
|
{ |
|
240
|
|
|
$foo = ['id' => '1']; |
|
241
|
|
|
|
|
242
|
|
|
$route = new Route('home', '/', []); |
|
243
|
|
|
$clone = $route->withAttributes($foo); |
|
244
|
|
|
|
|
245
|
|
|
$this->assertInstanceOf(RouteInterface::class, $clone); |
|
246
|
|
|
$this->assertNotEquals($clone, $route); |
|
247
|
|
|
|
|
248
|
|
|
$this->assertEquals($foo, $clone->getAttributes()); |
|
249
|
|
|
$this->assertEquals([], $route->getAttributes()); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function testSetAttributesPreservingPreviousValues() |
|
253
|
|
|
{ |
|
254
|
|
|
$foo = ['foo' => 'bar']; |
|
255
|
|
|
$bar = ['bar' => 'baz']; |
|
256
|
|
|
$baz = ['baz' => 'qux']; |
|
257
|
|
|
|
|
258
|
|
|
$route = new Route('home', '/', []); |
|
259
|
|
|
$clone1 = $route->withAttributes($foo); |
|
260
|
|
|
$clone2 = $clone1->withAttributes($bar); |
|
261
|
|
|
$clone3 = $clone2->withAttributes($baz); |
|
262
|
|
|
|
|
263
|
|
|
$this->assertEquals([], $route->getAttributes()); |
|
264
|
|
|
$this->assertEquals($foo, $clone1->getAttributes()); |
|
265
|
|
|
$this->assertEquals($foo + $bar, $clone2->getAttributes()); |
|
266
|
|
|
$this->assertEquals($foo + $bar + $baz, $clone3->getAttributes()); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function testLowercasedMethod() |
|
270
|
|
|
{ |
|
271
|
|
|
$route = new Route('home', '/', ['foo', 'bar']); |
|
272
|
|
|
|
|
273
|
|
|
$route->addMethod('baz'); |
|
274
|
|
|
$route->addMethod('qux'); |
|
275
|
|
|
|
|
276
|
|
|
$this->assertEquals([ |
|
277
|
|
|
'FOO', |
|
278
|
|
|
'BAR', |
|
279
|
|
|
'BAZ', |
|
280
|
|
|
'QUX', |
|
281
|
|
|
], $route->getMethods()); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|