Passed
Push — master ( aac959...f67e8c )
by Anatoly
01:26
created

RouteCollectionTest::testAddRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\RouteInterface;
8
use Sunrise\Http\Router\RouteCollection;
9
use Sunrise\Http\Router\RouteCollectionInterface;
10
11
class RouteCollectionTest extends TestCase
12
{
13
	use HelpersInjectTest;
14
15
	public function testConstructor()
16
	{
17
		$collection = new RouteCollection();
18
19
		$this->assertInstanceOf(RouteCollectionInterface::class, $collection);
20
	}
21
22
	public function testGetRoute()
23
	{
24
		$collection = new RouteCollection();
25
26
		// default value
27
		$this->assertNull($collection->getRoute('undefined.route.id'));
28
	}
29
30
	public function testGetRoutes()
31
	{
32
		$collection = new RouteCollection();
33
34
		// default value
35
		$this->assertEquals([], $collection->getRoutes());
36
	}
37
38
	public function testGetMiddlewareStack()
39
	{
40
		$collection = new RouteCollection();
41
42
		// default value
43
		$this->assertEquals([], $collection->getMiddlewareStack());
44
	}
45
46
	public function testAddRoute()
47
	{
48
		$route = $this->getRouteFoo();
49
		$collection = new RouteCollection();
50
51
		$this->assertInstanceOf(RouteCollectionInterface::class, $collection->addRoute($route));
52
		$this->assertEquals([$route->getId() => $route], $collection->getRoutes());
53
		$this->assertEquals($route, $collection->getRoute($route->getId()));
54
	}
55
56
	public function testAddSeveralRoutes()
57
	{
58
		$foo = $this->getRouteFoo();
59
		$bar = $this->getRouteBar();
60
		$baz = $this->getRouteBaz();
61
62
		$collection = new RouteCollection();
63
64
		$collection->addRoute($foo);
65
		$collection->addRoute($bar);
66
		$collection->addRoute($baz);
67
68
		$this->assertEquals([
69
			$foo->getId() => $foo,
70
			$bar->getId() => $bar,
71
			$baz->getId() => $baz,
72
		], $collection->getRoutes());
73
74
		$this->assertEquals($foo, $collection->getRoute($foo->getId()));
75
		$this->assertEquals($bar, $collection->getRoute($bar->getId()));
76
		$this->assertEquals($baz, $collection->getRoute($baz->getId()));
77
	}
78
79
	public function testAddMiddleware()
80
	{
81
		$middleware = $this->getMiddlewareFoo();
82
		$collection = new RouteCollection();
83
84
		$this->assertInstanceOf(RouteCollectionInterface::class, $collection->middleware($middleware));
85
		$this->assertEquals([$middleware], $collection->getMiddlewareStack());
86
	}
87
88
	public function testAddSeveralMiddlewares()
89
	{
90
		$foo = $this->getMiddlewareFoo();
91
		$bar = $this->getMiddlewareBar();
92
		$baz = $this->getMiddlewareBaz();
93
94
		$collection = new RouteCollection();
95
96
		$collection->middleware($foo);
97
		$collection->middleware($bar);
98
		$collection->middleware($baz);
99
100
		$this->assertEquals([
101
			$foo,
102
			$bar,
103
			$baz,
104
		], $collection->getMiddlewareStack());
105
	}
106
107
	public function testCreateRoute()
108
	{
109
		$routeId = 'foo';
110
		$routePath = '/foo';
111
		$routeAction = $this->getRouteActionFoo();
112
		$routeMethods = [];
113
114
		$collection = new RouteCollection();
115
		$route = $collection->add($routeId, $routePath, $routeAction);
116
117
		$this->assertInstanceOf(RouteInterface::class, $route);
118
		$this->assertEquals($routeId, $route->getId());
119
		$this->assertEquals($routePath, $route->getPath());
120
		$this->assertEquals($routeAction, $route->getAction());
121
		$this->assertEquals($routeMethods, $route->getMethods());
122
	}
123
124
	public function testCreateRouteWithHttpMethod()
125
	{
126
		$routeId = 'foo';
127
		$routePath = '/foo';
128
		$routeAction = $this->getRouteActionFoo();
129
		$routeMethods = [
130
			RequestMethodInterface::METHOD_GET,
131
		];
132
133
		$collection = new RouteCollection();
134
		$route = $collection->add($routeId, $routePath, $routeAction, $routeMethods);
135
136
		$this->assertInstanceOf(RouteInterface::class, $route);
137
		$this->assertEquals($routeId, $route->getId());
138
		$this->assertEquals($routePath, $route->getPath());
139
		$this->assertEquals($routeAction, $route->getAction());
140
		$this->assertEquals($routeMethods, $route->getMethods());
141
	}
142
143
	public function testCreateRouteWithSeveralHttpMethods()
144
	{
145
		$routeId = 'foo';
146
		$routePath = '/foo';
147
		$routeAction = $this->getRouteActionFoo();
148
		$routeMethods = [
149
			RequestMethodInterface::METHOD_GET,
150
			RequestMethodInterface::METHOD_POST,
151
		];
152
153
		$collection = new RouteCollection();
154
		$route = $collection->add($routeId, $routePath, $routeAction, $routeMethods);
155
156
		$this->assertInstanceOf(RouteInterface::class, $route);
157
		$this->assertEquals($routeId, $route->getId());
158
		$this->assertEquals($routePath, $route->getPath());
159
		$this->assertEquals($routeAction, $route->getAction());
160
		$this->assertEquals($routeMethods, $route->getMethods());
161
	}
162
163
	public function testCreateRouteForHttpMethodHead()
164
	{
165
		$routeId = 'foo';
166
		$routePath = '/foo';
167
		$routeAction = $this->getRouteActionFoo();
168
		$routeMethods = [
169
			RequestMethodInterface::METHOD_HEAD,
170
		];
171
172
		$collection = new RouteCollection();
173
		$route = $collection->head($routeId, $routePath, $routeAction);
174
175
		$this->assertInstanceOf(RouteInterface::class, $route);
176
		$this->assertEquals($routeId, $route->getId());
177
		$this->assertEquals($routePath, $route->getPath());
178
		$this->assertEquals($routeAction, $route->getAction());
179
		$this->assertEquals($routeMethods, $route->getMethods());
180
	}
181
182
	public function testCreateRouteForHttpMethodGet()
183
	{
184
		$routeId = 'foo';
185
		$routePath = '/foo';
186
		$routeAction = $this->getRouteActionFoo();
187
		$routeMethods = [
188
			RequestMethodInterface::METHOD_GET,
189
		];
190
191
		$collection = new RouteCollection();
192
		$route = $collection->get($routeId, $routePath, $routeAction);
193
194
		$this->assertInstanceOf(RouteInterface::class, $route);
195
		$this->assertEquals($routeId, $route->getId());
196
		$this->assertEquals($routePath, $route->getPath());
197
		$this->assertEquals($routeAction, $route->getAction());
198
		$this->assertEquals($routeMethods, $route->getMethods());
199
	}
200
201
	public function testCreateRouteForHttpMethodPost()
202
	{
203
		$routeId = 'foo';
204
		$routePath = '/foo';
205
		$routeAction = $this->getRouteActionFoo();
206
		$routeMethods = [
207
			RequestMethodInterface::METHOD_POST,
208
		];
209
210
		$collection = new RouteCollection();
211
		$route = $collection->post($routeId, $routePath, $routeAction);
212
213
		$this->assertInstanceOf(RouteInterface::class, $route);
214
		$this->assertEquals($routeId, $route->getId());
215
		$this->assertEquals($routePath, $route->getPath());
216
		$this->assertEquals($routeAction, $route->getAction());
217
		$this->assertEquals($routeMethods, $route->getMethods());
218
	}
219
220
	public function testCreateRouteForHttpMethodPut()
221
	{
222
		$routeId = 'foo';
223
		$routePath = '/foo';
224
		$routeAction = $this->getRouteActionFoo();
225
		$routeMethods = [
226
			RequestMethodInterface::METHOD_PUT,
227
		];
228
229
		$collection = new RouteCollection();
230
		$route = $collection->put($routeId, $routePath, $routeAction);
231
232
		$this->assertInstanceOf(RouteInterface::class, $route);
233
		$this->assertEquals($routeId, $route->getId());
234
		$this->assertEquals($routePath, $route->getPath());
235
		$this->assertEquals($routeAction, $route->getAction());
236
		$this->assertEquals($routeMethods, $route->getMethods());
237
	}
238
239
	public function testCreateRouteForHttpMethodPatch()
240
	{
241
		$routeId = 'foo';
242
		$routePath = '/foo';
243
		$routeAction = $this->getRouteActionFoo();
244
		$routeMethods = [
245
			RequestMethodInterface::METHOD_PATCH,
246
		];
247
248
		$collection = new RouteCollection();
249
		$route = $collection->patch($routeId, $routePath, $routeAction);
250
251
		$this->assertInstanceOf(RouteInterface::class, $route);
252
		$this->assertEquals($routeId, $route->getId());
253
		$this->assertEquals($routePath, $route->getPath());
254
		$this->assertEquals($routeAction, $route->getAction());
255
		$this->assertEquals($routeMethods, $route->getMethods());
256
	}
257
258
	public function testCreateRouteForHttpMethodDelete()
259
	{
260
		$routeId = 'foo';
261
		$routePath = '/foo';
262
		$routeAction = $this->getRouteActionFoo();
263
		$routeMethods = [
264
			RequestMethodInterface::METHOD_DELETE,
265
		];
266
267
		$collection = new RouteCollection();
268
		$route = $collection->delete($routeId, $routePath, $routeAction);
269
270
		$this->assertInstanceOf(RouteInterface::class, $route);
271
		$this->assertEquals($routeId, $route->getId());
272
		$this->assertEquals($routePath, $route->getPath());
273
		$this->assertEquals($routeAction, $route->getAction());
274
		$this->assertEquals($routeMethods, $route->getMethods());
275
	}
276
277
	public function testCreateRouteForHttpMethodPurge()
278
	{
279
		$routeId = 'foo';
280
		$routePath = '/foo';
281
		$routeAction = $this->getRouteActionFoo();
282
		$routeMethods = [
283
			RequestMethodInterface::METHOD_PURGE,
284
		];
285
286
		$collection = new RouteCollection();
287
		$route = $collection->purge($routeId, $routePath, $routeAction);
288
289
		$this->assertInstanceOf(RouteInterface::class, $route);
290
		$this->assertEquals($routeId, $route->getId());
291
		$this->assertEquals($routePath, $route->getPath());
292
		$this->assertEquals($routeAction, $route->getAction());
293
		$this->assertEquals($routeMethods, $route->getMethods());
294
	}
295
296
	public function testCreateRouteForHttpMethodOptions()
297
	{
298
		$routeId = 'foo';
299
		$routePath = '/foo';
300
		$routeAction = $this->getRouteActionFoo();
301
		$routeMethods = [
302
			RequestMethodInterface::METHOD_OPTIONS,
303
		];
304
305
		$collection = new RouteCollection();
306
		$route = $collection->options($routeId, $routePath, $routeAction);
307
308
		$this->assertInstanceOf(RouteInterface::class, $route);
309
		$this->assertEquals($routeId, $route->getId());
310
		$this->assertEquals($routePath, $route->getPath());
311
		$this->assertEquals($routeAction, $route->getAction());
312
		$this->assertEquals($routeMethods, $route->getMethods());
313
	}
314
315
	public function testCreateRouteForHttpMethodTrace()
316
	{
317
		$routeId = 'foo';
318
		$routePath = '/foo';
319
		$routeAction = $this->getRouteActionFoo();
320
		$routeMethods = [
321
			RequestMethodInterface::METHOD_TRACE,
322
		];
323
324
		$collection = new RouteCollection();
325
		$route = $collection->trace($routeId, $routePath, $routeAction);
326
327
		$this->assertInstanceOf(RouteInterface::class, $route);
328
		$this->assertEquals($routeId, $route->getId());
329
		$this->assertEquals($routePath, $route->getPath());
330
		$this->assertEquals($routeAction, $route->getAction());
331
		$this->assertEquals($routeMethods, $route->getMethods());
332
	}
333
334
	public function testCreateRouteForHttpMethodConnect()
335
	{
336
		$routeId = 'foo';
337
		$routePath = '/foo';
338
		$routeAction = $this->getRouteActionFoo();
339
		$routeMethods = [
340
			RequestMethodInterface::METHOD_CONNECT,
341
		];
342
343
		$collection = new RouteCollection();
344
		$route = $collection->connect($routeId, $routePath, $routeAction);
345
346
		$this->assertInstanceOf(RouteInterface::class, $route);
347
		$this->assertEquals($routeId, $route->getId());
348
		$this->assertEquals($routePath, $route->getPath());
349
		$this->assertEquals($routeAction, $route->getAction());
350
		$this->assertEquals($routeMethods, $route->getMethods());
351
	}
352
353
	public function testCreateRouteForHttpMethodSafe()
354
	{
355
		$routeId = 'foo';
356
		$routePath = '/foo';
357
		$routeAction = $this->getRouteActionFoo();
358
		$routeMethods = [
359
			RequestMethodInterface::METHOD_HEAD,
360
			RequestMethodInterface::METHOD_GET,
361
		];
362
363
		$collection = new RouteCollection();
364
		$route = $collection->safe($routeId, $routePath, $routeAction);
365
366
		$this->assertInstanceOf(RouteInterface::class, $route);
367
		$this->assertEquals($routeId, $route->getId());
368
		$this->assertEquals($routePath, $route->getPath());
369
		$this->assertEquals($routeAction, $route->getAction());
370
		$this->assertEquals($routeMethods, $route->getMethods());
371
	}
372
373
	public function testCreateRouteForHttpMethodAny()
374
	{
375
		$routeId = 'foo';
376
		$routePath = '/foo';
377
		$routeAction = $this->getRouteActionFoo();
378
		$routeMethods = [
379
			RequestMethodInterface::METHOD_HEAD,
380
			RequestMethodInterface::METHOD_GET,
381
			RequestMethodInterface::METHOD_POST,
382
			RequestMethodInterface::METHOD_PUT,
383
			RequestMethodInterface::METHOD_PATCH,
384
			RequestMethodInterface::METHOD_DELETE,
385
			RequestMethodInterface::METHOD_PURGE,
386
			RequestMethodInterface::METHOD_OPTIONS,
387
			RequestMethodInterface::METHOD_TRACE,
388
			RequestMethodInterface::METHOD_CONNECT,
389
		];
390
391
		$collection = new RouteCollection();
392
		$route = $collection->any($routeId, $routePath, $routeAction);
393
394
		$this->assertInstanceOf(RouteInterface::class, $route);
395
		$this->assertEquals($routeId, $route->getId());
396
		$this->assertEquals($routePath, $route->getPath());
397
		$this->assertEquals($routeAction, $route->getAction());
398
		$this->assertEquals($routeMethods, $route->getMethods());
399
	}
400
401
	public function testGroupPrefix()
402
	{
403
		$foo = $this->getRouteFoo();
404
		$bar = $this->getRouteBar();
405
		$baz = $this->getRouteBaz();
406
		$qux = $this->getRouteQux();
407
408
		$collection = new RouteCollection();
409
410
		$collection->group('/a', function(RouteCollectionInterface $collection) use($bar, $baz, $qux)
411
		{
412
			$collection->addRoute(clone $bar);
413
414
			$collection->group('/b', function(RouteCollectionInterface $collection) use($baz)
415
			{
416
				$collection->addRoute(clone $baz);
417
			});
418
419
			$collection->addRoute(clone $qux);
420
		});
421
422
		$collection->addRoute(clone $foo);
423
424
		$this->assertEquals($foo->getPath(), $collection->getRoute($foo->getId())->getPath());
425
		$this->assertEquals($foo->getMiddlewareStack(), $collection->getRoute($foo->getId())->getMiddlewareStack());
426
427
		$this->assertEquals('/a' . $bar->getPath(), $collection->getRoute($bar->getId())->getPath());
428
		$this->assertEquals($bar->getMiddlewareStack(), $collection->getRoute($bar->getId())->getMiddlewareStack());
429
430
		$this->assertEquals('/a/b' . $baz->getPath(), $collection->getRoute($baz->getId())->getPath());
431
		$this->assertEquals($baz->getMiddlewareStack(), $collection->getRoute($baz->getId())->getMiddlewareStack());
432
433
		$this->assertEquals('/a' . $qux->getPath(), $collection->getRoute($qux->getId())->getPath());
434
		$this->assertEquals($qux->getMiddlewareStack(), $collection->getRoute($qux->getId())->getMiddlewareStack());
435
	}
436
437
	public function testGroupMiddleware()
438
	{
439
		$routeFoo = $this->getRouteFoo();
440
		$routeBar = $this->getRouteBar();
441
		$routeBaz = $this->getRouteBaz();
442
		$routeQux = $this->getRouteQux();
443
444
		$middlewareFoo = $this->getMiddlewareFoo();
445
		$middlewareBar = $this->getMiddlewareBar();
446
		$middlewareBaz = $this->getMiddlewareBaz();
447
		$middlewareQux = $this->getMiddlewareQux();
448
449
		$collection = new RouteCollection();
450
451
		$collection->group('/a', function(RouteCollectionInterface $collection) use(
452
			$routeFoo,
453
			$routeBar,
454
			$routeBaz,
455
			$routeQux,
456
			$middlewareFoo,
457
			$middlewareBar,
458
			$middlewareBaz,
459
			$middlewareQux
460
		)
461
		{
462
			$collection->addRoute(clone $routeFoo);
463
			$collection->addRoute(clone $routeBar);
464
			$collection->middleware($middlewareFoo);
465
			$collection->middleware($middlewareBar);
466
467
			$collection->group('/b', function(RouteCollectionInterface $collection) use(
468
				$routeBaz,
469
				$routeQux,
470
				$middlewareBaz,
471
				$middlewareQux
472
			)
473
			{
474
				$collection->addRoute(clone $routeBaz);
475
				$collection->addRoute(clone $routeQux);
476
				$collection->middleware($middlewareBaz);
477
				$collection->middleware($middlewareQux);
478
			});
479
		});
480
481
		$this->assertEquals([
482
			$middlewareFoo,
483
			$middlewareBar,
484
		], $collection->getRoute($routeFoo->getId())->getMiddlewareStack());
485
486
		$this->assertEquals([
487
			$middlewareFoo,
488
			$middlewareBar,
489
		], $collection->getRoute($routeBar->getId())->getMiddlewareStack());
490
491
		$this->assertEquals([
492
			$middlewareBaz,
493
			$middlewareQux,
494
			$middlewareFoo,
495
			$middlewareBar,
496
		], $collection->getRoute($routeBaz->getId())->getMiddlewareStack());
497
498
		$this->assertEquals([
499
			$middlewareBaz,
500
			$middlewareQux,
501
			$middlewareFoo,
502
			$middlewareBar,
503
		], $collection->getRoute($routeQux->getId())->getMiddlewareStack());
504
	}
505
506
	public function testCount()
507
	{
508
		$collection = new RouteCollection();
509
		$this->assertEquals(0, $collection->count());
510
511
		$collection->addRoute($this->getRouteFoo());
512
		$this->assertEquals(1, $collection->count());
513
514
		$collection->addRoute($this->getRouteBar());
515
		$collection->addRoute($this->getRouteBaz());
516
		$collection->addRoute($this->getRouteQux());
517
		$this->assertEquals(4, $collection->count());
518
	}
519
}
520