1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunrise\Http\Router\Tests; |
4
|
|
|
|
5
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Sunrise\Http\Router\Route; |
8
|
|
|
use Sunrise\Http\Router\RouteInterface; |
9
|
|
|
use Sunrise\Http\Router\RouteCollection; |
10
|
|
|
use Sunrise\Http\Router\RouteCollectionInterface; |
11
|
|
|
|
12
|
|
|
class RouteCollectionTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testConstructor() |
15
|
|
|
{ |
16
|
|
|
$collection = new RouteCollection(); |
17
|
|
|
|
18
|
|
|
$this->assertInstanceOf(RouteCollectionInterface::class, $collection); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testGetRoutes() |
22
|
|
|
{ |
23
|
|
|
$collection = new RouteCollection(); |
24
|
|
|
|
25
|
|
|
$this->assertEquals([], $collection->getRoutes()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testAddRoute() |
29
|
|
|
{ |
30
|
|
|
$foo = new Route('foo', '/foo', []); |
31
|
|
|
|
32
|
|
|
$collection = new RouteCollection(); |
33
|
|
|
|
34
|
|
|
$this->assertInstanceOf(RouteCollectionInterface::class, $collection->addRoute($foo)); |
35
|
|
|
|
36
|
|
|
$this->assertEquals([$foo], $collection->getRoutes()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testAddSeveralRoutes() |
40
|
|
|
{ |
41
|
|
|
$foo = new Route('foo', '/foo', []); |
42
|
|
|
$bar = new Route('bar', '/bar', []); |
43
|
|
|
|
44
|
|
|
$collection = new RouteCollection(); |
45
|
|
|
|
46
|
|
|
$collection->addRoute($foo); |
47
|
|
|
$collection->addRoute($bar); |
48
|
|
|
|
49
|
|
|
$this->assertEquals([ |
50
|
|
|
$foo, |
51
|
|
|
$bar, |
52
|
|
|
], $collection->getRoutes()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCreateRoute() |
56
|
|
|
{ |
57
|
|
|
$routeId = 'foo'; |
58
|
|
|
$routePath = '/foo'; |
59
|
|
|
$routeMethods = ['HEAD', 'GET']; |
60
|
|
|
|
61
|
|
|
$collection = new RouteCollection(); |
62
|
|
|
$route = $collection->route($routeId, $routePath, $routeMethods); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
65
|
|
|
$this->assertEquals($routeId, $route->getId()); |
66
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
67
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
68
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testCreateRouteForHttpMethodHead() |
72
|
|
|
{ |
73
|
|
|
$routeId = 'foo'; |
74
|
|
|
$routePath = '/foo'; |
75
|
|
|
$routeMethods = [ |
76
|
|
|
RequestMethodInterface::METHOD_HEAD, |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
$collection = new RouteCollection(); |
80
|
|
|
$route = $collection->head($routeId, $routePath); |
81
|
|
|
|
82
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
83
|
|
|
$this->assertEquals($routeId, $route->getId()); |
84
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
85
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
86
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testCreateRouteForHttpMethodGet() |
90
|
|
|
{ |
91
|
|
|
$routeId = 'foo'; |
92
|
|
|
$routePath = '/foo'; |
93
|
|
|
$routeMethods = [ |
94
|
|
|
RequestMethodInterface::METHOD_GET, |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$collection = new RouteCollection(); |
98
|
|
|
$route = $collection->get($routeId, $routePath); |
99
|
|
|
|
100
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
101
|
|
|
$this->assertEquals($routeId, $route->getId()); |
102
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
103
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
104
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testCreateRouteForHttpMethodPost() |
108
|
|
|
{ |
109
|
|
|
$routeId = 'foo'; |
110
|
|
|
$routePath = '/foo'; |
111
|
|
|
$routeMethods = [ |
112
|
|
|
RequestMethodInterface::METHOD_POST, |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
$collection = new RouteCollection(); |
116
|
|
|
$route = $collection->post($routeId, $routePath); |
117
|
|
|
|
118
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
119
|
|
|
$this->assertEquals($routeId, $route->getId()); |
120
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
121
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
122
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testCreateRouteForHttpMethodPut() |
126
|
|
|
{ |
127
|
|
|
$routeId = 'foo'; |
128
|
|
|
$routePath = '/foo'; |
129
|
|
|
$routeMethods = [ |
130
|
|
|
RequestMethodInterface::METHOD_PUT, |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$collection = new RouteCollection(); |
134
|
|
|
$route = $collection->put($routeId, $routePath); |
135
|
|
|
|
136
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
137
|
|
|
$this->assertEquals($routeId, $route->getId()); |
138
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
139
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
140
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testCreateRouteForHttpMethodPatch() |
144
|
|
|
{ |
145
|
|
|
$routeId = 'foo'; |
146
|
|
|
$routePath = '/foo'; |
147
|
|
|
$routeMethods = [ |
148
|
|
|
RequestMethodInterface::METHOD_PATCH, |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
$collection = new RouteCollection(); |
152
|
|
|
$route = $collection->patch($routeId, $routePath); |
153
|
|
|
|
154
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
155
|
|
|
$this->assertEquals($routeId, $route->getId()); |
156
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
157
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
158
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testCreateRouteForHttpMethodDelete() |
162
|
|
|
{ |
163
|
|
|
$routeId = 'foo'; |
164
|
|
|
$routePath = '/foo'; |
165
|
|
|
$routeMethods = [ |
166
|
|
|
RequestMethodInterface::METHOD_DELETE, |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
$collection = new RouteCollection(); |
170
|
|
|
$route = $collection->delete($routeId, $routePath); |
171
|
|
|
|
172
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
173
|
|
|
$this->assertEquals($routeId, $route->getId()); |
174
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
175
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
176
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testCreateRouteForHttpMethodPurge() |
180
|
|
|
{ |
181
|
|
|
$routeId = 'foo'; |
182
|
|
|
$routePath = '/foo'; |
183
|
|
|
$routeMethods = [ |
184
|
|
|
RequestMethodInterface::METHOD_PURGE, |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
$collection = new RouteCollection(); |
188
|
|
|
$route = $collection->purge($routeId, $routePath); |
189
|
|
|
|
190
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
191
|
|
|
$this->assertEquals($routeId, $route->getId()); |
192
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
193
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
194
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testCreateRouteForHttpMethodSafe() |
198
|
|
|
{ |
199
|
|
|
$routeId = 'foo'; |
200
|
|
|
$routePath = '/foo'; |
201
|
|
|
$routeMethods = [ |
202
|
|
|
RequestMethodInterface::METHOD_HEAD, |
203
|
|
|
RequestMethodInterface::METHOD_GET, |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
$collection = new RouteCollection(); |
207
|
|
|
$route = $collection->safe($routeId, $routePath); |
208
|
|
|
|
209
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
210
|
|
|
$this->assertEquals($routeId, $route->getId()); |
211
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
212
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
213
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testCreateRouteForHttpMethodAny() |
217
|
|
|
{ |
218
|
|
|
$routeId = 'foo'; |
219
|
|
|
$routePath = '/foo'; |
220
|
|
|
$routeMethods = [ |
221
|
|
|
RequestMethodInterface::METHOD_HEAD, |
222
|
|
|
RequestMethodInterface::METHOD_GET, |
223
|
|
|
RequestMethodInterface::METHOD_POST, |
224
|
|
|
RequestMethodInterface::METHOD_PUT, |
225
|
|
|
RequestMethodInterface::METHOD_PATCH, |
226
|
|
|
RequestMethodInterface::METHOD_DELETE, |
227
|
|
|
RequestMethodInterface::METHOD_PURGE, |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
$collection = new RouteCollection(); |
231
|
|
|
$route = $collection->any($routeId, $routePath); |
232
|
|
|
|
233
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
234
|
|
|
$this->assertEquals($routeId, $route->getId()); |
235
|
|
|
$this->assertEquals($routePath, $route->getPath()); |
236
|
|
|
$this->assertEquals($routeMethods, $route->getMethods()); |
237
|
|
|
$this->assertEquals([$route], $collection->getRoutes()); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testGroup() |
241
|
|
|
{ |
242
|
|
|
$foo = new Route('foo', '/foo', []); |
243
|
|
|
$bar = new Route('bar', '/foo', []); |
244
|
|
|
$baz = new Route('baz', '/foo', []); |
245
|
|
|
$qux = new Route('qux', '/foo', []); |
246
|
|
|
|
247
|
|
|
$collection = new RouteCollection(); |
248
|
|
|
|
249
|
|
|
$collection->group('/bar', function(RouteCollectionInterface $collection) use($foo, $bar, $baz) |
250
|
|
|
{ |
251
|
|
|
$collection->addRoute($bar); |
252
|
|
|
|
253
|
|
|
$collection->group('/baz', function(RouteCollectionInterface $collection) use($foo, $baz) |
254
|
|
|
{ |
255
|
|
|
$collection->group('/qux', function(RouteCollectionInterface $collection) use($foo) |
256
|
|
|
{ |
257
|
|
|
$collection->addRoute($foo); |
258
|
|
|
}); |
259
|
|
|
|
260
|
|
|
$collection->addRoute($baz); |
261
|
|
|
}); |
262
|
|
|
}); |
263
|
|
|
|
264
|
|
|
$collection->addRoute($qux); |
265
|
|
|
|
266
|
|
|
$this->assertEquals([ |
267
|
|
|
$bar, |
268
|
|
|
$foo, |
269
|
|
|
$baz, |
270
|
|
|
$qux, |
271
|
|
|
], $collection->getRoutes()); |
272
|
|
|
|
273
|
|
|
$this->assertEquals('/bar/baz/qux/foo', $foo->getPath()); |
274
|
|
|
$this->assertEquals('/bar/foo', $bar->getPath()); |
275
|
|
|
$this->assertEquals('/bar/baz/foo', $baz->getPath()); |
276
|
|
|
$this->assertEquals('/foo', $qux->getPath()); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|